From ca477c183d94a3bd77c47fe07bcfd2bfa14166b6 Mon Sep 17 00:00:00 2001 From: Xiangzhong Liu Date: Thu, 21 Dec 2023 15:52:52 +0100 Subject: [PATCH] adjust highway and merging scenarios,add support for beyondpoint labelfunction --- .../blueprints/highway/highway.py | 27 ++++++++++--------- .../blueprints/merging/merging.py | 12 ++++----- bark_ml/evaluators/evaluator_configs.py | 5 +++- bark_ml/evaluators/general_evaluator.py | 2 ++ 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/bark_ml/environments/blueprints/highway/highway.py b/bark_ml/environments/blueprints/highway/highway.py index 4ce9760a..aa2a0b55 100644 --- a/bark_ml/environments/blueprints/highway/highway.py +++ b/bark_ml/environments/blueprints/highway/highway.py @@ -8,14 +8,16 @@ import os import numpy as np -from bark.runtime.viewer.buffered_mp_viewer import BufferedMPViewer +# from bark.runtime.viewer.buffered_mp_viewer import BufferedMPViewer +from bark.runtime.viewer.matplotlib_viewer import MPViewer +from bark.runtime.viewer.video_renderer import VideoRenderer from bark.runtime.scenario.scenario_generation.config_with_ease import \ LaneCorridorConfig, ConfigWithEase from bark.core.world.goal_definition import GoalDefinitionPolygon from bark.core.geometry import Polygon2d, Point2d from bark_ml.environments.blueprints.blueprint import Blueprint -from bark_ml.evaluators.evaluator_configs import RewardShapingEvaluator +from bark_ml.evaluators.general_evaluator import GeneralEvaluator from bark_ml.behaviors.cont_behavior import BehaviorContinuousML from bark_ml.behaviors.discrete_behavior import BehaviorDiscreteMacroActionsML from bark_ml.core.observers import NearestObserver @@ -64,7 +66,7 @@ def __init__(self, ego_lane_id = 2 lane_configs = [] - for i in range(0, 4): + for i in range(1, 4): is_controlled = True if (ego_lane_id == i) else None s_min = 0 s_max = 250 @@ -92,18 +94,19 @@ def __init__(self, random_seed=random_seed, params=params, lane_corridor_configs=lane_configs) + + dt = 0.2 if viewer: - # viewer = MPViewer(params=params, - # use_world_bounds=True) - viewer = BufferedMPViewer( - params=params, - x_range=[-55, 55], - y_range=[-55, 55], - follow_agent_id=True) - dt = 0.2 + viewer = MPViewer(params=params, + x_range=[-150, 150], + y_range=[-150, 150], + follow_agent_id=True) + if params["Experiment"]["ExportVideos"]: + viewer = VideoRenderer(renderer=viewer, world_step_time=dt) + params["ML"]["RewardShapingEvaluator"]["PotentialVelocityFunctor"][ "DesiredVel", "Desired velocity for the ego agent.", 20] - evaluator = RewardShapingEvaluator(params) + evaluator = GeneralEvaluator(params) observer = NearestObserver(params) ml_behavior = ml_behavior diff --git a/bark_ml/environments/blueprints/merging/merging.py b/bark_ml/environments/blueprints/merging/merging.py index f5d5a68c..59754770 100644 --- a/bark_ml/environments/blueprints/merging/merging.py +++ b/bark_ml/environments/blueprints/merging/merging.py @@ -7,7 +7,8 @@ # https://opensource.org/licenses/MIT import os -from bark.runtime.viewer.buffered_mp_viewer import BufferedMPViewer +# from bark.runtime.viewer.buffered_mp_viewer import BufferedMPViewer +from bark.runtime.viewer.matplotlib_viewer import MPViewer from bark.runtime.scenario.scenario_generation.config_with_ease import \ LaneCorridorConfig, ConfigWithEase from bark.core.world.opendrive import XodrDrivingDirection @@ -96,11 +97,10 @@ def __init__(self, params=params, lane_corridor_configs=[left_lane, right_lane]) if viewer: - viewer = BufferedMPViewer( - params=params, - x_range=[-25, 25], - y_range=[-25, 25], - follow_agent_id=True) + viewer = MPViewer(params=params, + x_range=[-150, 150], + y_range=[-150, 150], + follow_agent_id=True) dt = 0.2 params["ML"]["RewardShapingEvaluator"]["PotentialVelocityFunctor"][ "DesiredVel", "Desired velocity for the ego agent.", 20] diff --git a/bark_ml/evaluators/evaluator_configs.py b/bark_ml/evaluators/evaluator_configs.py index cca0c802..1bd4282c 100644 --- a/bark_ml/evaluators/evaluator_configs.py +++ b/bark_ml/evaluators/evaluator_configs.py @@ -6,7 +6,7 @@ from bark.core.world.evaluation import \ EvaluatorGoalReached, EvaluatorCollisionEgoAgent, \ EvaluatorStepCount, EvaluatorDrivableArea - +from bark.core.geometry import Point2d from bark_ml.evaluators.general_evaluator import * class GoalReached(GeneralEvaluator): @@ -177,6 +177,9 @@ def __init__(self, params): labels_list = [] for label_conf in rule_config["RuleConfig"]["labels"]: label_params_dict = label_conf["params"].ConvertToDict() + if label_conf["type"] == "EgoBeyondPointLabelFunction" or label_conf["type"] == "AgentBeyondPointLabelFunction": + merge_point = label_params_dict["point"] + label_params_dict["point"] = Point2d(merge_point[0],merge_point[1]) labels_list.append(eval("{}(*(label_params_dict.values()))".format(label_conf["type"]))) print("labels_list:",labels_list) diff --git a/bark_ml/evaluators/general_evaluator.py b/bark_ml/evaluators/general_evaluator.py index fdfe2a20..c944f7da 100644 --- a/bark_ml/evaluators/general_evaluator.py +++ b/bark_ml/evaluators/general_evaluator.py @@ -238,12 +238,14 @@ def __call__(self, observed_world, action, eval_results): prev_state, cur_state = self.GetPrevAndCurState(observed_world) prev_dist = self.DistanceToPolygon(observed_world, prev_state) cur_dist = self.DistanceToPolygon(observed_world, cur_state) + # print("!!!!!!!!!!!!!current distance to goal is ", cur_dist) prev_pot = self.DistancePotential( prev_dist, self._params["MaxDist", "", 100.], self._params["DistExponent", "", 0.2]) cur_pot = self.DistancePotential( cur_dist, self._params["MaxDist", "", 100.], self._params["DistExponent", "", 0.2]) + print("!!!!!!!!!!!!!current potential is ", 0.99*cur_pot - prev_pot) return False, self.WeightedReward(self._params["Gamma", "", 0.99]*cur_pot - prev_pot), {} return False, 0, {}