Commit 54079509 by Jaeyoung Lee

Merge branch 'improve_Wait_and_highlevel' into 'master'

Improve wait and highlevel See merge request !7
parents 8c6ddf97 599d89f9
......@@ -275,9 +275,6 @@ class DQNLearner(LearnerBase):
model.add(Flatten(input_shape=(1, ) + self.input_shape))
model.add(Dense(64, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(64, activation='tanh'))
model.add(Dense(self.nb_actions))
print(model.summary())
......@@ -386,9 +383,10 @@ class DQNLearner(LearnerBase):
termination_reason_counter[termination_reason] += 1
else:
termination_reason_counter[termination_reason] = 1
env.reset()
if episode_reward >= success_reward_threshold:
#TODO: remove below env-specific code
if env.env.goal_achieved:
success_count += 1
env.reset()
print("Episode {}: steps:{}, reward:{}".format(
n + 1, step, episode_reward))
......
from model_checker import Parser
from verifier import Parser
class GymCompliantEnvBase:
......
......@@ -10,7 +10,7 @@ from .utilities import BoundingBox as bb_utils
from .features import Features
from .constants import *
from model_checker.simple_intersection import LTLProperty
from verifier.simple_intersection import LTLProperty
# import the classes for graphical output.
from .shapes import Text, Image
......
from . import road_geokinemetry as rd
import numpy as np
from model_checker.simple_intersection import AtomicPropositions
from verifier.simple_intersection import AtomicPropositions
from .utilities import BoundingBox as bb_utils
from .constants import *
......
......@@ -2,12 +2,19 @@ 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
from verifier.simple_intersection import LTLProperty
import numpy as np
# TODO: separate out into different files?? is it really needed?
"""
enable_additional_properties=
True means the maneuver uses the additional properties,
False means not.
"""
enable_additional_properties = False
class KeepLane(ManeuverBase):
def _init_param(self):
......@@ -35,7 +42,8 @@ class KeepLane(ManeuverBase):
# the goal reward and termination is led by the SimpleIntersectionEnv
self.env._terminate_in_goal = False
self.env._reward_in_goal = None
self._enable_low_level_training_properties = True
global enable_additional_properties
self._enable_low_level_training_properties = enable_additional_properties
self._extra_action_weights_flag = True
def generate_validation_scenario(self):
......@@ -45,8 +53,9 @@ class KeepLane(ManeuverBase):
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 = False
self.env._reward_in_goal = None
self.env._terminate_in_goal = True
self.env._reward_in_goal = 200
self._enable_low_level_training_properties = False
@staticmethod
def _features_dim_reduction(features_tuple):
......@@ -91,7 +100,8 @@ class Halt(ManeuverBase):
self._target_lane = self.env.ego.APs['lane']
def _init_LTL_preconditions(self):
# TODO: give another variable in ManeuverBase that enables high-level-only properties
# (currently, this functionality is implemented by "not self._enable_low_level_training_properties")
self._LTL_preconditions.append(
LTLProperty(
"G ( (veh_ahead and before_but_close_to_stop_region) U highest_priority )",
......@@ -114,11 +124,14 @@ class Halt(ManeuverBase):
self.env._terminate_in_goal = False
self.env._reward_in_goal = None
self._reward_in_goal = 200
self._enable_low_level_training_properties = True
global enable_additional_properties
self._enable_low_level_training_properties = enable_additional_properties
self._extra_action_weights_flag = True
def generate_validation_scenario(self):
self._ego_pos_range = (rd.hlanes.start_pos, rd.hlanes.end_pos)
# TODO: Separate out the high-level-only properties and add a code
# "self._enable_low_level_training_properties = False" (just like the other maneuvers).
self.generate_learning_scenario()
def _low_level_manual_policy(self):
......@@ -172,7 +185,7 @@ class Stop(ManeuverBase):
self._penalty_in_violation))
self._LTL_preconditions.append(
LTLProperty("G ( not stopped_now U in_stop_region )", 100,
LTLProperty("G ( not stopped_now U in_stop_region )", 200,
self._enable_low_level_training_properties))
self._LTL_preconditions.append(
......@@ -201,7 +214,8 @@ class Stop(ManeuverBase):
ego_heading_towards_lane_centre=True)
self._reward_in_goal = 200
self._penalty_in_violation = 150
self._enable_low_level_training_properties = True
global enable_additional_properties
self._enable_low_level_training_properties = enable_additional_properties
self._extra_action_weights_flag = True
def _low_level_manual_policy(self):
......@@ -228,7 +242,7 @@ class Stop(ManeuverBase):
elif (rd.speed_limit / 5 < self._v_ref) and \
(self.env.ego.v < self._v_ref / 2) and self.env.ego.acc < 0:
self._extra_r_terminal = -100
self._extra_r_terminal = -200
return True
else:
......@@ -241,13 +255,13 @@ class Stop(ManeuverBase):
class Wait(ManeuverBase):
_reward_in_goal = None
_terminate_in_goal = True
_terminate_in_goal = False
def _init_LTL_preconditions(self):
self._LTL_preconditions.append(
LTLProperty("G ( (in_stop_region and stopped_now) U (highest_priority and intersection_is_clear))",
None, not self._enable_low_level_training_properties)) # not available in low-level training...
LTLProperty(
"G ( (in_stop_region and has_stopped_in_stop_region) U (highest_priority and intersection_is_clear))",
None, not self._enable_low_level_training_properties)) # not available in low-level training...
self._LTL_preconditions.append(
LTLProperty("G ( not (in_intersection and highest_priority and intersection_is_clear) )",
......@@ -255,13 +269,13 @@ class Wait(ManeuverBase):
self._LTL_preconditions.append(
LTLProperty(
"G ( in_stop_region U (highest_priority and intersection_is_clear) )", 150,
"G ( in_stop_region U (highest_priority and intersection_is_clear) )", 200,
self._enable_low_level_training_properties))
self._LTL_preconditions.append(
LTLProperty(
"G ( (lane and target_lane) or (not lane and not target_lane) )",
150, self._enable_low_level_training_properties))
#self._LTL_preconditions.append(
# LTLProperty(
# "G ( (lane and target_lane) or (not lane and not target_lane) )",
# 150, self._enable_low_level_training_properties))
def _init_param(self):
self._update_param()
......@@ -270,8 +284,10 @@ class Wait(ManeuverBase):
def _update_param(self):
if self.env.ego.APs['highest_priority'] and self.env.ego.APs['intersection_is_clear']:
self._v_ref = rd.speed_limit
self._extra_action_weights_flag = True
else:
self._v_ref = 0
self._extra_action_weights_flag = False
def generate_learning_scenario(self):
n_others = 0 if np.random.rand() <= 0 else np.random.randint(1, 4)
......@@ -298,17 +314,21 @@ class Wait(ManeuverBase):
self.env.ego.waited_count = np.random.randint(min_waited_count, max_waited_count + 21)
self.env.init_APs(False)
self.env._terminate_in_goal = False
self._reward_in_goal = 200
self._enable_low_level_training_properties = True
self._extra_action_weights_flag = True
global enable_additional_properties
self._enable_low_level_training_properties = enable_additional_properties
self._extra_action_weights_flag = False
@property
def extra_termination_condition(self):
if self._enable_low_level_training_properties: # activated only for the low-level training.
if self.env.ego.APs['highest_priority'] and self.env.ego.APs['intersection_is_clear'] \
and np.random.rand() <= 0.1 and self.env.ego.v <= self._v_ref / 10 \
and self.env.ego.acc < 0:
self._extra_r_terminal = - 100
if self.env.ego.APs['highest_priority'] and \
self.env.ego.APs['intersection_is_clear'] and \
np.random.rand() <= 0.25 and \
self.env.ego.v <= self._v_ref / 10 and \
self.env.ego.acc < 0:
self._extra_r_terminal = -200
return True
else:
self._extra_r_terminal = None
......@@ -447,19 +467,23 @@ class ChangeLane(ManeuverBase):
# 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 = 150
self._enable_low_level_training_properties = True
global enable_additional_properties
self._enable_low_level_training_properties = enable_additional_properties
self._extra_action_weights_flag = True
self.env._terminate_in_goal = False
def generate_validation_scenario(self):
self.generate_scenario(
enable_LTL_preconditions=False,
ego_pos_range=(rd.hlanes.start_pos, rd.hlanes.end_pos),
ego_pos_range=(rd.intersection_width_w_offset, rd.hlanes.end_pos),
ego_perturb_lim=(rd.hlanes.width / 4, 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 = 150
self._enable_low_level_training_properties = True
self._enable_low_level_training_properties = False
self.env._terminate_in_goal = False
@staticmethod
def _features_dim_reduction(features_tuple):
......@@ -508,12 +532,10 @@ class Follow(ManeuverBase):
self.env._terminate_in_goal = False
self._penalty_for_out_of_range = 200
self._penalty_for_change_lane = 200
self._enable_low_level_training_properties = True
global enable_additional_properties
self._enable_low_level_training_properties = enable_additional_properties
self._extra_action_weights_flag = True
def generate_validation_scenario(self):
self.generate_learning_scenario()
def _init_param(self):
self._set_v_ref()
......
from model_checker.scanner import Scanner
from model_checker.parser import Parser, Errors
from verifier.scanner import Scanner
from verifier.parser import Parser, Errors
class LTLPropertyBase(object):
......
......@@ -29,10 +29,10 @@
import sys
# Sean changed Scanner to model_checker.scanner
from model_checker.scanner import Token
from model_checker.scanner import Scanner
from model_checker.scanner import Position
# Sean changed Scanner to verifier.scanner
from verifier.scanner import Token
from verifier.scanner import Scanner
from verifier.scanner import Position
class ErrorRec(object):
......
from model_checker.simple_intersection import LTLProperty
from model_checker.simple_intersection.AP_dict import AP_dict_simple_intersection
from model_checker import Parser
from verifier.simple_intersection import LTLProperty
from verifier.simple_intersection.AP_dict import AP_dict_simple_intersection
from verifier import Parser
# TODO: Refactor LTL_test to model_checker part.
# TODO: Refactor LTL_test to verifier part.
def test_model_check(LTL, trace):
......
from model_checker import LTLPropertyBase, AtomicPropositionsBase
from verifier import LTLPropertyBase, AtomicPropositionsBase
from .AP_dict import AP_dict_simple_intersection
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment