def run(self): """ Run the optimisation process using PSO algorithm. :param converge_info: optional run the optimisation with convergence information :param converge_info: optional run the optimisation with population information :return: """ print("Start the optimisation process...") if self.algorithm_type == 'nsga-2': uda = pg.nsga2(gen=self.generation) elif self.algorithm_type == 'moea-d': uda = pg.moead(gen=self.generation) elif self.algorithm_type == 'ihs': uda = pg.ihs(gen=self.generation) algo = pg.algorithm(uda) pop = pg.population(self.problem, self.pop_size) pop = algo.evolve(pop) self.pop = pop
# inequality constraints def get_nic(self): return 1 def get_bounds(self): """Return bounds of decision variables.""" return ([1, 1], [40, 40]) optimize = OptimizationProblem() optimize.model = PowerPlant() prob = pg.problem(optimize) num_gen = 15 pop = pg.population(prob, size=10) algo = pg.algorithm(pg.ihs(gen=num_gen)) result = { 'champion': [], 'efficiency': [], 'generation': [], 'extraction 1': [], 'extraction 2': [] } for gen in range(num_gen): result["generation"].append(gen) result["champion"].append(100 / pop.champion_f[0]) decision_var = pop.get_x() for pressure in decision_var:
def multivariate_optimization(**input_data): fluid_list = input_data['working_fluid_list'] result = {} opt_results = pd.DataFrame() for fluid in fluid_list: print('+' * 75) print('Working fluid:', fluid) print('+' * 75) optimize = _MultivariateOptimizationProblem() optimize.model = PowerPlant(working_fluid=fluid) optimize.params_to_opt = OrderedDict(input_data['variables']) optimize.boundary_conditions = input_data['boundary_conditions'] optimize.objective = optimize.model.get_objective_func( input_data['objective']) objectives_list = [input_data['objective']] constraint_list = ['constraints'] params_list = list(optimize.params_to_opt.keys()) # run optimization problem prob = pg.problem(optimize) num_gen = input_data['num_gen'] num_ind = input_data['num_ind'] pop = pg.population(prob, size=num_ind) algo = pg.algorithm(pg.ihs(gen=num_gen)) individuals = pd.DataFrame( columns=params_list + objectives_list + constraint_list, index=[(gen, ind) for gen in range(num_gen) for ind in range(num_ind)]) gen = 0 for gen in range(num_gen - 1): individuals = _process_generation_data( pop, gen, individuals, params_list, objectives_list, constraint_list) print() print('Evolution: {}'.format(gen)) for i in range(len(objectives_list)): print(objectives_list[i] + ': {}'.format(round(-pop.champion_f[i]/1e6, 4))) for i in range(len(params_list)): print(params_list[i] + ': {}'.format(round(pop.champion_x[i], 4))) pop = algo.evolve(pop) gen += 1 individuals = _process_generation_data( pop, gen, individuals, params_list, objectives_list, constraint_list) print() optimize.fitness(pop.champion_x) for conn, param_list in input_data['result_data']['connections'].items(): for param in param_list: opt_results.loc[fluid, param + '_' + conn] = optimize.model.get_connection_param(conn, param) for comp, param_list in input_data['result_data']['components'].items(): for param in param_list: opt_results.loc[fluid, param + '_' + comp] = optimize.model.get_component_param(comp, param) for param in input_data['result_data']['misc']: opt_results.loc[fluid, param] = optimize.model.get_misc_param(param) print('Final evolution: {}'.format(gen)) for i in range(len(objectives_list)): print(objectives_list[i] + ': {}'.format(round(pop.champion_f[i], 4))) for i in range(len(params_list)): print(params_list[i] + ': {}'.format(round(pop.champion_x[i], 4))) print() result[fluid] = individuals if input_data['save_result']: if not os.path.isdir(input_data['scenario']): os.mkdir('./' + input_data['scenario']) for fluid in fluid_list: result[fluid].to_csv(input_data['scenario'] + '/' + fluid + '.csv') opt_results.to_csv(input_data['scenario'] + '/champion_data.csv') return result, opt_results
import pygmo as po import numpy as np import myUDP import time generations = 400 sizePop = 35 #pathsave = '/home/oscar/Documents/PythonProjects/kuramotoAO/optimizationResults/' pathsave = '/Users/p277634/python/kaoModel/optimResult/' filenameTXT = 'ihs.txt' filenameNPZ = 'ihs.npz' print('Running: ', filenameNPZ[:-4]) # algorithm algo = po.algorithm(po.ihs(gen=generations, seed=22)) algo.set_verbosity(1) # problem prob = po.problem(myUDP.kaoSimplMultiObjContr()) # population pop = po.population(prob=prob, size=sizePop) # evolution start = time.time() popE = algo.evolve(pop) print('time evolution: ', time.time() - start) # save TXT fie with general description of the optimization bestFstr = 'ideal found fit: ' + str(po.ideal( popE.get_f())) + '; best fit possible: -1' bestChamp = 'champion decission vector' #bestXstr = 'velocity: ' + str(popE.champion_x[0]) + ', kL:' + str(popE.champion_x[1]),', kG: ' + str(popE.champion_x[2])
def create_variants(self, n, desc, category, constructor): def assign_2nd_alg(archipelago, algo): if category == 'rings': for island in archipelago.topology.every_other_island(): island.algorithm = algo elif hasattr(archipelago.topology, 'endpoints'): for island in archipelago.topology.endpoints: island.algorithm = algo elif isinstance(archipelago.topology, FullyConnectedTopology): for island in islice(archipelago.topology.islands, None, None, 2): island.algorithm = algo return archipelago def assign_algs(archipelago, algos): ''' Evenly partitions and assigns algorithms to islands. ''' for island,algo in zip(archipelago.topology.islands, cycle(algos)): island.algorithm = algo g = self.generations self.new_topology( desc='{}, de'.format(desc), category=category, algorithms=['de'], archipelago=Archipelago(constructor(de(gen=g),n))) self.new_topology( desc='{}, de1220'.format(desc), category=category, algorithms=['de1220'], archipelago=Archipelago(constructor(de1220(gen=g),n))) self.new_topology( desc='{}, sade'.format(desc), category=category, algorithms=['sade'], archipelago=Archipelago(constructor(sade(gen=g),n))) self.new_topology( desc='{}, ihs'.format(desc), category=category, algorithms=['ihs'], archipelago=Archipelago(constructor(ihs(gen=g),n))) self.new_topology( desc='{}, pso'.format(desc), category=category, algorithms=['pso'], archipelago=Archipelago(constructor(pso(gen=g),n))) self.new_topology( desc='{}, pso_gen'.format(desc), category=category, algorithms=['pso_gen'], archipelago=Archipelago(constructor(pso_gen(gen=g),n))) # self.new_topology( # desc='{}, simulated_annealing'.format(desc), # category=category, # algorithms=['simulated_annealing'], # archipelago=Archipelago(constructor(simulated_annealing(),n))) self.new_topology( desc='{}, bee_colony'.format(desc), category=category, algorithms=['bee_colony'], archipelago=Archipelago(constructor(bee_colony(gen=g),n))) self.new_topology( desc='{}, cmaes'.format(desc), category=category, algorithms=['cmaes'], archipelago=Archipelago(constructor(cmaes(gen=g),n))) self.new_topology( desc='{}, nsga2'.format(desc), category=category, algorithms=['nsga2'], archipelago=Archipelago(constructor(nsga2(gen=g),n))) self.new_topology( desc='{}, xnes'.format(desc), category=category, algorithms=['xnes'], archipelago=Archipelago(constructor(xnes(gen=g),n))) # de + nelder mead combo self.new_topology( desc='{}, de+nelder mead'.format(desc), category=category, algorithms=['de','neldermead'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), self.make_nelder_mead())) # de + praxis combo self.new_topology( desc='{}, de+praxis'.format(desc), category=category, algorithms=['de','praxis'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), self.make_praxis())) # de + nsga2 combo self.new_topology( desc='{}, de+nsga2'.format(desc), category=category, algorithms=['de','nsga2'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), nsga2(gen=g))) # de + de1220 combo self.new_topology( desc='{}, de+de1220'.format(desc), category=category, algorithms=['de','de1220'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), de1220(gen=g))) # de + sade combo self.new_topology( desc='{}, de+sade'.format(desc), category=category, algorithms=['de','sade'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), sade(gen=g))) # de + pso combo self.new_topology( desc='{}, de+pso'.format(desc), category=category, algorithms=['de','pso'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), pso(gen=g))) # extra configurations for fully connected topology if constructor is self.factory.createFullyConnected: self.new_topology( desc='{}, de+pso+praxis'.format(desc), category=category, algorithms=['de','pso','praxis'], archipelago=assign_algs(Archipelago(constructor(de(gen=g),n)), (de(gen=g), pso(gen=g), self.make_praxis()))) self.new_topology( desc='{}, de+pso+praxis+nsga2'.format(desc), category=category, algorithms=['de','pso','praxis','nsga2'], archipelago=assign_algs(Archipelago(constructor(de(gen=g),n)), (de(gen=g), pso(gen=g), self.make_praxis(), nsga2(gen=g)))) self.new_topology( desc='{}, de+pso+praxis+cmaes'.format(desc), category=category, algorithms=['de','pso','praxis','cmaes'], archipelago=assign_algs(Archipelago(constructor(de(gen=g),n)), (de(gen=g), pso(gen=g), self.make_praxis(), cmaes(gen=g)))) self.new_topology( desc='{}, de+pso+praxis+xnes'.format(desc), category=category, algorithms=['de','pso','praxis','xnes'], archipelago=assign_algs(Archipelago(constructor(de(gen=g),n)), (de(gen=g), pso(gen=g), self.make_praxis(), xnes(gen=g))))