Exemple #1
0
def test_loop(phenotypes, iteration):
    """Test the phenotypes in a completely different environment."""
    # Create a validation environment instance
    test = TestModel(100, width, height, 10, iter=iteration)
    # Build the environment
    test.build_environment_from_json()
    # Create the agents in the environment from the sampled behaviors
    test.create_agents(phenotypes=phenotypes)
    # Store the initial result
    testresults = SimulationResults(test.pname, test.connect,
                                    test.sn, test.stepcnt,
                                    test.foraging_percent(), phenotypes[0])
    # Save the phenotype to a json file
    testresults.save_phenotype()
    # Save the data in a result csv file
    testresults.save_to_file()
    # Save the phenotype of json file
    phenotype_to_json(test.pname, test.runid, phenotypes)
    # Execute the BT in the environment
    for i in range(iteration):
        test.step()

        testresults = SimulationResults(test.pname, test.connect,
                                        test.sn, test.stepcnt,
                                        test.foraging_percent(), phenotypes[0])
        testresults.save_to_file()

    # Plot the result in the graph
    graph = GraphACC(test.pname, 'simulation.csv')
    graph.gen_plot()
Exemple #2
0
def simulate_res1(env, iteration, prob=0.5):
    """Test the performane of evolved behavior."""
    phenotypes = env[0]
    threshold = 1.0

    sim = SimModelRes1(150,
                       100,
                       100,
                       10,
                       iter=iteration,
                       xmlstrings=phenotypes,
                       pname=env[1],
                       prob=prob)
    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()
        value = sim.food_in_hub()

        foraging_percent = (value * 100.0) / (sim.num_agents * 1.0)
        simresults = SimulationResults(sim.pname, sim.connect, sim.sn,
                                       sim.stepcnt, foraging_percent,
                                       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))

    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()
Exemple #3
0
def simulate(agent, iteration):
    # Testing the performane of evolved behavior
    phenotype = agent.individual[0].phenotype
    # iteration = 10000
    threshold = 75.0
    sim = RunEnvironmentModel(100,
                              100,
                              100,
                              10,
                              iter=iteration,
                              xmlstring=phenotype)
    sim.build_environment_from_json()

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

    simresults = SimulationResults(sim.pname, sim.connect, sim.sn, sim.stepcnt,
                                   sim.food_in_hub(), phenotype)
    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(), phenotype)
        simresults.save_to_file()

    # print("Total food in the hub", len(food_objects))
    value = sim.food_in_hub()

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

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

    # sim.experiment.update_experiment_simulation(value, sucess)
    sim.experiment.update_experiment_simulation(value, sucess)

    # Plot the fitness in the graph
    graph = GraphACC(sim.pname, 'simulation.csv')
    graph.gen_plot()
Exemple #4
0
def validation_loop(phenotypes,
                    iteration,
                    parentname=None,
                    ratio=1,
                    threshold=10.0):
    """Validate the evolved behaviors."""
    # Create a validation environment instance
    # print('len of phenotype', len(set(phenotypes)))
    valid = ValidationModel(100,
                            width,
                            height,
                            10,
                            iter=iteration,
                            parent=parentname,
                            ratio=ratio)
    # print('parent:', parentname, ' children:', valid.runid)
    # Build the environment
    valid.build_environment_from_json()
    # Create the agents in the environment from the sampled behaviors
    valid.create_agents(phenotypes=phenotypes)
    # print('total food units', valid.total_food_units)
    # Print the BT
    # py_trees.display.print_ascii_tree(valid.agents[1].bt.behaviour_tree.root)
    # py_trees.logging.level = py_trees.logging.Level.DEBUG

    for i in range(iteration):
        valid.step()
        # print ([agent.location for agent in valid.agents])
        validresults = SimulationResults(valid.pname, valid.connect,
                                         valid.sn, valid.stepcnt,
                                         valid.foraging_percent(),
                                         phenotypes[0])
        validresults.save_to_file()

    # print('food in the hub', valid.agents[0].get_food_in_hub(False))
    # Save the phenotype to json file
    phenotype_to_json(valid.pname, valid.runid + '-' + str(i), phenotypes)

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

    # Return true if the sample behavior achieves a threshold
    if valid.foraging_percent() > threshold:
        return True
    else:
        return False
Exemple #5
0
def after_simluation(sim, phenotypes, iteration, threshold):
    """Compute stuffs after defining experiment environment."""
    # 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()
Exemple #6
0
def after_simulation(sim, phenotypes, iteration, threshold):
    """Step for all simulation after defining environment."""
    # for all agents store the information about hub
    for agent in sim.agents:
        agent.shared_content['Hub'] = {sim.hub}
        agent.shared_content['Obstacles'] = set(sim.obstacles)

    simresults = SimulationResults(sim.pname, sim.connect, sim.sn, sim.stepcnt,
                                   len(sim.debris_cleaned()), 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()
        debris_objects = sim.debris_cleaned()

        value = len(debris_objects)

        cleaned_percent = (value * 100.0) / (200.0)

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

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

    if cleaned_percent >= threshold:
        print('Debris clean 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()
Exemple #7
0
def simulate(env, iteration):
    """Test the performane of evolved behavior."""
    # phenotype = agent.individual[0].phenotype
    # phenotypes = extract_phenotype(agents)
    phenotypes = env[0]
    # phenotypes = ['<?xml version=\"1.0\" encoding=\"UTF-8\"?><Sequence><Sequence><Sequence><Selector><cond>IsDropable_Sites</cond><act>Explore</act></Selector><Sequence><cond>NeighbourObjects</cond><act>CompositeSingleCarry_Food</act></Sequence></Sequence> <Sequence><Sequence><cond>NeighbourObjects</cond><act>Explore</act></Sequence> <Sequence><cond>NeighbourObjects</cond><act>MoveTowards_Hub</act></Sequence></Sequence></Sequence> <Sequence><Selector><cond>NeighbourObjects</cond><act>MoveAway_Sites</act></Selector><Sequence><cond>NeighbourObjects</cond><act>CompositeDrop_Food</act></Sequence></Sequence></Sequence>', '<?xml version=\"1.0\" encoding=\"UTF-8\"?><Sequence><Sequence><Sequence><Selector><cond>IsDropable_Sites</cond><act>Explore</act></Selector><Sequence><cond>NeighbourObjects</cond><act>CompositeSingleCarry_Food</act></Sequence></Sequence> <Sequence><Sequence><cond>NeighbourObjects</cond><act>Explore</act></Sequence> <Sequence><cond>NeighbourObjects</cond><act>MoveTowards_Hub</act></Sequence></Sequence></Sequence> <Sequence><Selector><cond>NeighbourObjects</cond><act>MoveAway_Sites</act></Selector><Sequence><cond>NeighbourObjects</cond><act>CompositeDrop_Food</act></Sequence></Sequence></Sequence>','<?xml version=\"1.0\" encoding=\"UTF-8\"?><Sequence><Sequence><Sequence><Selector><cond>IsDropable_Sites</cond><act>Explore</act></Selector><Sequence><cond>NeighbourObjects</cond><act>CompositeSingleCarry_Food</act></Sequence></Sequence> <Sequence><Sequence><cond>NeighbourObjects</cond><act>Explore</act></Sequence> <Sequence><cond>NeighbourObjects</cond><act>MoveTowards_Hub</act></Sequence></Sequence></Sequence> <Sequence><Selector><cond>NeighbourObjects</cond><act>MoveAway_Sites</act></Selector><Sequence><cond>NeighbourObjects</cond><act>CompositeDrop_Food</act></Sequence></Sequence></Sequence>','<?xml version=\"1.0\" encoding=\"UTF-8\"?><Sequence><Sequence><Sequence><Selector><cond>IsDropable_Sites</cond><act>Explore</act></Selector><Sequence><cond>NeighbourObjects</cond><act>CompositeSingleCarry_Food</act></Sequence></Sequence> <Sequence><Sequence><cond>NeighbourObjects</cond><act>Explore</act></Sequence> <Sequence><cond>NeighbourObjects</cond><act>MoveTowards_Hub</act></Sequence></Sequence></Sequence> <Sequence><Selector><cond>NeighbourObjects</cond><act>MoveAway_Sites</act></Selector><Sequence><cond>NeighbourObjects</cond><act>CompositeDrop_Food</act></Sequence></Sequence></Sequence>','<?xml version=\"1.0\" encoding=\"UTF-8\"?><Sequence><Sequence><Sequence><Sequence><Sequence><cond>NeighbourObjects</cond><act>Explore</act></Sequence> <Sequence><cond>NeighbourObjects</cond><act>CompositeSingleCarry_Food</act></Sequence></Sequence> <Sequence><cond>IsDropable_Hub</cond><act>CompositeDrop_Food</act></Sequence></Sequence> <Sequence><cond>IsDropable_Hub</cond><cond>IsDropable_Hub</cond><act>CompositeDrop_Food</act></Sequence></Sequence> <Selector><Selector><cond>IsDropable_Hub</cond><act>MoveTowards_Hub</act></Selector> <Selector><cond>IsDropable_Hub</cond><cond>NeighbourObjects</cond><act>MoveTowards_Sites</act></Selector></Selector></Sequence>','<?xml version=\"1.0\" encoding=\"UTF-8\"?><Sequence><Sequence><Sequence><Sequence><Sequence><cond>NeighbourObjects</cond><act>Explore</act></Sequence> <Sequence><cond>NeighbourObjects</cond><act>CompositeSingleCarry_Food</act></Sequence></Sequence> <Sequence><cond>IsDropable_Hub</cond><act>CompositeDrop_Food</act></Sequence></Sequence> <Sequence><cond>IsDropable_Hub</cond><cond>IsDropable_Hub</cond><act>CompositeDrop_Food</act></Sequence></Sequence> <Selector><Selector><cond>IsDropable_Hub</cond><act>MoveTowards_Hub</act></Selector> <Selector><cond>IsDropable_Hub</cond><cond>NeighbourObjects</cond><act>MoveTowards_Sites</act></Selector></Selector></Sequence>','<?xml version=\"1.0\" encoding=\"UTF-8\"?><Selector><Selector><Sequence><Sequence><cond>IsDropable_Hub</cond><act>Explore</act></Sequence> <Sequence><cond>NeighbourObjects</cond><act>CompositeDrop_Food</act></Sequence></Sequence><Sequence><Sequence><cond>IsDropable_Hub</cond><act>CompositeSingleCarry_Food</act></Sequence> <Selector><cond>NeighbourObjects</cond><act>MoveTowards_Sites</act></Selector></Sequence></Selector> <Selector><Selector><cond>IsDropable_Sites</cond><cond>IsDropable_Hub</cond><act>CompositeSingleCarry_Food</act></Selector> <Selector><cond>IsDropable_Hub</cond><cond>NeighbourObjects</cond><act>CompositeDrop_Food</act></Selector></Selector></Selector>','<?xml version=\"1.0\" encoding=\"UTF-8\"?><Selector><Selector><Sequence><Sequence><cond>IsDropable_Hub</cond><act>Explore</act></Sequence> <Sequence><cond>NeighbourObjects</cond><act>CompositeDrop_Food</act></Sequence></Sequence><Sequence><Sequence><cond>IsDropable_Hub</cond><act>CompositeSingleCarry_Food</act></Sequence> <Selector><cond>NeighbourObjects</cond><act>MoveTowards_Sites</act></Selector></Sequence></Selector> <Selector><Selector><cond>IsDropable_Sites</cond><cond>IsDropable_Hub</cond><act>CompositeSingleCarry_Food</act></Selector> <Selector><cond>IsDropable_Hub</cond><cond>NeighbourObjects</cond><act>CompositeDrop_Food</act></Selector></Selector></Selector>','<?xml version=\"1.0\" encoding=\"UTF-8\"?><Selector><Selector><Sequence><Sequence><cond>IsDropable_Hub</cond><act>Explore</act></Sequence> <Sequence><cond>NeighbourObjects</cond><act>CompositeDrop_Food</act></Sequence></Sequence><Sequence><Sequence><cond>IsDropable_Hub</cond><act>CompositeSingleCarry_Food</act></Sequence> <Selector><cond>NeighbourObjects</cond><act>MoveTowards_Sites</act></Selector></Sequence></Selector> <Selector><Selector><cond>IsDropable_Sites</cond><cond>IsDropable_Hub</cond><act>CompositeSingleCarry_Food</act></Selector> <Selector><cond>IsDropable_Hub</cond><cond>NeighbourObjects</cond><act>CompositeDrop_Food</act></Selector></Selector></Selector>','<?xml version=\"1.0\" encoding=\"UTF-8\"?><Selector><Selector><Sequence><Sequence><cond>IsDropable_Hub</cond><act>Explore</act></Sequence> <Sequence><cond>NeighbourObjects</cond><act>CompositeDrop_Food</act></Sequence></Sequence><Sequence><Sequence><cond>IsDropable_Hub</cond><act>CompositeSingleCarry_Food</act></Sequence> <Selector><cond>NeighbourObjects</cond><act>MoveTowards_Sites</act></Selector></Sequence></Selector> <Selector><Selector><cond>IsDropable_Sites</cond><cond>IsDropable_Hub</cond><act>CompositeSingleCarry_Food</act></Selector> <Selector><cond>IsDropable_Hub</cond><cond>NeighbourObjects</cond><act>CompositeDrop_Food</act></Selector></Selector></Selector>']
    threshold = 1.0

    sim = SimModelComm(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()