Esempio n. 1
0
class SimpleConnector(AgentConnector):
    def __init__(self, agent):
        AgentConnector.__init__(self, agent)
        self.add_output_command("increase-number")
        self.num = SoarWME("number", 0)
        #self.target = SoarWME("target", 10)

    def on_input_phase(self, input_link):
        if not self.num.is_added():
            self.num.add_to_wm(input_link)
        else:
            self.num.update_wm()

    def on_init_soar(self):
        self.num.remove_from_wm()

    def on_output_event(self, command_name, root_id):
        if command_name == "increase-number":
            self.process_increase_command(root_id)

    def process_increase_command(self, root_id):
        number = root_id.GetChildInt("number")
        if number:
            self.num.set_value(self.num.val + number)
        root_id.AddStatusComplete()
Esempio n. 2
0
class GymConnector(AgentConnector):
    """Handle arbitrary Gym inputput and output data

    Input:
        - on_input_phase will be automatically called before each input phase
        - adds observation vector to WMEs
    Output:
        - call add_output_command to add the name of an output-link command to look for
        - on_output_event will then be called if such a command is added by the agent
        - sends commands as actions
    """
    def __init__(self, agent, motor, perception):

        AgentConnector.__init__(self, agent)
        self.motor = motor
        self.perception = perception
        self.waiting = False

        #TODO: loop through obs and action space; for action in agent.actions[]:

        # self.add_output_command("increase-number")
        # self.num = SoarWME("number", 0)

        # self.add_output_command("direction")
        self.add_output_command("move-cart")

        self.cart_pos = SoarWME("cart-position", 0.)
        self.cart_vel = SoarWME("cart-velocity", 0.)
        self.pole_pos = SoarWME("pole-angle", 0.)
        self.pole_vel = SoarWME("pole-tip-velocity", 0.)

    def on_input_phase(self, input_link):
        """update working memory, automatically called before each input phase """

        print(f"on_input_phase()")

        #cycle through WMEs, add if missing, and update.

        #global input_wmes
        #(cart_pos, cart_vel, pole_pos, pole_vel) = input_wmes

        # if not self.waiting:

        observation = self.perception.observation
        self.cart_pos.set_value(observation[0])
        self.cart_vel.set_value(observation[1])
        self.pole_pos.set_value(observation[2])
        self.pole_vel.set_value(observation[3])

        # self.waiting = True

        #TODO: loop through collection of WMEs

        if not self.cart_pos.is_added():
            self.cart_pos.add_to_wm(input_link)
        else:
            self.cart_pos.update_wm()
        if not self.cart_vel.is_added():
            self.cart_vel.add_to_wm(input_link)
        else:
            self.cart_vel.update_wm()
        if not self.pole_pos.is_added():
            self.pole_pos.add_to_wm(input_link)
        else:
            self.pole_pos.update_wm()
        if not self.pole_vel.is_added():
            self.pole_vel.add_to_wm(input_link)
        else:
            self.pole_vel.update_wm()

        # if not self.num.is_added():
        #     self.num.add_to_wm(input_link)
        # else:
        #     self.num.update_wm()

    def on_init_soar(self):
        """handles an init-soar event (remove references to SML objects """

        print(f"Connector:on_init_soar()")

        # if self.current_observation != None:
        #     self.current_observation.remove_from_wm()
        # if self.next_action != None:
        #     self.next_action.remove_from_wm()

        if not self.cart_pos != None:
            self.cart_pos.remove_from_wm()

        if not self.cart_vel != None:
            self.cart_vel.remove_from_wm()

        if not self.pole_pos != None:
            self.pole_pos.remove_from_wm()

        if not self.pole_vel != None:
            self.pole_vel.remove_from_wm()

        # self.num.remove_from_wm()

    def on_output_event(self, command_name, root_id):
        """handle output commands with the given name (added by add_output_command)

        root_id is the root Identifier of the command (e.g. (<output-link> ^command_name <root_id>)
        """
        print(f"**** OUTPUT : {command_name}")

        # if command_name == "increase-number":
        #     self.process_increase_command(root_id)

        if command_name == "move-cart":
            self.process_move_direction(root_id)

    def process_move_direction(self, root_id):
        # next_action  = root_id.GetChildString("direction")
        next_action = root_id.GetChildInt("direction")
        # print(f"MOVE DIRECTION: {next_action}")
        if next_action:
            # self.motor.next_action = next_action
            print(f"MOVE DIRECTION: {next_action}")
            self.motor.next_action = next_action
        root_id.AddStatusComplete()