Example #1
0
 def run(self):
     data4D = open("data/RBFN_params.txt", 'w+')
     d = Data()  #new object of selected 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()
     #normalize training data
     x = d.normalize_input(x)
     y = d.normalize_Y(y)
     #get input parameters
     cross_rate = float(self.e4.get())
     mutation_rate = float(self.e3.get())
     pop_size = int(self.e2.get())
     iterations = int(self.e1.get())
     if (self.file_name != 'data/RBFN_params.txt'):
         #training weight --start--
         rbf = RBF(3, 50, 1)  #new object of RBFN
         x_dim, w, centers, beta = rbf.get_parameter()
         ga = genetic(len(x), cross_rate, mutation_rate, pop_size, x_dim, x,
                      y, w, centers, beta)  #new object of ga
         temp = 100
         for i in range(0, iterations):
             fitness = ga.F()
             best_idx = np.argmin(fitness)
             if (fitness[best_idx] < temp):
                 temp = fitness[best_idx]
                 best_DNA = ga.get_best_DNA(best_idx)
                 ga.update(best_DNA)
         #training weight --end--
         ga.w, ga.centers, ga.beta = ga.get_data()
         rbf.set_parameter(ga.w, ga.centers, ga.beta)
         rbf.train(x, y)
         z1 = rbf.predict(x, rbf.get_weight())
         y = d.inverse_normalize_Y(y)
         z = d.inverse_normalize_Y(z1)
         dim, output_W, output_centers, output_beta = rbf.get_parameter()
         for i in range(0, len(output_W)):  #save trained parameters
             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(load_w, load_centers, load_beta)
     m = map(self.data)  #new object of map
     self.edges = m.getEdges()
     c = car(self.data[0])  #new object of car
     Y = c.getPosition()  #get the position of car
     self.mapping(self.edges)
     while (Y[1] < 43):
         self.draw_car(c.getPosition())  #draw car on the window
         c.sensor(self.edges)  #calculate the distane of front, left, right
         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)
         wheel = rbf.predict(input_x,
                             rbf.get_weight())  #predict the degree of wheel
         wheel = d.inverse_normalize_Y(wheel)
         if wheel < -40:
             wheel = -40
         elif wheel > 40:
             wheel = 40
         c.setWheel(wheel)
         c.update_car_direction()
         c.update_car_pos()
Example #2
0
 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()