def store_agent_memory(self, message: Message): self.subject_id = "TEST!" #message.get_payload()["subject_id"] new_message = Message() new_message.set_sender(self.myAddress) new_message.set_directive("store_agent_memory") new_message.set_payload({"agent_memory": self.agent_memory}) self.send(self.dispatcher, new_message)
def make_choice(self, message): print("received choices from institution...") element_selected = self.select_box() choice_message = Message() choice_message.set_sender(self) choice_message.set_directive("choice") choice_message.set_payload({"selected_element": element_selected}) self.send(self.institution, choice_message)
def run_simulation(self, configurations, run_number=None): #self.component_registrar.instance.components[""] asys = ActorSystem('multiprocTCPBase', capabilities) source_hash = SimulationController.instance.component_registrar.get_source_hash( ) for configuration in configurations: configuration["source_hash"] = source_hash configuration_message = Message() configuration_message.set_directive("simulation_configurations") configuration_message.set_payload(configuration) address = asys.createActor("live_dispatcher.LiveDispatcher", globalName="dispatcher") asys.tell(address, configuration_message)
def grab_response(self, message: Message): '''send the response back to the institution''' #create message message = Message() message.set_sender(self) message.set_directive("returned_response") #make payload payload = {} payload["choice"] = self.choice message.set_payload(payload) self.send(self.institutions[0], message) self.choice = []
def collect_data(self, response): '''collect data and send lottery payoff to agents''' random_int = random.uniform(0, 1) # a winning float from 0 to 1 first = response[0] # tuple of 1st choice second = response[1] if random_int < first[0]: # choice 1 is chosen response.append(first[1]) else: # choice 2 is chosen response.append(second[1]) outcome = response[2] #get outcome: A_x or B_x self.outcome_history.append(outcome) #print(self.outcome_history) numA = 0 for choice in self.outcome_history: if 'A' in choice: numA += 1 risk_preference = self.get_theta(int(numA)) self.payoff_list.append( response[3]) #record payoffs of all played lottery if len(self.outcome_history ) == 10: #make sure agent complete all lotteries print(self.outcome_history) print("completed rows:", len(self.payoff_list)) print(self.risk_preference) x = random.randint(0, 9) payload = {} payload["payoff"] = self.payoff_list[ x] #randomly choose one played lottery message = Message() message.set_sender(self) message.set_directive("get payoff") message.set_payload(payload) self.send(self.agents[0], message) self.experiment_log({ "subject id": self.agent_id, "choice": self.outcome_history, "risk preference": risk_preference, "est_theta": self.est_theta, "theta_range": self.theta_range, "played lotteries payoff": self.payoff_list, "payoff received": self.payoff_list[x] })
def request_system_status(self): ''' This provides system simulation status suitable for emission via websockets ''' web_socket_router_actor = self.createActor( Actor, globalName="WebSocketRouterActor") output = [] for run in self.simulation_runs: output.append(run.to_data_row()) message = Message() message.set_directive("system_status") message.set_sender(self.myAddress) payload = {"status": output} message.set_payload(payload) self.send(web_socket_router_actor, message)
def send_message(self, directive, receiver, payload=None): """Send message Constructs and sends a message inside the system """ new_message = Message() new_message.set_sender(self.myAddress) new_message.set_directive(directive) if payload is not None: new_message.set_payload(payload) if isinstance(receiver, list): for target_address in receiver: self.send(target_address, new_message) else: receiver_address = self.address_book.select_addresses( {"short_name": receiver}) self.send(receiver_address, new_message)
def excepted_mes_shutdown(self, environment_address, exception_payload): for run in self.simulation_runs: if run.mes_base_address == environment_address: run.mark_excepted() message = Message() message.set_directive("update_mes_status") message.set_sender(self.myAddress) payload = {} payload["status"] = run.status payload["start_time"] = str(run.start_time) payload["end_time"] = str(run.end_time) payload["total_time"] = str(run.end_time - run.start_time) payload["exception_payload"] = exception_payload message.set_payload(payload) self.send(run.mes_base_address, message) self.send(environment_address, ActorExitRequest())
def start_round(self, message: Message): print("Getting a start round request from the environment") # first we will redact the box list for use with the agents. self.redact_box_list() # now we can send a redacted box list to the agent and then we can tell them to make a choice setup_agent_box_list_message = Message() setup_agent_box_list_message.set_sender(self) setup_agent_box_list_message.set_directive("setup_boxes") setup_agent_box_list_message.set_payload( {"box_list": self.redacted_box_list}) for agent in self.agents: self.send(agent, setup_agent_box_list_message) # now we can send a redacted box list to the agent and then we can tell them to make a choice agent_choice_message = Message() agent_choice_message.set_sender(self) agent_choice_message.set_directive("make_choice") for agent in self.agents: self.send(agent, agent_choice_message)
def kill_run_by_id(self, message): for run in self.simulation_runs: logging.info("DISOPATCHEWR IKILL REUEST " + str(message.get_payload())) if run.run_code == message.get_payload()["run_id"]: run.mark_killed() message = Message() message.set_directive("update_mes_status") message.set_sender(self.myAddress) payload = {} payload["status"] = run.status payload["start_time"] = str(run.start_time) payload["end_time"] = str(run.end_time) payload["total_time"] = str(run.end_time - run.start_time) message.set_payload(payload) self.send(run.mes_base_address, message) self.send(run.mes_base_address, ActorExitRequest())
def reminder(self, seconds_to_reminder, message, addresses=None): if addresses is None: if type(seconds_to_reminder) is timedelta: self.wakeupAfter(seconds_to_reminder, payload=message) else: # TODO if not seconds then reject self.wakeupAfter(timedelta(seconds=seconds_to_reminder), payload=message) else: new_message = Message() new_message.set_directive("external_reminder") new_message.set_sender(self.myAddress) payload = {} payload["reminder_message"] = message payload["seconds_to_reminder"] = seconds_to_reminder new_message.set_payload(payload) for agent in addresses: self.send(agent, new_message)
def start_experiment(self, message: Message): '''Starts the experiment for all agents and institutions''' #loop through all agents and bundles for agent in self.agents: print("sending to agent") for row in self.bundles: #get agent making choice payload = {} payload["row"] = row message = Message() message.set_sender(self) message.set_directive("make_choice") message.set_payload(payload) self.send(agent, message) #get response from the agent message2 = Message() message2.set_sender(self) message2.set_directive("get_response") self.send(self.agents[0], message2)
def start_experiment(self, message: Message): '''Starts the experiment for all agents and institutions''' #loop through all agents and bundles for agent in self.agents: print("sending to agent") #print(self.experiment) #print(self.run) #print(self.bundles) #print("bundles #",len(self.bundles)) for row in self.bundles: payload = {} payload["row"] = row message = Message() message.set_sender(self) message.set_directive("make_choice") #TO DO watch out for message cuing message.set_payload(payload) self.send(agent, message) message2 = Message() message2.set_sender(self) message2.set_directive("grab_response") self.send(self.agents[0], message2)
def excepted_mes(self, exception_payload): new_message = Message() new_message.set_directive("excepted_mes") new_message.set_sender(self.myAddress) new_message.set_payload(exception_payload) self.send(self.environment, new_message)
def run_simulation(self, configuration, run_number=None, configuration_obect=None): #### # get the source hash for the newly loaded MES components #### source_hash = configuration["source_hash"] #### # Create the environment for the new MES #### logging.info('Creating a simulation environment') source_hash = configuration["source_hash"] environment_class = configuration["environment"] environment = self.createActor(environment_class, sourceHash=source_hash) # environment created self.environment = environment #### # Initializing Environment #### # Initialization Message 1 self.send(environment, str(environment_class)) startup_payload = {} startup_payload["properties"] = configuration["properties"] startup_payload["dispatcher"] = self.myAddress startup_payload["simulation_id"] = configuration["id"] startup_payload["simulation_run_id"] = configuration[ "simulation_run_id"] startup_payload["short_name"] = str(environment_class) if run_number is not None: startup_payload["run_number"] = run_number # Initialization Message 2 self.send(environment, startup_payload) # if "properties" in configuration.keys(): # message = Message() # message.set_directive("simulation_properties") # message.set_sender(self.myAddress) # payload = {"properties": configuration["properties"], "dispatcher":self.myAddress} # payload["simulation_id"] = configuration["id"] # payload["simulation_run_id"] = configuration["simulation_run_id"] # if run_number is not None: # payload["run_number"] = run_number # message.set_payload(payload) # self.send(environment, message) logging.info('Simulation environment created') if configuration_obect is not None: configuration_obect.set_mes_base_address(environment) #### # Setup logger for the MES # This will setup the folders necessary for the simulation files to be written to #### message = Message() message.set_directive("logger_setup") payload = {} logging.info("SHOULD HAVE AN ENVIRONMENT CLASS: " + str(environment_class)) payload["short_name"] = str(environment_class) payload["simulation_id"] = configuration["id"] payload["simulation_run_id"] = configuration["simulation_run_id"] payload["simulation_run_number"] = run_number payload["mes_directory"] = configuration["mes_directory"] payload["simulation_configuration"] = configuration payload["run_code"] = configuration_obect.run_code payload["status"] = configuration_obect.status # payload["run_code"] = configuration_obect.run_code # payload["run_code"] = configuration_obect.run_code if "data_logging" in configuration.keys(): payload["data_logging"] = configuration["data_logging"] message.set_payload(payload) self.send(environment, message) logging.info('Environment logger created') #### # Setup Institution(s) for the MES # This preps configuration, but won't intitiate instantiation #### institutions = [] institution_requests = [] if "institution" in configuration.keys(): institution_request = ComponentRequest() institution_request.source_hash = source_hash institution_request.number = agent_d["number"] institution_request.source_class = agent_d["agent_name"] institutions = [configuration["institution"]] elif "institutions" in configuration.keys(): institutions = configuration["institutions"] # if len(configuration["institutions"]) == 1: # institutions = [configuration["institutions"]] # else: # pass # # for institution_d in configuration["institutions"]: # institution_class = institution_d # institutions.append(institution_class) #### # Setup Agent(s) for the MES # This preps configuration, but won't intitiate instantiation #### agents = [] agent_requests = [] for agent_d in configuration["agents"]: agent_type = agent_d["agent_name"] agent_count = agent_d["number"] # message.set_payload({"agent_class": agent[0], "num_agents": agent[1], "source_hash": source_hash}) agent_request = ComponentRequest() agent_request.source_hash = source_hash agent_request.number = agent_d["number"] agent_request.source_class = agent_d["agent_name"] agent_requests.append(agent_request) agents.append((agent_type, agent_count)) # for i in range(0, agent_count): # agents.append((agent_type, 1)) if "properties" in configuration.keys(): message = Message() message.set_directive("simulation_properties") message.set_sender(self.myAddress) payload = { "properties": configuration["properties"], "dispatcher": self.myAddress } payload["simulation_id"] = configuration["id"] payload["simulation_run_id"] = configuration["simulation_run_id"] if run_number is not None: payload["run_number"] = run_number message.set_payload(payload) self.send(environment, message) # if 'institutions' not in locals(): # message = Message() # message.set_directive("setup_institution") # message.set_payload({"order": 2, "institution_class": institution, "source_hash": source_hash}) # self.send(environment, message) # else: for index, setup_inst in enumerate(institutions): order = index + 1 message = Message() message.set_directive("setup_institution") if isinstance(setup_inst, dict): message.set_payload({ "order": order, "institution_class": setup_inst["institution"], "source_hash": source_hash }) else: message.set_payload({ "order": order, "institution_class": institutions, "source_hash": source_hash }) self.send(environment, message) # if hasattr(self, 'agent_memory_prepared'): # for agent in zip(agents, self.agent_memory): # message = Message() # message.set_directive("setup_agents") # message.set_payload({"agent_class": agent[0][0], "num_agents": agent[0][1], "agent_memory": agent[1], "source_hash": source_hash}) # self.send(environment, message) # else: # TODO replace with agent requests for agent in agents: message = Message() message.set_directive("setup_agents") message.set_payload({ "agent_class": agent[0], "num_agents": agent[1], "source_hash": source_hash }) self.send(environment, message) # for agent_request in agent_requests: # message = Message() # message.set_directive("setup_agent_requests") # message.set_payload(agent_request) # self.send(environment, message) start_message = Message() start_message.set_sender("experimenter") start_message.set_directive("distribute_address_book") self.send(environment, start_message) start_message = Message() start_message.set_sender("experimenter") start_message.set_directive("start_environment") self.send(environment, start_message) logging.info('Simulation environment should have started')
def _outcome(self, response): '''calcualtes the payoff of the agents choice''' #print(self.experiment) random_int = random.uniform(0, 1) # a winning float from 0 to 1 first = response[0] #tuple of 1st choice second = response[1] if random_int < first[0]: # choice 1 is chosen response.append(first[1]) else: # choice 2 is chosen response.append(second[1]) outcome_list = response[2] #get outcome: A_x or B_x #print((self.outcome_history).keys()) if self.experiment not in self.outcome_history.keys(): self.outcome_history[self.experiment] = {} print("Alex") if self.run not in self.outcome_history[self.experiment].keys(): self.outcome_history[self.experiment][self.run] = [] temp_round_list = self.outcome_history[self.experiment][self.run] #print(temp_round_list) #print(outcome_list) #print(self.outcome_history[self.experiment]) temp_round_list.append(outcome_list) temp_round_list = temp_round_list[0:10] self.outcome_history[self.experiment][self.run] = temp_round_list print(temp_round_list) print("*****") print(self.experiment) print(self.run) print(outcome_list) print(self.outcome_history) print("^^^^^^") message = Message() message.set_sender(self) message.set_directive("send_data") payload = {} # self.outcome.append("None") # payload["bundle_response"] = self.outcome message.set_payload(payload) #make sure it stops at the end of list if "B_10" in temp_round_list: #print(temp_round_list) self.outcome = temp_round_list self.choice_outcome = [ self.experiment, self.run, self.theta, self.delta, self.outcome, self.epsilon ] #print(self.choice_outcome) self.outcome_history = {} w = csv.writer(open("Holt_Laury_Output raw2.csv", "a")) # run the same experiment w.writerow(self.choice_outcome) # print(self.choice_outcome) self.choice_outcome = [] #reset choice_outcome
def run_simulation(self, configuration, run_number=None): #self.component_registrar.instance.components[""] source_hash = configuration["source_hash"] environment = configuration["environment"] environment = self.createActor(environment, sourceHash=source_hash) self.environment = environment if "institution" in configuration.keys(): institution = configuration["institution"] elif "institutions" in configuration.keys(): institutions = [] for institution_d in configuration["institutions"]: institution_class = institution_d["institution"] institutions.append(institution_class) agents = [] for agent_d in configuration["agents"]: agent_type = agent_d["agent_name"] agent_count = agent_d["number"] for i in range(0, agent_count): agents.append((agent_type, 1)) if "properties" in configuration.keys(): message = Message() message.set_directive("simulation_properties") payload = { "properties": configuration["properties"], "dispatcher": self.myAddress } payload["simulation_id"] = configuration["id"] if run_number is not None: payload["run_number"] = run_number message.set_payload(payload) self.send(environment, message) if 'institutions' not in locals(): message = Message() message.set_directive("setup_institution") message.set_payload({ "institution_class": institution, "source_hash": source_hash }) self.send(environment, message) else: for institution in institutions: message = Message() message.set_directive("setup_institution") message.set_payload({ "institution_class": institution, "source_hash": source_hash }) self.send(environment, message) if hasattr(self, 'agent_memory_prepared'): for agent in zip(agents, self.agent_memory): message = Message() message.set_directive("setup_agents") message.set_payload({ "agent_class": agent[0][0], "num_agents": agent[0][1], "agent_memory": agent[1], "source_hash": source_hash }) self.send(environment, message) else: for agent in agents: message = Message() message.set_directive("setup_agents") message.set_payload({ "agent_class": agent[0], "num_agents": agent[1], "source_hash": source_hash }) self.send(environment, message) start_message = Message() start_message.set_sender("experimenter") start_message.set_directive("start_environment") self.send(environment, start_message)
def run_human_subject_experiment(self, configuration, run_number=None, configuration_obect=None): # self.(simulation_configuration.configuration, simulation_configuration.run_number, configuration_obect=simulation_configuration) #### # get the source hash for the newly loaded MES components #### source_hash = configuration["source_hash"] #### # Create the environment for the new MES #### logging.info('Creating a simulation environment') source_hash = configuration["source_hash"] environment_class = configuration["environment"] environment = self.createActor(environment_class, sourceHash=source_hash) # environment created self.environment = environment #### # Initializing Environment #### self.send(environment, str(environment_class)) logging.info('Simulation environment created') if configuration_obect is not None: configuration_obect.set_mes_base_address(environment) #### # Setup logger for the MES # This will setup the folders necessary for the simulation files to be written to #### message = Message() message.set_directive("logger_setup") payload = {} payload["short_name"] = str(environment_class) payload["simulation_id"] = configuration["id"] payload["simulation_run_id"] = configuration["simulation_run_id"] payload["simulation_run_number"] = run_number payload["mes_directory"] = configuration["mes_directory"] payload["simulation_configuration"] = configuration payload["run_code"] = "a1" #configuration_obect.run_code payload["status"] = "running" #configuration_obect.status payload["subjects"] = configuration["subjects"] # payload["run_code"] = configuration_obect.run_code # payload["run_code"] = configuration_obect.run_code if "data_logging" in configuration.keys(): payload["data_logging"] = configuration["data_logging"] payload["human_subjects"] = True message.set_payload(payload) self.send(environment, message) logging.info('Environment logger created') #### # Setup Institution(s) for the MES # This preps configuration, but won't intitiate instantiation #### institutions = [] if "institution" in configuration.keys(): institutions = [configuration["institution"]] elif "institutions" in configuration.keys(): institutions = configuration["institutions"] # if len(configuration["institutions"]) == 1: # institutions = [configuration["institutions"]] # else: # pass # # for institution_d in configuration["institutions"]: # institution_class = institution_d # institutions.append(institution_class) #### # Setup Agent(s) for the MES # This preps configuration, but won't intitiate instantiation #### agents = [] for agent_d in configuration["agents"]: agent_type = agent_d["agent_name"] agent_count = agent_d["number"] agents.append((agent_type, agent_count)) # for i in range(0, agent_count): # agents.append((agent_type, 1)) if "properties" in configuration.keys(): message = Message() message.set_directive("simulation_properties") message.set_sender(self.myAddress) payload = { "properties": configuration["properties"], "dispatcher": self.myAddress } payload["simulation_id"] = configuration["id"] payload["simulation_run_id"] = configuration["simulation_run_id"] payload["subjects"] = configuration["subjects"] if run_number is not None: payload["run_number"] = run_number message.set_payload(payload) self.send(environment, message) # if 'institutions' not in locals(): # message = Message() # message.set_directive("setup_institution") # message.set_payload({"order": 2, "institution_class": institution, "source_hash": source_hash}) # self.send(environment, message) # else: for index, setup_inst in enumerate(institutions): order = index + 1 message = Message() message.set_directive("setup_institution") logging.info("DISPATCHING INSTITUTIONS./..") logging.info(institutions) message.set_payload({ "order": order, "institution_class": setup_inst["institution"], "source_hash": source_hash }) # if isinstance(setup_inst, dict): # message.set_payload({"order": order, "institution_class": setup_inst["institution"], "source_hash": source_hash}) # else: # message.set_payload({"order": order, "institution_class": institutions, "source_hash": source_hash}) self.send(environment, message) # if hasattr(self, 'agent_memory_prepared'): # for agent in zip(agents, self.agent_memory): # message = Message() # message.set_directive("setup_agents") # message.set_payload({"agent_class": agent[0][0], "num_agents": agent[0][1], "agent_memory": agent[1], "source_hash": source_hash}) # self.send(environment, message) # else: for agent in agents: message = Message() message.set_directive("setup_agents") message.set_payload({ "agent_class": agent[0], "num_agents": agent[1], "source_hash": source_hash }) self.send(environment, message) start_message = Message() start_message.set_sender("experimenter") start_message.set_directive("start_environment") self.send(environment, start_message) logging.info('Simulation environment should have started')
def receiveMessage(self, message, sender): #outconnect = ActorSystem("multiprocTCPBase").createActor(OutConnect, globalName = "OutConnect") #self.send(outconnect, message) #logging.info("MESSAGE RCVD: %s DIRECTIVE: %s SENDER: %s", self, message, sender) # with open("/Users/Shared/repos/mTree_auction_examples/sample_output", "a") as file_object: # file_object.write("SHOULD BE RUNNING SIMULATION" + str(message) + "\n") logging.info("Dispatcher recieved message: " + str(message)) if not isinstance(message, ActorSystemMessage): if isinstance(message, AdminMessage): logging.info('DISPATCHER RECEIVED ADMIN MESSAGE') logging.info(message) if message.get_request() == "system_status": logging.info('System status message received') self.return_admin_status() elif message.get_request() == "kill_run_by_id": self.kill_run_by_id(message) elif isinstance(message, dict): # agent action processing... new_message = Message() new_message.set_directive("agent_action_forward") new_message.set_sender(self.myAddress) payload = message new_message.set_payload(payload["payload"]) self.send(self.environment, new_message) else: if message.get_directive() == "simulation_configurations": logging.info( 'Preparing to run a new set of simulation configurations' ) self.configurations_pending = message.get_payload() self.prepare_simulation_run(message.get_payload()) logging.info('Prepared the simulation') self.begin_simulations() logging.info('Simulations started') elif message.get_directive() == "human_subject_configuration": logging.info('Human Subject Configuration Starting!') self.run_human_subject_experiment(message.get_payload()) elif message.get_directive() == "check_status": self.get_status(sender) elif message.get_directive() == "kill_run_by_id": self.kill_run_by_id(message) elif message.get_directive() == "end_round": self.agent_memory = [] self.agents_to_wait = len(message.get_payload()["agents"]) elif message.get_directive() == "shutdown_mes": self.shutdown_mes(sender) elif message.get_directive() == "excepted_mes": self.excepted_mes_shutdown(sender, message.get_payload()) elif message.get_directive() == "request_system_status": self.request_system_status() elif message.get_directive() == "register_dispatcher": pass # system_status_actor = self.createActor(Actor, globalName = "SystemStatusActor") # message = AdminMessage(request="register_dispatcher") # self.send(system_status_actor, message) elif message.get_directive() == "register_websocket_router": # registering the dispatcher at the system status actor to prevent starting another self.websocket_router = sender self.send(self.websocket_router, "SLAMBACK") elif message.get_directive() == "store_agent_memory": if self.agents_to_wait > 1: self.agents_to_wait -= 1 self.agent_memory.append( message.get_payload()["agent_memory"]) self.send(sender, ActorExitRequest()) else: self.agent_memory.append( message.get_payload()["agent_memory"]) self.agents_to_wait -= 1 self.agent_memory_prepared = True self.send(sender, ActorExitRequest()) self.end_round() self.next_run()