Exemplo n.º 1
0
def extract_phenotype(agents, filename, method='ratio'):
    """Extract phenotype of the learning agents.

    Sort the agents based on the overall fitness and then based on the
    method extract phenotype of the agents.
    Method can take {'ratio','higest','sample'}
    """
    sorted_agents = sorted(agents,
                           key=lambda x: x.individual[0].fitness,
                           reverse=True)

    if method == 'ratio':
        ratio_value = 0.4
        upper_bound = ratio_value * len(agents)
        selected_agents = agents[0:int(upper_bound)]
        selected_phenotype = [
            agent.individual[0].phenotype for agent in selected_agents
        ]
        # return selected_phenotype
    else:
        selected_phenotype = [sorted_agents[0].individual[0].phenotype]
        # return [sorted_agents[0].individual[0].phenotype]

    # Save the phenotype to a json file
    JsonPhenotypeData.to_json(selected_phenotype, filename)

    # Return the phenotype
    return selected_phenotype
Exemplo n.º 2
0
def evolve(iteration):
    """Learning Algorithm block."""
    # iteration = 10000

    env = EvolModel(100,
                    100,
                    100,
                    10,
                    iter=iteration,
                    expname='COTCommEvolve',
                    agent='EvolAgent',
                    parm='swarm_mcarry_comm.txt')
    env.build_environment_from_json()

    # for all agents store the information about hub
    for agent in env.agents:
        agent.shared_content['Hub'] = {env.hub}

    # Hub and site object
    # print(env.hub, env.site)

    # Iterate and execute each step in the environment
    for i in range(iteration):
        env.step()

    env.experiment.update_experiment()

    # Collecting phenotypes on the basis of food collected
    # Find if food has been deposited in the hub

    food_objects = env.food_in_loc(env.hub.location)
    # print('Total food in the hub evolution:', len(food_objects))
    env.phenotypes = []
    for food in food_objects:
        print(food.phenotype)
        env.phenotypes += list(food.phenotype.values())

    jfilename = env.pname + '/' + env.runid + '.json'

    JsonPhenotypeData.to_json(env.phenotypes, jfilename)

    # Not using this method right now
    # env.phenotypes = extract_phenotype(env.agents, jfilename)

    # Plot the fitness in the graph
    graph = Graph(env.pname, 'best.csv', ['explore', 'foraging'])
    graph.gen_best_plots()

    # Test the evolved behavior
    return env
Exemplo n.º 3
0
def evolve(iteration):
    """Learning Algorithm block."""
    # iteration = 10000

    env = EvolModel(100,
                    100,
                    100,
                    10,
                    iter=iteration,
                    expname='NMComm',
                    agent='EvolAgent',
                    parm='swarm_comm.txt')
    env.build_environment_from_json()

    # for all agents store the information about hub
    for agent in env.agents:
        agent.shared_content['Hub'] = {env.hub}

    # Iterate and execute each step in the environment
    for i in range(iteration):
        env.step()

    env.experiment.update_experiment()

    # Collecting phenotypes on the basis of debris collected
    # Find if debris has been cleaned from the hub
    debris_objects = env.debris_cleaned()

    env.phenotypes = []
    for debris in debris_objects:
        print(debris.phenotype)
        env.phenotypes += list(debris.phenotype.values())

    jfilename = env.pname + '/' + env.runid + '.json'
    JsonPhenotypeData.to_json(env.phenotypes, jfilename)

    # Not using this method right now
    # env.phenotypes = extract_phenotype(env.agents, jfilename)

    # Plot the fitness in the graph
    graph = Graph(env.pname, 'best.csv', ['explore', 'foraging'])
    graph.gen_best_plots()

    # Test the evolved behavior
    return env
Exemplo n.º 4
0
def phenotype_to_json(pname, runid, phenotypes):
    """Store the phenotype to a json file."""
    jfilename = pname + '/' + runid + '.json'
    JsonPhenotypeData.to_json(phenotypes, jfilename)