Esempio n. 1
0
def evaluation(best, N, test_runs, output_dir, random=False):
    print 'TEST RUN'
    results = []
    position = []

    for _ in range(test_runs):
        agents = [Agent([-1 for _ in range(IND_SIZE)]) for _ in range(NUM_IND)]

        if random:
            best = Agent([-1] * 32)

        agents[0] = best
        game = Game(agents, N)

        game.play()
        results.append(best.score)
        position.append(
            sorted([a.score for a in agents], reverse=True).index(best.score))

    # Save results
    if not os.path.exists(output_dir):
        os.mkdir(output_dir)

    plt.figure()
    plt.hist(results)
    plt.xlabel('Score')
    plt.ylabel('Frequency')
    plt.savefig(os.path.join(output_dir, 'scores.pdf'))

    plt.figure()
    plt.hist(position)
    plt.xlabel('Rank')
    plt.ylabel('Frequency')
    plt.savefig(os.path.join(output_dir, 'ranks.pdf'))

    with open(os.path.join(output_dir, 'scores.csv'), 'w') as f:
        w = writer(f)
        w.writerow(results)

    with open(os.path.join(output_dir, 'ranks.csv'), 'w') as f:
        w = writer(f)
        w.writerow(position)

    with open(os.path.join(output_dir, 'strategy.txt'), 'w') as f:
        f.write(str(best.strategy))

    return results, position
Esempio n. 2
0
def singleAgentEvolution(CXPB, MUTPB, NGEN, N):
    pop = toolbox.population(n=NUM_IND)
    agents = [Agent(ind) for ind in pop]
    game = Game(agents, N)

    # Evaluate the entire population
    fitnesses = game.play()
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit

    for g in range(NGEN):
        # Select individual to be evolved
        index = random.randint(0, len(pop) - 1)
        # Select the new generation individuals
        offspring = toolbox.select(pop, len(pop))
        # Clone the selected individuals
        offspring = map(toolbox.clone, offspring)

        # Apply crossover and mutation on the offspring
        child1 = offspring[index]
        for child2 in [
                element for idx, element in enumerate(pop) if idx != index
        ]:
            if random.random() < CXPB:
                toolbox.mate(child1, child2)
                continue

        mutant = offspring[index]
        if random.random() < MUTPB:
            toolbox.mutate(mutant)

        # Set new (evolved) genotype
        agents[index].setStrategy(pop[index])

        # The chosen individual is replaced by the offspring
        pop[index] = offspring[index]

        # Evaluate the individuals
        fitnesses = game.play()
        for ind, fit in zip(pop, fitnesses):
            ind.fitness.values = fit

    # get best agent
    best = agents[0]
    for agent in agents:
        if best.score < agent.score:
            best = agent

    return best
Esempio n. 3
0
def multiAgentEvolution(CXPB, MUTPB, NGEN, N):
    pop = toolbox.population(n=NUM_IND)
    agents = [Agent(ind) for ind in pop]
    game = Game(agents, N)

    # Evaluate the entire population
    fitnesses = game.play()
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit

    for g in range(NGEN):
        # Select the next generation individuals
        offspring = toolbox.select(pop, len(pop))
        # Clone the selected individuals
        offspring = map(toolbox.clone, offspring)

        # Apply crossover and mutation on the offspring
        for child1, child2 in zip(offspring[::2], offspring[1::2]):
            if random.random() < CXPB:
                toolbox.mate(child1, child2)

        for mutant in offspring:
            if random.random() < MUTPB:
                toolbox.mutate(mutant)

        # Set new (evolved) genotypes
        for agent, ind in zip(agents, offspring):
            agent.setStrategy(ind)

        # Evaluate the individuals
        fitnesses = game.play()
        for ind, fit in zip(offspring, fitnesses):
            ind.fitness.values = fit

        # The population is entirely replaced by the offspring
        pop[:] = offspring

    # get best agent
    best = agents[0]
    for agent in agents:
        if best.score < agent.score:
            best = agent

    return best
Esempio n. 4
0
    with open(os.path.join(output_dir, 'ranks.csv'), 'w') as f:
        w = writer(f)
        w.writerow(position)

    with open(os.path.join(output_dir, 'strategy.txt'), 'w') as f:
        f.write(str(best.strategy))

    return results, position


if __name__ == '__main__':
    CXPB, MUTPB, NGEN, N, TEST_RUNS = 0.5, 0.2, 10000, 1000, 1000

    NUM_IND = 20
    # Baseline agent who never changes society
    best = Agent([-1] * 32)
    evaluation(best, N, TEST_RUNS, 'baseline_20', random=True)

    # All agents evolving at the same time
    best = multiAgentEvolution(CXPB, MUTPB, NGEN, N)
    evaluation(best, N, TEST_RUNS, 'multi_evolution_20')

    # One agent evolving at a time
    best = singleAgentEvolution(CXPB, MUTPB, NGEN, N)
    evaluation(best, N, TEST_RUNS, 'single_evolution_20')

    NUM_IND = 100
    # Baseline agent who never changes society
    best = Agent([-1] * 32)
    evaluation(best, N, TEST_RUNS, 'baseline_100', random=True)
Esempio n. 5
0
 def __init__(self,sys1 = Agent(),sys2 = Agent()):
     self.system_1_model = System1Agent()
     self.system_2_model = System2Agent()
     self.count = 0
def game_setup(num_agents):
    game = Game()
    agents = [Agent() for i in range(num_agents)]
    ratings = [Rating() for i in range(num_agents)]
    return game, agents, ratings