Ejemplo n.º 1
0
def optimal_value_strategy(s, action_list):

    actions_consequences = []
    rotations = []
    for action in action_list:

        if action.name is "none":
            rotation = 0
        else:
            # optimize action
            rotation, _ = heinrich_test(s, action, False, iterations=20)

        rotations += [rotation]

        # apply optimized rotation
        s.pose.rotate(rotation)

        actions_consequences.append(
            Sim.simulateAction(action, s, num_particles=30))

        # restore previous rotation
        s.pose.rotate(-rotation)

    # Decide best action
    selected_action_idx = Sim.decide_minimal(actions_consequences, s)

    return selected_action_idx, rotations[selected_action_idx]
Ejemplo n.º 2
0
    def step(self):

        # reset stuff
        self.turn_around_ball = 0
        self.rotate = 0
        self.walk_dist = 0

        # ---------------
        # approach ball
        # ---------------
        self.rotate = self.state.ball_position.angle()
        self.walk_dist = self.state.ball_position.abs()

        # HACK: execute motion request
        self.state.pose.rotate(self.rotate)
        self.state.pose.translate(self.walk_dist, 0)
        self.state.ball_position = m2d.Vector2(100.0, 0.0)

        # -----------------
        #  make a decision
        # -----------------

        self.selected_action_idx, self.turn_around_ball = self.strategy(
            self.state, self.action_list)
        selected_action = self.action_list[self.selected_action_idx]

        # --------------------
        #  execute the action
        # --------------------
        if selected_action.name == "none":
            # print("INFO: NONE action while ball outside of the field.")
            if self.state_category == a.Category.INFIELD:
                print("WARNING: action is NONE, what should we do here? " +
                      str(self.selected_action_idx))
                print("STATE: robot = {0}".format(self.state.pose))
        else:
            # rotate around ball if necessary
            self.state.pose.rotate(self.turn_around_ball)

            # hack: perfect world without noise
            perfect_world = False
            if perfect_world:
                real_action = a.Action("real_action", selected_action.speed, 0,
                                       selected_action.angle, 0)
            else:
                real_action = selected_action

            # expected_ball_pos should be in local coordinates for rotation calculations
            action_results = Sim.simulateAction(real_action,
                                                self.state,
                                                num_particles=1)

            # store the state and update the state
            new_ball_position = action_results.positions()[0]
            self.state_category = new_ball_position.cat()
            self.state.ball_position = new_ball_position.pos()
Ejemplo n.º 3
0
def minimal_rotation(s, action, turn_direction):

    turn_speed = math.radians(5.0)
    action_dir = 0

    none = a.Action("none", 0, 0, 0, 0)
    none_actions_consequences = Sim.simulateAction(none, s, num_particles=30)

    while True:
        # turn towards the direction of the action
        s.pose.rotate(action_dir)

        # Simulate Consequences
        actions_consequences = Sim.simulateAction(action, s, num_particles=30)

        # Decide best action
        selected_action_idx = Sim.decide_minimal(
            [none_actions_consequences, actions_consequences], s)

        # restore the previous orientation
        s.pose.rotate(-action_dir)

        if selected_action_idx != 0:
            break
        elif np.abs(action_dir) > math.pi:
            # print("WARNING: in minimal_rotation no kick found after rotation {0}.".format(action_dir))
            break
        else:
            # decide on rotation direction once
            if turn_direction == 0:
                attack_direction = attack_dir.get_attack_direction(s)
                turn_direction = np.sign(
                    attack_direction.angle())  # "> 0" => left, "< 0" => right

            # set motion request
            action_dir += turn_direction * turn_speed

    return action_dir
Ejemplo n.º 4
0
def direct_kick_strategy_cool(s, action_list, take_best=False):

    actions_consequences = []
    rotations = []

    fastest_action_dir = None
    fastest_action_idx = 0

    for idx, action in enumerate(action_list):

        if action.name is "none":
            rotation = 0
        else:
            # optimize action
            a0 = minimal_rotation(s, action, 1)
            a1 = minimal_rotation(s, action, -1)
            if np.abs(a0) < np.abs(a1):
                rotation = a0
            else:
                rotation = a1

            if np.abs(rotation) > 3:
                print(
                    "WARNING: in direct_kick_strategy_cool no kick found after rotation {0}."
                    .format(rotation))

        rotations += [rotation]

        # apply optimized rotation
        s.pose.rotate(rotation)

        actions_consequences.append(
            Sim.simulateAction(action, s, num_particles=30))

        # restore previous rotation
        s.pose.rotate(-rotation)

        if action.name is not "none":
            if fastest_action_dir is None or np.abs(rotation) < np.abs(
                    fastest_action_dir):
                fastest_action_dir = rotation
                fastest_action_idx = idx

    # Decide best action
    if take_best:
        selected_action_idx = Sim.decide_minimal(actions_consequences, s)
        return selected_action_idx, rotations[selected_action_idx]
    else:
        # print fastest_action_idx, selected_action_idx
        return fastest_action_idx, fastest_action_dir
Ejemplo n.º 5
0
def direct_kick_strategy(s, action_list):

    turn_speed = math.radians(5.0)
    action_dir = 0
    turn_direction = 0

    while True:
        # turn towards the direction of the action
        s.pose.rotate(action_dir)

        # Simulate Consequences
        actions_consequences = [
            Sim.simulateAction(action, s, num_particles=30)
            for action in action_list
        ]

        # Decide best action
        selected_action_idx = Sim.decide_minimal(actions_consequences, s)

        # restore the previous orientation
        s.pose.rotate(-action_dir)

        if selected_action_idx != 0:
            break
        elif np.abs(action_dir) > math.pi:
            print(
                "WARNING: in direct_kick_strategy no kick found after rotation {0}."
                .format(action_dir))
            break
        else:
            # decide on rotation direction once
            if turn_direction == 0:
                attack_direction = attack_dir.get_attack_direction(s)
                turn_direction = np.sign(
                    attack_direction.angle())  # "> 0" => left, "< 0" => right

            # set motion request
            action_dir += turn_direction * turn_speed

    return selected_action_idx, action_dir