def __init__(self, basename, use_locks=False): """ All data will be stored under directory `basename`. If there is a server there already, it will be loaded (resumed). The server object is stateless in RAM -- its state is defined entirely by its location. There is therefore no need to store the server object. """ if not os.path.isdir(basename): raise ValueError("%r must be a writable directory" % basename) self.basename = basename self.use_locks = use_locks self.lock_update = threading.RLock() if use_locks else gensim.utils.nocm try: self.fresh_index = SimIndex.load(self.location('index_fresh')) except: logger.debug("starting a new fresh index") self.fresh_index = None try: self.opt_index = SimIndex.load(self.location('index_opt')) except: logger.debug("starting a new optimized index") self.opt_index = None try: self.model = SimModel.load(self.location('model')) except: self.model = None self.payload = SqliteDict(self.location('payload'), autocommit=True, journal_mode=JOURNAL_MODE) self.flush(save_index=False, save_model=False, clear_buffer=True) logger.info("loaded %s" % self)
def simulate(env, iteration, N=150): """Test the performane of evolved behavior.""" # phenotype = agent.individual[0].phenotype # phenotypes = extract_phenotype(agents) phenotypes = env[0] threshold = 1.0 sim = SimModel(N, 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() 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()
from simmodel import SimModel sim = SimModel() sim.main()
from simmodel import SimModel import matplotlib.pyplot as plt elements = 2 timesList = [] elList = [] while elements <= 50: sim = SimModel() time = sim.main(elements) elList.append(elements) elements += 1 timesList.append(time) print(timesList) print(elList) timesArr = [i['time'] for i in timesList] plt.plot(timesArr) plt.ylabel('Execution time') plt.show()