maneuvers.py 11 KB
Newer Older
Aravind Bk's avatar
Aravind Bk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
from .maneuver_base import ManeuverBase
from env.simple_intersection.constants import *
import env.simple_intersection.road_geokinemetry as rd
from env.simple_intersection.features import extract_ego_features, extract_other_veh_features
from model_checker.simple_intersection import LTLProperty

import numpy as np
# TODO: separate out into different files?? is it really needed?


class KeepLane(ManeuverBase):

    def _init_param(self):
        self._v_ref = rd.speed_limit
        self._target_lane = self.env.ego.APs['lane']

    def _init_LTL_preconditions(self):
        self._LTL_preconditions.append(LTLProperty("G ( not veh_ahead )", 0))
        self._LTL_preconditions.append(LTLProperty("G ( not stopped_now )", 200,
                                    self._enable_low_level_training_properties))
        self._LTL_preconditions.append(
            LTLProperty("G ( (lane and target_lane) or (not lane and not target_lane) )", 200,
                        self._enable_low_level_training_properties))

    def generate_learning_scenario(self):
        self.generate_scenario(enable_LTL_preconditions=False,
Jae Young Lee's avatar
Jae Young Lee committed
27
                               ego_pos_range=(rd.hlanes.start_pos, rd.hlanes.end_pos),
Aravind Bk's avatar
Aravind Bk committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
                               ego_perturb_lim=(rd.hlanes.width / 4, np.pi / 6),
                               ego_heading_towards_lane_centre=True)
        # the goal reward and termination is led by the SimpleIntersectionEnv
        self.env._terminate_in_goal = True
        self.env._reward_in_goal = 200
        self._enable_low_level_training_properties = True

    @staticmethod
    def _features_dim_reduction(features_tuple):
        return extract_ego_features(features_tuple,
                                'pos_near_stop_region',
                                'v', 'v_ref', 'e_y',
                                'psi', 'theta', 'acc', 'psi_dot')


class Stop(ManeuverBase):

    _terminate_in_goal = True
    _reward_in_goal = None

    def _init_param(self):
        self._set_v_ref()
        self._target_lane = self.env.ego.APs['lane']

    def _init_LTL_preconditions(self):
        self._LTL_preconditions.append(LTLProperty("G ( not has_stopped_in_stop_region )",
                                       self._penalty(self._reward_in_goal)))

        self._LTL_preconditions.append(
            LTLProperty("G ( (before_but_close_to_stop_region or in_stop_region) U has_stopped_in_stop_region )", 0))

        self._LTL_preconditions.append(
            LTLProperty("G ( not stopped_now U in_stop_region )", 200,
                        self._enable_low_level_training_properties))

        self._LTL_preconditions.append(
            LTLProperty("G ( (lane and target_lane) or (not lane and not target_lane) )", 200,
                        self._enable_low_level_training_properties))

    def _update_param(self):
        self._set_v_ref()

    def _set_v_ref(self):
        self._v_ref = rd.speed_limit
        x = self.env.ego.x
        if x <= rd.hlanes.near_stop_region:
            self._v_ref = rd.speed_limit
        elif x <= rd.hlanes.stop_region_centre:
            self._v_ref = - (rd.speed_limit / abs(rd.hlanes.near_stop_region)) * (x - rd.hlanes.stop_region_centre)
        else:
            self._v_ref = 0

    def generate_learning_scenario(self):
        self.generate_scenario(ego_pos_range=(rd.hlanes.near_stop_region, - rd.intersection_width_w_offset / 2),
                               ego_perturb_lim=(rd.hlanes.width / 4, np.pi / 6),
                               ego_heading_towards_lane_centre=True)
        self._reward_in_goal = 200
        self._enable_low_level_training_properties = True

    def _low_level_manual_policy(self):
        return self.env.aggressive_driving_policy(EGO_INDEX)

    @staticmethod
    def _features_dim_reduction(features_tuple):
        return extract_ego_features(features_tuple,
                                'pos_near_stop_region',
                                'v', 'v_ref', 'e_y',
                                'psi', 'theta', 'acc', 'psi_dot',
                                'not_in_stop_region')


class Wait(ManeuverBase):

    _terminate_in_goal = True
    _reward_in_goal = None

    def _init_LTL_preconditions(self):
        self._LTL_preconditions.append(
106
            LTLProperty("G ( (in_stop_region and stopped_now) U highest_priority )", 0))
Aravind Bk's avatar
Aravind Bk committed
107 108 109 110 111 112 113 114 115

        self._LTL_preconditions.append(
            LTLProperty("G ( not (in_intersection and highest_priority) )",
                        self._penalty(self._reward_in_goal)))

    def _init_param(self):
        ego = self.env.ego
        self._v_ref = rd.speed_limit if self.env.ego.APs['highest_priority'] else 0
        self._target_lane = ego.APs['lane']
116
        self._ego_stop_count = 0
Aravind Bk's avatar
Aravind Bk committed
117 118 119 120

    def _update_param(self):
        if self.env.ego.APs['highest_priority']:
            self._v_ref = rd.speed_limit
121 122 123
            if self._enable_low_level_training_properties:
                if self.env.n_others_with_higher_priority == 0:
                    self._ego_stop_count += 1
Aravind Bk's avatar
Aravind Bk committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

    def generate_learning_scenario(self):
        n_others = np.random.randint(0, 3)
        self.generate_scenario(enable_LTL_preconditions=True, timeout=62,
                               n_others_range=(n_others, n_others),
                               ego_pos_range=rd.hlanes.stop_region,
                               n_others_stopped_in_stop_region=n_others,
                               ego_v_upper_lim=0, ego_perturb_lim=(rd.hlanes.width / 4, np.pi / 6),
                               ego_heading_towards_lane_centre=True)

        max_waited_count = 0
        for veh in self.env.vehs[1:]:
            max_waited_count = max(max_waited_count, veh.waited_count)

        self._extra_action_weights_flag = False
        self.env.ego.waited_count = np.random.randint(0, max_waited_count + 21)
        self.env.init_APs(False)

        self._reward_in_goal = 200
143 144 145 146 147 148 149 150 151 152 153 154 155
        self._extra_r_on_timeout = -200
        self._enable_low_level_training_properties = True
        self._ego_stop_count = 0

    @property
    def extra_termination_condition(self):
        if self._enable_low_level_training_properties:  # activated only for the low-level training.
            if self._ego_stop_count >= 50:
                self._extra_r_terminal = -200
                return True
            else:
                self._extra_r_terminal = None
                return False
Aravind Bk's avatar
Aravind Bk committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

    @staticmethod
    def _features_dim_reduction(features_tuple):
        return extract_ego_features(features_tuple,
                                    'v', 'v_ref', 'psi', 'theta',
                                    'acc', 'psi_dot', 'pos_stop_region',
                                    'intersection_is_clear', 'highest_priority')


class ChangeLane(ManeuverBase):

    min_y_distance = rd.hlanes.width / 4

    _terminate_in_goal = True
    _reward_in_goal = None

    _violation_penalty_in_low_level_training = None

    high_level_extra_reward = -20

    def _init_param(self):
        self._v_ref = rd.speed_limit
        self._target_lane = not self.env.ego.APs['lane']
        self._terminate_in_goal = True

    def _init_LTL_preconditions(self):
        self._LTL_preconditions.append(
            LTLProperty("G ( on_route and not over_speed_limit )",
                        self._violation_penalty_in_low_level_training,
                        self._enable_low_level_training_properties))
        self._LTL_preconditions.append(LTLProperty("G ( not stopped_now )",
                        self._violation_penalty_in_low_level_training,
                        self._enable_low_level_training_properties))
    @property
    def goal_achieved(self):
        ego = self.env.ego
        APs = self.env.ego.APs
        on_other_lane = APs['lane'] == self._target_lane
        achieved_y_displacement = np.sign(ego.y) * \
                                  (ego.y - rd.hlanes.centres[APs['target_lane']]) >= - self.min_y_distance
        return on_other_lane and APs['on_route'] and \
               achieved_y_displacement and APs['parallel_to_lane']

    def _low_level_manual_policy(self):
        return self.env.aggressive_driving_policy(EGO_INDEX)

    def generate_learning_scenario(self):
        self.generate_scenario(enable_LTL_preconditions=False, timeout=15,
                               ego_pos_range=(rd.hlanes.start_pos, rd.hlanes.end_pos),
                               ego_lane=np.random.choice([0, 1]),
                               ego_perturb_lim=(rd.hlanes.width/5, np.pi/6))
        # print('our range was %s, %s, ego at %s' % (before_intersection, after_intersection, self.env.ego.x))
        self._reward_in_goal = 200
        self._violation_penalty_in_low_level_training = 200
        self._enable_low_level_training_properties = True

    # TODO: It is not a good idea to specify features by numbers, as the list
    # of features is ever changing. We should specify them by strings.
    @staticmethod
    def _features_dim_reduction(features_tuple):
        return extract_ego_features(features_tuple,
                                    'v', 'v_ref', 'e_y', 'psi',
                                    'v tan(psi/L)', 'theta', 'lane',
                                    'acc', 'psi_dot')


class Follow(ManeuverBase):

    _target_veh_i = None
    _penalty_for_out_of_follow_range = None

    def _init_LTL_preconditions(self):
        self._LTL_preconditions.append(
            LTLProperty("G ( veh_ahead )", self._penalty_for_out_of_follow_range))

        self._LTL_preconditions.append(
            LTLProperty("G ( (lane and target_lane) or (not lane and not target_lane) )",
                        self._penalty_for_out_of_follow_range))

        self._LTL_preconditions.append(
            LTLProperty("G ( not stopped_now U veh_ahead_stopped_now)", 200,
                        self._enable_low_level_training_properties))

        self._LTL_preconditions.append(
            LTLProperty("G ( not veh_ahead_too_close )", 200,
                        self._enable_low_level_training_properties))

    def generate_learning_scenario(self):
        self.generate_scenario(enable_LTL_preconditions=False,
                               n_others_range=(1, 1),
                               ego_perturb_lim=(rd.hlanes.width/2, np.pi/4),
                               veh_ahead_scenario=True)
        self.env._terminate_in_goal = False
        self._penalty_for_out_of_follow_range = 200
        self._enable_low_level_training_properties = True

    def _update_param(self):
        self._target_veh_i, _ = self.env.get_V2V_distance()

    def _low_level_manual_policy(self):
        return self.env.aggressive_driving_policy(EGO_INDEX)

    def _features_dim_reduction(self, features_tuple):
        ego_features = extract_ego_features(features_tuple,
                                            'v', 'v_ref', 'e_y', 'psi', 'v tan(psi/L)', 'theta', 'lane', 'e_y,lane',
                                            'acc', 'psi_dot')
        if self._target_veh_i is not None:
            return ego_features + extract_other_veh_features(features_tuple,
                                    self._target_veh_i, 'rel_x', 'rel_y', 'v', 'acc')
        else:
            return ego_features + (0.0, 0.0, 0.0, 0.0)