The main content of this article is to use particle swarm optimization algorithm and genetic algorithm to achieve optimal power flow. After using two different methods for testing, the data results obtained from the two methods are used to compare the advantages and disadvantages of the two methods, in order to further optimize the better method and obtain a more accurate optimal power flow.