In order to reduce the comprehensive power cost of the independent microgrid and to improve environmental protection and power supply reliability, a two-layer power capacity optimization model of a microgrid with electric vehicles (EVs) was established that considered uncertainty and demand response. Based on the load and energy storage characteristics of electric vehicles, the classification of electric vehicles was proposed, and their mathematical models were established. The idea of robust optimization was adopted to construct the uncertain scenario set. Considering the incentive demand response, a two-layer power capacity optimization model of a microgrid was constructed. The improved pelican optimization algorithm (IPOA) was proposed as the two-layer model. In view of the slow convergence rate of the pelican optimization algorithm (POA) and its tendency to fall into the local optimum, methods such as elite reverse learning were proposed to generate the initial population, set disturbance inhibitors, and introduce Lévy flight to improve the initial population of the algorithm and enhance its global search ability. Finally, an independent microgrid was used as an example to verify the effectiveness of the proposed model and the improved algorithm. Considering that the total power capacity optimization cost of the microgrid after addition of electric vehicles was reduced by CNY 139,600, the total power capacity optimization cost of the microgrid after IOPA optimization was reduced by CNY 49,600 compared with that after POA optimization.