This study developed a model to compensate for both active and passive power in an electric microgrid with an optimal power flow to allow a detailed analysis of its power flow, which encourages the integration of renewable energy sources (RESs). Mixture integer linear programming (MILP) is proposed, with distributed generation (DG) integrated into the existing bars. It is developed in the GAMS tool to minimize operating costs in the microgrid as an objective function, to guarantee reliability, and quality in the microgrid. The algorithm calculates an optimal solution that meets all requirements for reliability and quality in the microgrid. Finally, a comparison of data between the two systems without and with DG was performed, showing an improvement in voltage profiles, reduced active power losses in the lines, a better power factor (PF), and improved angular behavior; that also adheres to the restrictions set forth at a minimum operating cost. A nine-bar model has been used to model the problem by IEEE standards.