Esempio n. 1
0
 def post_connect(self, agent_id, entities, config):
     self.entity_id = agent_id
     self.world_model = WorldModel()
     self.world_model.add_entities(entities)
     #todo: check whether we need to merge agent config and the config coming from kernel
     self.config = config
     print('post_connect agent_id:' + str(agent_id.get_value()))
Esempio n. 2
0
    def _update_receive_ball(self):

        ball_pose = WorldModel.get_pose('Ball')
        ball_vel = WorldModel.get_velocity('Ball')
        result = False

        if WorldModel.ball_is_moving():
            angle_velocity = tool.getAngleFromCenter(ball_vel)
            trans = tool.Trans(ball_pose, angle_velocity)

            role_pose = WorldModel.get_pose(self._my_role)
            if role_pose is None:
                return False

            tr_pose = trans.transform(role_pose)

            fabs_y = math.fabs(tr_pose.y)

            if self._receiving == False and \
                    fabs_y < self._can_receive_dist - self._can_receive_hysteresis:
                self._receiving = True

            elif self._receiving == True and \
                    fabs_y > self._can_receive_dist + self._can_receive_hysteresis:
                self._receiving = False

            if self._receiving and tr_pose.x > 0.0:
                tr_pose.y = 0.0
                inv_pose = trans.invertedTransform(tr_pose)
                angle_to_ball = tool.getAngle(inv_pose, ball_pose)
                self.pose = Pose(inv_pose.x, inv_pose.y, angle_to_ball)
                result = True

        return result
Esempio n. 3
0
    def _update_interpose(self):
        base_pose = WorldModel.get_pose(self._base)
        target_pose = WorldModel.get_pose(self._target)

        if base_pose is None or target_pose is None:
            return False

        angle_to_target = tool.getAngle(base_pose, target_pose)

        interposed_pose = Pose(0, 0, 0)
        if not self._to_dist is None:
            trans = tool.Trans(base_pose, angle_to_target)
            tr_interposed_pose = Pose(self._to_dist, 0.0, 0)
            interposed_pose = trans.invertedTransform(tr_interposed_pose)
        elif not self._from_dist is None:
            angle_to_base = tool.getAngle(target_pose, base_pose)
            trans = tool.Trans(target_pose, angle_to_base)
            tr_interposed_pose = Pose(self._from_dist, 0.0, 0)
            interposed_pose = trans.invertedTransform(tr_interposed_pose)

        interposed_pose.theta = angle_to_target

        self.pose = interposed_pose

        return True
Esempio n. 4
0
    def connect(self, host, port, teamname, version=11):
        """
        Gives us a connection to the server as one player on a team.  This
        immediately connects the agent to the server and starts receiving and
        parsing the information it sends.
        """

        # if already connected, raise an error since user may have wanted to
        # connect again to a different server.
        if self.__connected:
            msg = "Cannot connect while already connected, disconnect first."
            raise sp_exceptions.AgentConnectionStateError(msg)

        # the pipe through which all of our communication takes place
        self.__sock = sock.Socket(host, port)

        # our models of the world and our body
        self.wm = WorldModel(handler.ActionHandler(self.__sock))

        # set the team name of the world model to the given name
        self.wm.teamname = teamname

        # handles all messages received from the server
        self.msg_handler = handler.MessageHandler(self.wm)

        # set up our threaded message receiving system
        self.__parsing = True  # tell thread that we're currently running
        self.__msg_thread = threading.Thread(target=self.__message_loop,
                                             name="message_loop")
        self.__msg_thread.daemon = True  # dies when parent thread dies

        # start processing received messages. this will catch the initial server
        # response and all subsequent communication.
        self.__msg_thread.start()

        # send the init message and allow the message handler to handle further
        # responses.
        init_address = self.__sock.address
        init_msg = "(init %s (version %d))"
        self.__sock.send(init_msg % (teamname, version))

        # wait until the socket receives a response from the server and gets its
        # assigned port.
        while self.__sock.address == init_address:
            time.sleep(0.0001)

        # create our thinking thread.  this will perform the actions necessary
        # to play a game of robo-soccer.
        self.__thinking = False
        self.__think_thread = threading.Thread(target=self.__think_loop,
                                               name="think_loop")
        self.__think_thread.daemon = True

        # set connected state.  done last to prevent state inconsistency if
        # something goes wrong beforehand.
        self.__connected = True
Esempio n. 5
0
    def update(self):
        WorldModel.update_world()

        self._select_play()

        WorldModel.update_assignments(self._play.assignment_type)

        self._execute_play()

        self._evaluate_play()
Esempio n. 6
0
def run_simulated_mission(model, display=None, use_delays=False):
    print("Simulated mission running.")

    world_model = WorldModel(BLUEPRINT, CONFIG_FILE, simulated=True)
    ticks_left = 5 * MAX_EPISODE_TIME
    total_reward = 0
    current_r = 0

    while (ticks_left > 0 and world_model.is_mission_running()):
        ticks_left -= 1
        current_r = world_model.reward()
        action = model.act(current_r, world_model.get_observation())
        if display is not None:
            display.update(world_model)
        total_reward += current_r
        world_model.simulate(action)
        if use_delays:
            print(action)
            time.sleep(ACTION_DELAY)

    # Collect last reward, and give to model, then end the mission
    current_r = world_model.reward()
    model.act(current_r, world_model.get_observation())
    total_reward += current_r
    model.mission_ended()
    print("Simulated mission ended")

    return total_reward, (MAX_EPISODE_TIME - (ticks_left / 5))
Esempio n. 7
0
    def load(dir):
        with open(f"{dir}/planner.pkl", 'rb') as f:
            planner = pickle.load(f)

        planner.world_model = WorldModel.load(dir)

        return planner
Esempio n. 8
0
    def connect(self, host, port, teamname, version=11, goalie=False):
        """
        Gives us a connection to the server as one player on a team.  This
        immediately connects the agent to the server and starts receiving and
        parsing the information it sends.
        """
        # if already connected, raise an error since user may have wanted to
        # connect again to a different server.
        if self.__connected:
            msg = "Cannot connect while already connected, disconnect first."
            raise sp_exceptions.AgentConnectionStateError(msg)

        # the pipe through which all of our communication takes place
        self.__sock = sock.Socket(host, port)

        # our models of the world and our body
        self.wm = WorldModel(handler.ActionHandler(self.__sock))

        # set the team name of the world model to the given name
        self.wm.teamname = teamname

        # handles all messages received from the server
        self.msg_handler = handler.MessageHandler(self.wm)

        # set up our threaded message receiving system
        self.__parsing = True # tell thread that we're currently running
        self.__msg_thread = threading.Thread(target=self.__message_loop,
                name="message_loop")
        self.__msg_thread.daemon = True # dies when parent thread dies

        # start processing received messages. this will catch the initial server
        # response and all subsequent communication.
        self.__msg_thread.start()

        # send the init message and allow the message handler to handle further
        # responses.
        init_address = self.__sock.address

        if goalie is True:
            init_msg = "(init %s (version %d)(goalie))"
        else:
            init_msg = "(init %s (version %d))"

        self.__sock.send(init_msg % (teamname, version))

        # wait until the socket receives a response from the server and gets its
        # assigned port.
        while self.__sock.address == init_address:
            time.sleep(0.0001)

        # create our thinking thread.  this will perform the actions necessary
        # to play a game of robo-soccer.
        self.__thinking = False
        self.__think_thread = threading.Thread(target=self.__think_loop,
                name="think_loop")
        self.__think_thread.daemon = True

        # set connected state.  done last to prevent state inconsistency if
        # something goes wrong beforehand.
        self.__connected = True
Esempio n. 9
0
        async def on_start(self):
            self.terminate = False
            self.SUCCESS = False
            self.verbose = False

            # Initialization
            self.beliefs = WorldModel()  # B := B0; Initial Beliefs
            self.goals = self.beliefs.current_world_model.goals
            self.intentions = self.beliefs.current_world_model.goals  # I := I0; Initial Intentions
            self.htn_planner = HierarchicalTaskNetworkPlanner(self.beliefs)
            self.perception = Perception(self.beliefs)
            self.coordination = Coordination(self.beliefs)
            self.monitoring = Monitoring()

            self.what, self.why, self.how_well, self.what_else, self.why_failed = "", "", "", "", ""
            self.plans = []
            self.selected_plan = []
            self.percept = {}
            self.action = ""
            self.start_time = datetime.datetime.now()
Esempio n. 10
0
    def _update_intersection(self):
        target_pose = WorldModel.get_pose(self._target)
        base_pose = WorldModel.get_pose(self._base)

        if target_pose is None or base_pose is None:
            return False

        intersection = tool.get_intersection(base_pose, target_pose,
                                             self._pose1, self._pose2)

        angle = tool.getAngle(intersection, target_pose)

        intersection.x = tool.limit(intersection.x, self._range_x[0],
                                    self._range_x[1])
        intersection.y = tool.limit(intersection.y, self._range_y[0],
                                    self._range_y[1])

        self.pose = Pose(intersection.x, intersection.y, angle)

        return True
Esempio n. 11
0
def run_simulated_mission(model, mission, cfg, demo=False):
    print("Simulated mission running.")

    world_model = WorldModel(mission.blueprint,
                             cfg,
                             simulated=True,
                             agent_pos=mission.start_position)
    ticks_left = 5 * mission.max_episode_time
    total_reward = 0
    current_r = 0
    use_delays = mission.action_delay > 0

    while (ticks_left > 0 and world_model.is_mission_running()):
        ticks_left -= 1
        current_r = world_model.reward()
        if demo:
            action = model.demo_act(world_model.get_observation())
        else:
            action = model.act(current_r, world_model.get_observation())
        if mission.display is not None:
            mission.display.update(world_model)
        total_reward += current_r
        world_model.simulate(action)
        if use_delays:
            print(action)
            time.sleep(mission.action_delay)

    # Collect last reward, and give to model, then end the mission
    if mission.display is not None:
        mission.display.update(world_model)
    current_r = world_model.reward()
    if not demo:
        model.act(current_r, world_model.get_observation())
    total_reward += current_r
    model.mission_ended()
    print("Simulated mission ended")

    return MissionStats(reward=total_reward,
                        length=(mission.max_episode_time - (ticks_left / 5)))
Esempio n. 12
0
 async def setup(self):
     print(
         f"--- arm_agent: PeriodicSenderAgent started at {datetime.datetime.now().time()}"
     )
     init_world_model = WorldModel()
     start_at = datetime.datetime.now() + datetime.timedelta(
         seconds=init_world_model.current_world_model.
         init_delay_seconds["arm"])
     bdi_behaviour = self.BDIBehaviour(
         period=init_world_model.current_world_model.
         real_time_clock_period_seconds["arm"],
         start_at=start_at)
     self.add_behaviour(bdi_behaviour)
Esempio n. 13
0
        async def on_start(self):

            self.terminate = False
            self.SUCCESS = False
            self.verbose = False
            # Initialization
            self.htn_planner = HierarchicalTaskNetworkPlanner()
            self.goal = [('transfer_target_object_to_container', 'arm',
                          'target_object', 'table', 'container')]
            self.intentions = self.goal  # I := I0; Initial Intentions
            self.beliefs = WorldModel()  # B := B0; Initial Beliefs
            self.monitoring = Monitoring()
            self.perception = Perception()
            self.coordination = Coordination()

            # Disable all 3 coordination switches for testing
            self.coordination.control.send_requests = True
            self.coordination.control.center_init = False
            self.coordination.control.detect_last_position = True

            self.what, self.why, self.how_well, self.what_else, self.why_failed = "", "", "", "", ""
            self.plans = []
            self.start_time = datetime.datetime.now()
Esempio n. 14
0
    def _update_keep_y(self):
        target_pose = WorldModel.get_pose(self._target)

        if target_pose is None:
            return False

        keep_pose = Pose(target_pose.x, self._keep_y, 0.0)

        keep_pose.x = tool.limit(keep_pose.x, self._range_x[0],
                                 self._range_x[1])

        angle = tool.getAngle(keep_pose, target_pose)
        self.pose = Pose(keep_pose.x, keep_pose.y, angle)

        return True
Esempio n. 15
0
    def is_arrived(self, role):
        # robotが目標位置に到着したかを判断する
        # 厳し目につける

        role_pose = WorldModel.get_pose(role)

        if role_pose is None:
            return False

        arrived = False

        distance = tool.getSize(self.pose, role_pose)

        # 目標位置との距離、目標角度との差がtolerance以下であれば到着判定
        if distance < self._arrived_position_tolerance:
            diff_angle = tool.normalize(self.pose.theta - role_pose.theta)

            if tool.normalize(diff_angle) < self._arrived_angle_tolerance:
                arrived = True

        return arrived
Esempio n. 16
0
    def _update_look_intersection(self):
        target_pose = WorldModel.get_pose(self._target)

        if target_pose is None:
            return False

        target_theta = target_pose.theta
        dist = 300  # フィールドサイズが300 mを超えたら書き直す必要あり
        look_pose = Pose(dist * math.cos(target_theta),
                         dist * math.sin(target_theta), 0)

        intersection = tool.get_intersection(look_pose, target_pose,
                                             self._pose1, self._pose2)

        angle = tool.getAngle(intersection, target_pose)

        intersection.x = tool.limit(intersection.x, self._range_x[0],
                                    self._range_x[1])
        intersection.y = tool.limit(intersection.y, self._range_y[0],
                                    self._range_y[1])

        self.pose = Pose(intersection.x, intersection.y, angle)

        return True
Esempio n. 17
0
class Agent:
    def __init__(self):
        # whether we're connected to a server yet or not
        self.__connected = False

        # set all variables and important objects to appropriate values for
        # pre-connect state.

        # the socket used to communicate with the server
        self.__sock = None

        # models and the message handler for parsing and storing information
        self.wm = None
        self.msg_handler = None

        # parse thread and control variable
        self.__parsing = False
        self.__msg_thread = None

        self.__thinking = False # think thread and control variable
        self.__think_thread = None

        # whether we should run the think method
        self.__should_think_on_data = False

        # whether we should send commands
        self.__send_commands = False

        self.goal_pos = None

    def connect(self, host, port, teamname, version=11, goalie=False):
        """
        Gives us a connection to the server as one player on a team.  This
        immediately connects the agent to the server and starts receiving and
        parsing the information it sends.
        """
        # if already connected, raise an error since user may have wanted to
        # connect again to a different server.
        if self.__connected:
            msg = "Cannot connect while already connected, disconnect first."
            raise sp_exceptions.AgentConnectionStateError(msg)

        # the pipe through which all of our communication takes place
        self.__sock = sock.Socket(host, port)

        # our models of the world and our body
        self.wm = WorldModel(handler.ActionHandler(self.__sock))

        # set the team name of the world model to the given name
        self.wm.teamname = teamname

        # handles all messages received from the server
        self.msg_handler = handler.MessageHandler(self.wm)

        # set up our threaded message receiving system
        self.__parsing = True # tell thread that we're currently running
        self.__msg_thread = threading.Thread(target=self.__message_loop,
                name="message_loop")
        self.__msg_thread.daemon = True # dies when parent thread dies

        # start processing received messages. this will catch the initial server
        # response and all subsequent communication.
        self.__msg_thread.start()

        # send the init message and allow the message handler to handle further
        # responses.
        init_address = self.__sock.address

        if goalie is True:
            init_msg = "(init %s (version %d)(goalie))"
        else:
            init_msg = "(init %s (version %d))"

        self.__sock.send(init_msg % (teamname, version))

        # wait until the socket receives a response from the server and gets its
        # assigned port.
        while self.__sock.address == init_address:
            time.sleep(0.0001)

        # create our thinking thread.  this will perform the actions necessary
        # to play a game of robo-soccer.
        self.__thinking = False
        self.__think_thread = threading.Thread(target=self.__think_loop,
                name="think_loop")
        self.__think_thread.daemon = True

        # set connected state.  done last to prevent state inconsistency if
        # something goes wrong beforehand.
        self.__connected = True

    def goto_kickoff_position(self):
        """
        Return the Agent to the kickoff position
        """

        # move player to kickoff location
        self.wm.teleport_to_point(self.calc_kickoff_pos())


    def calc_kickoff_pos(self):
        """
        Calculate the Agent's kickoff position
        on the soccer field
        """
        # used to flip x coords for other side
        side_mod = 1
        if self.wm.side == WorldModel.SIDE_R:
            side_mod = -1

        if self.wm.uniform_number == 1:
            return (-5 * side_mod, 30)
        elif self.wm.uniform_number == 2:
            return (-40 * side_mod, 15)
        elif self.wm.uniform_number == 3:
            return (-40 * side_mod, 00)
        elif self.wm.uniform_number == 4:
            return (-40 * side_mod, -15)
        elif self.wm.uniform_number == 5:
            return (-5 * side_mod, -30)
        elif self.wm.uniform_number == 6:
            return (-20 * side_mod, 20)
        elif self.wm.uniform_number == 7:
            return (-20 * side_mod, 0)
        elif self.wm.uniform_number == 8:
            return (-20 * side_mod, -20)
        elif self.wm.uniform_number == 9:
            return (-10 * side_mod, 0)
        elif self.wm.uniform_number == 10:
            return (-10 * side_mod, 20)
        elif self.wm.uniform_number == 11:
            return (-10 * side_mod, -20)

        # an error occured, place the player at 0,0
        return (0, 0)


    def play(self):
        """
        Kicks off the thread that does the agent's thinking, allowing it to play
        during the game.  Throws an exception if called while the agent is
        already playing.
        """

        # ensure we're connected before doing anything
        if not self.__connected:
            msg = "Must be connected to a server to begin play."
            raise sp_exceptions.AgentConnectionStateError(msg)

        # throw exception if called while thread is already running
        if self.__thinking:
            raise sp_exceptions.AgentAlreadyPlayingError(
                "Agent is already playing.")

        # run the method that sets up the agent's persistant variables
        self.setup_environment()

        # tell the thread that it should be running, then start it
        self.__thinking = True
        self.__should_think_on_data = True
        self.__think_thread.start()

    def disconnect(self):
        """
        Tell the loop threads to stop and signal the server that we're
        disconnecting, then join the loop threads and destroy all our inner
        methods.

        Since the message loop thread can conceiveably block indefinitely while
        waiting for the server to respond, we only allow it (and the think loop
        for good measure) a short time to finish before simply giving up.

        Once an agent has been disconnected, it is 'dead' and cannot be used
        again.  All of its methods get replaced by a method that raises an
        exception every time it is called.
        """

        # don't do anything if not connected
        if not self.__connected:
            return

        # tell the loops to terminate
        self.__parsing = False
        self.__thinking = False

        # tell the server that we're quitting
        self.__sock.send("(bye)")

        # tell our threads to join, but only wait breifly for them to do so.
        # don't join them if they haven't been started (this can happen if
        # disconnect is called very quickly after connect).
        if self.__msg_thread.is_alive():
            self.__msg_thread.join(0.01)

        if self.__think_thread.is_alive():
            self.__think_thread.join(0.01)

        # reset all standard variables in this object.  self.__connected gets
        # reset here, along with all other non-user defined internal variables.
        Agent.__init__(self)

    def __message_loop(self):
        """
        Handles messages received from the server.

        This SHOULD NOT be called externally, since it's used as a threaded loop
        internally by this object.  Calling it externally is a BAD THING!
        """

        # loop until we're told to stop
        while self.__parsing:
            # receive message data from the server and pass it along to the
            # world model as-is.  the world model parses it and stores it within
            # itself for perusal at our leisure.
            raw_msg = self.__sock.recv()
            msg_type = self.msg_handler.handle_message(raw_msg)

            # we send commands all at once every cycle, ie. whenever a
            # 'sense_body' command is received
            if msg_type == handler.ActionHandler.CommandType.SENSE_BODY:
                self.__send_commands = True

            # flag new data as needing the think loop's attention
            self.__should_think_on_data = True

    def __think_loop(self):
        """
        Performs world model analysis and sends appropriate commands to the
        server to allow the agent to participate in the current game.

        Like the message loop, this SHOULD NOT be called externally.  Use the
        play method to start play, and the disconnect method to end it.
        """

        while self.__thinking:
            # tell the ActionHandler to send its enqueued messages if it is time
            if self.__send_commands:
                self.__send_commands = False
                self.wm.ah.send_commands()

            # only think if new data has arrived
            if self.__should_think_on_data:
                # flag that data has been processed.  this shouldn't be a race
                # condition, since the only change would be to make it True
                # before changing it to False again, and we're already going to
                # process data, so it doesn't make any difference.
                self.__should_think_on_data = False

                # performs the actions necessary for the agent to play soccer
                self.think()
            else:
                # prevent from burning up all the cpu time while waiting for data
                time.sleep(0.0001)

    def setup_environment(self):
        """
        Called before the think loop starts, this allows the user to store any
        variables/objects they'll want access to across subsequent calls to the
        think method.
        """

        # determine the enemy goal position
        goal_pos = None
        if self.wm.side == WorldModel.SIDE_R:
            goal_pos = (-55, 0)
        else:
            goal_pos = (55, 0)

        self.in_kick_off_formation = False

    def ball_visible(self):
        """
        Determine whether or not the ball is within the player's field of
        vision. Is its location known?
        """
        #TODO OR or AND?
        return (self.wm.ball is None) or (self.wm.ball.direction is None)

    def think(self):
        """
        Performs a single step of thinking for our agent.  Gets called on every
        iteration of our think loop.
        """

        # DEBUG:  tells us if a thread dies
        if not self.__think_thread.is_alive() or not self.__msg_thread.is_alive():
            raise Exception("A thread died.")

        # take places on the field by uniform number
        if self.wm.is_before_kick_off:
            # teleport if player has not already done so
            if not self.in_kick_off_formation:
                # increment kickoff flag
                self.in_kick_off_formation = True

                # go to kickoff position
                self.goto_kickoff_position()
                print "Teleport %i" % self.wm.uniform_number

                # exit think loop, decision made
                return
            # decide to take kickoff or not
            elif self.dec_take_kickoff():
                # thake the kickoff
                self.act_take_kickoff()

            # exit think loop, decision made
            return

        # clear kickoff formation flag after kickoff occurs
        if self.in_kick_off_formation:
            self.in_kick_off_formation = False

        # if the location of the ball is unkown, find it
        if not self.ball_visible():
            self.act_find_ball()
            return

        # attack!
        else:
            # find the ball
            if self.wm.ball is None or self.wm.ball.direction is None:
                self.wm.ah.turn(30)

                return

            # kick it at the enemy goal
            if self.wm.is_ball_kickable():
                self.wm.kick_to(self.goal_pos, 1.0)
                return
            else:
                # move towards ball
                if -7 <= self.wm.ball.direction <= 7:
                    self.wm.ah.dash(65)
                else:
                    # face ball
                    self.wm.ah.turn(self.wm.ball.direction / 2)

                return

# Helper methods below this line
    def angle(self, point1, point2):
        rel_point_dir = 0
        ang_1 = self.wm.angle_between_points(point1, point2)
        if self.wm.abs_body_dir is not None:
            rel_point_dir = self.wm.abs_body_dir - ang_1

        return rel_point_dir

    # it will calculate all paths from the point
    # expanding to possible points that are +- 6 from initial y
    def get_clear_point(self, point):

        result = None
        if not self.can_opponents_intercept():
            result = point

        return result

    # kicks to the point accurately
    def kick(self, power, point):
        self.wm.ah.kick(power, point)
        return

    def can_opponents_intercept(self):
        opponent = self.wm.get_nearest_opponent_to_point(self.wm.get_absolute_coords(self.wm.ball))
        distance = self.wm.get_distance_to_point(self.wm.get_object_absolute_coords(opponent))
        if opponent is not None and distance >= 10:
            return False
        return True

    # returns the power a shot should be taken with
    # from current point
    def get_power(self):
        goal = abs(self.goal_pos[0])
        x = abs(self.wm.abs_coords[0])
        result = (((goal - x) << 2) + 0.0) / 10
        return result


    # act - action prefix
    # scr - calculate action score returns score of (0 - 100)
    # dec - boolean decision returns (True/False)

    def act_move_to_ball(self):
        """
        move towards the ball, if the player does not know where the ball is
        call action_find_ball().
        """
        if self.wm.ball is not None:
            if (self.wm.ball.direction is not None) and (-7 <= self.wm.ball.direction <= 7):
                self.wm.ah.dash(50)
            else:
                self.wm.turn_body_to_point((0, 0))
        else:
            action_find_ball()


    def act_find_ball(self):
        """
        spin the spin the player until the ball is within the players
        field of vision
        """
        self.wm.ah.turn(30) #using the 30 degree default angle

    def dec_take_kickoff(self):
        """
        Should this player take the kickoff?
        """
        if not self.wm.is_kick_off_us():
            return False
        else:
            # calculate ball position
            (x,y) = get_object_absolute_coords(self.wm.ball)


            if self.side == WorldModel.SIDE_L:
                # if offensive player 1, and ball on opponent's side
                if (self.uniform_number == 1) and (x >= -10):
                    return True
                # if first defensive agent, take kickoff
                elif (self.uniform_number == 8):
                    return True
            else:
                # if offensive player 1, and ball on opponent's side
                if (self.uniform_number == 1) and (x <= 10):
                    return True
                # if first defensive agent, take kickoff
                elif (self.uniform_number == 8):
                    return True
        return False


    def act_take_kickoff(self):
        """
        perform the kickoff
        """
        if self.wm.is_ball_kickable():
            # kick with 100% extra effort at enemy goal
            self.wm.kick_to(goal_pos, 1.0)
        else:
            self.act_move_to_ball()

    def scr_shoot_ball(self):
        """
        Shoot the ball towards the enemy goal
        """
        #TODO ian's task
        score = 0
        if self.wm.ball is not None and self.wm.is_ball_kickable():
            score += 10
        if not self.can_opponents_intercept():
            score += 10


    def act_shoot_ball(self):
        """
        Shoot the ball towards the enemy goal
        """
        if self.wm.is_ball_kickable() and self.wm.ball is not None:
            self.wm.kick_to(self, self.get_clear_point(self.goal_pos), self.get_power())
        #ian's task

    def scr_dribble_ball(self):
        """
        calculate score for dribbling to ???
          direction?
          goal?
          point?
        """
        score = 0
        if not self.wm.is_ball_kickable():
            return 0
        if self.wm.is_ball_kickable():
            score += 5
        if not self.can_opponents_intercept():
            score += 5
        return score

        #TODO ian's task

    def act_dribble_ball(self):
        """
        dirbble to ball to ???
          direction?
          goal?
          point?
        """
        if self.wm.is_ball_kickable():
            self.wm.ah.turn(self.wm.ball.direction / 2)
            for x in range(0, 7):
                # get absolute direction to the point
                angle = self.angle(self.wm.abs_coords, self.goal_pos)
                self.wm.ah.kick(2, angle)
                self.wm.ah.dash(2)
        return
        #TODO ian's task

    def scr_pass_ball(self, target):
        """
        Should the player pass the ball to player (param target)
        calculate score 0-100
        """
        #TODO john's task


    def act_pass_ball(self, target):
        """
        pass the ball to param taraget
        """
        #TODO john's task

    def scr_mark_player(self, target):
        """
        Should the player (param target) be covered?
        """
        # 

    def act_mark_player(self, target):
        """
        Cover the player (param target)
        """
        #TODO renan's task

    def goto_ball(self):
        pass

    def intercept(self, target):
        """
        Intercept ball or player? target
        """
        pass
Esempio n. 18
0
File: train.py Progetto: ctoto93/pip
            pickle.dump(transformer, f)

    n_obs = transformer.rbf_dim if transformer else env.observation_space.shape[
        0]
    n_action = env.action_space.shape[0]
    max_action = float(env.action_space.high[0])

    critic = build_critic(n_obs, args.learning_rate)
    if transformer:
        critic = NetworkWithTransformer(critic, transformer)

    # Initialize agent
    if args.agent == "PIP":
        copy_tree(f"world_models/{args.world_model}",
                  f"{model_path}/world_model")
        world_model = WorldModel.load(model_path)
        planner = PSOPlanner(
            env.action_space,
            world_model,
            critic,
            length=args.plan_length,
            gamma=args.gamma,
            particles=args.particles,
            iterations=args.iterations,
            c1=args.c1,
            c2=args.c2,
            w=args.w,
            k=args.k,
        )

        agent = PIP(planner, critic, gamma=args.gamma)
Esempio n. 19
0
                               goal,
                               verbose=self.verbose,
                               all_plans=True,
                               sort_asc=True)
        else:
            return ""


if __name__ == '__main__':

    htn_planner = HierarchicalTaskNetworkPlanner()
    end_goal = [('transfer_target_object_to_container', 'arm', 'target_object',
                 'table', 'container')]
    intentions = end_goal  # I := I0; Initial Intentions
    from world_model import WorldModel
    beliefs = WorldModel()  # B := B0; Initial Beliefs

    print()
    beliefs.current_world_model.xyz["target_object"] = [-10, -10, 0]
    htn_plans = htn_planner.get_plans(
        beliefs.current_world_model,
        intentions)  # π := plan(B, I); MEANS_END REASONING
    if not htn_plans:
        print("-- No valid plan. Failure_reason: {}".format(
            htn_planner.failure_reason))
    else:
        beliefs.current_world_model.plans = htn_plans
        print("== Best current_world_model.plan: ",
              beliefs.current_world_model.plans[0])

    print()
Esempio n. 20
0
def run_mission(model, display=None):

    # Create default Malmo objects:
    my_mission = MalmoPython.MissionSpec(MISSION_XML, True)
    my_mission_record = MalmoPython.MissionRecordSpec()
    world_model = WorldModel(BLUEPRINT, CONFIG_FILE, simulated=False)
    # Attempt to start a mission:
    for retry in range(MAX_RETRIES):
        try:
            AGENT_HOST.startMission(my_mission, my_mission_record)
            break
        except RuntimeError as e:
            if retry == MAX_RETRIES - 1:
                print("Error starting mission:", e)
                exit(1)
            else:
                time.sleep(2**retry)

    # Loop until mission starts:
    print("Waiting for the mission to start ", end=' ')
    world_state = AGENT_HOST.getWorldState()
    while not world_state.has_mission_begun:
        print(".", end="")
        time.sleep(0.1)
        world_state = AGENT_HOST.getWorldState()
        for error in world_state.errors:
            print("Error:", error.text)

    print("\nMission running.")

    total_reward = 0
    current_r = 0

    start = time.time()
    # Loop until mission ends
    while (world_state.is_mission_running
           and world_model.is_mission_running()):
        world_state = AGENT_HOST.getWorldState()
        for error in world_state.errors:
            print("Error:", error.text)
        current_r += sum(r.getValue() for r in world_state.rewards)
        if len(world_state.observations) > 0:
            raw_obs = json.loads(world_state.observations[-1].text)
            world_model.update(raw_obs)
            current_r += world_model.reward()
            action = model.act(current_r, world_model.get_observation())
            if display is not None:
                display.update(world_model)
            total_reward += current_r
            current_r = 0
            if world_model.mission_complete(
            ) or not world_model.agent_in_arena():
                AGENT_HOST.sendCommand('quit')
            elif world_state.is_mission_running:
                AGENT_HOST.sendCommand(action)
        time.sleep(ACTION_DELAY)
    end = time.time()

    model.mission_ended()

    print()
    print("Mission ended")

    return total_reward, end - start
Esempio n. 21
0
def callback_friend_odom(msg, robot_id):
    WorldModel.set_friend_odom(msg, robot_id)
Esempio n. 22
0
        elif action == ('close_hand', ):
            action_successful = self.control.close_hand(
                world_model.size["object_side_length"])
        elif action == ('move_arm_above', 'container'):
            action_successful = self.control.move_arm_above_xyz(
                world_model.xyz["container"],
                world_model.location["servo_values"], 14)

        return action_successful


if __name__ == '__main__':

    # Sequence for testing
    from world_model import WorldModel
    current_world_model = WorldModel()
    coordination = Coordination(current_world_model)
    coordination.control.control_world_model["send_requests"] = False
    coordination.control.control_world_model["center_init"] = False
    coordination.control.control_world_model["detect_last_position"] = False
    coordination.execute_action(('initialize', 'arm'),
                                current_world_model.current_world_model)
    coordination.execute_action(('open_hand', ),
                                current_world_model.current_world_model)
    coordination.execute_action(('move_arm_above', 'target_object'),
                                current_world_model.current_world_model)
    coordination.execute_action(('move_arm', 'target_object'),
                                current_world_model.current_world_model)
    coordination.execute_action(('close_hand', ),
                                current_world_model.current_world_model)
    coordination.execute_action(('move_arm_up', 'target_object'),
Esempio n. 23
0
    class BDIBehaviour(PeriodicBehaviour):
        async def on_start(self):

            self.terminate = False
            self.SUCCESS = False
            self.verbose = False
            # Initialization
            self.htn_planner = HierarchicalTaskNetworkPlanner()
            self.goal = [('transfer_target_object_to_container', 'arm',
                          'target_object', 'table', 'container')]
            self.intentions = self.goal  # I := I0; Initial Intentions
            self.beliefs = WorldModel()  # B := B0; Initial Beliefs
            self.monitoring = Monitoring()
            self.perception = Perception()
            self.coordination = Coordination()

            # Disable all 3 coordination switches for testing
            self.coordination.control.send_requests = True
            self.coordination.control.center_init = False
            self.coordination.control.detect_last_position = True

            self.what, self.why, self.how_well, self.what_else, self.why_failed = "", "", "", "", ""
            self.plans = []
            self.start_time = datetime.datetime.now()

        async def run(self):
            # print(f"-- Sender Agent: PeriodicSenderBehaviour running at {datetime.datetime.now().time()}: {self.counter}")
            print("run tick: {}".format(self.beliefs.current_world_model.tick))

            # msg = Message(to="madks2@temp3rr0r-pc")  # Instantiate the message
            # msg.body = "Hello World: " + str(self.counter)  # Set the message content
            # await self.send(msg)

            self.add_belief()
            self.add_desire()
            self.add_intention()
            print("-- Sender Agent: Message sent!")

            if self.beliefs.update_tick(
            ) > self.beliefs.current_world_model.max_ticks:
                self.done()
                self.kill()
            else:
                SUCCESS = True if self.intentions == "" else False
                self.plans = self.htn_planner.get_plans(
                    self.beliefs.current_world_model,
                    self.intentions)  # π := plan(B, I); MEANS_END REASONING

        async def on_end(self):
            print("-- on_end")
            # stop agent from behaviour
            await self.agent.stop()

        # async def _step(self):
        #     # the bdi stuff
        #     pass
        #
        def add_belief(self):
            print("-- Sender Agent: add_belief")
            percept = self.perception.get_percept(text_engraving=(
                self.why_failed,
                self.how_well))  # get next percept ρ; OBSERVE the world
            self.beliefs = self.perception.belief_revision(
                self.beliefs, percept)  # B:= brf(B, ρ);
            self.beliefs = self.monitoring.fire_events(self.beliefs, percept)

        def add_desire(self):
            print("-- Sender Agent: add_desire")

        def add_intention(self):
            print("-- Sender Agent: add_intention")
            self.intentions = self.deliberate(
                self.beliefs, self.intentions
            )  # DELIBERATE about what INTENTION to achieve next

        def done(self):
            # the done evaluation
            print("-- Sender Agent: Done")

        def deliberate(self, current_beliefs, current_intentions):
            """
            Decide WHAT sate of affairs you want to achieve.
            :param current_beliefs: WordModel instance, the world model, information about the world.
            :param current_intentions: List of tuples, tasks that the agent has COMMITTED to accomplish.
            :return: List of tuples, filtered intentions that the agent COMMITS to accomplish.
            """
            # 1. Option Generation: The agent generates a set of possible alternatives.
            current_desires = current_intentions  # TODO: D := option(B, I);

            # 2. Filtering: Agent chooses between competing alternatives and commits to achieving them.
            current_intentions = self.filter_intentions(
                current_beliefs, current_desires,
                current_intentions)  # I := filter(B, D, I);

            return current_intentions

        def filter_intentions(self, current_beliefs, current_desires,
                              current_intentions):
            """
            Choose between competing alternatives and COMMITTING to achieving them.
            :param current_beliefs: WordModel instance, the world model, information about the world.
            :param current_desires: List of tuples, tasks that the agent would like to accomplish.
            :param current_intentions: List of tuples, tasks that the agent has COMMITTED to accomplish.
            :return: List of tuples, filtered intentions that the agent COMMITS to accomplish.
            """
            for current_intention in current_intentions:
                if current_intention == (
                'transfer_target_object_to_container', 'arm', 'target_object', 'table', 'container') \
                        and current_beliefs.current_world_model.location["target_object"] == "container":
                    current_intentions = ""  # if goal(s) achieved, empty I
            return current_intentions
Esempio n. 24
0
def enemyIDCallback(msg):
    WorldModel.set_existing_enemies_id(msg.data)
Esempio n. 25
0
def ballCallback(msg):
    WorldModel.set_ball_odom(msg)
Esempio n. 26
0
def refboxCallback(msg):
    WorldModel.set_refbox_command(msg.data)
Esempio n. 27
0
class RescueAgent:
    """The base class for handling the rescue"""

    request_id = 0

    def __init__(self):
        self.connection = None
        self.name = ''
        self.connect_request_id = None
        self.world_model = None
        self.config = None
        self.random = None
        self.entity_id = None
        pass

    def connect(self, _connection):
        self.connection = _connection
        self.connection.set_agent(self)
        connect_msg = AKConnect()
        connect_msg.set_message(RescueAgent.request_id, 1, self.name,
                                self.get_requested_entities())
        self.connect_request_id = RescueAgent.request_id
        RescueAgent.request_id += 1
        self.connection.send_msg(connect_msg)

    def message_received(self, msg):
        if isinstance(msg, KAConnectOK):
            self.handle_connect_ok(msg)
        elif isinstance(msg, KAConnectError):
            self.handle_connect_error(msg)
        elif isinstance(msg, KASense):
            self.process_sense(msg)

    def process_sense(self, msg):
        self.world_model.merge(msg.get_change_set())
        heard = msg.get_hearing()
        self.think(msg.get_time(), msg.get_change_set(), heard)

    def think(self, timestep, change_set, heard):
        print('think method is supposed to be overriden')

    # that will be overriden by agents
    def get_requested_entities(self):
        return ''

    # that will be overriden by agents
    def post_connect(self, agent_id, entities, config):
        self.entity_id = agent_id
        self.world_model = WorldModel()
        self.world_model.add_entities(entities)
        #todo: check whether we need to merge agent config and the config coming from kernel
        self.config = config
        print('post_connect agent_id:' + str(agent_id.get_value()))
        # print('config test comms.channels.count:' + self.config.get_value('comms.channels.count'))

    def handle_connect_ok(self, msg):
        if msg.request_id_comp.get_value() == self.connect_request_id:
            # print("handle connect ok")
            self.post_connect(msg.agent_id_comp.get_value(),
                              msg.world_comp.get_entities(),
                              msg.config_comp.get_config())
            ack_msg = AKAcknowledge(self.connect_request_id,
                                    msg.agent_id_comp.get_value())
            self.connection.send_msg(ack_msg)

    def handle_connect_error(self, msg):
        if msg.request_id_comp.get_value() == self.connect_request_id:
            print('KAConnectionError:' + msg.get_reason())

    def random_walk(self, timestep):
        # calculate 10 step path
        path = []
        start_pos = self.get_position()
        print('random walk agent_pos:' + str(start_pos))
        # construct random path
        for i in range(10):
            edges = self.world_model.get_entity(start_pos).get_edges()
            neighbors = []
            for edge in edges:
                if edge.get_neighbor() is not None:
                    neighbors.append(edge.get_neighbor())
            next = random.choice(neighbors)
            path.append(next)
            start_pos = next

        move_msg = AKMove()
        move_msg.agent_id.set_value(self.entity_id)
        move_msg.time.set_value(timestep)
        move_msg.path.set_ids(path)
        self.send_kernel_msg(move_msg)

    def get_location(self):
        return self.world_model.get_entity(self.entity_id).get_location(
            self.world_model)

    def get_position(self):
        # print('agent pos:' + str(self.world_model.get_entity(self.entity_id)))
        return self.world_model.get_entity(self.entity_id).get_position()

    def send_kernel_msg(self, msg):
        self.connection.send_msg(msg)
Esempio n. 28
0
def callback_test_name(msg):
    WorldModel.set_test_name(msg.data)
Esempio n. 29
0
def callback_yellow_info(msg):
    WorldModel.set_yellow_info(msg)
Esempio n. 30
0
class Agent:
    def __init__(self):
        # whether we're connected to a server yet or not
        self.__connected = False

        # set all variables and important objects to appropriate values for
        # pre-connect state.

        # the socket used to communicate with the server
        self.__sock = None

        # models and the message handler for parsing and storing information
        self.wm = None
        self.msg_handler = None

        # parse thread and control variable
        self.__parsing = False
        self.__msg_thread = None

        self.__thinking = False  # think thread and control variable
        self.__think_thread = None

        # whether we should run the think method
        self.__should_think_on_data = False

        # whether we should send commands
        self.__send_commands = False

    def connect(self, host, port, teamname, version=11):
        """
        Gives us a connection to the server as one player on a team.  This
        immediately connects the agent to the server and starts receiving and
        parsing the information it sends.
        """

        # if already connected, raise an error since user may have wanted to
        # connect again to a different server.
        if self.__connected:
            msg = "Cannot connect while already connected, disconnect first."
            raise sp_exceptions.AgentConnectionStateError(msg)

        # the pipe through which all of our communication takes place
        self.__sock = sock.Socket(host, port)

        # our models of the world and our body
        self.wm = WorldModel(handler.ActionHandler(self.__sock))

        # set the team name of the world model to the given name
        self.wm.teamname = teamname

        # handles all messages received from the server
        self.msg_handler = handler.MessageHandler(self.wm)

        # set up our threaded message receiving system
        self.__parsing = True  # tell thread that we're currently running
        self.__msg_thread = threading.Thread(target=self.__message_loop,
                                             name="message_loop")
        self.__msg_thread.daemon = True  # dies when parent thread dies

        # start processing received messages. this will catch the initial server
        # response and all subsequent communication.
        self.__msg_thread.start()

        # send the init message and allow the message handler to handle further
        # responses.
        init_address = self.__sock.address
        init_msg = "(init %s (version %d))"
        self.__sock.send(init_msg % (teamname, version))

        # wait until the socket receives a response from the server and gets its
        # assigned port.
        while self.__sock.address == init_address:
            time.sleep(0.0001)

        # create our thinking thread.  this will perform the actions necessary
        # to play a game of robo-soccer.
        self.__thinking = False
        self.__think_thread = threading.Thread(target=self.__think_loop,
                                               name="think_loop")
        self.__think_thread.daemon = True

        # set connected state.  done last to prevent state inconsistency if
        # something goes wrong beforehand.
        self.__connected = True

    def play(self):
        """
        Kicks off the thread that does the agent's thinking, allowing it to play
        during the game.  Throws an exception if called while the agent is
        already playing.
        """

        # ensure we're connected before doing anything
        if not self.__connected:
            msg = "Must be connected to a server to begin play."
            raise sp_exceptions.AgentConnectionStateError(msg)

        # throw exception if called while thread is already running
        if self.__thinking:
            raise sp_exceptions.AgentAlreadyPlayingError(
                "Agent is already playing.")

        # run the method that sets up the agent's persistant variables
        self.setup_environment()

        # tell the thread that it should be running, then start it
        self.__thinking = True
        self.__should_think_on_data = True
        self.__think_thread.start()

    def disconnect(self):
        """
        Tell the loop threads to stop and signal the server that we're
        disconnecting, then join the loop threads and destroy all our inner
        methods.

        Since the message loop thread can conceiveably block indefinitely while
        waiting for the server to respond, we only allow it (and the think loop
        for good measure) a short time to finish before simply giving up.

        Once an agent has been disconnected, it is 'dead' and cannot be used
        again.  All of its methods get replaced by a method that raises an
        exception every time it is called.
        """

        # don't do anything if not connected
        if not self.__connected:
            return

        # tell the loops to terminate
        self.__parsing = False
        self.__thinking = False

        # tell the server that we're quitting
        self.__sock.send("(bye)")

        # tell our threads to join, but only wait breifly for them to do so.
        # don't join them if they haven't been started (this can happen if
        # disconnect is called very quickly after connect).
        if self.__msg_thread.is_alive():
            self.__msg_thread.join(0.01)

        if self.__think_thread.is_alive():
            self.__think_thread.join(0.01)

        # reset all standard variables in this object.  self.__connected gets
        # reset here, along with all other non-user defined internal variables.
        Agent.__init__(self)

    def __message_loop(self):
        """
        Handles messages received from the server.

        This SHOULD NOT be called externally, since it's used as a threaded loop
        internally by this object.  Calling it externally is a BAD THING!
        """

        # loop until we're told to stop
        while self.__parsing:
            # receive message data from the server and pass it along to the
            # world model as-is.  the world model parses it and stores it within
            # itself for perusal at our leisure.
            raw_msg = self.__sock.recv()
            msg_type = self.msg_handler.handle_message(raw_msg)

            # we send commands all at once every cycle, ie. whenever a
            # 'sense_body' command is received
            if msg_type == handler.ActionHandler.CommandType.SENSE_BODY:
                self.__send_commands = True

            # flag new data as needing the think loop's attention
            self.__should_think_on_data = True

    def __think_loop(self):
        """
        Performs world model analysis and sends appropriate commands to the
        server to allow the agent to participate in the current game.

        Like the message loop, this SHOULD NOT be called externally.  Use the
        play method to start play, and the disconnect method to end it.
        """

        while self.__thinking:
            # tell the ActionHandler to send its enqueued messages if it is time
            if self.__send_commands:
                self.__send_commands = False
                self.wm.ah.send_commands()

            # only think if new data has arrived
            if self.__should_think_on_data:
                # flag that data has been processed.  this shouldn't be a race
                # condition, since the only change would be to make it True
                # before changing it to False again, and we're already going to
                # process data, so it doesn't make any difference.
                self.__should_think_on_data = False

                # performs the actions necessary for the agent to play soccer
                self.think()
            else:
                # prevent from burning up all the cpu time while waiting for data
                time.sleep(0.0001)

    def setup_environment(self):
        """
        Called before the think loop starts, this allows the user to store any
        variables/objects they'll want access to across subsequent calls to the
        think method.
        """

        self.in_kick_off_formation = False

    def player1(self):
        self.player10()
        #play
        xxxx = 0

    def player2(self):
        self.player10()
        #play
        xxxx = 0

    def player3(self):
        self.player10()
        #play
        xxxx = 0

    def player4(self):
        self.player10()
        #play
        xxxx = 0

    def player5(self):
        self.player10()
        #play
        xxxx = 0

    def player6(self):
        self.player10()
        #play
        xxxx = 0

    def player7(self):
        self.player10()
        #play
        xxxx = 0

    def player8(self):
        self.player10()
        #play
        xxxx = 0

    def player9(self):
        #play atacante
        self.player10()
        if False:
            time.sleep(0.1)

            #determinando o gol inimigo
            enemy_goal_pos = None
            enemy_goal_pos = (
                55, 0) if self.wm.side != WorldModel.SIDE_R else (-55, 0)
            # attack!
            # find the ball
            if self.wm.ball is None or self.wm.ball.direction is None:
                self.wm.ah.turn(30)

                return

            # kick it at the enemy goal
            if self.wm.is_ball_kickable():
                self.wm.kick_to((55, 0), 1.0)
                return
            else:
                # move towards ball
                if -7 <= self.wm.ball.direction <= 7:
                    self.wm.ah.dash(65)
                else:
                    # face ball
                    self.wm.ah.turn(self.wm.ball.direction / 2)

                return

    def player10(self):
        if True:
            print(self.cont)
            if self.cont % 10 > 0:
                self.wm.ah.dash(65)
            else:
                self.wm.ah.turn(90)
            self.cont += 1
            time.sleep(0.1)

    def player11(self):
        self.player10()

    def think(self):
        """
        Performs a single step of thinking for our agent.  Gets called on every
        iteration of our think loop.
        """

        # DEBUG:  tells us if a thread dies
        if not self.__think_thread.is_alive(
        ) or not self.__msg_thread.is_alive():
            raise Exception("A thread died.")

        # take places on the field by uniform number
        if not self.in_kick_off_formation:

            # used to flip x coords for other side
            side_mod = 1
            #COCDQTL Logica nao funciona, nao sei pq
            #if self.wm.side == WorldModel.SIDE_R:
            #    side_mod = -1

            if self.wm.uniform_number == 1:
                self.wm.teleport_to_point((-45 * side_mod, 0))
            elif self.wm.uniform_number == 2:
                self.wm.teleport_to_point((-40 * side_mod, 15))
            elif self.wm.uniform_number == 3:
                self.wm.teleport_to_point((-40 * side_mod, 00))
            elif self.wm.uniform_number == 4:
                self.wm.teleport_to_point((-40 * side_mod, -15))
            elif self.wm.uniform_number == 5:
                self.wm.teleport_to_point((-15 * side_mod, -25))
            elif self.wm.uniform_number == 6:
                self.wm.teleport_to_point((-15 * side_mod, 25))
            elif self.wm.uniform_number == 7:
                self.wm.teleport_to_point((-20 * side_mod, 12))
            elif self.wm.uniform_number == 8:
                self.wm.teleport_to_point((-20 * side_mod, -12))
            elif self.wm.uniform_number == 9:
                self.wm.teleport_to_point((-18 * side_mod, 0))
            elif self.wm.uniform_number == 10:
                self.wm.teleport_to_point((-18 * side_mod, 10))
            elif self.wm.uniform_number == 11:
                self.wm.teleport_to_point((-18 * side_mod, -10))

            self.in_kick_off_formation = True

            return

        # determine the enemy goal position

        if self.wm.uniform_number == 1:
            self.player1()
        elif self.wm.uniform_number == 2:
            self.player2()
        elif self.wm.uniform_number == 3:
            self.player3()
        elif self.wm.uniform_number == 4:
            self.player4()
        elif self.wm.uniform_number == 5:
            self.player5()
        elif self.wm.uniform_number == 6:
            self.player6()
        elif self.wm.uniform_number == 7:
            self.player7()
        elif self.wm.uniform_number == 8:
            self.player8()
        elif self.wm.uniform_number == 9:
            self.player9()
        elif self.wm.uniform_number == 10:
            self.player10()
        elif self.wm.uniform_number == 11:
            self.player11()
Esempio n. 31
0
class Agent:

	#variables
	goal_pos = None

#Start of methods
#############################################################################################

	"""@_init_
		see if server is connected
		initialize self
	"""
	def __init__(self):

		# whether we're connected to a server yet or not
		self.__connected = False

		# set all variables and important objects to appropriate values for
		# pre-connect state.

		# the socket used to communicate with the server
		self.__sock = None

		# models and the message handler for parsing and storing information
		self.wm = None
		self.msg_handler = None

		# parse thread and control variable
		self.__parsing = False
		self.__msg_thread = None

		self.__thinking = False # think thread and control variable
		self.__think_thread = None

		# whether we should run the think method
		self.__should_think_on_data = False

		# whether we should send commands
		self.__send_commands = False
		 #end of method

	"""@connect
		Gives us a connection to the server as one player on a team.  This
		immediately connects the agent to the server and starts receiving and
		parsing the information it sends.
	"""
	def connect(self, host, port, teamname, version=11):

		# if already connected, raise an error since user may have wanted to
		# connect again to a different server.
		if self.__connected:
		    msg = "Cannot connect while already connected, disconnect first."
		    raise sp_exceptions.AgentConnectionStateError(msg)

		# the pipe through which all of our communication takes place
		self.__sock = sock.Socket(host, port)

		# our models of the world and our body
		self.wm = WorldModel(handler.ActionHandler(self.__sock))

		# set the team name of the world model to the given name
		self.wm.teamname = teamname

		# handles all messages received from the server
		self.msg_handler = handler.MessageHandler(self.wm)

		# set up our threaded message receiving system
		self.__parsing = True # tell thread that we're currently running
		self.__msg_thread = threading.Thread(target=self.__message_loop,
			name="message_loop")
		self.__msg_thread.daemon = True # dies when parent thread dies

		# start processing received messages. this will catch the initial server
		# response and all subsequent communication.
		self.__msg_thread.start()

		# send the init message and allow the message handler to handle further
		# responses.
		init_address = self.__sock.address
		init_msg = "(init %s (version %d))"
		self.__sock.send(init_msg % (teamname, version))

		# wait until the socket receives a response from the server and gets its
		# assigned port.
		while self.__sock.address == init_address:
		    time.sleep(0.0001)

		# create our thinking thread.  this will perform the actions necessary
		# to play a game of robo-soccer.
		self.__thinking = False
		self.__think_thread = threading.Thread(target=self.__think_loop,
			name="think_loop")
		self.__think_thread.daemon = True

		# set connected state.  done last to prevent state inconsistency if
		# something goes wrong beforehand.
		self.__connected = True
		 #end of method

	"""@starting_field_position
		get all the player to the field
	"""
	def starting_field_position(self):

		self.wm.teleport_to_point(self.set_starting_postion())
		 #end of method

	"""@set_starting_position
		sets the initial positions of the players 
		according to jersey number
	"""
	def set_starting_postion(self):

	# used to flip x coords for other side
            if self.wm.side == WorldModel.SIDE_L:
                side_mod = 1

	    elif self.wm.side == WorldModel.SIDE_R:
                side_mod = 1

            if self.wm.uniform_number == 1:
		print "UNIFORM 1 %d " % (-50 * side_mod)
		return(-50 * side_mod, 0)
	    elif self.wm.uniform_number == 2:
		return(-35 * side_mod, 20)
	    elif self.wm.uniform_number == 3:
		return(-35 * side_mod, 5)
	    elif self.wm.uniform_number == 4:
		return(-35 * side_mod, -5)
	    elif self.wm.uniform_number == 5:
		return(-35 * side_mod, -20)
	    elif self.wm.uniform_number == 6:
		return(-20 * side_mod, -27)
	    elif self.wm.uniform_number == 7:
		return(-20 * side_mod, 10)
	    elif self.wm.uniform_number == 8:
		return(-20 * side_mod, -10)
	    elif self.wm.uniform_number == 9:
		return(-20 * side_mod, 27)
	    elif self.wm.uniform_number == 10:
		return(-10 * side_mod, 5)
	    elif self.wm.uniform_number == 11:
		return(-10 * side_mod, -5)

		#if an error
	    return (0,0)

	"""@play
		Kicks off the thread that does the agent's thinking, allowing it to play
	during the game.  Throws an exception if called while the agent is
	already playing.
	"""
	def play(self):

		# ensure we're connected before doing anything
		if not self.__connected:
		    msg = "Must be connected to a server to begin play."
		    raise sp_exceptions.AgentConnectionStateError(msg)

		# throw exception if called while thread is already running
		if self.__thinking:
		    raise sp_exceptions.AgentAlreadyPlayingError(
			"Agent is already playing.")

		# run the method that sets up the agent's persistant variables
		self.setup_environment()

		# tell the thread that it should be running, then start it
		self.__thinking = True
		self.__should_think_on_data = True
		self.__think_thread.start()
		 #end of method

	"""@disconnect
		Tell the loop threads to stop and signal the server that we're
	disconnecting, then join the loop threads and destroy all our inner
	methods.

	Since the message loop thread can conceiveably block indefinitely while
	waiting for the server to respond, we only allow it (and the think loop
	for good measure) a short time to finish before simply giving up.

	Once an agent has been disconnected, it is 'dead' and cannot be used
	again.  All of its methods get replaced by a method that raises an
	exception every time it is called.
	"""
	def disconnect(self):
	
		# don't do anything if not connected
		if not self.__connected:
		    return

		# tell the loops to terminate
		self.__parsing = False
		self.__thinking = False

		# tell the server that we're quitting
		self.__sock.send("(bye)")

		# tell our threads to join, but only wait breifly for them to do so.
		# don't join them if they haven't been started (this can happen if
		# disconnect is called very quickly after connect).
		if self.__msg_thread.is_alive():
		    self.__msg_thread.join(0.01)

		if self.__think_thread.is_alive():
		    self.__think_thread.join(0.01)

		# reset all standard variables in this object.  self.__connected gets
		# reset here, along with all other non-user defined internal variables.
		Agent.__init__(self)
		 #end of method

	"""@_message_loop
		Handles messages received from the server.

	This SHOULD NOT be called externally, since it's used as a threaded loop
	internally by this object.  Calling it externally is a BAD THING!
	"""
	def __message_loop(self):

		# loop until we're told to stop
		while self.__parsing:
		    # receive message data from the server and pass it along to the
		    # world model as-is.  the world model parses it and stores it within
		    # itself for perusal at our leisure.
		    raw_msg = self.__sock.recv()
		    msg_type = self.msg_handler.handle_message(raw_msg)

		    # we send commands all at once every cycle, ie. whenever a
		    # 'sense_body' command is received
		    if msg_type == handler.ActionHandler.CommandType.SENSE_BODY:
			self.__send_commands = True

		    # flag new data as needing the think loop's attention
		    self.__should_think_on_data = True
		 	#end of method

	"""@_think_loop
		Performs world model analysis and sends appropriate commands to the
	server to allow the agent to participate in the current game.

	Like the message loop, this SHOULD NOT be called externally.  Use the
	play method to start play, and the disconnect method to end it.
	"""
	def __think_loop(self):

		while self.__thinking:
		    # tell the ActionHandler to send its enqueued messages if it is time
		    if self.__send_commands:
			self.__send_commands = False
			self.wm.ah.send_commands()

		    # only think if new data has arrived
		    if self.__should_think_on_data:
			# flag that data has been processed.  this shouldn't be a race
			# condition, since the only change would be to make it True
			# before changing it to False again, and we're already going to
			# process data, so it doesn't make any difference.
			self.__should_think_on_data = False

			# performs the actions necessary for the agent to play soccer
			self.think()
		    else:
			# prevent from burning up all the cpu time while waiting for data
			time.sleep(0.0001)
			 #end of method

	"""@setup_environment
		Called before the think loop starts, this allows the user to store any
	variables/objects they'll want access to across subsequent calls to the
	think method.
	"""
	def setup_environment(self):

		# determine the enemy goal position
		#goal_pos = None
		if self.wm.side == WorldModel.SIDE_R:
		    self.goal_pos = (-55, 0)
		else:
		    self.goal_pos = (55, 0)

		self.in_kick_off_formation = False
		 #end of method

	"""@think
		Performs a single step of thinking for our agent.  Gets called on every
	iteration of our think loop.
	"""
	def think(self):
		
		# DEBUG:  tells us if a thread dies
		if not self.__think_thread.is_alive() or not self.__msg_thread.is_alive():
		    raise Exception("A thread died.")

		# take places on the field by uniform number
		if self.wm.is_before_kick_off():
			
		 # take places on the field by uniform number
        		if not self.in_kick_off_formation:

			#set the players on the field
			    	self.starting_field_position()
			    

			    	self.in_kick_off_formation = True
			    
			return

		# Referee started Kick Off
		if self.wm.is_before_kick_off():
		    
		    # player 10 takes the kick off
		    if self.wm.uniform_number == 10:

			if self.wm.is_ball_kickable():
			    # kick with 100% extra effort at enemy goal
			    self.wm.kick_to(self.goal_pos, 1.0)

			else:
			    # move towards ball
			    if self.wm.ball is not None:

				if (self.wm.ball.direction is not None and
				        -7 <= self.wm.ball.direction <= 7):
				    self.wm.ah.dash(50)

				else:
				    self.wm.turn_body_to_point((0, 0))

			# turn to ball if we can see it, else face the enemy goal
			if self.wm.ball is not None:
			    self.wm.turn_neck_to_object(self.wm.ball)

			return

		# attack!
		else:

		    # find the ball
		    if self.wm.ball is None or self.wm.ball.direction is None:
			self.wm.ah.turn(30)

			return

		    # kick it at the enemy goal if agent is within range of goal
		    if self.wm.is_ball_kickable():
			self.wm.kick_to(self.goal_pos, 1.0)
			return
		    else:
			# move towards ball
			if -7 <= self.wm.ball.direction <= 7:
			    self.wm.ah.dash(65)
			else:
			    # face ball
			    self.wm.ah.turn(self.wm.ball.direction / 2)

			return
Esempio n. 32
0
def callback_blue_info(msg):
    WorldModel.set_blue_info(msg)
Esempio n. 33
0
def callback_enemy_odom(msg, robot_id):
    WorldModel.set_enemy_odom(msg, robot_id)