示例#1
0
def test():
    # Initialize
    darwinian = Evolution(10, 5)

    # Evolve
    darwinian.evolve(1000)

    # Get the best agent
    best_agent = darwinian.get_best_agent()

    # Visualize the performance
    for i_episode in range(10):
        observation = darwinian.env.reset()
        for t in range(500):
            darwinian.env.render()

            action = best_agent.action(observation)
            observation, reward, done, info = darwinian.env.step(action)
            if done:
                print("Episode finished after {} timesteps".format(t + 1))
                break

    # Plot the statistics
    stats = darwinian.getStats()
    plt.plot(stats[0], stats[1])
    plt.ylabel("Best Rewarded Genome")
    plt.xlabel("Generation")
    plt.show()

    # Sleep a bit
    time.sleep(1)
    darwinian.env.close()
示例#2
0
文件: egt.py 项目: peihy/PyEGT
def repeat_b():
    # 博弈收益参数不同,合作率曲线
    e = Evolution(has_mut=False)
    G = nx.random_regular_graph(4, 1000)
    p = Population(G)
    b = 5
    for i in range(b):
        g = game.PDG(i * 2 + 2)
        e.set_population(p).set_game(g).set_rule(u)
        print('Control Variable b: %d' % (i * 2 + 2))
        e.evolve(100000, restart=True, quiet=True, autostop=False)
        e.show(fmt[i], label="b=%d" % (i * 2 + 2))
    plt.legend(loc='lower right')
    plt.show()
示例#3
0
文件: egt.py 项目: peihy/PyEGT
def repeat2d():
    e = Evolution()
    bs = np.linspace(1, 10, 3)
    # fig, axes = plt.subplots()
    colors = 'brgcmykwa'
    symbs = '.ox+*sdph-'
    for i in range(1, 10):
        i = 4
        G = nx.random_regular_graph(i + 1, 1000)
        p = Population(G)
        a = [0] * len(bs)
        for j, b in enumerate(bs):
            g = game.PDG(b)
            e.set_population(p).set_game(g).set_rule(u)
            e.evolve(10000)
            a[j] = e.cooperate[-1]
            plt.plot(bs, a, colors[j] + symbs[j], label='b=%f' % b)
        break
    plt.show()
示例#4
0
文件: egt.py 项目: peihy/PyEGT
def repeat_start_pc():
    # 初始Pc不同的合作变化曲线
    # G = nx.watts_strogatz_graph(1000, 4, 0.2)
    G = nx.barabasi_albert_graph(1000, 3)
    p = Population(G)
    g = game.PDG(b=10)
    u = rule.DeathBirth()
    e = Evolution(has_mut=False)
    e.set_population(p).set_game(g).set_rule(u)
    for i in range(5):
        pc = (2 * i + 1) / 10.0
        p.init_strategies(g, [pc, 1 - pc])
        print('Initial P(C) is %.2f' % pc)
        e.evolve(100000, restart=True, quiet=True, autostop=False)
        e.show(fmt[i], label=r'start $P_C$=%.2f' % pc)
    plt.legend(loc='lower right')
    plt.title(r'Evolution under Different Start $P_C$')
    plt.xlabel('Number of generations')
    plt.ylabel(r'Fraction of cooperations, $\rho_c$')
    plt.show()
示例#5
0
文件: egt.py 项目: peihy/PyEGT
def repeat_k():
    # 网络平均度不同,合作率曲线
    e = Evolution(has_mut=False)
    k = 5
    a = [0] * k
    for i in range(k):
        G = nx.random_regular_graph(i * 2 + 2, 1000)
        p = Population(G)
        e.set_population(p).set_game(g).set_rule(u)
        print('Control Variable k: %d' % (i * 2 + 2))
        e.evolve(100000, restart=True, quiet=True)
        # TODO: if e is CoEvolution, population need re-copy
        # a[i] = e.cooperate[-1]
        e.show(fmt[i], label="k=%d" % (i * 2 + 2))
    # plt.plot(range(2, k+1), a[1:], 'r-')
    # plt.plot([400+i*i for i in range(20)], 'ro--', label='k=4')
    # plt.plot([400 + i for i in range(20)], 'g^-.', label='k=6')
    # plt.plot([400 - i for i in range(20)], 'cx:', label='k=8')
    plt.legend(loc='lower right')
    plt.show()
示例#6
0
def run():
    # Initialize
    darwinian = Evolution(15, 6)

    # Evolve
    darwinian.evolve(150)

    # Get the best agent
    best_genome = darwinian.get_best_genome()

    # Visualize the performance
    for i_episode in range(10):
        observation = darwinian.env.reset()
        for t in range(500):
            darwinian.env.render()

            action = best_genome.action(observation)
            observation, reward, done, info = darwinian.env.step(action)
            if done:
                print("Episode finished after {} timesteps".format(t+1))
                break

    # Display the network architecture
    visualize(best_genome.get_inputs(),
        best_genome.get_outputs(), best_genome.get_connections())

    # Plot the statistics
    stats = darwinian.get_statistics()
    plt.plot(stats[0], stats[1]) # average
    plt.plot(stats[0], stats[2]) # best
    plt.ylabel("Reward")
    plt.xlabel("Generation")
    plt.legend(['average', 'best'], loc='upper left')
    plt.show()

    # Sleep a bit
    time.sleep(1)
    darwinian.env.close()
示例#7
0
 def next_generation(self):
     self.current_id = 0
     self.avg_fitness = 0
     if self.create:
         brains = [snake.brain for snake in self.snakes.values()]
         generation = Evolution(brains, self.selection, self.crossover,
                                self.mutation, self.seed, self.generation)
         brains = generation.evolve()
         self.snakes = dict()
         for snk_id in range(self.population):
             snake = Snake(snk_id, self.initial_pos, brains[snk_id])
             self.snakes[snk_id] = snake
     else:
         for snk_id in range(self.population):
             snake = Snake.load(self.generation, snk_id)
             self.snakes[snk_id] = snake
     self.reset_game()
示例#8
0
def f2(x):
    m = len(x)
    g = 1.0 + 10.0 * (m - 1)
    for i in range(1, m):
        g += (x[i]**2 - 10.0 * np.cos(4.0 * np.pi * x[i]))

    h = 1.0 - np.sqrt(x[0] / g)

    result = g * h

    return result


problem = Problem(num_of_variables=10,
                  objectives=[f1, f2],
                  variables_range=[(-5, 5)],
                  same_range=True,
                  expand=False)
problem.variables_range[0] = (0, 1)
print(problem.variables_range)
evo = Evolution(problem, mutation_param=5)
func = [i.objectives for i in evo.evolve()]

function1 = [i[0] for i in func]
function2 = [i[1] for i in func]
plt.xlabel('Function 1', fontsize=15)
plt.ylabel('Function 2', fontsize=15)
plt.plot(function1, function2, 'bo')
plt.show()

np.save('zdt4', func)
示例#9
0
from evolution import Evolution, Species

# define a species (gene pool, gene length)
species = Species()

# define a set of 10 organisms of species `Species`
organisms = Evolution(species, pop_size=10)

organims.create()
fittest_organism = organisms.evolve(evolve_until_thresh_reached=True)
示例#10
0
    networks = evolution_obj.generate_population(population)

    for generations_idx in range(1, generations + 1):
        print("Generation - {} / {} :".format(generations_idx, generations))

        for network in networks:
            network.fit()

        average_loss = find_average_loss(networks)
        history_of_errors.append(average_loss)

        print("Average Loss : {}".format(average_loss))
        print("----------------------------------------------------")

        if (generations_idx != generations):
            networks = evolution_obj.evolve(networks)

    #----------------------------------------- Sort Final Generation Population.

    networks = sorted(networks, key=lambda x: x.loss, reverse=False)

    #-------------------------------------Top 3 Winners of the Evolutions.
    for network in networks[:3]:
        print(network.network)
        print(network.loss)

    #---------------------------------------for visualize
    plt.plot(list(range(1, generations + 1)), history_of_errors)

    # sns.lmplot(x='Attack', y='Defense', data=(range(1, generations+1), history_of_errors))
    # seaborn.scatterplot(x=None, y=None,
示例#11
0
文件: tsp.py 项目: hoangharry/tsp_ga
    start_time = time.time()
    
    def f_round(v):
        return int(v + 1) if (v - int(v)) > 0.5 else int(v) 

    def getvalue(idx_lst):

        value = 0
        if len(idx_lst) > len(set(idx_lst)):
            value = 1000000
        for idxf in idx_lst:
            idx = f_round(idxf)
            fromCity = tourmanager.getCity(idx)
            destination = None
            if idx + 1 < len(tourmanager.destinationCities):
                destination = tourmanager.getCity(idx + 1)
            else:
                destination = tourmanager.getCity(idx_lst[0])
            value += fromCity.distanceTo(destination)
        return -value
        
    prob = Problem(num_of_variables=len(tourmanager.destinationCities),
    objectives=[getvalue],
    variables_range=[(0,len(tourmanager.destinationCities) - 1)],
    expand=False
    )
    evo = Evolution(prob, mutation_param = 20, num_of_generations=100)
    evol = evo.evolve()
    print(-evol[0].objectives[0])

示例#12
0
文件: egt.py 项目: peihy/PyEGT
def once():
    e = Evolution(has_mut=True)
    e.set_population(p).set_game(g).set_rule(u)
    e.evolve(1000)
    e.show()