Пример #1
0
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])
Пример #2
0
    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
Пример #3
0
def worker(temp):
    temp[0].train_network(temp[1])
    Network.save_networks(temp[0])