class Simulation: def __init__(self): self.__environment = Environment() self.__frequency_falsepos = 0 self.__searchagent = SearchAgent() def __setup_test_environment(self, x=10, y=10): self.__environment.clear() self.__environment.generate(x, y) self.__environment.add_target() def __setup_search_agent(self, searchagent_genes=list()): self.__searchagent.set_brain(searchagent_genes) self.__environment.add_search_agent() def run_simulation(self, num_of_turns=300): return_scores = list() for i in range(num_of_turns): # agent think and validation of the decision move_result = self.__environment.move_searchagent( self.__searchagent.agent_id(), self.__searchagent.think( self.__environment.get_adjacent_tiles( self.__searchagent.agent_id()))) if move_result[1] == True: # agent move if the move is valid self.__searchagent.move(move_result[0]) if self.__environment.check_target( self.__searchagent.agent_id()): self.__searchagent.search_success() else: # if the move is invalid, returns a value far beyond the lowest possible value self.__searchagent.reset() return -10000 # self.__environment.draw() return_scores = grade(self.__searchagent) self.__environment.clear() self.__searchagent.reset() return return_scores def setup_simulation(self, x=10, y=10, num_search_targets=1, num_search_agents=1, searchagent_genes=list()): self.__setup_test_environment(x, y) self.__setup_search_agent(searchagent_genes)
self.environment = environment def run(self): while(True): # Movement of targets eq.(2): if(np.random.random() > 0.9999): print("Change speed!") with self.lock: self.environment.set_targets_command((np.random.random((self.environment.xi.shape))-0.5)) self.environment.update() time.sleep(self.environment.t) env = Environment(10,10,0.01, True) for i in range(5): env.add_target(np.random.random()*2.5-2.5, np.random.random()*2.5-2.5, 0.005, 0.005) env.add_agent(np.random.random()*2.5-2.5, np.random.random()*2.5-2.5, 0.005, 0.005) # env.add_agent(np.random.random()*2.5-2.5, np.random.random()*2.5-2.5, 0.005, 0.005) main = Main(env) main.start() #main.run() plt.gcf() plt.show() # plt.savefig("output/" + "MRS" + str(time.time()) + ".png") main.join() # To recover a final video of de execution: # ffmpeg -f image2 -s 1920x1080 -i output/MRS%5d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p MRS.mp4