Offline and real-time optimization of EGR rate and injection timing in diesel engines