Exemplo n.º 1
0
def main():
    """Block main."""
    iteration = 9000

    # Create a test environment to visualize
    env = ViewerModel(50, width, height, 10, iter=iteration, viewer=viewer)

    # Build environment from json
    env.build_environment_from_json()

    # Load a json file containing the phenotype
    pfileloc = '/home/aadeshnpn/Documents/BYU/hcmi/hri/cooperative_transport/1538447335350COT/'
    jname = pfileloc + '1538447335350.json'

    phenotypes = JsonPhenotypeData.load_json_file(jname)['phenotypes']
    # Create the agents in the environment from the sampled behaviors
    # print (len(phenotypes))
    env.create_agents(phenotypes=phenotypes[30:90])
    # Overiding the default viewer properties
    env.ui = UI((width, height), [env.hub],
                env.agents, [env.site],
                food=env.foods)
    # Iterate and execute each step in the environment
    for i in range(iteration):
        env.step()

    print('foraging percent', env.foraging_percent())
    # print('Execution done')
    # Find if food has been deposited in the hub
    grid = env.grid
    neighbours = grid.get_neighborhood(env.hub.location, 10)
    food_objects = grid.get_objects_from_list_of_grid('Food', neighbours)
Exemplo n.º 2
0
def main():
    """Block main."""
    iteration = 5000

    # Create a test environment to visualize
    env = ViewerModel(100, width, height, 10, iter=iteration, viewer=viewer)

    # Build environment from json
    env.build_environment_from_json()

    # Load a json file containing the phenotype
    pfileloc = ('/home/aadeshnpn/Documents/BYU/hcmi/swarm/' +
                'results/1550156973273193-5000ValidateSForge/')
    jname = pfileloc + '1550156973273193-4999.json'

    phenotypes = JsonPhenotypeData.load_json_file(jname)['phenotypes']
    # Create the agents in the environment from the sampled behaviors
    # print (len(phenotypes))
    env.create_agents(phenotypes=phenotypes)
    # Overiding the default viewer properties
    env.ui = UI((width, height), [env.hub],
                env.agents, [env.site],
                food=env.foods)
    # Iterate and execute each step in the environment
    for i in range(iteration):
        env.step()

    # print('Execution done')
    # Find if food has been deposited in the hub
    print('foraging percent', env.foraging_percent())
Exemplo n.º 3
0
def main(iter):
    """Block for the main function."""
    print('=======Start=========')
    # env = evolve(iter)
    pname = (
        '/home/aadeshnpn/Documents/BYU/hcmi/hri/nest_maint/10000NestMAgents')
    jfilename = pname + '/1539014820252.json'
    jdata = JsonPhenotypeData.load_json_file(jfilename)
    phenotypes = jdata['phenotypes']

    # if len(env.phenotypes) > 1:
    for N in range(425, 550, 25):
        if len(phenotypes) > 1:
            steps = [5000 for i in range(16)]
            aname = pname + '/' + str(N)
            os.mkdir(aname)
            env = (phenotypes, aname)
            Parallel(n_jobs=16)(delayed(simulate)(env, i, N) for i in steps)
        # jname = '/home/aadeshnpn/Documents/BYU/hcmi/swarm/results/
        # 1538612308183NestM/1538612308183.json'
        # phenotype = JsonPhenotypeData.load_json_file(jname)
        # pname = '/home/aadeshnpn/Documents/BYU/hcmi/swarm/results
        # /1538612308183NestM/'
        # env = (phenotype['phenotypes'], pname)
        # Parallel(n_jobs=8)(delayed(simulate)(env, i) for i in steps)
        # for step in steps:
        #    simulate(env, step)
        # Parallel(n_jobs=4)(delayed(simulate_res1)(env, i) for i in steps)
        # Parallel(n_jobs=4)(delayed(simulate_res2)(env, i) for i in steps)
        # simulate(env, 10000)

    print('=======End=========')
Exemplo n.º 4
0
def test_json_phenotype(json):
    jname = '/home/aadeshnpn/Documents/BYU/hcmi/swarm/results/1550083569946511-12000EvoSForge/' + json  # noqa : E501
    # jname = '/tmp/1543367322976111-8000EvoSForge/' + json
    phenotype = JsonPhenotypeData.load_json_file(jname)['phenotypes']
    print(len(phenotype))
    # phenotype = ' '
    test_loop(phenotype, 5000)
Exemplo n.º 5
0
def main():
    """Block main."""
    iteration = 5000

    # Create a test environment to visualize
    env = ViewerModel(100, width, height, 20, iter=iteration, viewer=viewer)

    # Build environment from json
    env.build_environment_from_json()

    # Load a json file containing the phenotype
    pfileloc = '/home/aadeshnpn/Documents/BYU/hcmi/hri/thesis/sf/'
    jname = pfileloc + '1538473090382007.json'

    phenotypes = JsonPhenotypeData.load_json_file(jname)['phenotypes']
    # Create the agents in the environment from the sampled behaviors
    env.create_agents(phenotypes=phenotypes)
    # Overiding the default viewer properties
    env.ui = UI((width, height), [env.hub],
                env.agents,
                env.sites,
                food=env.foods,
                debris=[])
    # Iterate and execute each step in the environment
    for i in range(iteration):
        env.step()

    # print('Execution done')
    # Find if food has been deposited in the hub
    grid = env.grid
    neighbours = grid.get_neighborhood(env.hub.location, 10)
    food_objects = grid.get_objects_from_list_of_grid('Food', neighbours)
    for food in food_objects:
        print('food', food.id, food.location)
        """
Exemplo n.º 6
0
def main():
    """Block main."""
    iteration = 5000

    # Create a test environment to visualize
    env = ViewerModel(100, width, height, 10, iter=iteration, viewer=viewer)

    # Build environment from json
    env.build_environment_from_json()

    # Load a json file containing the phenotype
    pfileloc = '/home/aadeshnpn/Documents/BYU/hcmi/hri/nest_maint/1539014820252NestM/'
    jname = pfileloc + '1539014820252.json'

    phenotypes = JsonPhenotypeData.load_json_file(jname)['phenotypes']
    # Create the agents in the environment from the sampled behaviors
    print(len(phenotypes))

    env.create_agents(phenotypes=phenotypes)
    # Overiding the default viewer properties
    env.ui = UI((width, height), [env.hub],
                env.agents, [],
                food=[],
                debris=env.debris)
    # Iterate and execute each step in the environment
    for i in range(iteration):
        env.step()

    # print('Execution done')
    # Find if food has been deposited in the hub
    print('Cleaning Percentage', env.foraging_percent())
    print(len(env.debris_cleaned()))
Exemplo n.º 7
0
def test_json_phenotype(json):
    # jname = '/home/aadeshnpn/Documents/BYU/hcmi/swarm/results/1543189989736115-62000EvoSForge/' + json  # noqa : E501
    jname = '/tmp/1543367322976111-8000EvoSForge/' + json
    phenotype = JsonPhenotypeData.load_json_file(jname)['phenotypes']
    # print (phenotype)
    # phenotype = ' '
    if validation_loop(phenotype, 2000):
        print('foraging success')
Exemplo n.º 8
0
def main():
    """Parse args and call bt visualize module."""
    # dname = '/home/aadeshnpn/Documents/BYU/hcmi/hri/supporting_materials/nest_maintenance'    # noqa: E501
    # jfname = dname + '/1539014820252.json'
    # dname = '/home/aadeshnpn/Documents/BYU/hcmi/hri/supporting_materials/cooperative_transport'   # noqa: E501
    # jfname = dname + '/1538447335350.json'
    dname = '/home/aadeshnpn/Documents/BYU/hcmi/hri/supporting_materials/foraging'  # noqa: E501
    jfname = dname + '/1538473090382007.json'
    jdata = JsonPhenotypeData.load_json_file(jfname)
    phenotypes = jdata['phenotypes']
    for i in range(len(phenotypes)):
        # if i >= 5:
        #    break
        m = Model()
        a = Agent('1', m)
        bt = BTConstruct(None, a, phenotypes[i])
        bt.construct()
        bt.visualize(name=dname + '/' + str(i))
Exemplo n.º 9
0
def simulate(env, iteration):
    """Test the performane of evolved behavior."""
    # phenotype = agent.individual[0].phenotype
    # phenotypes = extract_phenotype(agents)
    phenotypes = JsonPhenotypeData.load_json_file(env[0])
    # print (phenotypes)
    if len(phenotypes['phenotypes']) < 1:
        return
    else:
        phenotypes = phenotypes['phenotypes']
    threshold = 1.0

    sim = SimModel(
        100, 100, 100, 10, iter=iteration, xmlstrings=phenotypes, pname=env[1])
    sim.build_environment_from_json()

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

    simresults = SimulationResults(
        sim.pname, sim.connect, sim.sn, sim.stepcnt, sim.food_in_hub(),
        phenotypes[0]
        )

    simresults.save_phenotype()
    simresults.save_to_file()

    # Iterate and execute each step in the environment
    for i in range(iteration):
        # For every iteration we need to store the results
        # Save them into db or a file
        sim.step()
        simresults = SimulationResults(
            sim.pname, sim.connect, sim.sn, sim.stepcnt, sim.food_in_hub(),
            phenotypes[0]
            )
        simresults.save_to_file()

    # print ('food at site', len(sim.food_in_loc(sim.site.location)))
    # print ('food at hub', len(sim.food_in_loc(sim.hub.location)))
    # print("Total food in the hub", len(food_objects))

    # food_objects = sim.food_in_loc(sim.hub.location)

    # for food in food_objects:
    #    print('simulate phenotye:', dir(food))
    value = sim.food_in_hub()

    foraging_percent = (
        value * 100.0) / (sim.num_agents * 2.0)

    sucess = False
    print('Foraging percent', value)

    if foraging_percent >= threshold:
        print('Foraging success')
        sucess = True

    sim.experiment.update_experiment_simulation(value, sucess)

    # Plot the fitness in the graph
    graph = GraphACC(sim.pname, 'simulation.csv')
    graph.gen_plot()