The optimization of a microgrid is a typical multivariable nonlinear programming problem with multiple hybrid constraints. In this paper, a kind of constrained multiobjective optimization based on nondominated immune algorithm is used, in which the constraint is transformed into a target function, a relatively isolated nondominated antibody is selected as the active antibody, and ratio cloning, recombination, and mutation are performed in order, according to the crowding distance of the active antibody, thus obtaining the convergence of an ideal Pareto front and a uniform distribution of the Pareto-optimal solution. The optimization model of the power output of the microgrid and the simulation of the operation in two microgrids for 24 h are carried out by utilizing the algorithm. The results show that good environmental protection is obtained at the lowest cost of running and maintenance, thus satisfying the stability of power supply requirements for users.