This article presents an application of the Particle Swarm Optimization (PSO) on the optimization of the power flow in a IEEE system with 30 nodes, which has some nodes with distributed generation. In first place, the mathematical model used for the optimization of the electricity generation costs. Afterwards, this model is applied in a case study with a IEEE system with 30 nodes. The results obtained through PSO are compared to other optimization methods, demonstrating that the cost and losses for the 30-node system are less than the values delivered by other methods. Then the same model is applied for the same power system with distributed generation in some of their nodes.