def worker(arr): p = [1, 0, 1, 6] network = arr[0] # torch.manual_seed(i*233212) # network=Network(p) # network=networks[i] network.learning_rate = 0.009 # network.create_weights() network.train_network() networks = [network] Network.save_networks(networks, arr[1])
def do_cross_over(network1, network2): network_n1 = Network() network_n2 = Network() network_n1.create_weights(False) network_n2.create_weights(False) split = randint(0, 21) for i in range(0, split): network_n1.dictLayers[0].data[0, i] = network1.dictLayers[0].data[0, i] network_n2.dictLayers[0].data[0, i] = network2.dictLayers[0].data[0, i] for i in range(split, 22): network_n1.dictLayers[0].data[0, i] = network2.dictLayers[0].data[0, i] network_n2.dictLayers[0].data[0, i] = network1.dictLayers[0].data[0, i] split = randint(0, 19) for j in range(0, 62): for i in range(0, split): network_n1.dictLayers[1].data[ j, i] = network1.dictLayers[1].data[j, i] network_n2.dictLayers[1].data[ j, i] = network2.dictLayers[1].data[j, i] for i in range(split, 19): network_n1.dictLayers[1].data[ j, i] = network2.dictLayers[1].data[j, i] network_n2.dictLayers[1].data[ j, i] = network1.dictLayers[1].data[j, i] split = randint(0, 2) for j in range(0, 19): for i in range(0, split): network_n1.dictLayers[2].data[ j, i] = network1.dictLayers[2].data[j, i] network_n2.dictLayers[2].data[ j, i] = network2.dictLayers[2].data[j, i] for i in range(split, 3): network_n1.dictLayers[2].data[ j, i] = network2.dictLayers[2].data[j, i] network_n2.dictLayers[2].data[ j, i] = network1.dictLayers[2].data[j, i] return network_n1, network_n2
def do_mutate_network_sin(self, network1): #layer=0 #random.seed(datetime.now()) layer = randint(0, 2) deviation = random.uniform(0, 0.09) network2 = Network.create_copy(network1) network2.layer_changed = layer print("layer " + str(layer) + " " + str(deviation)) if (layer == 0): random.seed(datetime.now()) num_layers = randint(0, 22) for layer in range(0, num_layers): layer_val = randint(0, 21) deviation = random.uniform(0, 0.01) noise = np.random.normal(0, deviation, 1 * 62) noise = np.reshape(noise, (1, 62)) noise = (torch.from_numpy(noise)).float() network2.dictLayers[0].data[ layer_val] = network2.dictLayers[0].data[layer_val] + noise if (layer == 1): num_layers = randint(0, 62) for layer in range(0, num_layers): layer_val = randint(0, 61) deviation = random.uniform(0, 0.008) noise = np.random.normal(0, deviation, 1 * 19) noise = np.reshape(noise, (1, 19)) noise = (torch.from_numpy(noise)).float() network2.dictLayers[1].data[ layer_val] = network2.dictLayers[1].data[layer_val] + noise if (layer == 2): num_layers = randint(0, 19) for layer in range(0, num_layers): layer_val = randint(0, 18) deviation = random.uniform(0, 0.004) noise = np.random.normal(0, deviation, 1 * 3) noise = np.reshape(noise, (1, 3)) noise = (torch.from_numpy(noise)).float() network2.dictLayers[2].data[ layer_val] = network2.dictLayers[2].data[layer_val] + noise return network2
def drive(self, carstate: State) -> Command: """ Produces driving command in response to newly received car state. This is a dummy driving routine, very dumb and not really considering a lot of inputs. But it will get the car (if not disturbed by other drivers) successfully driven along the race track. """ command = Command() command.meta = 0 #self.steer(carstate, 0.0, command) # ACC_LATERAL_MAX = 6400 * 5 # v_x = min(80, math.sqrt(ACC_LATERAL_MAX / abs(command.steering))) x_predict = [carstate.speed_x] x_predict.append(carstate.distance_from_center) x_predict.append(carstate.angle) [x_predict.append(i) for i in carstate.distances_from_edge] l_predict = x_predict x_predict = np.array(x_predict) x_predict = x_predict.reshape(1, 22) #x_predict = scaler.transform(x_predict) input_sensor = torch.Tensor(1, 22) for i in range(0, 22): input_sensor[0, i] = float(x_predict[0][i]) x_temp = Variable(torch.zeros(1, 22), requires_grad=False) for j in range(0, 22): x_temp.data[0, j] = input_sensor[0, j] MyDriver.network.forward(x_temp) output = MyDriver.network.output self.speed_x += carstate.speed_x * (3.6) #self.distance += carstate.distance_raced self.distance = carstate.distance_raced self.brake += abs(output.data[0, 1]) self.count += 1 command.accelerator = max(0.0, output.data[0, 0]) command.accelerator = min(1.0, command.accelerator) #command.accelerator=0.8*command.accelerator command.steering = output.data[0, 2] command.steering = max(-1, command.steering) command.steering = min(1, command.steering) if (carstate.damage > 0 or carstate.distance_raced > 6400.00 or carstate.current_lap_time > 160.00): self.msg = 'f' #print("Layer Changed "+str(MyDriver.network.layer_changed)) fitness = MyDriver.w_distance * ( self.distance) + MyDriver.w_speed * ( self.speed_x / self.count) - MyDriver.w_brake * ( self.brake) + -MyDriver.w_damage * (carstate.damage) print(fitness) self.net_score[MyDriver.index] = fitness print( str(self.speed_x / self.count) + " " + str(self.distance) + " " + str(self.brake / self.count) + " " + str(carstate.current_lap_time) + " " + str(carstate.damage)) MyDriver.network.fitness = fitness if ((MyDriver.index + 1) < len(MyDriver.networks)): print(MyDriver.index) MyDriver.index += 1 else: print("else") mutate = Mutate() if (MyDriver.index <= MyDriver.num_childs): MyDriver.add_network(MyDriver.network) child_netowrk = mutate.do_mutate_network_sin( MyDriver.get_best_network()) MyDriver.index += 1 MyDriver.networks.append(child_netowrk) else: print(str(datetime.now())) folder_name = "data/evolution/" + str(datetime.now()) os.makedirs(folder_name) for i in range(0, len(MyDriver.networks)): path = folder_name + "/" + str(i) + ".pkl" Network.save_networks(MyDriver.networks[i], path) MyDriver.index = 0 print("Mutation on") MyDriver.heap_size += 5 self.sort() #MyDriver.networks=mutate.mutate_list(networks) MyDriver.num_childs = MyDriver.num_childs + int( 0.1 * MyDriver.num_childs) mutate = Mutate() child_netowrk = mutate.do_mutate_network_sin( MyDriver.get_best_network()) MyDriver.networks = [] MyDriver.networks.append(child_netowrk) MyDriver.network = MyDriver.networks[MyDriver.index] self.reinitiaze() command.meta = 1 car_speed = carstate.speed_x * (3.6) if (car_speed >= 0.0 and car_speed < 40.0): command.gear = 1 if (car_speed >= 40.0 and car_speed < 70.0): command.gear = 2 if (car_speed >= 70.0 and car_speed < 120.0): command.gear = 3 if (car_speed >= 120.0 and car_speed < 132.0): command.gear = 4 if (car_speed >= 132.0 and car_speed < 150.0): command.gear = 5 if (car_speed >= 150.0): command.gear = 6 if not command.gear: command.gear = carstate.gear or 1 command.brake = min(1, output.data[0, 1]) command.brake = max(0, command.brake) #self.accelerate(carstate, v_x, command) #if self.data_logger: # self.data_logger.log(carstate, command) logging.info( str(command.accelerator) + "," + str(command.brake) + "," + str(command.steering) + "," + str(l_predict)) return command
def activate_networks(): p = [1, 0, 1, 6] networks = [] new_networks = [] for i in range(1, 7): network = Network.read_networks(i)[0] networks.append(network) for i in range(0, 6): temp_i_network = networks[i] for j in range(i, 6): temp_j_network = networks[j] if (temp_i_network.error.data[0] > temp_j_network.error.data[0]): temp_network = networks[i] networks[i] = networks[j] networks[j] = temp_network for i in range(0, 3): network = Network(p) network.create_weights() network_1 = Network(p) network_1.create_weights() parent1_index = randint(0, 4) parent2_index = randint(0, 4) w1 = networks[parent1_index].dict_layers[0] w2 = networks[parent2_index].dict_layers[0] w_new, w_new_1 = create_new_weight(w1, w2) network.dict_layers[0] = w_new network_1.dict_layers[0] = w_new_1 w1 = networks[parent1_index].dict_layers[1] w2 = networks[parent2_index].dict_layers[1] w_new, w_new_1 = create_new_weight(w1, w2) network.dict_layers[1] = w_new network_1.dict_layers[1] = w_new_1 new_networks.append(network) new_networks.append(network_1) print(len(new_networks)) with Pool(6) as p: p.map( worker, [[new_networks[0], 1], [new_networks[1], 2], [new_networks[2], 3], [new_networks[3], 4], [new_networks[4], 5], [new_networks[5], 6]])
def worker(temp): temp[0].train_network(temp[1]) Network.save_networks(temp[0])
import torch from torch.autograd import Variable from multiprocessing import Pool from random import randint from mutate import Mutate from nueral import Network from cardata import createCarDataList import logging logging.basicConfig(filename='data.log',level=logging.INFO) car_data_list=createCarDataList() print("Complete") networks=Network.read_networks() def sort(networks): for i in range(0,len(networks)): temp_i=networks[i] for j in range(i+1,len(networks)): temp_j=networks[j] if(temp_i.error.data[0]>temp_j.error.data[0]): temp=temp_i networks[i]=networks[j] networks[j]=temp networks2=[] for i in range(0,10): networks2.append(networks[i]) return networks2