def on_tick(self): if self.__wait_ticks: self.__wait_ticks -= 1 return """ try: if len(self.__population) > self.agent.max_agent_population: return agents = random.sample( self.__population, self.agent.max_agent_population - len(self.__population)) except: agents = self.__population for agent in agents: logging.info('Master send Repro message to {0}'.format(agent)) self.agent.add_behaviour( RequestInitiatorBehaviour( store=agent, request='reproduce')) """ logging.info('Master agent trying to acquire fitness lock...') EvolAgent.fitness_function_lock.acquire() logging.info('Master agent acquired fitness lock {0}.'.format( EvolAgent.fitness_function_num_instances)) sample_n = EvolAgent.fitness_function_max_instances - \ EvolAgent.fitness_function_num_instances EvolAgent.fitness_function_lock.release() logging.info('Master agent released fitness lock.') agents = random.sample(self.__population, sample_n) for agent in agents: logging.info('Master send Repro message to {0}'.format(agent)) self.agent.add_behaviour( RequestInitiatorBehaviour(store=agent, request='reproduce'))
def view_node(self, nid, language): IBASGlobal.print_message( 'IA: View Node: ' + str(nid) + ' language ' + str(language), 1) self.node_rep = None aid = self.mts.ams.find_agent(nid) if aid is None: aid = self.aid pyroloc = str(aid.addresses[0]) self.add_behaviour( RequestInitiatorBehaviour( store=AID(pyroloc + 'ViewAgent'), request=str(nid) + ' ' + str(language), handle_response=self._handle_response, handle_agree=self._handle_agree, handle_refuse=self._handle_refuse, handle_inform=self._handle_inform_view, handle_inform_done=self._handle_inform_done_view, handle_failure=self._handle_failure, cancel=self._cancel, check_cancel=self._check_cancel)) while self.node_rep == None: time.sleep(0.5) res = self.node_rep self.node_rep = None return res
def search_node_by_attribute(self, att_type, att_value): IBASGlobal.print_message( 'IA: SEARCH FOR ATT_TYPE ' + str(att_type) + ' AND VALUE WITH ' + str(att_value), 1) self.search_result = None if att_type != "" and att_value == "": request = 'att_type:' + att_type elif att_type == "" and att_value != "": request = 'att_value:' + att_value else: request = 'att_type_value:"' + att_type + '"#"' + att_value + '"' self.add_behaviour( RequestInitiatorBehaviour( store=AID('Search Agent'), request=request, handle_response=self._handle_response, handle_agree=self._handle_agree, handle_refuse=self._handle_refuse, handle_inform=self._handle_inform, handle_inform_done=self._handle_inform_done, handle_failure=self._handle_failure, cancel=self._cancel, check_cancel=self._check_cancel)) while self.search_result is None: time.sleep(0.5) res = self.search_result self.search_result = None return res
def setup(self, store, request): self.add_behaviour(RequestInitiatorBehaviour( store=store, request=request, handle_no_participant=self._handle_no_participant, handle_response=self._handle_response, handle_agree=self._handle_agree, handle_refuse=self._handle_refuse, handle_inform=self._handle_inform, handle_inform_done=self._handle_inform_done, handle_failure=self._handle_failure, cancel=self._cancel, check_cancel=self._check_cancel ))
def on_tick(self): if self.agent.sorted_fitnesses is None or \ len(self.agent.sorted_fitnesses) == 0: return try: agent = random.choice(self.agent.sorted_fitnesses)['agent_id'] logging.info('Sending migrate command to {0}...'.format(agent)) self.agent.add_behaviour( RequestInitiatorBehaviour( store=agent, request='migrate')) except Exception as e: logging.exception('ERROR: Could not migrate agent(s). {0}'.format(e)) pass
def on_tick(self): if self.agent.sorted_fitnesses is None or \ len(self.agent.sorted_fitnesses) == 0: return # If there are too many agents, kill the weakest ones while len( self.agent.sorted_fitnesses) > self.agent.max_agent_population: try: agent = self.agent.sorted_fitnesses.pop(0)['agent_id'] logging.info('Sending kill command to {0}...'.format(agent)) self.agent.add_behaviour( RequestInitiatorBehaviour(store=agent, request='die')) # Need to remove entry for that entry in our list del self.agent.fitness_datastore[agent] except Exception as e: logging.exception('ERROR: Could not kill agent(s).') pass
def _perform_request(self, request): request = request.split(' ') if self.status == REQUEST_NODE: IBASGlobal.print_message( 'VA: trying to contact ' + str(request[0]), 2) self.add_behaviour( RequestInitiatorBehaviour( store=AID(str(request[0])), request="normal", handle_no_participant=self._handle_no_participant, handle_response=self._handle_response, handle_agree=self._handle_agree, handle_inform=self._handle_inform, handle_inform_done=self._handle_inform_done, check_cancel=self._check_cancel)) self.status = REQUEST_INFO elif self.status == REQUEST_INFO: if self.data[NODE_KEY]: for att in self.data[NODE_KEY].attributes: self._handle_attribute(att, request[1]) for stat in self.data[NODE_KEY].statements: restricted = False for att in stat.attributes: if str(att.type) == RESTRICTED_TO: restricted = True if restricted: self.data[NID_DICT_KEY][stat.predicate] = 'Restricted' self.data[NID_DICT_KEY][stat.object] = 'Restricted' else: self._request_nid_name(stat.predicate, request[1]) self._request_nid_name(stat.object, request[1]) for att in stat.attributes: self._handle_attribute(att, request[1]) for name in self.data[NODE_KEY].names: self._request_nid_name(name[1], request[1]) self.status = REQUEST_WAIT elif self.status == REQUEST_WAIT: if self.name_responses >= self.name_requests and \ self.value_type_responses >= self.value_type_requests: return True return False
def _handle_attribute(self, attribute, language): self.__lock.acquire() self.value_type_requests = self.value_type_requests + 1 self.__lock.release() self._request_nid_name(attribute.type, language) #self._request_nid_name(attribute.value, language) value = attribute.value if isinstance(value, unicode): value = value.encode("utf-8") self.add_behaviour( RequestInitiatorBehaviour( store=AID(str(attribute.type)), request='value-type:' + str(value) + ":" + str(language), handle_no_participant=self._handle_no_participant_value_type, handle_inform=self._handle_inform_value_type, check_cancel=self._check_cancel)) for att in attribute.attributes: self._handle_attribute(att, language)
def _request_nid_name(self, nid, language): IBASGlobal.print_message('VA requesting nid-name ' + str(nid), 2) try: nid = str(nid) except: return self.__lock.acquire() if self.data[NID_DICT_KEY].has_key(nid): self.__lock.release() return self.data[NID_DICT_KEY][nid] = None self.name_requests = self.name_requests + 1 self.__lock.release() self.add_behaviour( RequestInitiatorBehaviour( store=AID(str(nid)), request='nid-name:' + str(language), handle_no_participant=self._handle_no_participant_name, handle_agree=self._handle_agree, handle_inform=self._handle_inform_name, handle_inform_done=self._handle_inform_done_name, check_cancel=self._check_cancel)) return
def add_node(self, path): IBASGlobal.print_message('IA: Add Node: ' + str(path), 1) self.node_rep = None self.add_behaviour( RequestInitiatorBehaviour( store=AID('Node Manager Agent'), request=path, handle_response=self._handle_response, handle_agree=self._handle_agree, handle_refuse=self._handle_refuse, handle_inform=self._handle_inform_view, handle_inform_done=self._handle_inform_done_view, handle_failure=self._handle_failure, cancel=self._cancel, check_cancel=self._check_cancel)) while self.node_rep == None: time.sleep(0.5) res = self.node_rep self.node_rep = None return res
def search_node_by_name(self, name): IBASGlobal.print_message('IA: SEARCH FOR ' + str(name) + ' INITIATED', 1) self.search_result = None self.add_behaviour( RequestInitiatorBehaviour( store=AID('Search Agent'), request='name:' + name, handle_response=self._handle_response, handle_agree=self._handle_agree, handle_refuse=self._handle_refuse, handle_inform=self._handle_inform, handle_inform_done=self._handle_inform_done, handle_failure=self._handle_failure, cancel=self._cancel, check_cancel=self._check_cancel)) while self.search_result is None: time.sleep(0.5) res = self.search_result self.search_result = None return res
def get_nid_name(self, nid, language): IBASGlobal.print_message( 'IA: GET NAME FOR ' + str(nid) + ' language' + str(language), 2) self.search_result = None self.add_behaviour( RequestInitiatorBehaviour( store=AID(str(nid)), request='name:' + language, handle_response=self._handle_response, handle_agree=self._handle_agree, handle_refuse=self._handle_refuse, handle_inform=self._handle_inform, handle_inform_done=self._handle_inform_done, handle_failure=self._handle_failure, handle_no_participant=self._handle_no_participant, cancel=self._cancel, check_cancel=self._check_cancel)) while self.search_result is None: time.sleep(0.1) res = self.search_result self.search_result = None return res