Ejemplo n.º 1
0
def main():
    iteration = 100

    env = EnvironmentModel(100, 100, 100, 10, iter=iteration)
    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()

    # Find if food has been deposited in the hub
    grid = env.grid
    food_loc = (0, 0)
    neighbours = grid.get_neighborhood(food_loc, 5)
    food_objects = grid.get_objects_from_list_of_grid('Food', neighbours)
    for food in food_objects:
        print('food', food.id, food.location)

    # Plot the fitness in the graph
    graph = Graph(env.pname, 'best.csv', ['diversity', 'explore'])
    graph.gen_best_plots()
    """
Ejemplo n.º 2
0
def learning_phase(iteration, early_stop=False):
    """Learning Algorithm block."""
    # Evolution environment
    env = EvolveModel(100, width, height, 10, iter=iteration)
    env.build_environment_from_json()
    env.create_agents()
    # Validation Step parameter
    # Run the validation test every these many steps
    validation_step = 6000

    # Iterate and execute each step in the environment
    # Take a step i number of step in evolution environment
    # Take a 1000 step in validation environment sampling from the evolution
    # Make the validation envronmnet same as the evolution environment
    for i in range(iteration):
        # Take a step in evolution
        env.step()
        if (i + 1) % validation_step == 0:
            try:
                phenotypes = env.behavior_sampling_objects(ratio_value=0.1)
                # save the phenotype to json file
                phenotype_to_json(
                    env.pname, env.runid, phenotypes)
                validation_loop(
                    phenotypes, 5000, parentname=env.pname)
            except ValueError:
                pass
            # Plot the fitness in the graph
            graph = Graph(
                env.pname, 'best.csv', [
                    'explore', 'foraging', 'prospective', 'fitness'],
                pname='best' + str(i))
            graph.gen_best_plots()
            """
            if early_stop:
                # Update the experiment table
                env.experiment.update_experiment()

                # Return phenotypes
                return phenotypes
            """
    # Update the experiment table
    env.experiment.update_experiment()

    allphenotypes = env.behavior_sampling_objects(ratio_value=0.99)
    # save the phenotype to json file
    phenotype_to_json(
        env.pname, env.runid + '-' + 'all', allphenotypes)
    try:
        # return list(phenotypes.keys())
        return phenotypes
    except UnboundLocalError:
        return None
Ejemplo n.º 3
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
Ejemplo n.º 4
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
Ejemplo n.º 5
0
def learning_phase(iteration, early_stop=False):
    """Learning Algorithm block."""
    # Evolution environment
    env = EvolveModel(100, 100, 100, 10, iter=iteration)
    env.build_environment_from_json()
    env.create_agents()
    # Validation Step parameter
    # Run the validation test every these many steps
    validation_step = 1000

    # Iterate and execute each step in the environment
    # Take a step i number of step in evolution environment
    # Take a 1000 step in validation environment sampling from the evolution
    # Make the validation envronmnet same as the evolution environment
    for i in range(iteration):
        # Take a step in evolution
        env.step()
        if (i + 1) % validation_step == 0:
            phenotypes = env.behavior_sampling()
            # save the phenotype to json file
            phenotype_to_json(env.pname, env.runid + '-' + str(i), phenotypes)
            # early_stop = validation_loop(phenotypes, 2000)

            # Plot the fitness in the graph
            graph = Graph(env.pname,
                          'best.csv',
                          ['explore', 'foraging', 'prospective', 'fitness'],
                          pname='best' + str(i))
            graph.gen_best_plots()
            """
            if early_stop:
                # Update the experiment table
                env.experiment.update_experiment()

                # Return phenotypes
                return phenotypes
            """
    # Update the experiment table
    env.experiment.update_experiment()
    return phenotypes
Ejemplo n.º 6
0
def learning_phase(iteration, early_stop=False):
    """Learning Algorithm block."""
    # Evolution environment
    env = EvolveModel(50, width, height, 10, iter=iteration)
    env.build_environment_from_json()
    env.create_agents()
    # Validation Step parameter
    # Run the validation test every these many steps
    validation_step = 6000

    # Iterate and execute each step in the environment
    # Take a step i number of step in evolution environment
    # Take a 1000 step in validation environment sampling from the evolution
    # Make the validation envronmnet same as the evolution environment
    for i in range(iteration):
        # Take a step in evolution
        env.step()
        if (i + 1) % validation_step == 0:
            try:
                # print([agent.individual[0].fitness for agent in env.agents])
                # msg = []
                # for agent in env.agents:
                #    encode = agent.individual[0].phenotype.encode('utf-8')
                #    msg += [(
                #        agent.name, hashlib.sha224(encode).hexdigest(
                #        ), agent.individual[0].fitness)]
                # n,p,f = zip(*msg)
                # print (i, p[:10])

                # ratio = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.99]
                # for r in ratio:
                phenotypes = env.behavior_sampling_objects(ratio_value=0.1)
                # save the phenotype to json file
                phenotype_to_json(env.pname, env.runid, phenotypes)
                # early_stop = validation_loop(phenotypes, 5000)
                validation_loop(phenotypes, 5000, parentname=env.pname)
            except ValueError:
                pass
            # Plot the fitness in the graph
            graph = Graph(env.pname,
                          'best.csv',
                          ['explore', 'foraging', 'prospective', 'fitness'],
                          pname='best' + str(i))
            graph.gen_best_plots()
            """
            if early_stop:
                # Update the experiment table
                env.experiment.update_experiment()

                # Return phenotypes
                return phenotypes
            """
    # Update the experiment table
    env.experiment.update_experiment()
    """
    hashlist = dict()
    phenotypes = dict()
    # generations = [agent.individual for agent in env.agents]
    for agent in env.agents:
        encode = agent.individual[0].phenotype.encode('utf-8')
        hashval = hashlib.sha224(encode).hexdigest()
        print(
            agent.name, hashval,
            agent.individual[0].fitness, agent.food_collected)
        phenotypes[agent.individual[0].phenotype] = agent.individual[0].fitness
        try:
            hashlist[hashval] += 1
        except KeyError:
            hashlist[hashval] = 1
    print(hashlist)
    """
    # print('max, min generations', np.max(generations), np.min(generations))
    # pdb.set_trace()
    allphenotypes = env.behavior_sampling_objects(ratio_value=0.99)
    # save the phenotype to json file
    phenotype_to_json(env.pname, env.runid + '-' + 'all', allphenotypes)
    try:
        # return list(phenotypes.keys())
        return phenotypes
    except UnboundLocalError:
        return None