def menu_solver(): while True: options_solutions() i = int(input("\nOptiunea dorita: ")) new_file = "" if i == 1: n = int(input("Marimea problemei: ")) p_size = int(input("Marimea populatie: ")) c_rate = int(input("Rata de incrucisare: ")) m_rate = float(input("Rata de mutatie: ")) nr_gen = int(input("Numarul de generatii: ")) j = int( input( "Alege operatorul de incrucisare:\n1.avg\n2.discreet\nAlegerea ta: " )) if j == 1: op_c = "avg" else: op_c = "discreet" p = float(input("Probabilitatea: ")) j = int( input( "Alege operatorul de mutatie:\n1.uniform\n2.nonuniform\nAlegerea ta: " )) if j == 1: op_m = "uniform" else: op_m = "nonuniform" j = int( input( "Alege operatorul de selectie:\n1.turnir\n2.rank\nAlegerea ta: " )) if j == 1: op_s = "turnir" s = int(input("Marimea turneului: ")) else: op_s = "rank" s = float( input( "Alegeti presiunea de selectie(un numar intre 1 si 2): " )) nr_exec = int(input("De cate ori sa se execute algoritmul: ")) new_file += "GA_{0}_{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8}_{9}.txt".format( str(p_size), str(c_rate), str(m_rate), str(nr_gen), str(nr_exec), op_c, str(p), op_m, op_s, str(s)) bests, averages, worsts, times = [], [], [], [] for i in range(nr_exec): start_time = time.time() best, avg, worst = evolutionary_algorithm( n, p_size, m_rate, c_rate, nr_gen, op_c, op_m, op_s, s, p) start_time = time.time() - start_time bests.append(best[-1]) averages.append(avg[-1]) worsts.append(worst[-1]) times.append(start_time) see_evolution(best, avg, worst) write_to_file_ea(new_file, bests, averages, worsts, times) elif i == 2: n = int(input("Marimea problemei: ")) no_p = int(input("Numarul de particule: ")) no_i = int(input("Numarul de iteratii: ")) w = float(input("Factorul de inertie: ")) c1 = float(input("Factorul de invatare cognitiv: ")) c2 = float(input("Factorul de invatare social: ")) nr_exec = int(input("De cate ori sa se execute algoritmul: ")) new_file += "PSO_{0}_{1}_{2}_{3}_{4}_{5}_{6}.txt".format( str(n), str(no_p), str(no_i), str(w), str(c1), str(c2), str(nr_exec)) bests, times = [], [] for i in range(nr_exec): start_time = time.time() g_best, best = pso(n, no_p, no_i, w, c1, c2) start_time = time.time() - start_time times.append(start_time) bests.append(best[-1]) see_evolution(best, [], []) print(g_best, " ", best[-1]) write_to_file_pso(new_file, bests, times) else: break
def run(self): d = Data() self.data = d.getData() if(self.file_name == 'data/train4dAll.txt'): x, y = d.getTrainData4d() elif(self.file_name == 'data/train6dAll.txt'): x, y = d.getTrainData6d() else: x, y = d.getTrainData4d() print('---------') print('data:',self.data) x = d.normalize_input(x) y = d.normalize_Y(y) print('x:', x) print('y:', y) learn_rate1 = float(self.e4.get()) learn_rate2 = float(self.e3.get()) pop_size = int(self.e2.get()) iterations = int(self.e1.get()) if(self.file_name != 'data/RBFN_params.txt'): data4D = open("data/RBFN_params.txt",'w+') #training weight --start-- rbf = RBF(3, 50, 1) x_dim, w, centers, beta = rbf.get_parameter() tStart = time.time() my_pso = pso(pop_size, iterations, learn_rate1, learn_rate2,x_dim, x, y, w, centers, beta) my_pso.initialize() print('pso!!!!!!!!!!!!!') fitness = my_pso.training() tEnd = time.time() print('It cost', (tEnd - tStart), ' sec.') print('fitness: ', fitness) theta, w, centers, beta = my_pso.get_parameter() rbf.set_parameter(theta, w, centers, beta) rbf.train(x,y) z1 = rbf.predict(x, rbf.get_weight()) print('predict_z', z1) y = d.inverse_normalize_Y(y) z = d.inverse_normalize_Y(z1) print('inverse_z', z) dim, output_W, output_centers, output_beta = rbf.get_parameter() print('output_W:',output_W) for i in range(0, len(output_W)): print(str(output_W[i][0]), str(output_centers[i][0]),str(output_centers[i][1]),str(output_centers[i][2]), str(output_beta[i][0]),file=data4D) data4D.close() else: load_w, load_centers, load_beta = d.load_parameters() rbf = RBF(3, len(load_centers), 1) rbf.set_parameter(0.5, load_w, load_centers, load_beta) print('rbf_W:', rbf.get_weight()) m = map(self.data) self.edges = m.getEdges() c = car(self.data[0]) Y = c.getPosition() self.mapping(self.edges) data4 = open("/Users/chengshu-yu/Documents/train4D.txt",'w+') data6 = open("/Users/chengshu-yu/Documents/train6D.txt",'w+') while(Y[1] < 43): self.draw_car(c.getPosition()) print('car direction:', c.getDirection(), ', wheel degree: ', c.getWheel()) c.sensor(self.edges) F, R, L = c.getDistance() input_x = [] if(self.file_name == 'data/train4dAll.txt'): input_x.append([F, R, L]) elif(self.file_name == 'data/train6dAll.txt'): pos = c.getPosition() input_x.append([pos[0], pos[1], F, R, L]) else: input_x.append([F, R, L]) input_x = np.array(input_x) input_x = d.normalize_input(input_x) print('input_x:', input_x) wheel = rbf.predict(input_x, rbf.get_weight()) print('predict_wheel:', wheel) wheel = d.inverse_normalize_Y(wheel) print('inverse wheel:', wheel) if wheel < -40: wheel = -40 elif wheel > 40: wheel = 40 print('-----------front right left:', input_x, '-----------') print('-----------predict wheel:', wheel, '-----------') c.setWheel(wheel) c.update_car_direction() c.update_car_pos() print(str(F),' ',str(R),' ',str(L),' ',str(c.getDirection()),file=data4) print(str(c.getPosX()),'',str(c.getPosY()),' ',str(F),' ',str(R),' ',str(L),' ',str(c.getDirection()),file=data6) data4.close() data6.close()
# -*- coding: utf-8 -*- import numpy as np from PSO import pso x, value = pso(100, 0.2, 0.2, 0.5, 300, 3) print(x, value)
import numpy as np from PSO import pso from M_NEH import m_neh import time PT = np.array([[45, 48, 50, 35, 35, 30, 30, 35, 25, 26], [45, 50, 45, 35, 36, 35, 35, 34, 25, 30], [50, 45, 46, 35, 36, 36, 31, 34, 30, 31], [50, 48, 48, 34, 38, 35, 32, 33, 27, 31], [45, 46, 48, 30, 35, 50, 34, 32, 28, 31], [45, 45, 45, 30, 35, 50, 33, 32, 30, 26], [47, 50, 47, 31, 30, 35, 35, 31, 29, 25], [50, 45, 48, 32, 30, 34, 34, 30, 24, 27], [48, 46, 46, 33, 34, 30, 34, 30, 25, 25], [45, 47, 47, 33, 33, 30, 35, 34, 32, 26], [46, 50, 45, 34, 30, 50, 30, 35, 31, 25], [48, 50, 47, 35, 31, 35, 32, 30, 25, 30]]) mop = [3, 3, 2, 2] start = time.time() g, fg = pso(m_neh, PT, mop, debug=True) end = time.time() print() print('The optimum is at:') print('{}'.format(g)) while m_neh(g, PT, mop, False, fg) != fg: pass print('The processing time is : \n', fg)
blit=False) # Save animation as gif #ani.save('PSO_schaeffer.gif', fps=10) plt.show() # Visualise results for f in [schaeffer, griewank, ackley_function]: history = pso(f, bounds=[[-5, 5], [-5, 5]], particle_size=20, inertia=0.5, w_min=0.6, w_max=0.7, n_iterations=20, func_name='') print('global best:', history['global_best'][-1], ', global best position:', history['global_best'][-1]) visualise(f=f, history=history, bounds=[[-5, 5], [-5, 5]], minima=[0, 0], func_name='') # initial guess x0 = [(-2, 2)]
from PSO import pso maxIt = 1000 ac1 = 2 ac2 = 2 vMax = 5 vMin = -5 x, v, melhoresSolucoes, valoresMinimos, valoresMedios = pso( maxIt, ac1, ac2, vMax, vMin) print("\nResultados:") print("\nx:\n", x) print("\nv:\n", v) print("\nMelhores Soluções:\n", melhoresSolucoes) print("\nValores Minimos:", valoresMinimos) print("\nValores Médios:", valoresMedios)