diff --git a/.gitignore b/.gitignore index 6a86e2a..f2c7869 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ final_rules/* resources/* results/* test/test_bt_xml -test/test_bt_xml/* \ No newline at end of file +test/test_bt_xml/* +examples/*/[0-9]+[A-Za-z]* \ No newline at end of file diff --git a/examples/single_foraging_evolution/agent.py b/examples/single_foraging_evolution/agent.py index 8397be7..e2e3b07 100644 --- a/examples/single_foraging_evolution/agent.py +++ b/examples/single_foraging_evolution/agent.py @@ -3,6 +3,7 @@ from swarms.lib.agent import Agent import numpy as np from swarms.utils.bt import BTConstruct +from swarms.utils.results import Results from ponyge.operators.initialisation import initialisation from ponyge.fitness.evaluation import evaluate_fitness @@ -25,6 +26,7 @@ def __init__(self, name, model): self.direction = model.random.rand() * (2 * np.pi) self.speed = 2 self.radius = 3 + self.results = "db" # This can take 2 values. db or file # self.exchange_time = model.random.randint(2, 4) # This doesn't help. Maybe only perform genetic operations when @@ -165,6 +167,10 @@ def step(self): # print ('bt tree', output, self.individual[0].phenotype, # self.individual[0].fitness) # Get the value of food from hub before ticking the behavior + + ## Create a results instance and save it to a file + # self.diversity_fitness + self.results = Results(self.model.pname, self.name, self.step_count, self.beta, self.individual[0].fitness, ) self.timestamp += 1 self.step_count += 1 # Increase beta diff --git a/examples/single_foraging_evolution/model.py b/examples/single_foraging_evolution/model.py index f40076f..faf88c9 100644 --- a/examples/single_foraging_evolution/model.py +++ b/examples/single_foraging_evolution/model.py @@ -8,7 +8,7 @@ from agent import SwarmAgent from swarms.lib.objects import Hub, Sites, Food, Derbis, Obstacles import os, imp - +import datetime #filename = os.path.join( # "/home/aadeshnpn/Documents/BYU/hcmi/swarm/swarms/" + "utils/world.json") filename = os.path.join(imp.find_module("swarms")[1] + "/utils/world.json") @@ -24,6 +24,11 @@ def __init__(self, N, width, height, grid=10, seed=None): else: super(EnvironmentModel, self).__init__(seed) + self.pname = os.getcwd() + '/' + datetime.datetime.now().strftime("%s") + "SForaging" + + # Create a folder to store results + os.mkdir(self.pname) + self.num_agents = N self.grid = Grid(width, height, grid) diff --git a/examples/single_foraging_evolution/run.py b/examples/single_foraging_evolution/run.py index 9534b63..3a5687a 100644 --- a/examples/single_foraging_evolution/run.py +++ b/examples/single_foraging_evolution/run.py @@ -24,7 +24,7 @@ def main(): food_objects = grid.get_objects_from_list_of_grid('Food', neighbours) # print ('TOtal Food prev', len(food_objects)) - for i in range(100000): + for i in range(10000): env.step() # best = env.find_higest_performer() # best = env.find_higest_food_collector() diff --git a/swarms/utils/results.py b/swarms/utils/results.py new file mode 100644 index 0000000..f67a0ff --- /dev/null +++ b/swarms/utils/results.py @@ -0,0 +1,45 @@ +# Store the information from the experiments + +import datetime + + +class Results: + """Define the results atrributes. + + This class defines the common attributes for each experiments. + """ + def __init__( + self, foldername, agent_name, step, beta, fitness, diversity, explore, + foraging, neighbour, genotype, phenotype, bt + ): + """Initialize the attributes.""" + self.foldername = foldername + self.context = { + "id": datetime.datetime.now().strftime("%s"), + "name": agent_name, + "step": step, + "beta": beta, + "fitness": fitness, + "diversity": diversity, + "explore": explore, + "foraging": foraging, + "neighbour": neighbour, + "genotype": genotype, + "phenotype": phenotype, + "bt_source": bt + } + # self.template = """ + # Id, Agent Name, Step, Beta, Fitness, Diversity, Explore, Foraging, Neighbours, Genotype, Phenotype, BT + self.template = """ + {id}, {name}, {step}, {beta}, {fitness}, {diversity}, {explore}, {foraging}, {neighbour}, {genotype}, {phenotype}, {bt} + """ + + def save_to_file(self): + """Save results to a flat file.""" + filename = self.foldername + '/' + str(self.context['step']) + '_' + str(self.context['name']) + with open(filename, 'a') as statsfile: + statsfile.write(self.template.format(**self.context)) + + def save_to_db(self): + """Save results to a database.""" + pass