def log_situation(self, step): self._consistent = False agents = self.get_agents(addr=False) self._consistent = False for agent in agents: aiomas.run(until=agent.log_situation())
def shutdown(self): """shutdown the cluster""" tasks = [ asyncio.ensure_future(manager.stop()) for manager in self._managers ] aiomas.run(asyncio.gather(*tasks)) self._container.shutdown()
def vote_and_save_info(self, age): # self.age = age # self._candidates = aiomas.run(until=self.gather_candidates()) # self.suggested_cand.append(len(self.candidates)) # self.validate_candidates() # self.valid_cand.append(len(self.candidates)) # artifacts = self.perform_voting(method=self.voting_method) # threshold = 0.0 # # for a, v in artifacts: # accepted = True if v >= threshold else False # a.accepted = accepted # self.add_artifact(a) # tasks = [] # for addr in self._manager_addrs: # tasks.append(asyncio.ensure_future(self._add_domain_artifact(addr, a))) # aiomas.run(until=asyncio.gather(*tasks)) # # self.clear_candidates() # self.valid_candidates = [] super().vote_and_save_info(age) agents = self.get_agents(addr=False) self._consistent = False for agent in agents: aiomas.run(until=agent.process_rewards())
def main(addr, start_date, log_level): logging.basicConfig(level=getattr(logging, log_level.upper())) try: container_kwargs = util.get_container_kwargs(start_date) aiomas.run(aiomas.subproc.start(addr, **container_kwargs)) finally: asyncio.get_event_loop().close()
def start_node(port): """start a node""" task = Node.start(('0.0.0.0', port), codec=aiomas.codecs.MsgPackBlosc, extra_serializers=[serializers.get_np_serializer]) # terminates when the node's manager is given the 'stop' command aiomas.run(until=task)
def set_agent_acquaintances(self): agents = self.get_agents(addr=False) self._consistent = False addresses = self.get_agents(addr=True) self._consistent = False for agent in agents: aiomas.run(until=agent.set_acquaintances(addresses))
def main(addr, log_level): """Run the multi-agent system.""" logging.basicConfig(level=getattr(logging, log_level.upper())) try: # Run the "run()" coroutine which starts the RPC server for mosaik: aiomas.run(until=run(addr, log_level)) finally: # Make sure the event loop is closed so that everything is properly # cleaned up and we don't get nasty log messages at the end: asyncio.get_event_loop().close()
def main(addr, log_level, log_file): """Open VPP multi-agent system.""" try: # change event loop in case the platform is windows if sys.platform == 'win64' or sys.platform == 'win32': loop = asyncio.ProactorEventLoop() asyncio.set_event_loop(loop) initialize_logger(log_level, log_file) aiomas.run(until=run(addr, log_level, log_file)) finally: asyncio.get_event_loop().close()
def test_behaviors_called(self): class NewAgent(syd.Agent): state_vars = ['cash'] behaviors = [behavior] agent = NewAgent(self.node, {'cash': 100}) self.assertEquals(agent.state.cash, 100) aiomas.run(agent.decide()) agent.apply_updates() self.assertEquals(agent.state.cash, 10)
def test_behaviors_with_param_called(self): class NewAgent(syd.Agent): state_vars = ['cash'] behaviors = [partial(behavior_with_params, param=2)] agent = NewAgent(self.node, {'cash': 100}) self.assertEquals(agent.state.cash, 100) aiomas.run(agent.decide()) agent.apply_updates() self.assertEquals(agent.state.cash, 200)
def get_dictionary(self, func_name): agents = self.get_agents(addr=False) dict = {} for agent in agents: name = aiomas.run(until=agent.get_name()) func = getattr(agent, func_name) dict[name] = aiomas.run(until=func()) return dict
def destroy(self, folder=None): '''Destroy the environment and the subprocesses. ''' ameans = [(0, 0, 0) for _ in range(3)] #ret = [self.save_info(folder, ameans)] aiomas.run(until=self.stop_slaves(folder)) # Close and join the process pool nicely. self._pool.close() self._pool.terminate() self._pool.join() self._env.shutdown() return None
def async_step(self): '''Progress simulation by running all agents once asynchronously. ''' assert len(self._agents_to_act) == 0 self._init_step() t = time.time() tasks = [asyncio.ensure_future(self.env.trigger_act(addr)) for addr in self._agents_to_act] aiomas.run(until=asyncio.gather(*tasks)) self._agents_to_act = [] self._step_processing_time = time.time() - t self._finalize_step()
def runContainer(): # Creating container contains the home agents # and prediction agents RC = aiomas.Container.create(('localhost', 5555)) # Create the DB engine # db_engine = create_engine("mysql+pymysql://{}@{}/{}".format(CF.DB_USER, CF.DB_HOST, CF.DB_NAME)) db_engine = CF.get_db_engine() # Initiate the blockchain agent blockChainAgent = BlockchainAgent(container=RC, ) # Dump the blockchain agent address logger.info("Blcokchain agent initiated at {}".format( blockChainAgent.addr)) # Record this agent to DB status = recordAgent(agent_addr=blockChainAgent.addr, agent_type='blockchain', db_engine=db_engine) # Run the event loop try: logger.info( "Running the event loop. The blockchain agent is open to be connected!" ) aiomas.run() except KeyboardInterrupt: logging.info("Keyboard Interrupted") except Exception as e: traceback.print_exc(file=sys.stdout) # Shutting donw the controller and thereby cleaning # all agents try: logger.info("Shutting down the root container...") RC.shutdown() logger.info("Done.") logger.info("Killing Blockchain agent") status = killAgent(agent_addr=blockChainAgent.addr, agent_type='blockchain', db_engine=db_engine) if status: logger.info("Done.") else: logger.info("Couldnot kill the agent!") except Exception as e: logger.info("Failed to shutdown the root container") traceback.print_exc(file=sys.stdout)
def main(addr, start_date, log_level, log_file): """ :param addr: address :param start_date: start date :param log_level: log_level (string, possibly lower case) :param log_file: log file (debug messages will be stored in that file) :return: """ initialize_logger(log_level, log_file) container_kwargs = util.get_container_kwargs(start_date) try: aiomas.run(aiomas.subproc.start(addr, **container_kwargs)) finally: asyncio.get_event_loop().close()
def main(): logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%H:%M:%S') parser = argparse.ArgumentParser(description="Master for cluster dispatch system") parser.add_argument("-s", "--master_ip", type=str, default="0.0.0.0", help="Ip address of master server") parser.add_argument("-p", "--master_port", type=check_no_negative_integer, default=PORT,help="port of master service") args = parser.parse_args() master = Master() logging.info("start to listen %s:%s ..." % (args.master_ip, args.master_port)) master = aiomas.run(aiomas.rpc.start_server((args.master_ip, args.master_port), master, master.client_connected, codec = aiomas.MsgPackBlosc, extra_serializers=EXTRA_SERIALIZERS)) aiomas.run(master.wait_closed())
def states(self): """iterate over all agent states across the cluster""" tasks = [asyncio.ensure_future(manager.states()) for manager in self._managers] for states in aiomas.run(asyncio.gather(*tasks)): yield from states
def next(self): '''Trigger next agent to :py:meth:`~creamas.core.CreativeAgent.act` in the current step. ''' # all agents acted, init next step t = time.time() if len(self._agents_to_act) == 0: self._init_step() agent = self._agents_to_act.pop(0) aiomas.run(until=self.env.trigger_act(agent)) t2 = time.time() self._step_processing_time += t2 - t # all agents acted, finalize current step if len(self._agents_to_act) == 0: self._finalize_step()
def states(self): """iterate over all agent states across the cluster""" tasks = [ asyncio.ensure_future(manager.states()) for manager in self._managers ] for states in aiomas.run(asyncio.gather(*tasks)): yield from states
def ctrl_obs(containers): # ctrl = controller.ControllerAgent(containers[0]) ctrl_kwargs = {} obs_kwargs = {} ctrl, obs = aiomas.run(controller.ControllerAgent.factory( containers[0], ctrl_kwargs, observer.ObserverAgent, obs_kwargs)) # obs = observer.ObserverAgent(containers[0], ctrl.addr) yield (ctrl, obs) ctrl.stop() obs.stop()
def get_comparison_count(self): agents = self.get_agents(addr=False) self._consistent = False total_comparisons = 0 for agent in agents: total_comparisons += aiomas.run(until=agent.get_comparison_count()) return total_comparisons
def get_total_reward(self): agents = self.get_agents(addr=False) self._consistent = False total_reward = 0 for agent in agents: total_reward += aiomas.run(until=agent.get_total_reward()) return total_reward
def __init__(self, port, hosts, distributor=RoundRobin, venv=None): """create a new cluster listening on `port`, managing `hosts`""" self.hosts = sum((self._start_node(host, user, start_port, venv) for host, user, start_port in hosts), []) self._container = aiomas.Container.create(('localhost', port), codec=aiomas.codecs.MsgPackBlosc, extra_serializers=[get_np_serializer]) self._container.has_manager = True self._managers = aiomas.run(self._connect_to_managers(self.hosts)) self._distributor = distributor(self._managers)
def __init__(self, port, hosts, distributor=RoundRobin, venv=None): """create a new cluster listening on `port`, managing `hosts`""" self.hosts = sum((self._start_node(host, user, start_port, venv) for host, user, start_port in hosts), []) self._container = aiomas.Container.create( ('localhost', port), codec=aiomas.codecs.MsgPackBlosc, extra_serializers=[get_np_serializer]) self._container.has_manager = True self._managers = aiomas.run(self._connect_to_managers(self.hosts)) self._distributor = distributor(self._managers)
def ua(containers, ctrl_mock, obs_mock, event_loop): ua = aiomas.run(unit.UnitAgent.factory( containers[0], ctrl_agent_addr=ctrl_mock.addr, obs_agent_addr=obs_mock.addr, unit_model=(__name__ + ':UnitModelMock', {}), unit_if=(__name__ + ':UnitIfMock', {}), planner=(__name__ + ':PlannerMock', {}), sleep_before_connect=False)) yield ua ua.stop()
def main(): logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%H:%M:%S') # logging.getLogger().addHandler(logging.handlers.SysLogHandler()) parser = argparse.ArgumentParser(description="Slave for cluster dispatch system") parser.add_argument("-s","--master_ip", type=str,required=True,help="Ip address of master server") parser.add_argument("-p","--master_port",type=check_no_negative_integer,help="port of master service") parser.add_argument("-n","--name", type=str, default=platform.node(), help="slave name") parser.add_argument("-t","--total", type=check_no_negative_integer, default=multiprocessing.cpu_count(),help="total cpu cores of this node") parser.add_argument("-q","--queue_name",type=str, default="main", help="Queue name") args = parser.parse_args() slave = Slave() slave.master_ip = args.master_ip slave.master_port = args.master_port slave.name = args.name slave.total_cores = args.total slave.queue_name = args.queue_name asyncio.ensure_future(slave.run()) aiomas.run()
def vote_and_save_info(self, age): self.age = age self._candidates = aiomas.run(until=self.gather_candidates()) self.suggested_cand.append(len(self.candidates)) self.validate_candidates() self.valid_cand.append(len(self.candidates)) artifacts = self.perform_voting(method=self.voting_method) threshold = 0.0 for a, v in artifacts: accepted = True if v >= threshold else False a.accepted = accepted self.add_artifact(a) tasks = [] for addr in self._manager_addrs: tasks.append( asyncio.ensure_future(self._add_domain_artifact(addr, a))) aiomas.run(until=asyncio.gather(*tasks)) self.clear_candidates() self.valid_candidates = []
def irun(self, steps, reports=None): """run the simulation lazily (as an iterator)""" # reports = {name: (fn, mod step)} aggs = defaultdict(dict) reports = reports or {} for name, (fn, mod_step) in reports.items(): aggs[mod_step][name] = fn aiomas.run(self.container.setup_agents()) for i in range(steps): aiomas.run(self._step()) # TODO any way to make this more efficient? report = {} states = None for mod_step, agg_fns in aggs.items(): if i % mod_step == 0: for name, fn in agg_fns.items(): if states is None: states = self.container.states() report[name] = fn(states) yield report
def main(session_id, agent_id, port): """ """ # DB engine db_engine = CF.get_db_engine() agent_addr = getAgentAddress(agent_id=int(agent_id), session_id=session_id, db_engine=db_engine) if agent_addr is None: logging.info("Agent address couldn't be retreived. Make sure to provide correct session ID.") return logging.info("Agent's address: {}".format(agent_addr)) try: # Create the container the host trigger agent c = aiomas.Container.create(('localhost', int(port))) # Host the trigger agent trigger_agent = TriggerAgent(container=c) # Kick the home agent by trigger agent aiomas.run(until=trigger_agent.run(agent_addr)) except OSError: logger.info("Probably the provided port is already in use or the home agent is dead!") return except ConnectionResetError: logger.info("Probably the home agent died.") except Exception as e: logger.info("Failed to open/create container or run the triggering agent!") traceback.print_exc(file=sys.stdout) # Shutting down the container logger.info("Shutting down the triggering agents container.") c.shutdown()
def _validate_candidates(self): '''Validate current candidates in the environment by pruning candidates that are not validated at least by one agent, i.e. they are vetoed. In larger societies this method might be costly, as it calls each spiro' ``validate_candidates``-method. ''' valid_candidates = set(self.candidates) tasks = [] for a in self._manager_addrs: tasks.append(self._validate_candidates(a)) ret = aiomas.run(until=asyncio.gather(*tasks)) for r in ret: result = aiomas.run(until=r) vc = set(result) valid_candidates = valid_candidates.intersection(vc) self._candidates = list(valid_candidates) self._log( logging.INFO, "{} valid candidates after get_agents used veto.".format( len(self.candidates)))
def spawn_creator(desired_novelty): ret = aiomas.run( until=menv.spawn('agents.maze.creator_agent:CreatorAgent', log_folder=log_folder, memsize=normal_memsize, critic_threshold=critic_threshold, veto_threshold=veto_threshold, log_level=logging.DEBUG, maze_shape=maze_shape, search_width=normal_search_width, ask_criticism=ask_criticism, ask_random=ask_random, choose_funcs=choose_funcs, desired_novelty=desired_novelty, hedonic_std=hedonic_std)) print(ret)
def spawn_gatekeeper(desired_novelty): ret = aiomas.run(until=menv.spawn( 'agents.maze.gatekeeper_agent:GatekeeperAgent', log_folder=log_folder, memsize=gatekeeper_memsize, critic_threshold=critic_threshold, veto_threshold=veto_threshold, log_level=logging.DEBUG, maze_shape=maze_shape, ask_criticism=ask_criticism, ask_random=ask_random, desired_novelty=desired_novelty, hedonic_std=hedonic_std)) menv.gatekeepers.append(ret[0]) gatekeeper_agents.append(run(ret[0].get_addr())) print(ret)
def spawn(self, agent_cls, *args, **kwargs): """spawn an agent on the cluster""" manager = self._distributor.next(self._managers) qual_name = '{}:{}'.format(agent_cls.__module__, agent_cls.__name__) return aiomas.run(manager.spawn(qual_name, *args, **kwargs))
def test_local_proxy(self): agent = self.agents[0] res = aiomas.run(agent.what()) self.assertTrue(isinstance(agent, LocalProxy)) self.assertEquals(res, agent.obj.what())
def test_agent_update(self, mock_method): aiomas.run(self.node.update_agents()) self.assertEquals(mock_method.call_count, 2)
def test_bulk_get(self): results = aiomas.run(self.node.bulk_get([a.addr for a in self.agents], 'sup')) self.assertEqual(results, [10, 20])
def test_gets_local_agent(self): addr = self.agents[0].addr agent_proxy = aiomas.run(self.node.connect(addr)) self.assertTrue(isinstance(agent_proxy, LocalProxy))
def test_run(self): n_steps = 4 self.sim.run(n_steps) for agent in self.agents: count = aiomas.run(agent.counter()) self.assertEquals(count, n_steps)
ret = run(menv.wait_slaves(30)) ret = run(menv.set_host_managers()) ret = run(menv.is_ready()) # Create the agents critic_agents = [] print('Critics:') for _ in range(num_of_critic_agents): ret = aiomas.run( until=menv.spawn('agents.maze.maze_agent:MazeAgent', log_folder=log_folder, memsize=critic_memsize, critic_threshold=critic_threshold, veto_threshold=veto_threshold, log_level=logging.DEBUG, choose_func=cell_choosing_func, maze_shape=maze_shape, search_width=critic_search_width, ask_criticism=ask_criticism, ask_random=ask_random)) print(ret) critic_agents.append(run(ret[0].get_name(), loop)) print('Normies:') for _ in range(num_of_normal_agents): ret = aiomas.run( until=menv.spawn('agents.maze.maze_agent:MazeAgent', log_folder=log_folder, memsize=normal_memsize, critic_threshold=critic_threshold,
loop = asyncio.get_event_loop() ret = loop.run_until_complete(env.set_host_managers()) ret = loop.run_until_complete(env.wait_slaves(30)) ret = loop.run_until_complete(env.is_ready()) # art = SprAgent(env, ((120, 120))).create(50, 100) # scipy.misc.imsave('test.jpg', art) # art2 = SprAgent(env, ((120, 120))).create(50, -100) # scipy.misc.imsave('test2.jpg', art2) rand = False print(aiomas.run(until=env.spawn('spiro.spr_agent:SprAgent', states=((25, 25), (85, 85)), rand=rand, desired_novelty=0.001, log_folder=log_folder))) print(aiomas.run(until=env.spawn('spiro.spr_agent:SprAgent', states=((25, -25), (85, -85)), rand=rand, desired_novelty=10, log_folder=log_folder))) print(aiomas.run(until=env.spawn('spiro.spr_agent:SprAgent', states=((-25, 25), (-85, 85)), rand=rand, desired_novelty=10, log_folder=log_folder))) env.set_agent_acquaintances()
def shutdown(self): """shutdown the cluster""" tasks = [asyncio.ensure_future(manager.stop()) for manager in self._managers] aiomas.run(asyncio.gather(*tasks)) self._container.shutdown()
def runContainer(): # Creating container contains the home agents # and prediction agents HC = aiomas.Container.create(('localhost', 5556), clock=CLOCK) # HC = aiomas.Container.create(('localhost', 5556), clock=CLOCK) # Set the clcok # t_clock_setter = asyncio.async(clock_setter()) # List of Homes homes = [9019, 9981] # 7881, 100237, 7850, 980, 9981,] # Create the DB engine # db_engine = create_engine("mysql+pymysql://{}@{}/{}".format(CF.DB_USER, CF.DB_HOST, CF.DB_NAME)) db_engine = CF.get_db_engine() # Initiate the agents into HC homeAgents = [ HomeAgent( container=HC, agent_id=home, db_engine=db_engine, ) for home in homes ] # Creating the session session_id = createSession(agents=homeAgents, db_engine=db_engine) # Address of the blockchain agent # Later, it will be retreived from the Agent Server bc_address = getActiveBlockchainAddress(db_engine=db_engine) if bc_address is None: logging.info("Blockchain is not initiated.") else: # Bind the blockchain with home agents for agent in homeAgents: agent.setBlockchainAddress(bc_address=bc_address) # Run the event loop try: logger.info( "Running the event loop. One of the home agents is trying to connect with BC agent!" ) logger.info("Session ID:{}".format(session_id)) # Run the even loop aiomas.run() except KeyboardInterrupt: logger.info("Stopping the event loop") # Try to stop the event loop except Exception as e: traceback.print_exc(file=sys.stdout) finally: # Killing the current session killSession(session_id=session_id, db_engine=db_engine) # Shutting donw the controller and thereby cleaning # all agents try: logger.info( "Shutting down the home container...and cancelling the clock") HC.shutdown() # t_clock_setter.cancel() logger.info("Done.") except Exception as e: logger.info("Failed to shutdown the home container") traceback.print_exc(file=sys.stdout)