The renewable energy sources, cogeneration and trigeneration units, loads that are connected together by means of a controller form a microgrid. In this paper is presented the optimization of the power cost for a small community (a microgrid) that comprises loads and four distributed sources (two photovoltaic, one wind turbine and one micro-hydro). In the microgrid there are also present electric vehicles. The power demand of the microgrid loads is added with the power required by the electric vehicles, which will be charged in a controlled manner. The optimization of the power cost is conducted by means of the fmincon function from the MATLAB software. Several charging scenarios of the electric vehicles will be considered, as well as the power cost from the power markets (day-ahead market and bilateral contracts market) in 2021 and 2022. The results give the optimal power cost for the microgrid and the best option to supply the microgrid.KeywordsOptimizationPower costMicrogridDistributed sourcesElectric vehiclesPower markets