In today's human society, diesel generators (DGs) are highly applied into the human energy and electricity supply system due to its technical, operational and economic advantages. This paper proposes an intelligent nonlinear $H_2/H_{\infty}$ robust controller based on the chaos particle swarm gravity search optimization algorithm (CPSOGSA), which controls the speed and excitation of a DG. In this method, firstly, establish the nonlinear mathematical model of the DG and then design the nonlinear $H_2/H_{\infty}$ robust controller based on this. The direct feedback linearization and the $H_2/H_{\infty}$ robust control theory are combined and applied. Based on the design of the integrated controller for DG speed and excitation, the system's performance requirements are transformed into a standard robust $H_2/H_{\infty}$ control problem. The parameters of the proposed solution controller are optimized by using the proposed CPSOGSA. The introduction of CPSOGSA completes the design of an intelligent nonlinear $H_2/H_{\infty}$ robust controller for DG. The simulation is implemented in MATLAB/Simulink, and the results are compared with the PID control method. The obtained results prove that the proposed method can effectively improve the dynamic accuracy of the system and the ability to suppress disturbances and improve the stability of the system.