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')
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
def evolve(iteration): """Learning Algorithm block.""" # iteration = 10000 env = EvolModelComm(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() # 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
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))
def phenotype_to_json(pname, runid, phenotypes): """Store the phenotype to a json file.""" jfilename = pname + '/' + runid + '.json' JsonPhenotypeData.to_json(phenotypes, jfilename)
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()