# # bus[pv, 15] = 0 # dc_bus_sol, _, _ = loadflow_dc_pro(bus, line, printout=False) # dc_ThetaMean = Theta_abs_mean(dc_bus_sol) # print(dc_ThetaMean) # return dc_ThetaMean # Define fitness function. @engine.fitness_register def fitness(indv): x = np.array(indv.solution) # y = -float(sum(x**2 - 10 * np.cos(2*np.pi*x) + 10)) bus[pv, 15] = np.rint(x) # bus[pv, 15] = 0 dc_bus_sol, _, _ = loadflow_dc_pro(bus, line, printout=False) dc_ThetaMean = Theta_abs_mean(dc_bus_sol) print(dc_ThetaMean) return -float(dc_ThetaMean) if '__main__' == __name__: time_start = time() engine.run(ng=generations) time_end = time() print("GA costs %.4f seconds!" % (time_end - time_start)) best_indv = engine.population.best_indv(engine.fitness) print(best_indv.solution) print(-engine.fitness(best_indv)) best_population_point = best_indv.solution bus[pv, 15] = np.rint(best_population_point) converge, bus_sol, line_f_from, line_f_to = loadflow(bus, line, printout=True)
self.logger.info(msg) def finalize(self, population, engine): best_indv = population.best_indv(engine.fitness) x = best_indv.solution y = engine.ori_fmax msg = 'Optimal solution: ({}, {})'.format(x, y) self.logger.info(msg) if '__main__' == __name__: # Run the GA engine and print every generation engine.run(ng=500) best_indv = engine.population.best_indv(engine.fitness) print('Max({0},{1})'.format(best_indv.solution[0], engine.fitness(best_indv))) x = np.linspace(0, 15, 10000) y = [-3 * (i - 30)**2 * math.sin(i) for i in x] plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('function') plt.axis([-1, 16, -3000, 3000]) plt.scatter(best_indv.solution[0], engine.fitness(best_indv), color='r') a = round(best_indv.solution[0], 4) b = round(engine.fitness(best_indv), 4) plt.annotate('Max(' + str(a) + ',' + str(b) + ')', xy=(best_indv.solution[0], engine.fitness(best_indv)), xytext=(7, 2500), arrowprops=dict(facecolor='black', shrink=0.1, width=2)) plt.show()