示例#1
0
    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
文件: gui.py 项目: hakon0601/SubSym
 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()
示例#4
0
文件: gui.py 项目: hakon0601/SubSym
class Gui(tk.Tk):
    def __init__(self, delay, nr_of_runs=1, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.delay = delay
        self.nr_of_runs = nr_of_runs
        self.current_run = 0
        self.current_generation = 0
        self.fitness_log_average = []
        self.fitness_log_best = []
        self.standard_deviation_log = []
        self.ea = None
        self.build_parameter_menu()

    def build_parameter_menu(self):
        self.horizontal_slider_1 = tk.Scale(self, length=1000, from_=2, to=1000, orient=tk.HORIZONTAL,
                                            label="Genotype Pool Size", resolution=2,
                                            command=self.control_pool_size_genotype)
        self.horizontal_slider_1.set(GENOTYPE_POOL_SIZE)
        self.horizontal_slider_1.pack()
        self.horizontal_slider_2 = tk.Scale(self, length=1000, from_=2, to=1000, orient=tk.HORIZONTAL,
                                            label="Adult Pool Size", resolution=2, command=self.control_pool_size_adult)
        self.horizontal_slider_2.set(ADULT_POOL_SIZE)
        self.horizontal_slider_2.pack()
        self.horizontal_slider_3 = tk.Scale(self, length=1000, from_=2, to=1000, orient=tk.HORIZONTAL,
                                            label="Genotype Length")
        self.horizontal_slider_3.set(GENOTYPE_LENGTH)
        self.horizontal_slider_3.pack()
        crossover_slider_group = tk.Frame(self)
        self.horizontal_slider_4 = tk.Scale(crossover_slider_group, length=500, from_=0.0, to=1.0, resolution=-1,
                                            orient=tk.HORIZONTAL, label="Crossover Rate")
        self.horizontal_slider_4.set(CROSSOVER_RATE)
        self.horizontal_slider_4.pack(side=tk.LEFT)
        self.horizontal_slider_5 = tk.Scale(self, length=1000, from_=0, to=1, resolution=-1, orient=tk.HORIZONTAL,
                                            label="Mutation Rate")
        self.horizontal_slider_5.set(MUTATION_RATE)
        self.horizontal_slider_5.pack()
        self.horizontal_slider_7 = tk.Scale(crossover_slider_group, length=500, from_=1, to=100, orient=tk.HORIZONTAL,
                                            label="Points of Crossover")
        self.horizontal_slider_7.set(POINTS_OF_CROSSOVER)
        self.horizontal_slider_7.pack(side=tk.LEFT)
        crossover_slider_group.pack(anchor=tk.W)
        self.horizontal_slider_8 = tk.Scale(self, length=1000, from_=0, to=300, orient=tk.HORIZONTAL,
                                            label="Zero-threshold")
        self.horizontal_slider_8.set(ZERO_THRESHOLD)
        self.horizontal_slider_8.pack()
        self.horizontal_slider_9 = tk.Scale(self, length=1000, from_=1, to=50, orient=tk.HORIZONTAL,
                                            label="Symbol Set Size")
        self.horizontal_slider_9.set(SYMBOL_SET_SIZE)
        self.horizontal_slider_9.pack(anchor=tk.W)
        tournament_slider_group = tk.Frame(self)
        self.horizontal_slider_11 = tk.Scale(tournament_slider_group, length=500, from_=3, to=500, orient=tk.HORIZONTAL,
                                            label="Tournament Size")
        self.horizontal_slider_11.set(TOURNAMENT_SIZE)
        self.horizontal_slider_11.pack(side=tk.LEFT)
        self.horizontal_slider_12 = tk.Scale(tournament_slider_group, length=500, from_=0, to=1, resolution=-1, orient=tk.HORIZONTAL,
                                             label="Tournament Slip-through Probability")
        self.horizontal_slider_12.set(TOURNAMENT_SLIP_THROUGH_PROBABILITY)
        self.horizontal_slider_12.pack(side=tk.LEFT)
        tournament_slider_group.pack(anchor=tk.W)
        self.horizontal_slider_13 = tk.Scale(self, length=1000, from_=1, to=1000, orient=tk.HORIZONTAL,
                                             label="Initial Temperature")
        self.horizontal_slider_13.set(INITIAL_TEMPERATURE)
        self.horizontal_slider_13.pack()
        self.horizontal_slider_14 = tk.Scale(self, length=1000, from_=1, to=10000, orient=tk.HORIZONTAL,
                                             label="Max Number of Generations")
        self.horizontal_slider_14.set(MAX_GENERATIONS)
        self.horizontal_slider_14.pack()

        self.mutation_protocol = tk.IntVar()
        self.mutation_protocol.set(MUTATION_PROTOCOL)
        radiogroup0 = tk.Frame(self)
        tk.Label(radiogroup0, text="Mutation Protocol").pack(anchor=tk.W)
        tk.Radiobutton(radiogroup0, text="Individual", variable=self.mutation_protocol, value=1).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup0, text="Component", variable=self.mutation_protocol, value=2).pack(side=tk.LEFT)
        radiogroup0.pack(anchor=tk.W)

        self.adult_selection_protocol = tk.IntVar()
        self.adult_selection_protocol.set(ADULT_SELECTION_PROTOCOL)
        radiogroup1 = tk.Frame(self)
        tk.Label(radiogroup1, text="Adult Selection Protocol").pack(anchor=tk.W)
        tk.Radiobutton(radiogroup1, text="Full", variable=self.adult_selection_protocol,
                       command=self.control_pool_size_adult, value=1).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup1, text="Over Production", variable=self.adult_selection_protocol,
                       command=self.control_pool_size_adult, value=2).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup1, text="Mixing", variable=self.adult_selection_protocol, value=3).pack(side=tk.LEFT)
        radiogroup1.pack(anchor=tk.W)

        self.parent_selection_protocol = tk.IntVar()
        self.parent_selection_protocol.set(PARENT_SELECTION_PROTOCOL)
        radiogroup2 = tk.Frame(self)
        tk.Label(radiogroup2, text="Parent Selection Protocol").pack(anchor=tk.W)
        tk.Radiobutton(radiogroup2, text="Fitness Proportionate", variable=self.parent_selection_protocol,
                       value=1).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup2, text="Sigma-scaling", variable=self.parent_selection_protocol,
                       value=2).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup2, text="Tournament selection", variable=self.parent_selection_protocol,
                       value=3).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup2, text="Boltzmann Selection", variable=self.parent_selection_protocol,
                       value=4).pack(side=tk.LEFT)
        radiogroup2.pack(anchor=tk.W)

        self.problem_type = tk.IntVar()
        self.problem_type.set(PROBLEM)
        radiogroup3 = tk.Frame(self)
        tk.Label(radiogroup3, text="Problem type").pack(anchor=tk.W)
        tk.Radiobutton(radiogroup3, text="One Max", variable=self.problem_type, value=1).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup3, text="LOLZ Prefix", variable=self.problem_type, value=2).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup3, text="Surprising Sequence Local", variable=self.problem_type,
                       value=3).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup3, text="Surprising Sequence Global", variable=self.problem_type,
                       value=4).pack(side=tk.LEFT)
        radiogroup3.pack(anchor=tk.W)

        start_button = tk.Button(self, text="Start", width=20, command=self.start_simulation)
        start_button.pack()

    def control_pool_size_genotype(self, event):
        if self.adult_selection_protocol.get() == 1:
            self.horizontal_slider_2.set(self.horizontal_slider_1.get())
        elif self.adult_selection_protocol.get() == 2:
            self.horizontal_slider_2.set(min(self.horizontal_slider_2.get(), self.horizontal_slider_1.get() - 2))

    def control_pool_size_adult(self, event=None):
        if self.adult_selection_protocol.get() == 1:
            self.horizontal_slider_1.set(self.horizontal_slider_2.get())
        elif self.adult_selection_protocol.get() == 2:
            self.horizontal_slider_1.set(max(self.horizontal_slider_1.get(), self.horizontal_slider_2.get() - 6))
        if self.parent_selection_protocol.get() == 3:
            self.horizontal_slider_11.set(min(self.horizontal_slider_11.get(), self.horizontal_slider_2.get() // 2))

    def start_simulation(self):
        self.current_run = 0
        self.fitness_log_average = []
        self.fitness_log_best = []
        self.standard_deviation_log = []
        self.start_ea()

    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()

    def run_ea(self):
        self.ea.run_one_life_cycle()
        self.write_to_log()
        self.current_generation += 1
        if self.current_generation < self.horizontal_slider_14.get() and \
                self.ea.phenotype_adult_pool[0].fitness_value < 1.0:
            self.after(self.delay, lambda: self.run_ea())
        else:
            print "End"
            self.current_run += 1
            if self.current_run < self.nr_of_runs:
                self.start_ea()
            else:
                print "Average Generations:", sum([len(l) for l in self.fitness_log_average])/len(self.fitness_log_average)
                self.plot_data()

    def write_to_log(self):
        print "Gen:", "%.2d" % self.current_generation, "\tBest fitness:", \
            "%.3f" % round(self.ea.phenotype_adult_pool[0].fitness_value, 3), "\tAverage fitness:", \
            "%.3f" % round(self.ea.avg_fitness, 3), "\tStandard deviation: ", \
            "%.5f" % round(self.ea.standard_deviation, 3), "\tBest Phenotype:", \
            self.ea.phenotype_adult_pool[0].components
        if self.problem_type.get() > 2:
            print "Violations: ", self.ea.phenotype_adult_pool[0].violations
        self.fitness_log_average[self.current_run].append(self.ea.avg_fitness)
        self.fitness_log_best[self.current_run].append(self.ea.phenotype_adult_pool[0].fitness_value)
        self.standard_deviation_log[self.current_run].append(self.ea.standard_deviation)

    def plot_data(self):
        plt.figure(1)
        plt.subplot(311)
        plt.plot(self.fitness_log_average[-1], label="Average fitness")
        plt.plot(self.fitness_log_best[-1], label="Best fitness")
        #plt.legend(['y = Average fitness in adult pool', 'y = Best fitness in adult pool'], loc='lower right')
        plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=2, mode="expand", borderaxespad=0.)


        plt.subplot(312)
        plt.plot(self.fitness_log_best[-1])
        plt.legend(['y = Best fitness in adult pool'], loc='lower right')

        plt.subplot(313)
        plt.plot(self.standard_deviation_log[-1])
        plt.legend(['y = Standard deviation'], loc='upper right')
        plt.show()
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)
    '''
示例#6
0
class EAGui(tk.Tk):
    def __init__(self, delay, nr_of_runs=1, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.delay = delay
        self.nr_of_runs = nr_of_runs
        self.current_run = 0
        self.current_generation = 0
        self.fitness_log_average = []
        self.fitness_log_best = []
        self.standard_deviation_log = []
        self.center_window()
        self.beertracker_worlds = None
        self.ea = None
        self.build_parameter_menu()

    def center_window(self):
        ws = self.winfo_screenwidth()  # width of the screen
        hs = self.winfo_screenheight()  # height of the screen
        w = 1000
        h = 900
        x = (ws/2) - (w/2)
        y = (hs/2) - (h/2)
        self.geometry('%dx%d+%d+%d' % (w, h, x, y))

    def build_parameter_menu(self):
        self.horizontal_slider_1 = tk.Scale(self, length=1000, from_=2, to=1000, orient=tk.HORIZONTAL,
                                            label="Genotype Pool Size", resolution=2,
                                            command=self.control_pool_size_genotype)
        self.horizontal_slider_1.set(GENOTYPE_POOL_SIZE)
        self.horizontal_slider_1.pack()
        self.horizontal_slider_2 = tk.Scale(self, length=1000, from_=2, to=1000, orient=tk.HORIZONTAL,
                                            label="Adult Pool Size", resolution=2, command=self.control_pool_size_adult)
        self.horizontal_slider_2.set(ADULT_POOL_SIZE)
        self.horizontal_slider_2.pack()
        self.horizontal_slider_3 = tk.Scale(self, length=1000, from_=0, to=1000, orient=tk.HORIZONTAL,
                                            label="Elitism", resolution=1, command=self.control_pool_size_adult)
        self.horizontal_slider_3.set(ELITISM)
        self.horizontal_slider_3.pack()

        ann_entry_group = tk.Frame(self)
        tk.Label(ann_entry_group, text="Hidden layers:").pack(side=tk.LEFT)
        self.hidden_layers = tk.StringVar()
        self.hidden_layers.set("3")
        tk.Entry(ann_entry_group, textvariable=self.hidden_layers).pack(side=tk.LEFT)
        tk.Label(ann_entry_group, text="Activation functions (0:Sigmoid, 1:Hyperbolic tangent, 2:Rectify, 3:Softmax):",
                 padx=20).pack(side=tk.LEFT)
        self.activation_functions = tk.StringVar()
        self.activation_functions.set("0, 0")
        tk.Entry(ann_entry_group, textvariable=self.activation_functions).pack(side=tk.LEFT)
        ann_entry_group.pack(anchor=tk.W)

        crossover_slider_group = tk.Frame(self)
        self.horizontal_slider_4 = tk.Scale(crossover_slider_group, length=500, from_=0.0, to=1.0, resolution=-1,
                                            orient=tk.HORIZONTAL, label="Crossover Rate")
        self.horizontal_slider_4.set(CROSSOVER_RATE)
        self.horizontal_slider_4.pack(side=tk.LEFT)
        self.horizontal_slider_5 = tk.Scale(self, length=1000, from_=0, to=1, resolution=-1, orient=tk.HORIZONTAL,
                                            label="Mutation Rate")
        self.horizontal_slider_5.set(MUTATION_RATE)
        self.horizontal_slider_5.pack()
        self.horizontal_slider_6 = tk.Scale(crossover_slider_group, length=500, from_=1, to=100, orient=tk.HORIZONTAL,
                                            label="Points of Crossover")
        self.horizontal_slider_6.set(POINTS_OF_CROSSOVER)
        self.horizontal_slider_6.pack(side=tk.LEFT)
        crossover_slider_group.pack(anchor=tk.W)

        self.horizontal_slider_7 = tk.Scale(self, length=1000, from_=1, to=10000, orient=tk.HORIZONTAL,
                                            label="Symbol Set Size")
        self.horizontal_slider_7.set(SYMBOL_SET_SIZE)
        self.horizontal_slider_7.pack(anchor=tk.W)

        tournament_slider_group = tk.Frame(self)
        self.horizontal_slider_8 = tk.Scale(tournament_slider_group, length=500, from_=3, to=500, orient=tk.HORIZONTAL,
                                            label="Tournament Size")
        self.horizontal_slider_8.set(TOURNAMENT_SIZE)
        self.horizontal_slider_8.pack(side=tk.LEFT)
        self.horizontal_slider_9 = tk.Scale(tournament_slider_group, length=500, from_=0, to=1, resolution=-1,
                                            orient=tk.HORIZONTAL, label="Tournament Slip-through Probability")
        self.horizontal_slider_9.set(TOURNAMENT_SLIP_THROUGH_PROBABILITY)
        self.horizontal_slider_9.pack(side=tk.LEFT)
        tournament_slider_group.pack(anchor=tk.W)

        self.horizontal_slider_10 = tk.Scale(self, length=1000, from_=1, to=1000, orient=tk.HORIZONTAL,
                                             label="Initial Temperature")
        self.horizontal_slider_10.set(INITIAL_TEMPERATURE)
        # self.horizontal_slider_10.pack()
        self.horizontal_slider_11 = tk.Scale(self, length=1000, from_=1, to=20, orient=tk.HORIZONTAL,
                                             label="Nr of Scenarios")
        self.horizontal_slider_11.set(NR_OF_SCENARIOS)
        self.horizontal_slider_11.pack(anchor=tk.W)
        self.horizontal_slider_12 = tk.Scale(self, length=1000, from_=1, to=500, orient=tk.HORIZONTAL,
                                             label="Max Number of Generations")
        self.horizontal_slider_12.set(MAX_GENERATIONS)
        self.horizontal_slider_12.pack()

        self.scenario_protocol = tk.IntVar()
        self.scenario_protocol.set(SCENARIO_PROTOCOL)
        scenario_protocol_group = tk.Frame(self)
        tk.Label(scenario_protocol_group, text="Scenario Protocol").pack(anchor=tk.W)
        tk.Radiobutton(scenario_protocol_group, text="Static", variable=self.scenario_protocol,
                       value=1).pack(side=tk.LEFT)
        tk.Radiobutton(scenario_protocol_group, text="Dynamic", variable=self.scenario_protocol,
                       value=2).pack(side=tk.LEFT)
        tk.Radiobutton(scenario_protocol_group, text="Static, test on random", variable=self.scenario_protocol,
                       value=3).pack(side=tk.LEFT)
        scenario_protocol_group.pack(anchor=tk.W)
        
        self.agent_type = tk.IntVar()
        self.agent_type.set(AGENT_TYPE)
        agent_type_group = tk.Frame(self)
        tk.Label(agent_type_group, text="Agent Type").pack(anchor=tk.W)
        tk.Radiobutton(agent_type_group, text="Standard", variable=self.agent_type,
                       value=1).pack(side=tk.LEFT)
        tk.Radiobutton(agent_type_group, text="No wrap", variable=self.agent_type,
                       value=2).pack(side=tk.LEFT)
        tk.Radiobutton(agent_type_group, text="Pull", variable=self.agent_type,
                       value=3).pack(side=tk.LEFT)
        agent_type_group.pack(anchor=tk.W)

        self.mutation_protocol = tk.IntVar()
        self.mutation_protocol.set(MUTATION_PROTOCOL)
        mutation_protocol_group = tk.Frame(self)
        tk.Label(mutation_protocol_group, text="Mutation Protocol").pack(anchor=tk.W)
        tk.Radiobutton(mutation_protocol_group, text="Individual", variable=self.mutation_protocol,
                       value=1).pack(side=tk.LEFT)
        tk.Radiobutton(mutation_protocol_group, text="Component", variable=self.mutation_protocol,
                       value=2).pack(side=tk.LEFT)
        mutation_protocol_group.pack(anchor=tk.W)

        self.adult_selection_protocol = tk.IntVar()
        self.adult_selection_protocol.set(ADULT_SELECTION_PROTOCOL)
        adult_selection_group = tk.Frame(self)
        tk.Label(adult_selection_group, text="Adult Selection Protocol").pack(anchor=tk.W)
        tk.Radiobutton(adult_selection_group, text="Full", variable=self.adult_selection_protocol,
                       command=self.control_pool_size_adult, value=1).pack(side=tk.LEFT)
        tk.Radiobutton(adult_selection_group, text="Over Production", variable=self.adult_selection_protocol,
                       command=self.control_pool_size_adult, value=2).pack(side=tk.LEFT)
        tk.Radiobutton(adult_selection_group, text="Mixing", variable=self.adult_selection_protocol,
                       value=3).pack(side=tk.LEFT)
        adult_selection_group.pack(anchor=tk.W)

        self.parent_selection_protocol = tk.IntVar()
        self.parent_selection_protocol.set(PARENT_SELECTION_PROTOCOL)
        parent_selection_group = tk.Frame(self)
        tk.Label(parent_selection_group, text="Parent Selection Protocol").pack(anchor=tk.W)
        tk.Radiobutton(parent_selection_group, text="Fitness Proportionate", variable=self.parent_selection_protocol,
                       value=1).pack(side=tk.LEFT)
        tk.Radiobutton(parent_selection_group, text="Sigma-scaling", variable=self.parent_selection_protocol,
                       value=2).pack(side=tk.LEFT)
        tk.Radiobutton(parent_selection_group, text="Tournament selection", variable=self.parent_selection_protocol,
                       value=3).pack(side=tk.LEFT)
        tk.Radiobutton(parent_selection_group, text="Boltzmann Selection", variable=self.parent_selection_protocol,
                       value=4).pack(side=tk.LEFT)
        parent_selection_group.pack(anchor=tk.W)

        start_button = tk.Button(self, text="Start", width=20, command=self.start_simulation)
        start_button.pack()

    def control_pool_size_genotype(self, event=None):
        if self.adult_selection_protocol.get() == 1:
            self.horizontal_slider_2.set(self.horizontal_slider_1.get())
        elif self.adult_selection_protocol.get() == 2:
            self.horizontal_slider_2.set(min(self.horizontal_slider_2.get(), self.horizontal_slider_1.get() - 2))

    def control_pool_size_adult(self, event=None):
        if self.adult_selection_protocol.get() == 1:
            self.horizontal_slider_1.set(self.horizontal_slider_2.get())
        elif self.adult_selection_protocol.get() == 2:
            self.horizontal_slider_1.set(max(self.horizontal_slider_1.get(), self.horizontal_slider_2.get() - 6))
        if self.parent_selection_protocol.get() == 3:
            self.horizontal_slider_8.set(min(self.horizontal_slider_8.get(), self.horizontal_slider_2.get() // 2))
        self.horizontal_slider_3.set(min(self.horizontal_slider_3.get(), self.horizontal_slider_2.get()))

    def start_simulation(self):
        self.current_run = 0
        self.fitness_log_average = []
        self.fitness_log_best = []
        self.standard_deviation_log = []
        self.start_ea()

    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()

    def run_ea(self):
        self.ea.run_one_life_cycle(self.beertracker_worlds)
        if self.current_generation == 0:
            self.timer = time() - self.timer
        self.current_generation += 1
        self.write_to_log()
        self.reset_scenarios()
        if self.current_generation < self.horizontal_slider_12.get() and \
                self.ea.phenotype_adult_pool[0].fitness_value < 1.0:
            self.after(self.delay, lambda: self.run_ea())
        else:
            self.current_run += 1
            if self.current_run < self.nr_of_runs:
                print "Current run", self.current_run
                self.start_ea()
            else:
                self.end_ea_run_visualisation()

    def end_ea_run_visualisation(self):
        print "Avg best fitness:", sum([self.fitness_log_best[i][-1] for i in range(len(self.fitness_log_best))]) / \
                                   len(self.fitness_log_best)
        print "Best fitness:", max([self.fitness_log_best[i][-1] for i in range(len(self.fitness_log_best))])
        BeerTrackerGui(
                    phenotype=self.ea.phenotype_adult_pool[0],
                    environments=self.beertracker_worlds,
                    fitness_log_average=self.fitness_log_average,
                    fitness_log_best=self.fitness_log_best,
                    standard_deviation_log=self.standard_deviation_log)
        print "Average Generations:", \
            sum([len(l) for l in self.fitness_log_average]) / len(self.fitness_log_average)

    def initialize_board(self):
        beertracker_world = BeerTrackerWorld(width=WORLD_WIDTH, height=WORLD_HEIGHT, agent_type=self.agent_type.get())
        return beertracker_world

    def reset_scenarios(self):
        if self.scenario_protocol.get() == 1:
            for world in self.beertracker_worlds:
                world.reset()
        else:
            self.beertracker_worlds = [self.initialize_board() for _ in range(len(self.beertracker_worlds))]

    def parse_ann_input(self):
        if self.hidden_layers.get() != '':
            self.layers_list = [INPUT_NODES] + \
                               map(int, self.hidden_layers.get().replace(" ", "").split(",")) + \
                               [OUTPUT_NODES]
        else:
            self.layers_list = [INPUT_NODES, OUTPUT_NODES]
        if not ONE_HOT_OUTPUT:
            self.layers_list[-1] = 2
        # Two extra nodes for detecting walls
        if self.agent_type.get() == 2:
            self.layers_list[0] += 2
        if self.agent_type.get() == 3:
            self.layers_list[-1] += 1
        if CENTERED_NODE:
            self.layers_list[0] += 1
        if FULL_NODE:
            self.layers_list[0] += 1
        self.activations_list = map(int, str(self.activation_functions.get()).replace(" ", "").split(","))
        self.genotype_size = sum([self.layers_list[i] * self.layers_list[i + 1]
                                  for i in range(len(self.layers_list) - 1)])
        if RECURENCE:
            for i in range(1, len(self.layers_list)):
                self.genotype_size += pow(self.layers_list[i], 2)
        # A Bias, time constant and gains value for each non-input neuron
        self.genotype_size += sum(self.layers_list[1:]) * 3

    def write_to_log(self):
        print "Gen:", "%.2d" % self.current_generation, "\tBest fitness:", \
            "%.3f" % round(self.ea.phenotype_adult_pool[0].fitness_value, 3), "\tAverage fitness:", \
            "%.3f" % round(self.ea.avg_fitness, 3), "\tStandard deviation: ", \
            "%.5f" % round(self.ea.standard_deviation, 3), \
            "Fitness components:", self.ea.phenotype_adult_pool[0].fitness_components, "\tTime left:", \
            "%02d:%02d" % (divmod(self.timer * (self.horizontal_slider_12.get() - self.current_generation), 60)), \
            "\tBest Phenotype:", self.ea.phenotype_adult_pool[0].components
        self.fitness_log_average[self.current_run].append(self.ea.avg_fitness)
        self.fitness_log_best[self.current_run].append(self.ea.phenotype_adult_pool[0].fitness_value)
        self.standard_deviation_log[self.current_run].append(self.ea.standard_deviation)
示例#7
0
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)
示例#8
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)
示例#9
0
from evolutionary_algorithm import EvolutionaryAlgorithm

if __name__ == '__main__':
    ea = EvolutionaryAlgorithm(100, 100, [], 40, 3)
    ea.print_population()
    print(ea.run())
    print(ea.prun())
示例#10
0
class Gui(tk.Tk):
    def __init__(self, delay, nr_of_runs=1, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.delay = delay
        self.nr_of_runs = nr_of_runs
        self.current_run = 0
        self.current_generation = 0
        self.fitness_log_average = []
        self.fitness_log_best = []
        self.standard_deviation_log = []
        self.ea = None
        self.build_parameter_menu()

    def build_parameter_menu(self):
        self.horizontal_slider_1 = tk.Scale(
            self,
            length=1000,
            from_=2,
            to=1000,
            orient=tk.HORIZONTAL,
            label="Genotype Pool Size",
            resolution=2,
            command=self.control_pool_size_genotype)
        self.horizontal_slider_1.set(GENOTYPE_POOL_SIZE)
        self.horizontal_slider_1.pack()
        self.horizontal_slider_2 = tk.Scale(
            self,
            length=1000,
            from_=2,
            to=1000,
            orient=tk.HORIZONTAL,
            label="Adult Pool Size",
            resolution=2,
            command=self.control_pool_size_adult)
        self.horizontal_slider_2.set(ADULT_POOL_SIZE)
        self.horizontal_slider_2.pack()
        self.horizontal_slider_3 = tk.Scale(self,
                                            length=1000,
                                            from_=2,
                                            to=1000,
                                            orient=tk.HORIZONTAL,
                                            label="Genotype Length")
        self.horizontal_slider_3.set(GENOTYPE_LENGTH)
        self.horizontal_slider_3.pack()
        crossover_slider_group = tk.Frame(self)
        self.horizontal_slider_4 = tk.Scale(crossover_slider_group,
                                            length=500,
                                            from_=0.0,
                                            to=1.0,
                                            resolution=-1,
                                            orient=tk.HORIZONTAL,
                                            label="Crossover Rate")
        self.horizontal_slider_4.set(CROSSOVER_RATE)
        self.horizontal_slider_4.pack(side=tk.LEFT)
        self.horizontal_slider_5 = tk.Scale(self,
                                            length=1000,
                                            from_=0,
                                            to=1,
                                            resolution=-1,
                                            orient=tk.HORIZONTAL,
                                            label="Mutation Rate")
        self.horizontal_slider_5.set(MUTATION_RATE)
        self.horizontal_slider_5.pack()
        self.horizontal_slider_7 = tk.Scale(crossover_slider_group,
                                            length=500,
                                            from_=1,
                                            to=100,
                                            orient=tk.HORIZONTAL,
                                            label="Points of Crossover")
        self.horizontal_slider_7.set(POINTS_OF_CROSSOVER)
        self.horizontal_slider_7.pack(side=tk.LEFT)
        crossover_slider_group.pack(anchor=tk.W)
        self.horizontal_slider_8 = tk.Scale(self,
                                            length=1000,
                                            from_=0,
                                            to=300,
                                            orient=tk.HORIZONTAL,
                                            label="Zero-threshold")
        self.horizontal_slider_8.set(ZERO_THRESHOLD)
        self.horizontal_slider_8.pack()
        self.horizontal_slider_9 = tk.Scale(self,
                                            length=1000,
                                            from_=1,
                                            to=50,
                                            orient=tk.HORIZONTAL,
                                            label="Symbol Set Size")
        self.horizontal_slider_9.set(SYMBOL_SET_SIZE)
        self.horizontal_slider_9.pack(anchor=tk.W)
        tournament_slider_group = tk.Frame(self)
        self.horizontal_slider_11 = tk.Scale(tournament_slider_group,
                                             length=500,
                                             from_=3,
                                             to=500,
                                             orient=tk.HORIZONTAL,
                                             label="Tournament Size")
        self.horizontal_slider_11.set(TOURNAMENT_SIZE)
        self.horizontal_slider_11.pack(side=tk.LEFT)
        self.horizontal_slider_12 = tk.Scale(
            tournament_slider_group,
            length=500,
            from_=0,
            to=1,
            resolution=-1,
            orient=tk.HORIZONTAL,
            label="Tournament Slip-through Probability")
        self.horizontal_slider_12.set(TOURNAMENT_SLIP_THROUGH_PROBABILITY)
        self.horizontal_slider_12.pack(side=tk.LEFT)
        tournament_slider_group.pack(anchor=tk.W)
        self.horizontal_slider_13 = tk.Scale(self,
                                             length=1000,
                                             from_=1,
                                             to=1000,
                                             orient=tk.HORIZONTAL,
                                             label="Initial Temperature")
        self.horizontal_slider_13.set(INITIAL_TEMPERATURE)
        self.horizontal_slider_13.pack()
        self.horizontal_slider_14 = tk.Scale(self,
                                             length=1000,
                                             from_=1,
                                             to=10000,
                                             orient=tk.HORIZONTAL,
                                             label="Max Number of Generations")
        self.horizontal_slider_14.set(MAX_GENERATIONS)
        self.horizontal_slider_14.pack()

        self.mutation_protocol = tk.IntVar()
        self.mutation_protocol.set(MUTATION_PROTOCOL)
        radiogroup0 = tk.Frame(self)
        tk.Label(radiogroup0, text="Mutation Protocol").pack(anchor=tk.W)
        tk.Radiobutton(radiogroup0,
                       text="Individual",
                       variable=self.mutation_protocol,
                       value=1).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup0,
                       text="Component",
                       variable=self.mutation_protocol,
                       value=2).pack(side=tk.LEFT)
        radiogroup0.pack(anchor=tk.W)

        self.adult_selection_protocol = tk.IntVar()
        self.adult_selection_protocol.set(ADULT_SELECTION_PROTOCOL)
        radiogroup1 = tk.Frame(self)
        tk.Label(radiogroup1,
                 text="Adult Selection Protocol").pack(anchor=tk.W)
        tk.Radiobutton(radiogroup1,
                       text="Full",
                       variable=self.adult_selection_protocol,
                       command=self.control_pool_size_adult,
                       value=1).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup1,
                       text="Over Production",
                       variable=self.adult_selection_protocol,
                       command=self.control_pool_size_adult,
                       value=2).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup1,
                       text="Mixing",
                       variable=self.adult_selection_protocol,
                       value=3).pack(side=tk.LEFT)
        radiogroup1.pack(anchor=tk.W)

        self.parent_selection_protocol = tk.IntVar()
        self.parent_selection_protocol.set(PARENT_SELECTION_PROTOCOL)
        radiogroup2 = tk.Frame(self)
        tk.Label(radiogroup2,
                 text="Parent Selection Protocol").pack(anchor=tk.W)
        tk.Radiobutton(radiogroup2,
                       text="Fitness Proportionate",
                       variable=self.parent_selection_protocol,
                       value=1).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup2,
                       text="Sigma-scaling",
                       variable=self.parent_selection_protocol,
                       value=2).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup2,
                       text="Tournament selection",
                       variable=self.parent_selection_protocol,
                       value=3).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup2,
                       text="Boltzmann Selection",
                       variable=self.parent_selection_protocol,
                       value=4).pack(side=tk.LEFT)
        radiogroup2.pack(anchor=tk.W)

        self.problem_type = tk.IntVar()
        self.problem_type.set(PROBLEM)
        radiogroup3 = tk.Frame(self)
        tk.Label(radiogroup3, text="Problem type").pack(anchor=tk.W)
        tk.Radiobutton(radiogroup3,
                       text="One Max",
                       variable=self.problem_type,
                       value=1).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup3,
                       text="LOLZ Prefix",
                       variable=self.problem_type,
                       value=2).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup3,
                       text="Surprising Sequence Local",
                       variable=self.problem_type,
                       value=3).pack(side=tk.LEFT)
        tk.Radiobutton(radiogroup3,
                       text="Surprising Sequence Global",
                       variable=self.problem_type,
                       value=4).pack(side=tk.LEFT)
        radiogroup3.pack(anchor=tk.W)

        start_button = tk.Button(self,
                                 text="Start",
                                 width=20,
                                 command=self.start_simulation)
        start_button.pack()

    def control_pool_size_genotype(self, event):
        if self.adult_selection_protocol.get() == 1:
            self.horizontal_slider_2.set(self.horizontal_slider_1.get())
        elif self.adult_selection_protocol.get() == 2:
            self.horizontal_slider_2.set(
                min(self.horizontal_slider_2.get(),
                    self.horizontal_slider_1.get() - 2))

    def control_pool_size_adult(self, event=None):
        if self.adult_selection_protocol.get() == 1:
            self.horizontal_slider_1.set(self.horizontal_slider_2.get())
        elif self.adult_selection_protocol.get() == 2:
            self.horizontal_slider_1.set(
                max(self.horizontal_slider_1.get(),
                    self.horizontal_slider_2.get() - 6))
        if self.parent_selection_protocol.get() == 3:
            self.horizontal_slider_11.set(
                min(self.horizontal_slider_11.get(),
                    self.horizontal_slider_2.get() // 2))

    def start_simulation(self):
        self.current_run = 0
        self.fitness_log_average = []
        self.fitness_log_best = []
        self.standard_deviation_log = []
        self.start_ea()

    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()

    def run_ea(self):
        self.ea.run_one_life_cycle()
        self.write_to_log()
        self.current_generation += 1
        if self.current_generation < self.horizontal_slider_14.get() and \
                self.ea.phenotype_adult_pool[0].fitness_value < 1.0:
            self.after(self.delay, lambda: self.run_ea())
        else:
            print "End"
            self.current_run += 1
            if self.current_run < self.nr_of_runs:
                self.start_ea()
            else:
                print "Average Generations:", sum([
                    len(l) for l in self.fitness_log_average
                ]) / len(self.fitness_log_average)
                self.plot_data()

    def write_to_log(self):
        print "Gen:", "%.2d" % self.current_generation, "\tBest fitness:", \
            "%.3f" % round(self.ea.phenotype_adult_pool[0].fitness_value, 3), "\tAverage fitness:", \
            "%.3f" % round(self.ea.avg_fitness, 3), "\tStandard deviation: ", \
            "%.5f" % round(self.ea.standard_deviation, 3), "\tBest Phenotype:", \
            self.ea.phenotype_adult_pool[0].components
        if self.problem_type.get() > 2:
            print "Violations: ", self.ea.phenotype_adult_pool[0].violations
        self.fitness_log_average[self.current_run].append(self.ea.avg_fitness)
        self.fitness_log_best[self.current_run].append(
            self.ea.phenotype_adult_pool[0].fitness_value)
        self.standard_deviation_log[self.current_run].append(
            self.ea.standard_deviation)

    def plot_data(self):
        plt.figure(1)
        plt.subplot(311)
        plt.plot(self.fitness_log_average[-1], label="Average fitness")
        plt.plot(self.fitness_log_best[-1], label="Best fitness")
        #plt.legend(['y = Average fitness in adult pool', 'y = Best fitness in adult pool'], loc='lower right')
        plt.legend(bbox_to_anchor=(0., 1.02, 1., .102),
                   loc=3,
                   ncol=2,
                   mode="expand",
                   borderaxespad=0.)

        plt.subplot(312)
        plt.plot(self.fitness_log_best[-1])
        plt.legend(['y = Best fitness in adult pool'], loc='lower right')

        plt.subplot(313)
        plt.plot(self.standard_deviation_log[-1])
        plt.legend(['y = Standard deviation'], loc='upper right')
        plt.show()