Skip to content

Commit

Permalink
working a module to save results from the experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
aadeshnpn committed Jul 30, 2018
1 parent 98c936b commit 452fbff
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ final_rules/*
resources/*
results/*
test/test_bt_xml
test/test_bt_xml/*
test/test_bt_xml/*
examples/*/[0-9]+[A-Za-z]*
6 changes: 6 additions & 0 deletions examples/single_foraging_evolution/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion examples/single_foraging_evolution/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/single_foraging_evolution/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
45 changes: 45 additions & 0 deletions swarms/utils/results.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 452fbff

Please sign in to comment.