def request_available_agent(self, message: Message): # ensure that the actor system and institution are running... # message = MessageSpace.create_agent(agent_class) num_agents = message.get_payload()["num_agents"] agent_class = message.get_payload()["agent_class"] for i in range(num_agents): new_agent = self.createActor(agent_class) self.agents.append(new_agent)
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 initialize_agents(self, message:Message): '''give the agent its theta and delta''' payload = message.get_payload() self.theta = payload["theta"] self.delta = payload["delta"] self.epsilon = payload["epsilon"]
def theta_sd(self, message: Message): '''gives theta and sd to self ''' payload = message.get_payload() self.theta = payload["theta"] self.delta = payload["delta"] self.epsilon = payload["epsilon"]
def returned_response(self, message: Message): '''records the response of the agent''' payload = message.get_payload() response = (payload["choice"][0]) #reset every choice everytime #print(response) self.collect_data(response) # collect data and calculate payoff
def returned_response(self, message: Message): ''''records the response of the agent''' payload = message.get_payload() response = (payload["choice"][0]) #reset every choice everytime #print(response) #check here self._outcome(response) # calculates which payout is chosen
def fill_in_rows(self, message: Message): ''' puts in the rows into the bundles + set period ''' payload = message.get_payload() self.bundles = payload["bundle"] self.experiment = payload["experiment"] self.total_experiments = payload["total_experiments"] self.run = payload["run"] self.total_runs = payload["total_runs"]
def make_choice(self, message:Message): # get row from bundle '''agent making choice''' #the agents gets the row and sd payload = message.get_payload() row = payload["row"] delta = self.delta #calculates the theta w/ triangularlly distributed delta if delta == 0: self.error = 0 else: self.error = np.random.triangular(-delta, 0, delta) theta = self.theta + self.error OptionA = row[0] # set optionA OptionB = row[1] # set optionB if len(OptionA) != len(OptionB): # ensure that there is no error in the row data raise ValueError("Length of options must be the same") # the probability and utility of OptionA Choice 1 prob_a1 = (OptionA[0][0]) out_a1 = self.CRRA(OptionA[0][1], theta) # the probability and utility OptionA Choice 2 prob_a2 = OptionA[1][0] out_a2 = self.CRRA(OptionA[1][1], theta) # the probability and utility OptionB Choice 1 prob_b1 = (OptionB[0][0]) out_b1 = self.CRRA(OptionB[0][1], theta) # the probability and utility OptionB Choice 2 prob_b2 = OptionB[1][0] out_b2 = self.CRRA(OptionB[1][1], theta) e = self.epsilon # calculates the utility w/ triangularlly distributed epsilon if e == 0: self.e = 0 else: self.e = np.random.triangular(-e, 0, e) # expected utility of OptionA and OptionB util_a = prob_a1 * out_a1 + prob_a2 * out_a2 + self.e util_b = prob_b1 * out_b1 + prob_b2 * out_b2 + self.e # selects and records the option according to agent's preferences if util_a > util_b: self.choice.append(OptionA) else: self.choice.append(OptionB)
def make_choice(self, message: Message): # get row from bundle '''agent makes their choice''' payload = message.get_payload() row = payload["row"] OptionA = row[0] OptionB = row[1] choice = input("You choose:") if choice == "A": self.choice.append(OptionA) elif choice == "B": self.choice.append(OptionB)
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 simulation_properties(self, message: Message): self.address_book = AddressBook(self) self.environment = message.get_sender() self.log_actor = message.get_payload()["log_actor"] if "mtree_properties" not in dir(self): self.mtree_properties = {} # if "agent_memory" not in dir(self): # self.agent_memory = {} if "properties" in message.get_payload().keys(): self.mtree_properties = message.get_payload()["properties"] if "agent_information" in message.get_payload().keys(): self.short_name = message.get_payload( )["agent_information"]["short_name"] self.agent_information = message.get_payload()["agent_information"] if "subject_id" in message.get_payload().keys(): self.subject_id = message.get_payload()["subject_id"]
def make_choice(self, message: Message): # get row from bundle '''agent makes their choice here''' #the agents gets the row and sd payload = message.get_payload() row = payload["row"] delta = self.delta #calculates the theta w/ normal distributed sd if delta == 0: error = 0 else: error = np.random.triangular(-delta, 0, delta) theta = self.theta + error + self.epsilon OptionA = row[0] # tuple of option 1 OptionB = row[1] # tuple of optoin 2 if len(OptionA) != len( OptionB): # ensure that there is no error in the row data raise ValueError("Length of options must be the same") # the probability and utility of OptionA Choice 1 prob_a1 = (OptionA[0][0]) out_a1 = self.CRRA(OptionA[0][1], theta) # the probability and utility OptionA Choice 2 prob_a2 = OptionA[1][0] out_a2 = self.CRRA(OptionA[1][1], theta) # the probability and utility OptionB Choice 1 prob_b1 = (OptionB[0][0]) out_b1 = self.CRRA(OptionB[0][1], theta) # the probability and utility OptionB Choice 2 prob_b2 = OptionB[1][0] out_b2 = self.CRRA(OptionB[1][1], theta) # expected utility of OptionA and OptionB util_a = prob_a1 * out_a1 + prob_a2 * out_a2 util_b = prob_b1 * out_b1 + prob_b2 * out_b2 # selects and records the option with larger expected utility if util_a > util_b: self.choice.append(OptionA) else: self.choice.append(OptionB)
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 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 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 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 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 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 fill_in_agents(self, message: Message): '''give agents to self''' payload = message.get_payload() self.agents = payload["agents"]
def get_payoff(self, message: Message): '''agent receive payoff after completion of all lotteries''' payload = message.get_payload() self.payoff.append(payload["payoff"]) print("You received:", payload["payoff"])
def fill_in_institution(self, message: Message): ''' let the agent know the institution ''' payload = message.get_payload() self.institutions = payload["institutions"]
def initialize_agents(self, message: Message): '''give the agent its theta and delta''' payload = message.get_payload() self.agent_id = payload["agent_id"]
def fill_in_institution(self, message: Message): ''' register agent in the institution ''' payload = message.get_payload() self.institutions = payload["institutions"]
def fill_in_rows(self, message: Message): ''' puts in the rows into the bundles & set num of experiments/runs ''' payload = message.get_payload() self.bundles = payload["bundle"]
def setup_boxes(self, message: Message): payload = message.get_payload() self.box_list = payload["box_list"]
def initialize_agent(self, message: Message): print("initializing agent") payload = message.get_payload() self.institution = payload["institution"]
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 theta_delta(self, message: Message): '''keep track of agent's theta, delta''' payload = message.get_payload() self.agent_id = payload["agent_id"]