コード例 #1
0
ファイル: ea_gui.py プロジェクト: hakon0601/SubSym
    def start_ea(self):
        self.parse_ann_input()
        self.ea = EvolutionaryAlgorithm(genotype_pool_size=self.horizontal_slider_1.get(),
                                        adult_pool_size=self.horizontal_slider_2.get(),
                                        elitism=self.horizontal_slider_3.get(),
                                        genotype_length=self.genotype_size,
                                        phenotype_length=self.genotype_size,
                                        adult_selection_protocol=self.adult_selection_protocol.get(),
                                        parent_selection_protocol=self.parent_selection_protocol.get(),
                                        crossover_rate=self.horizontal_slider_4.get(),
                                        mutation_rate=self.horizontal_slider_5.get(),
                                        mutation_protocol=self.mutation_protocol.get(),
                                        points_of_crossover=self.horizontal_slider_6.get(),
                                        symbol_set_size=self.horizontal_slider_7.get(),
                                        tournament_size=self.horizontal_slider_8.get(),
                                        tournament_slip_through_probability=self.horizontal_slider_9.get(),
                                        initial_temperature=self.horizontal_slider_10.get(),
                                        hidden_layers=self.layers_list,
                                        activation_functions=self.activations_list,
                                        generations=self.horizontal_slider_12.get())

        self.beertracker_worlds = [self.initialize_board() for _ in range(self.horizontal_slider_11.get())]
        self.current_generation = 0
        self.fitness_log_average.append([])
        self.fitness_log_best.append([])
        self.standard_deviation_log.append([])
        self.timer = time()
        self.run_ea()
コード例 #2
0
 def start_ea(self):
     self.ea = EvolutionaryAlgorithm(
         genotype_pool_size=self.horizontal_slider_1.get(),
         adult_pool_size=self.horizontal_slider_2.get(),
         genotype_length=self.horizontal_slider_3.get(),
         phenotype_length=self.horizontal_slider_3.get(
         ),  # Not a single slider
         adult_selection_protocol=self.adult_selection_protocol.get(),
         parent_selection_protocol=self.parent_selection_protocol.get(),
         crossover_rate=self.horizontal_slider_4.get(),
         mutation_rate=self.horizontal_slider_5.get(),
         mutation_protocol=self.mutation_protocol.get(),
         points_of_crossover=self.horizontal_slider_7.get(),
         zero_threshold=self.horizontal_slider_8.get(),
         symbol_set_size=self.horizontal_slider_9.get(),
         tournament_size=self.horizontal_slider_11.get(),
         tournament_slip_through_probability=self.horizontal_slider_12.get(
         ),
         initial_temperature=self.horizontal_slider_13.get(),
         problem=self.problem_type.get(),
         generations=self.horizontal_slider_14.get())
     self.current_generation = 0
     self.fitness_log_average.append([])
     self.fitness_log_best.append([])
     self.standard_deviation_log.append([])
     self.run_ea()
コード例 #3
0
def main():
    network = train_nn_from_pid()

    #Read in the benchmark paths that we will use
    Benchmark1 = pd.read_csv('Benchmark_DLC_31ms_reduced.csv',
                             sep=',',
                             header=0)
    Benchmark1 = Benchmark1.values
    Benchmark2 = pd.read_csv('Benchmark_SScorner_80m_left_reduced.csv',
                             sep=',',
                             header=0)
    Benchmark2 = Benchmark2.values
    Benchmark3 = pd.read_csv('Benchmark_SScorner_500m_left_25ms_reduced.csv',
                             sep=',',
                             header=0)
    Benchmark3 = Benchmark3.values
    '''$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    for  k in range(5):
        thing_range=list(range(50))
        np.random.shuffle(thing_range)
        total_loss=0
        for i in thing_range:
            running_loss=0
            for j in range(len(PID_Data)):
                #print(np.shape(input1))
                #print(network.parameters())
                #print(network.fc1.weight.data)
                #print(network.fc3.weight.data)
                #print(j)
                network_input=torch.tensor(input1[j])
                #print(network_input)
                network_target=torch.tensor(target1[j])
                network_target=network_target.double()
                network= network.double()
                
                out=network(network_input)
                network.zero_grad()
                criterion = nn.MSELoss()
                loss = criterion(out, network_target)
                loss.backward()
                running_loss += loss.item()
                #print(out.data,network_target.data, out.data-network_target.data)
                #print(loss.item())
                for f in network.parameters():
                    f.data.sub_(f.grad.data * learning_rate)
            print('[%5d] loss: %.3f' % (i + 1, running_loss))
            #if running_loss >= 5:
                #input('press  enter')
            total_loss+=running_loss
            PID_Data=pd.read_csv('random_path_pid_more_output_'+str(i)+'.csv',sep=',',header=0)
        #print(PID_Data)
            
            PID_Data=PID_Data.values
            np.random.shuffle(PID_Data)
            input1=PID_Data[:,0:2]
            input2=PID_Data[:,4:]
            input1=np.concat enate((input1,input2),axis=1)
        #print(input1)
            target1=PID_Data[:,3]
        print('total loss this set: ', total_loss)
        #print('[%5d] loss: %.3f' %
        #(i + 1, running_loss))
    '''

    #Train the network until it is sufficient, asking the human operator for input on whether the point it reached  is  good enough
    '''
    network=network.float()
    for i in range(10):
        train_network(network)
        a=input('is this good enough?')
        if a=='1':
            break
    '''
    '''
    test_suite.basic_fitness_comparison(network)
    test_suite.trailer_length_variation_test(network)
    test_suite.trailer_mass_variation_test(network)
    test_suite.trailer_stiffness_variation_test(network)
    test_suite.noisy_signal_test(network)
    test_suite.initial_displacement_test(network)
    '''
    #Initialize varriables to run the first benchmark test  on the PID mimicking controller
    network = network.float()

    controller = NN2Control()
    x_true = Benchmark1[:, 0]
    y_true = Benchmark1[:, 1]
    t = Benchmark1[:, 2]
    vel = Benchmark1[:, 3]
    xp = []
    yp = []
    x = []
    y = []
    pid = StanleyPID()

    #Run the same benchmark on both the PID controller and the PID mimicking network and compare  the two
    for i in range(2):
        ego = EgoSim(sim_timestep=t[1] - t[0], world_state_at_front=True)
        print('controller: ', i)
        th1t = 0
        th2t = 0
        th1 = []
        th2 = []
        x_truck = []
        y_truck = []
        for j in range(len(t)):
            if i == 1:
                state = ego.convert_world_state_to_front()
                ctrl_delta, ctrl_vel, err, interr, differr = pid.calc_steer_control(
                    t[i], state, x_true, y_true, vel)
                xt, yt, deltat, th1t, th2t = ego.simulate_timestep(
                    [ctrl_vel, ctrl_delta])
                x_truck.append(xt)
                y_truck.append(yt)
                th1.append(th1t)
                th2.append(th2t)
                xp.append(xt)
                yp.append(yt)
            else:
                state = ego.convert_world_state_to_front()
                ctrl_delta, ctrl_vel, err, interr, differr = controller.calc_steer_control(
                    t[i], state, x_true, y_true, vel, th1t - th2t, network)
                xt, yt, deltat, th1t, th2t = ego.simulate_timestep(
                    [ctrl_vel, ctrl_delta])
                x_truck.append(xt)
                y_truck.append(yt)
                th1.append(th1t)
                th2.append(th2t)
                x.append(xt)
                y.append(yt)
        if i == 1:
            pid_fitness, CTerr = calc_off_tracking(x_truck, y_truck, th1, th2,
                                                   ego.P, x_true, y_true)
        else:
            controller_fitness, CTerr = calc_off_tracking(
                x_truck, y_truck, th1, th2, ego.P, x_true, y_true)
    print('Benchmark 1 PID fitness: ', pid_fitness)
    print('Benchmark 1 controller fitness: ', controller_fitness)
    plt.plot(x, y)
    plt.plot(x_true, y_true, 'r--')
    plt.plot(xp, yp, 'g:')
    plt.legend(['Network Performance', 'True Path', 'PID Performance'])
    plt.xlabel('X location, (m)')
    plt.ylabel('Y Location, (m)')
    plt.show()

    #send the pid mimicking controller to the  evolutionary algorithm
    #print('bias   value before  evo: ', network.fc3.bias.data)
    evolution = EvolutionaryAlgorithm(network)
    for i in range(1000):
        print(i)
        evolution.iterate()
        #every 20 steps, run a benchmark on the best controller in the system to see how it is progressing
        if i % 100 == 0:
            '''
            Fc1=network.fc1.weight.data.numpy()
            Fc2=network.fc2.weight.data.numpy()
            Fc3=network.fc3.weight.data.numpy()
            Evo1=evolution.controllers[evolution.best_controller_idx].fc1.weight.data.numpy()
            Evo2=evolution.controllers[evolution.best_controller_idx].fc2.weight.data.numpy()
            Evo3=evolution.controllers[evolution.best_controller_idx].fc3.weight.data.numpy()
            print((Fc1-Evo1))
            print(np.linalg.norm((Fc1-Evo1)))
            print((Fc2-Evo2))
            print(np.linalg.norm((Fc2-Evo2)))
            print((Fc3-Evo3))
            print(np.linalg.norm((Fc3-Evo3)))
            Fc1b=network.fc1.bias.data.numpy()
            Fc2b=network.fc2.bias.data.numpy()
            Fc3b=network.fc3.bias.data.numpy()
            Evo1b=evolution.controllers[evolution.best_controller_idx].fc1.bias.data.numpy()
            Evo2b=evolution.controllers[evolution.best_controller_idx].fc2.bias.data.numpy()
            Evo3b=evolution.controllers[evolution.best_controller_idx].fc3.bias.data.numpy()
            print((Fc1b-Evo1b))
            print(np.linalg.norm((Fc1b-Evo1b)))
            print((Fc2b-Evo2b))
            print(np.linalg.norm((Fc2b-Evo2b)))
            print((Fc3b-Evo3b))
            print(np.linalg.norm((Fc3b-Evo3b)))
            '''
            controller = NN2Control()
            x_true = Benchmark1[:, 0]
            y_true = Benchmark1[:, 1]
            t = Benchmark1[:, 2]
            vel = Benchmark1[:, 3]
            x = []
            y = []
            xp = []
            yp = []

            x_truck = []
            y_truck = []
            th1t = 0
            th2t = 0
            th1 = []
            th2 = []
            pid = StanleyPID()
            for i in range(2):
                ego = EgoSim(sim_timestep=t[1] - t[0],
                             world_state_at_front=True)
                print('controller: ', i)
                th1t = 0
                th2t = 0
                th1 = []
                th2 = []
                x_truck = []
                y_truck = []
                for j in range(len(t)):
                    if i == 1:
                        state = ego.convert_world_state_to_front()
                        ctrl_delta, ctrl_vel, err, interr, differr = pid.calc_steer_control(
                            t[i], state, x_true, y_true, vel)
                        xt, yt, deltat, th1t, th2t = ego.simulate_timestep(
                            [ctrl_vel, ctrl_delta])
                        x_truck.append(xt)
                        y_truck.append(yt)
                        th1.append(th1t)
                        th2.append(th2t)
                        xp.append(xt)
                        yp.append(yt)
                    else:
                        state = ego.convert_world_state_to_front()
                        ctrl_delta, ctrl_vel, err, interr, differr = controller.calc_steer_control(
                            t[i], state, x_true, y_true, vel, th1t - th2t,
                            evolution.controllers[
                                evolution.best_controller_idx])
                        xt, yt, deltat, th1t, th2t = ego.simulate_timestep(
                            [ctrl_vel, ctrl_delta])
                        x_truck.append(xt)
                        y_truck.append(yt)
                        th1.append(th1t)
                        th2.append(th2t)
                        x.append(xt)
                        y.append(yt)
                if i == 1:
                    pid_fitness, CTerr = calc_off_tracking(
                        x_truck, y_truck, th1, th2, ego.P, x_true, y_true)
                else:
                    controller_fitness, CTerr = calc_off_tracking(
                        x_truck, y_truck, th1, th2, ego.P, x_true, y_true)

            print('Benchmark 1 PID fitness: ', pid_fitness)
            print('Benchmark 1 controller fitness: ', controller_fitness)
            plt.plot(x, y)
            plt.plot(x_true, y_true, 'r--')
            plt.plot(xp, yp, 'g:')
            plt.legend(['Network Performance', 'True Path', 'PID Performance'])
            plt.xlabel('X location, (m)')
            plt.ylabel('Y Location, (m)')
            plt.show()

    #Initialize varriables to run the first benchmark test
    controller = NN2Control()
    x_true = Benchmark1[:, 0]
    y_true = Benchmark1[:, 1]
    t = Benchmark1[:, 2]
    vel = Benchmark1[:, 3]
    x = []
    y = []
    xp = []
    yp = []
    x_truck = []
    y_truck = []
    th1t = 0
    th2t = 0
    th1 = []
    th2 = []
    pid = StanleyPID()
    #after the network has finished its evolutionary training, check it  the first benchmark
    for i in range(2):
        ego = EgoSim(sim_timestep=t[1] - t[0], world_state_at_front=True)
        print('controller: ', i)
        th1t = 0
        th2t = 0
        th1 = []
        th2 = []
        x_truck = []
        y_truck = []
        for j in range(len(t)):
            if i == 1:
                state = ego.convert_world_state_to_front()
                ctrl_delta, ctrl_vel, err, interr, differr = pid.calc_steer_control(
                    t[i], state, x_true, y_true, vel)
                xt, yt, deltat, th1t, th2t = ego.simulate_timestep(
                    [ctrl_vel, ctrl_delta])
                x_truck.append(xt)
                y_truck.append(yt)
                th1.append(th1t)
                th2.append(th2t)
                xp.append(xt)
                yp.append(yt)
            else:
                state = ego.convert_world_state_to_front()
                ctrl_delta, ctrl_vel, err, interr, differr = controller.calc_steer_control(
                    t[i], state, x_true, y_true, vel, th1t - th2t,
                    evolution.controllers[evolution.best_controller_idx])
                xt, yt, deltat, th1t, th2t = ego.simulate_timestep(
                    [ctrl_vel, ctrl_delta])
                x_truck.append(xt)
                y_truck.append(yt)
                th1.append(th1t)
                th2.append(th2t)
                x.append(xt)
                y.append(yt)

        if i == 1:
            pid_fitness, CTerr = calc_off_tracking(x_truck, y_truck, th1, th2,
                                                   ego.P, x_true, y_true)
        else:
            controller_fitness, CTerr = calc_off_tracking(
                x_truck, y_truck, th1, th2, ego.P, x_true, y_true)

    print('Benchmark 1 PID fitness: ', pid_fitness)
    print('Benchmark 1 controller fitness: ', controller_fitness)
    plt.plot(x, y)
    plt.plot(x_true, y_true, 'r--')
    plt.plot(xp, yp, 'g:')
    plt.legend(['Network Performance', 'True Path', 'PID Performance'])
    plt.xlabel('X location, (m)')
    plt.ylabel('Y Location, (m)')
    plt.show()

    #Initialize varriables to run the second benchmark test on the controller trained on the
    x_true = Benchmark2[:, 0]
    y_true = Benchmark2[:, 1]
    t = Benchmark2[:, 2]
    vel = Benchmark2[:, 3]
    x_truck = []
    y_truck = []
    th1t = 0
    th2t = 0
    th1 = []
    th2 = []
    pid = StanleyPID()
    x = []
    y = []
    xp = []
    yp = []
    #check it  the second benchmark
    for i in range(2):
        ego = EgoSim(sim_timestep=t[1] - t[0], world_state_at_front=True)
        print('controller: ', i)
        th1t = 0
        th2t = 0
        th1 = []
        th2 = []
        x_truck = []
        y_truck = []
        for j in range(len(t)):
            if i == 1:
                state = ego.convert_world_state_to_front()
                ctrl_delta, ctrl_vel, err, interr, differr = pid.calc_steer_control(
                    t[i], state, x_true, y_true, vel)
                xt, yt, deltat, th1t, th2t = ego.simulate_timestep(
                    [ctrl_vel, ctrl_delta])
                x_truck.append(xt)
                y_truck.append(yt)
                th1.append(th1t)
                th2.append(th2t)
                xp.append(xt)
                yp.append(yt)
            else:
                state = ego.convert_world_state_to_front()
                ctrl_delta, ctrl_vel, err, interr, differr = controller.calc_steer_control(
                    t[i], state, x_true, y_true, vel, th1t - th2t,
                    evolution.controllers[evolution.best_controller_idx])
                xt, yt, deltat, th1t, th2t = ego.simulate_timestep(
                    [ctrl_vel, ctrl_delta])
                x_truck.append(xt)
                y_truck.append(yt)
                th1.append(th1t)
                th2.append(th2t)
                x.append(xt)
                y.append(yt)
            #inputs=np.concatenate((err,ctrl_vel,interr,differr),axis=None)
            #network_input=torch.tensor(inputs)
            #out=self.controllers[i](network_input)
            #x.append(xt); y.append(yt); delta.append(deltat); th1.append(th1t); th2.append(th2t)
        if i == 1:
            pid_fitness, CTerr = calc_off_tracking(x_truck, y_truck, th1, th2,
                                                   ego.P, x_true, y_true)
        else:
            controller_fitness, CTerr = calc_off_tracking(
                x_truck, y_truck, th1, th2, ego.P, x_true, y_true)
    print('Benchmark 2 PID fitness: ', pid_fitness)
    print('Benchmark 2 controller fitness: ', controller_fitness)
    plt.plot(x, y)
    plt.plot(x_true, y_true, 'r--')
    plt.plot(xp, yp, 'g:')
    plt.legend(['Network Performance', 'True Path', 'PID Performance'])
    plt.xlabel('X location, (m)')
    plt.ylabel('Y Location, (m)')
    plt.show()
    x_true = Benchmark3[:, 0]
    y_true = Benchmark3[:, 1]
    t = Benchmark3[:, 2]
    vel = Benchmark3[:, 3]
    x = []
    y = []
    xp = []
    yp = []
    x_truck = []
    y_truck = []
    th1t = 0
    th2t = 0
    th1 = []
    th2 = []
    pid = StanleyPID()
    for i in range(2):
        ego = EgoSim(sim_timestep=t[1] - t[0], world_state_at_front=True)
        print('controller: ', i)
        th1t = 0
        th2t = 0
        th1 = []
        th2 = []
        x_truck = []
        y_truck = []
        for j in range(len(t)):
            if i == 1:
                state = ego.convert_world_state_to_front()
                ctrl_delta, ctrl_vel, err, interr, differr = pid.calc_steer_control(
                    t[i], state, x_true, y_true, vel)
                xt, yt, deltat, th1t, th2t = ego.simulate_timestep(
                    [ctrl_vel, ctrl_delta])
                x_truck.append(xt)
                y_truck.append(yt)
                th1.append(th1t)
                th2.append(th2t)
                xp.append(xt)
                yp.append(yt)

            else:
                state = ego.convert_world_state_to_front()
                ctrl_delta, ctrl_vel, err, interr, differr = controller.calc_steer_control(
                    t[i], state, x_true, y_true, vel, th1t - th2t,
                    evolution.controllers[evolution.best_controller_idx])
                xt, yt, deltat, th1t, th2t = ego.simulate_timestep(
                    [ctrl_vel, ctrl_delta])
                x_truck.append(xt)
                y_truck.append(yt)
                th1.append(th1t)
                th2.append(th2t)
                x.append(xt)
                y.append(yt)

            #inputs=np.concatenate((err,ctrl_vel,interr,differr),axis=None)
            #network_input=torch.tensor(inputs)
            #out=self.controllers[i](network_input)
            #x.append(xt); y.append(yt); delta.append(deltat); th1.append(th1t); th2.append(th2t)
        if i == 1:
            pid_fitness, CTerr = calc_off_tracking(x_truck, y_truck, th1, th2,
                                                   ego.P, x_true, y_true)
        else:
            controller_fitness, CTerr = calc_off_tracking(
                x_truck, y_truck, th1, th2, ego.P, x_true, y_true)
    print('Benchmark 3 PID fitness: ', pid_fitness)
    print('Benchmark 3 controller fitness: ', controller_fitness)
    plt.plot(x, y)
    plt.plot(x_true, y_true, 'r--')
    plt.plot(xp, yp, 'g:')
    plt.legend(['Network Performance', 'True Path', 'PID Performance'])
    plt.xlabel('X location, (m)')
    plt.ylabel('Y Location, (m)')
    plt.show()
    #
    #controller=NNControl()
    network = evolution.controllers[evolution.best_controller_idx]
    test_suite.basic_fitness_comparison(network)
    test_suite.trailer_length_variation_test(network)
    test_suite.trailer_mass_variation_test(network)
    test_suite.trailer_stiffness_variation_test(network)
    test_suite.noisy_signal_test(network)
    test_suite.initial_displacement_test(network)
    #controller.calc_steer_control(t,state,path_x,path_y,path_vel,network)
    #a=network.parameters()
    #print(a)
    '''
コード例 #4
0
ファイル: main.py プロジェクト: klenkiew/factory-location
def main():
    """Factory location optimizer entry point."""

    # default run parameters
    params = dict()
    params["Input_file"] = "Input.json"
    params["Tests_count"] = 20
    params["Enable_log"] = False
    params["Algorithm_config_file"] = False
    params["Algorithm"] = 0
    params["Neighbourhood_size"] = 100
    params["Neighbourhood_sigma"] = 1.0
    params["Neighbourhood_mean"] = 0.0
    params["Resources"] = []

    # check given options
    skip = False
    for i in range(1, len(sys.argv)):
        if skip:
            skip = False
            continue
        if sys.argv[i] == "-i" or sys.argv[i] == "--input":
            if len(sys.argv) <= i + 1:
                print("Not enough arguments!")
                return
            params["Input_file"] = sys.argv[i + 1]
            skip = True
            continue
        if sys.argv[i] == "-ac" or sys.argv[i] == "--algorithm_config":
            if len(sys.argv) <= i + 1:
                print("Not enough arguments!")
                return
            params["Algorithm_config_file"] = sys.argv[i + 1]
            skip = True
            continue
        if sys.argv[i] == "-h" or sys.argv[i] == "--help":
            print("Factory location optimizer.")
            print("Available options:")
            print("-i  --input\t\t[FileName]\tSets file with factory resources.")
            print("-ac --algorithm_config\t[FileName]\tSets file with algorithm options.")
            print("-h  --help\t\t\t\tShows help and close program.")
            print("Available algorithms:")
            print("0 - Hill climbing algorithm")
            print("1 - Evolutionary algorithm")
            print("Available selection methods (for evolutionary algorithm only):")
            print("0 - proportional selection")
            print("1 - tournament selection")
            print("2 - threshold selection")
            return
        print("Not recognized argument! Skipping...")

    # run interactive mode
    if params["Algorithm_config_file"] is False:
        interactive_mode(params, params["Input_file"] == "Input.json")
    else:
        print("Loading algorithm configuration from file: " + params["Algorithm_config_file"])
        try:
            algorithm_config = json.load(open(params["Algorithm_config_file"], 'r'))
            for key in algorithm_config.keys():
                if key == "Stop_condition":
                    func_dict = dict()
                    exec(algorithm_config[key], func_dict)
                    params[key] = func_dict["cond"]
                else:
                    params[key] = algorithm_config[key]
        except FileNotFoundError:
            print("Cannot open " + params["Algorithm_config_file"] + " file!")
            return

    # resources boundaries
    resources_bounds = [1000000, 1000000, -1000000, -1000000]

    print("Loading resources from: " + params["Input_file"])
    try:
        resources_json = json.load(open(params["Input_file"], 'r'))
        for res in resources_json["Resources"]:
            func_dict = dict()
            exec(res["Transport_cost_func"], func_dict)
            params["Resources"].append(ResourceRequirement(Resource(Location2D(res["Position"][0], res["Position"][1]), func_dict["f"]), res["Required_units"]))
            if res["Position"][0] < resources_bounds[0]:
                resources_bounds[0] = res["Position"][0]
            if res["Position"][0] > resources_bounds[2]:
                resources_bounds[2] = res["Position"][0]
            if res["Position"][1] < resources_bounds[1]:
                resources_bounds[1] = res["Position"][1]
            if res["Position"][1] > resources_bounds[3]:
                resources_bounds[3] = res["Position"][1]
    except FileNotFoundError:
        print("Cannot open " + params["Input_file"] + " file!")
        return
    print("Loaded " + str(len(params["Resources"])) + " resources!")
    print("Resources position boundaries (x_min, y_min, x_max, y_max): ({0:.2f}, {1:.2f}, {2:.2f}, {3:.2f})"
          .format(resources_bounds[0], resources_bounds[1], resources_bounds[2], resources_bounds[3]))

    evaluator = Evaluator(params["Resources"])
    plot_logger = PlotLogger()

    if params["Algorithm"] == 1:
        if params["Selection_method"] == 0:
            selector_obj = ProportionalSelector()
        if params["Selection_method"] == 1:
            selector_obj = TournamentSelector(params["Tournament_size"])
        if params["Selection_method"] == 2:
            selector_obj = ThresholdSelector(params["Selection_threshold"])

    if params["Algorithm"] == 0:
        algorith_name = "Hill climbing algorithm"
    else:
        algorith_name = "Evolutionary algorithm"

    stats_logger = StatisticsLogger()
    if params["Enable_log"]:
        logger = AggregateLogger([StdOutputLogger(algorith_name), plot_logger, stats_logger])
    else:
        logger = stats_logger

    if params["Algorithm"] == 0:
        neighbour_gen = create_gaussian_neighbour_gen(params["Neighbourhood_size"], params["Neighbourhood_sigma"],
                                                      params["Neighbourhood_mean"])
        algorithm = HillClimbingAlgorithm(evaluator, neighbour_gen, params["Stop_condition"], logger)
    else:
        options = EvolutionaryAlgorithmOptions(selector_obj, params["Population_size"], params["Crossover_probability"])
        neighbourhood_gen = create_gaussian_neighbour_gen(1, params["Neighbourhood_sigma"],
                                                          params["Neighbourhood_mean"])
        algorithm = EvolutionaryAlgorithm(evaluator, options,
                                          neighbourhood_gen,
                                          params["Stop_condition"], logger)
    results = []
    average_goal_func = 0.0
    print("Running tests...")
    for i in range(params["Tests_count"]):
        logger.clear()
        evaluator.evaluations = 0
        start_point = Location2D(np.random.uniform(resources_bounds[0], resources_bounds[2]),
                                 np.random.uniform(resources_bounds[1], resources_bounds[3]))
        if params["Algorithm"] == 0:
            results.append(algorithm.run(start_point))
        else:
            results.append(algorithm.run(resources_bounds))
        average_goal_func += results[i][1]
        if params["Enable_log"]:
            print("Best location: ({}, {}) [{}]".format(results[i][0].position_x,
                                                        results[i][0].position_y,
                                                        results[i][1]))
            print("Evaluations count: {}".format(evaluator.evaluations))
            # draw goal function plot
            plot_logger.draw("Goal function")
            # draw resources and factory location
            # resources as green dots and factory as red dot
            for res in params["Resources"]:
                plt.plot(res.resource.location.position_x, res.resource.location.position_y, "go")
            plt.plot(results[i][0].position_x, results[i][0].position_y, "ro")
            plt.xlabel("Position X")
            plt.ylabel("Position Y")
            plt.title("Resources and factory location")
            plt.show()

    average_goal_func_after_evaluations = {key: mean(value) for key, value in stats_logger.stats.items()}

    average_goal_func /= params["Tests_count"]
    print("Average goal function: {0:.4f}".format(average_goal_func))

    # prepare to save as json
    main_json = dict()
    main_json[algorith_name] = []
    for key, val in average_goal_func_after_evaluations.items():
        value_json = dict()
        value_json["X"] = key
        value_json["Y"] = val
        main_json[algorith_name].append(value_json)

    json.dump(main_json, open(algorith_name + ".json", 'w'), indent=4)
コード例 #5
0
ファイル: learn.py プロジェクト: drecker/snake_ai
if __name__ == '__main__':

    from evolutionary_algorithm import EvolutionaryAlgorithm
    from individual import Individual
    import pickle
    import datetime

    ea = EvolutionaryAlgorithm()
    ea.MAX_GEN = 25
    ea.CROSSOVER_PROB = 0.7
    starting_population = [Individual() for i in range(31)]
    ea.run(starting_population)

    with open(
            "logs/{}.txt".format(
                str(datetime.datetime.now()).replace(":", "-")), 'wb') as f:
        pickle.dump(ea.last_run_elites[-1], f)
    with open(
            "logs/population/{}.txt".format(
                str(datetime.datetime.now()).replace(":", "-") +
                "-population"), 'wb') as f:
        pickle.dump(ea.last_population, f)
    with open(
            "logs/fitnesses/{}.txt".format(
                str(datetime.datetime.now()).replace(":", "-") +
                "-population"), 'wb') as f:
        pickle.dump(ea.saved_fitnesses, f)
コード例 #6
0
from evolutionary_algorithm import EvolutionaryAlgorithm

if __name__ == '__main__':
    ea = EvolutionaryAlgorithm(100, 100, [], 40, 3)
    ea.print_population()
    print(ea.run())
    print(ea.prun())