def get_gatekeeper_dictionary(self, func_name): dict = {} for agent in self.gatekeepers: name = run(agent.get_name()) func = getattr(agent, func_name) dict[name] = run(func()) return dict
def save_artifacts(self, folder): agents = self.get_agents(addr=False) for agent in agents: name = run(agent.get_name()) agent_folder = '{}/{}'.format(folder, agent_name_parse(name)) if os.path.exists(agent_folder): shutil.rmtree(agent_folder) os.makedirs(agent_folder) run(agent.save_artifacts(agent_folder))
def get_creator_dictionary(self, func_name): agents = self.get_agents(addr=False) dict = {} for agent in agents: if agent not in self.gatekeepers: name = run(agent.get_name()) func = getattr(agent, func_name) dict[name] = run(func()) return dict
def create_environment(): addr = ('localhost', 5550) addrs = [ ('localhost', 5560), ('localhost', 5561), ('localhost', 5562), ('localhost', 5563), ('localhost', 5564), ('localhost', 5565), ('localhost', 5566), ('localhost', 5567), ] env_kwargs = { 'extra_serializers': [ get_type_ser, get_primitive_ser, get_terminal_ser, get_primitive_set_typed_ser, get_func_ser, get_toolbox_ser, get_rule_leaf_ser, get_genetic_image_artifact_ser, get_ndarray_ser, get_dummy_ser ], 'codec': aiomas.MsgPack } slave_kwargs = [{ 'extra_serializers': [ get_type_ser, get_primitive_ser, get_terminal_ser, get_primitive_set_typed_ser, get_func_ser, get_toolbox_ser, get_rule_leaf_ser, get_genetic_image_artifact_ser, get_ndarray_ser, get_dummy_ser ], 'codec': aiomas.MsgPack } for _ in range(len(addrs))] menv = StatEnvironment(addr, env_cls=Environment, mgr_cls=MultiEnvManager, logger=None, **env_kwargs) ret = run( menv.spawn_slaves(slave_addrs=addrs, slave_env_cls=Environment, slave_mgr_cls=EnvManager, slave_kwargs=slave_kwargs)) ret = run(menv.wait_slaves(30)) ret = run(menv.set_host_managers()) ret = run(menv.is_ready()) return menv
def graph_from_connections(env, directed=False): '''Create NetworkX graph from agent connections in a given environment. :param env: Environment where the agents live. The environment must be derived from :class:`~creamas.core.environment.Environment`, :class:`~creamas.mp.MultiEnvironment` or :class:`~creamas.ds.DistributedEnvironment`. :param bool directed: If ``True``, creates an instance of :class:`~networkx.digraph.DiGraph`, otherwise creates an instance of :class:`~networkx.graph.Graph`. :returns: The created NetworkX graph. :rtype: :class:`~networkx.digraph.DiGraph` or :class:`~networkx.graph.Graph` .. note:: If the created graph is undirected and two connected agents have different attitudes towards each other, then the value of ``"attitude"`` key in the resulting graph for the edge is chosen randomly from the two values. ''' G = DiGraph() if directed else Graph() conn_list = env.get_connections(data=True) labels = {} for agent, conns in conn_list: agent_proxy = run(env.connect(agent)) desired_novelty = run(agent_proxy.get_desired_novelty()) print(desired_novelty) labels[agent] = desired_novelty G.add_node(agent) ebunch = [] for nb, data in conns.items(): ebunch.append((agent, nb, data)) if len(ebunch) > 0: G.add_edges_from(ebunch) pos = nx.spring_layout(G) nx.draw_networkx_labels(G, pos, labels, font_size=16) return G
def save_creator_artifacts(self, folder): agents = self.get_agents(addr=False) for agent in agents: if agent not in self.gatekeepers: id = 0 name = run(agent.get_name()) parsed_name = agent_name_parse(name) agent_folder = folder + '/' + parsed_name if os.path.exists(agent_folder): shutil.rmtree(agent_folder) os.makedirs(agent_folder) artifacts = run(agent.get_memory_artifacts()) for artifact in artifacts: id += 1 self.save_artifact(artifact, agent_folder, id, artifact.evals[name])
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)
for _ in range(num_of_simulations): sim_count += 1 # Create the environments menv = MazeMultiEnvironment(addr, env_cls=Environment, mgr_cls=MultiEnvManager, logger=logger, **env_kwargs) loop = asyncio.get_event_loop() ret = run( menv.spawn_slaves(slave_addrs=addrs, slave_env_cls=VoteEnvironment, slave_mgr_cls=VoteManager, slave_kwargs=slave_kwargs)) 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',
def publish_artifacts(self, age): for gatekeeper in self.gatekeepers: run(gatekeeper.publish())
for _ in range(num_of_simulations): sim_count += 1 # Create the environments menv = GatekeeperMazeMultiEnvironment(addr, env_cls=Environment, mgr_cls=MultiEnvManager, logger=logger, **env_kwargs) loop = asyncio.get_event_loop() ret = run( menv.spawn_slaves(slave_addrs=addrs, slave_env_cls=Environment, slave_mgr_cls=EnvManager, slave_kwargs=slave_kwargs)) ret = run(menv.wait_slaves(30)) ret = run(menv.set_host_managers()) ret = run(menv.is_ready()) # Create the agents gatekeeper_agents = [] def spawn_gatekeeper(desired_novelty): ret = aiomas.run(until=menv.spawn( 'agents.maze.gatekeeper_agent:GatekeeperAgent', log_folder=log_folder,
print(agent1) rules = [rule_dict['red'], rule_dict['blue']] rule_weights = [0.3, 0.7] agent2 = aiomas.run(until=menv.spawn('deappi.collab_agent:CollabAgent', log_folder=log_folder, artifact_cls=GeneticImageArtifact, create_kwargs=create_kwargs, rules=rules, rule_weights=rule_weights)) print(agent2) run(agent1[0].init_connection(run(agent2[0].get_name()))) run(agent2[0].init_connection(run(agent1[0].get_name()))) sim = Simulation(menv, log_folder=log_folder) sim.async_steps(12) # artifact = run(agent1[0].get_artifact()) # eval1 = run(agent1[0].get_eval()) # eval2 = run(agent2[0].get_eval()) best1 = run(agent1[0].get_best_received()) best2 = run(agent2[0].get_best_received()) sim.end() # print('Eval1: ' + str(eval1)) # print('Eval2: ' + str(eval2))
stats = {'comps': [], 'novelty': [], 'gini': [], 'bestie_find_speed': []} # Run simulation x times and record stats for _ in range(num_of_simulations): env = SprEnvironmentEqual(addr, env_cls=Environment, mgr_cls=SpiroMultiEnvManager, logger=logger, **env_kwargs) loop = asyncio.get_event_loop() ret = run( env.spawn_slaves(slave_addrs=addrs, slave_env_cls=Environment, slave_mgr_cls=SpiroEnvManager, slave_kwargs=slave_kwargs)) ret = loop.run_until_complete(env.set_host_managers()) ret = loop.run_until_complete(env.wait_slaves(30, check_ready=True)) ret = loop.run_until_complete(env.is_ready()) for _ in range(num_of_agents): print( aiomas.run( until=env.spawn('agents.spiro.critic_q_agent:CriticQAgent', desired_novelty=-1, log_folder=log_folder, critic_threshold=critic_threshold, veto_threshold=veto_threshold,