示例#1
0
 def create_agent(cls, agent_class):
     message = Message()
     message.set_sender("Universe")  # SPECIAL CASE Environment
     message.set_recipients("Environment")
     message.set_directive("create_agent")
     message.set_payload(agent_class)
     return message
示例#2
0
 def list_agents(cls):
     message = Message()
     message.set_sender("Environment")  # SPECIAL CASE Environment
     message.set_recipients("Institution")
     message.set_directive("list_agents")
     message.set_payload(None)
     return message
示例#3
0
 def auction_result(self, message: Message):
     logging.log(EXPERIMENT, "Agent received item for bid %s", message.get_payload())
     new_message = Message()  # declare message
     new_message.set_sender(self.myAddress)  # set the sender of message to this actor
     new_message.set_directive("register_collateral")
     new_message.set_payload({"bid": self.min_value})
     self.send(self.collateral_institution, new_message)  # receiver_of_message, message
示例#4
0
 def initialize_agents(self):
     for agent in self.agents:
         new_message = Message()
         new_message.set_sender(self)
         new_message.set_directive("<INSERT DIRECTIVE NAME>")
         new_message.set_payload({"<PAYLOAD>": "<PAYLOAD VALUE>"})
         self.send(agent[0], new_message)
示例#5
0
 def shutdown_mes(self, message: Message = None):
     new_message = Message()
     new_message.set_sender(self.myAddress)
     new_message.set_directive("shutdown_mes")
     payload = {}
     new_message.set_payload(payload)
     self.send(self.dispatcher, new_message)
 def start_auction(self):
     new_message = Message()  # declare message
     new_message.set_sender(self)  # set the sender of message to this actor
     new_message.set_directive("start_auction")
     new_message.set_payload({"agents": self.agents})
     self.send(self.institutions[0],
               new_message)  # receiver_of_message, message
示例#7
0
 def start_auction(self, message: Message):
     if self.mtree_properties["num_auctions"] > 0:
         self.mtree_properties[
             "num_auctions"] = self.mtree_properties["num_auctions"] - 1
         print("INSTITUTION: Starting Auction")
         self.agents = message.get_payload()["agents"]
         self.bids = []
         self.bids_outstanding = len(self.agents)
         self.item_for_auction = random.randint(self.min_item_value,
                                                self.max_item_value)
         for agent in self.agents:
             new_message = Message()  # declare message
             new_message.set_sender(
                 self.myAddress)  # set the sender of message to this actor
             new_message.set_directive("item_for_bidding")
             new_message.set_payload({
                 "min_value": self.min_item_value,
                 "max_value": self.max_item_value
             })
             self.send(agent, new_message)  # receiver_of_message, message
     else:
         print("AUCTIONS COMPLETED")
         new_message = Message()  # declare message
         new_message.set_sender(
             self.myAddress)  # set the sender of message to this actor
         new_message.set_directive("close_environment")
         self.send(self.environment,
                   new_message)  # receiver_of_message, message
示例#8
0
    def initialize_agents(self, message: Message):
        '''give each of the agents their attributes and sends the info to institution for data collecting'''

        # contains the theta and sd
        payload = message.get_payload()
        #self.theta = payload["theta"]
        #self.sd = payload["sd"]

        #make the message to send to each agent
        message = Message()
        message.set_sender(self)
        message.set_directive("initialize_agents")
        message.set_payload(payload)

        #send message to all agents
        for agent in self.agents:
            self.send(agent, message)

        #create message for the institution
        message = Message()
        message.set_sender(self)
        message.set_directive("theta_sd")
        message.set_payload(payload)

        #send the theta and delta to the institution for data collection
        for institution in self.institutions:
            self.send(institution, message)
示例#9
0
 def make_bid(self):
     new_message = Message()  # declare message
     new_message.set_sender(self)  # set the sender of message to this actor
     new_message.set_directive("bid_for_item")
     new_message.set_payload({"bid": self.min_value})
     self.send(self.institution.myAddress,
               new_message)  # receiver_of_message, message
示例#10
0
 def forward_address_book(self, address):
     new_message = Message()
     new_message.set_sender(self.base_component.myAddress)
     new_message.set_directive("address_book_update")
     new_message.set_payload(self.addresses)
     #address = self.select_addresses(selector)
     self.base_component.send(address, new_message)
示例#11
0
 def shutdown_mes(self):
     new_message = Message()
     new_message.set_directive("shutdown_mes")
     new_message.set_sender(self.myAddress)
     payload = {}
     new_message.set_payload(payload)
     self.send(self.environment, new_message)
示例#12
0
 def update_mes_status(self, message: Message = None):
     new_message = Message()
     new_message.set_sender(self.myAddress)
     new_message.set_directive("update_mes_status")
     payload = message.get_payload()
     new_message.set_payload(payload)
     self.send(self.log_actor, new_message)
示例#13
0
 def request_agent_list(cls):
     print("AGENT LIST REQUESTED")
     message = Message()
     message.set_sender("Institution")  # SPECIAL CASE Environment
     message.set_recipients("Environment")
     message.set_directive("request_agent_list")
     message.set_payload(None)
     return message
示例#14
0
 def kill_run_by_id(self, run_id):
     dispatcher = ActorSystemConnector.__instance.actor_system.createActor(Dispatcher, globalName = "Dispatcher") #ActorSystem("multiprocTCPBase", self.capabilities).createActor(Dispatcher, globalName = "Dispatcher")
     configuration_message = Message()
     configuration_message.set_directive("kill_run_by_id")
     payload = {}
     payload["run_id"] = run_id
     configuration_message.set_payload(payload)
     ActorSystemConnector.__instance.actor_system.tell(dispatcher, configuration_message)
示例#15
0
 def alert_agents_of_price(self, current_price):
     for agent in self.agents:
         new_message = Message()  # declare message
         new_message.set_sender(
             self.myAddress)  # set the sender of message to this actor
         new_message.set_directive("current_price")
         new_message.set_payload({"current_price": current_price})
         self.send(agent, new_message)  # receiver_of_message, message
示例#16
0
 def agent_action_forward(self, message: Message):
     subject_id = message.get_payload()["subject_id"]
     subject_agent_map = self.subject_map[subject_id]
     new_message = Message()
     new_message.set_directive(message.get_payload()["action"])
     new_message.set_sender(self.myAddress)
     new_message.set_payload(message.get_payload())
     self.send(subject_agent_map, new_message)
示例#17
0
 def provide_endowment(self):
     endowment = self.mtree_properties["agent_endowment"]
     for agent in self.agent_addresses:
         new_message = Message()  # declare message
         new_message.set_sender(self.myAddress)  # set the sender of message to this actor
         new_message.set_directive("set_endowment")  # Set the directive (refer to 3. Make Messages) - has to match reciever decorator
         new_message.set_payload({"endowment": endowment})
         self.send(agent, new_message )  # receiver_of_message, message
示例#18
0
 def make_bid(self):
     print("Making a bid... Total Endowment: ", self.endowment)
     self.mtree_properties
     new_message = Message()  # declare message
     new_message.set_sender(self.myAddress)  # set the sender of message to this actor
     new_message.set_directive("bid_for_item")
     new_message.set_payload({"bid": self.min_value})
     self.send(self.institution, new_message)  # receiver_of_message, message
示例#19
0
    def setup_agent_requests(self, message: Message):
        if "address_book" not in dir(self):
            self.address_book = AddressBook(self)

        if "agents" not in dir(self):
            self.agents = []
            self.agent_addresses = []
        # ensure that the actor system and institution are running...
        #message = MessageSpace.create_agent(agent_class)
        num_agents = message.get_payload().number
        agent_class = message.get_payload().source_class

        # need to check source hash for simulation
        source_hash = message.get_payload().source_hash

        # memory = False
        # agent_memory = None
        # if "agent_memory" in message.get_payload().keys():
        #     memory = True
        #     agent_memory = message.get_payload()["agent_memory"]

        if "subjects" in dir(self):
            self.subject_map = {}

        for i in range(num_agents):
            agent_number = i + 1
            new_agent = self.createActor(agent_class, sourceHash=source_hash)
            self.send(new_agent, agent_class + " " + str(agent_number))
            self.agent_addresses.append(new_agent)
            self.agents.append([new_agent, agent_class])

            agent_info = {}
            agent_info["address_type"] = "agent"
            agent_info["address"] = new_agent
            agent_info["component_class"] = agent_class
            agent_info["component_number"] = agent_number
            agent_info["short_name"] = agent_class + " " + str(agent_number)

            self.address_book.add_address(agent_info["short_name"], agent_info)

            new_message = Message()
            #new_message.set_sender(self.myAddress)
            new_message.set_directive("simulation_properties")
            new_message.set_sender(self.myAddress)
            payload = {}
            #if "mtree_properties" not in dir(self):
            payload["log_actor"] = self.log_actor
            #payload["dispatcher"] = self.createActor("Dispatcher", globalName="dispatcher")
            payload["properties"] = self.mtree_properties
            payload["agent_information"] = agent_info
            if "subjects" in dir(self):
                payload["subject_id"] = self.subjects[i]["subject_id"]
                self.subject_map[payload["subject_id"]] = new_agent

            # if memory:
            #     payload["agent_memory"] = agent_memory
            new_message.set_payload(payload)
            self.send(new_agent, new_message)
示例#20
0
 def setup_environment_agents(self, agent_class, num_agents=1):
     #logging.info("AGENTS BEING SETUP FROM ENV")
     message = Message()
     message.set_directive("setup_agents")
     message.set_payload({
         "agent_class": agent_class,
         "num_agents": num_agents
     })
     self.actor_system.tell(self.environment, message)
示例#21
0
 def excepted_mes(self, message: Message):
     new_message = Message()
     new_message.set_sender(self.myAddress)
     new_message.set_directive("excepted_mes")
     try:
         new_message.set_payload(message.get_payload())
     except:
         new_message.set_payload(message)
     self.send(self.dispatcher, new_message)
示例#22
0
 def setup_environment_institution(self, environment_address,
                                   institution_class):
     classname = institution_class.__module__ + "." + institution_class.__name__
     message = Message()
     message.set_directive("setup_institution")
     message.set_payload({"institution_class": institution_class})
     self.actor_system.tell(self.environments[environment_address],
                            message)
     logging.info("INSTITUTION STARTED")
示例#23
0
 def create_root_environment(self, environment_class, properties=None):
     print("CREATING AN ENVIRONMENT")
     print(properties)
     self.environment = self.actor_system.createActor(environment_class)
     if properties is not None:
         message = Message()
         message.set_directive("simulation_properties")
         message.set_payload({"properties": properties})
         self.actor_system.tell(self.environment, message)
 def make_bid(self):
     new_message = Message()  # declare message
     new_message.set_sender(
         self.myAddress)  # set the sender of message to this actor
     new_message.set_directive("bid_for_item")
     self.last_bid = self.current_price + self.bid_increment
     new_message.set_payload({"bid": self.last_bid})
     self.send(self.institution,
               new_message)  # receiver_of_message, message
     self.log_message("Agent submitted bid: " + str(self.last_bid))
示例#25
0
    def run_simple_configuration(self):
        configuration = self.load_mtree_config(
            "/Users/Shared/repos/mTree_examples/mes_example_1/config/mes_example_1.json"
        )
        actor_system = ActorSystem('multiprocQueueBase')
        configuration_message = Message()
        configuration_message.set_directive("simulation_configurations")
        configuration_message.set_payload(configuration)

        actor_system.tell(self.dispatcher_address, configuration_message)
示例#26
0
    def run_simulation(self, mes_base_dir, configuration_filename, run_configuration):
        #sa = ActorSystem("multiprocTCPBase", capabilities).createActor(SimpleSourceAuthority)
        #ActorSystem("multiprocTCPBase").tell(sa, True)
        
        # print("!&@#*" * 25)
        # print(json.dumps(run_configuration))

        source_hash = self.load_base_mes(mes_base_dir)
        print(source_hash)
        # if self.instance.container is None:
        #     self.instance.container = SimulationContainer()
        # self.instance.container.create_dispatcher()
       
        #return
        # actor_system = ActorSystem()
        self.capabilities = dict([('Admin Port', 19000)])

        # Kill old dispatchers....

        # for dispatcher in self.__instance.dispatchers:
        #     ActorSystem().tell(dispatcher, ActorExitRequest())

        dispatcher = ActorSystemConnector.__instance.actor_system.createActor(Actor, globalName = "Dispatcher") # ActorSystem("multiprocTCPBase", self.capabilities).createActor(Dispatcher, globalName = "Dispatcher")
        self.__instance.dispatchers.append(dispatcher)
        #outconnect = ActorSystem("multiprocTCPBase").createActor(OutConnect, globalName = "OutConnect")

        configuration_message = Message()
        configuration_message.set_directive("simulation_configurations")
        # configuration = [{"mtree_type": "mes_simulation_description",
        #     "name":"Basic CVA Run",
        #     "id": "1",
        #     "environment": "CVAEnvironment",
        #     "institution": "CVAInstitution",
        #     "number_of_runs": 1,
        #     "agents": [{"agent_name": "CVASimpleAgent", "number": 5}],
        #     "properties": {
        #         "agent_endowment": 10
        #         }
        #     }]
        run_configuration["source_hash"] = source_hash

        # simulation_run_id is set here
        # we use a human readable date format
        
        config_base_name = os.path.basename(configuration_filename).split('.')[0]
        nowtime = datetime.datetime.now().timestamp()
        # Simulation Run ID Generator - TODO consolidate with subject ID generation
        nowtime_filename = datetime.datetime.now().strftime("%Y_%m_%d-%H_%M_%S")
        simulation_run_id = config_base_name + "-" + nowtime_filename #str(nowtime).split(".")[0]
        run_configuration["simulation_run_id"] = simulation_run_id
        run_configuration["mes_directory"] = mes_base_dir
        configuration_message.set_payload(run_configuration)
        print("SHOULD BE SENDING TO DISPATCHER")
        ActorSystemConnector.__instance.actor_system.tell(dispatcher, configuration_message) #createActor(Dispatcher, globalName = "Dispatcher")
        print("dispatcher request -> " + str(configuration_message))
示例#27
0
 def provide_endowment(self):
     endowment = 30
     for agent in self.agents:
         new_message = Message()  # declare message
         new_message.set_sender(
             self)  # set the sender of message to this actor
         new_message.set_directive(
             "set_endowment"
         )  # Set the directive (refer to 3. Make Messages) - has to match reciever decorator
         new_message.set_payload({"endowment": endowment})
         self.send(agent[0], new_message)  # receiver_of_message, message
示例#28
0
 def start_auction(self):
     new_message = Message()  # declare message
     new_message.set_sender(self.myAddress)  # set the sender of message to this actor
     new_message.set_directive("start_auction")
     new_message.set_payload({"agents": self.agent_addresses})
     self.send(self.institutions[0], new_message)  # receiver_of_message, message
     
     new_message = Message()  # declare message
     new_message.set_sender(self.myAddress)  # set the sender of message to this actor
     new_message.set_directive("register_for_collateral")
     new_message.set_payload({"agents": self.agent_addresses})
     self.send(self.institutions[1], new_message)  # receiver_of_message, message
示例#29
0
 def setup_environment_agents(self,
                              environment_name,
                              agent_class,
                              num_agents=1):
     message = Message()
     message.set_directive("setup_agents")
     message.set_payload({
         "agent_class": agent_class,
         "num_agents": num_agents,
         "subject_id": environment_name
     })
     self.actor_system.tell(self.environments[environment_name],
                            message)
示例#30
0
 def make_bid(self):
     self.mtree_properties
     new_message = Message()  # declare message
     new_message.set_sender(self.myAddress)  # set the sender of message to this actor
     new_message.set_directive("bid_for_item")
     bid_value = self.min_value
     print(self.agent_memory)
     if "loss_rate" in self.agent_memory.keys():
         if self.agent_memory["loss_rate"] > 0:
             bid_value = bid_value + self.agent_memory["loss_rate"]
     
     
     new_message.set_payload({"bid": bid_value})
     self.send(self.institution, new_message)  # receiver_of_message, message