Skip to content

Commit 82c5ecd

Browse files
author
sherryz
committed
init commit
0 parents  commit 82c5ecd

File tree

59 files changed

+1922
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1922
-0
lines changed

DrivingScenarioEnv.m

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
classdef DrivingScenarioEnv < rl.env.MATLABEnvironment
2+
% Copyright 2020 The MathWorks, Inc.
3+
%MYENVIRONMENT: Template for defining custom environment in MATLAB.
4+
5+
% parameters for simulation environment
6+
properties
7+
scenario
8+
network
9+
traffic
10+
cars
11+
state
12+
driver
13+
InjectionRate
14+
TurnRatio
15+
N = 3 % number of road
16+
phaseDuration = 50 % time duration for each of the phase
17+
T
18+
end
19+
20+
% simulation doesn't have yellow light
21+
% manually set up clearning phase here if needed
22+
properties
23+
clearingPhase = false
24+
clearingPhaseTime = 0
25+
TrafficSignalDesign
26+
ObservationSpaceDesign
27+
end
28+
29+
% parameter for reward definition
30+
properties
31+
rewardForPass = 0
32+
vehicleEnterJunction % keep record of cars pass the intersection
33+
hitPenalty = 20
34+
penaltyForFreqSwitch = 1
35+
safeDistance = 2.25 % check collision
36+
slowSpeedThreshold = 3.5 % check whether car is waiting
37+
end
38+
39+
properties
40+
recordVid = false
41+
vid
42+
end
43+
44+
properties
45+
discrete_action = [0 1 2];
46+
dim =10;
47+
end
48+
49+
properties(Access = protected)
50+
IsDone = false
51+
end
52+
53+
%% Necessary Methods
54+
methods
55+
function this = DrivingScenarioEnv()
56+
% Initialize Observation settings
57+
ObservationInfo = rlNumericSpec([10, 1]); % # of state
58+
ObservationInfo.Name = 'real-time traffic information';
59+
ObservationInfo.Description = '';
60+
61+
% Initialize action settings
62+
ActionInfo = rlFiniteSetSpec([0 1 2]); % three phases
63+
ActionInfo.Name = 'traffic signal phases';
64+
65+
% The following line implements built-in functions of the RL environment
66+
this = this@rl.env.MATLABEnvironment(ObservationInfo,ActionInfo);
67+
end
68+
69+
function [state, Reward,IsDone,LoggedSignals] = step(this, Action)
70+
Action = getForce(this, Action);
71+
% update the action
72+
pre_phase = this.traffic.IsOpen;
73+
if this.TrafficSignalDesign == 1
74+
cur_phase = signalPhaseDesign1(Action);
75+
elseif this.TrafficSignalDesign == 2
76+
cur_phase = signalPhaseDesign2(Action);
77+
elseif this.TrafficSignalDesign == 3
78+
cur_phase = signalPhaseDesign3(Action);
79+
end
80+
81+
% Reward: penalty for signal phase switch
82+
changed = ~isequal(pre_phase, cur_phase);
83+
Reward = this.penaltyForFreqSwitch * (1 - changed);
84+
85+
% (yellow light time)add clearing phase when signal phase switch
86+
if changed && this.clearingPhase
87+
for i = 1:this.clearingPhaseTime
88+
this.traffic.IsOpen = [0, 0, 0, 0, 0, 0];
89+
advance(this.scenario);
90+
this.T = this.T + this.scenario.SampleTime;
91+
notifyEnvUpdated(this);
92+
% check terminal condition
93+
IsHit = checkCollision(this);
94+
Reward = Reward - IsHit * this.hitPenalty;
95+
this.IsDone = IsHit || this.T+0.5 >= this.scenario.StopTime;
96+
if this.IsDone
97+
break
98+
end
99+
end
100+
end
101+
102+
% (green light time)simulate the signal phase based on the action by RL
103+
this.traffic.IsOpen = cur_phase;
104+
if ~this.IsDone
105+
for i = 1:this.phaseDuration
106+
% update traffic state
107+
advance(this.scenario);
108+
this.T = this.T + this.scenario.SampleTime;
109+
% update visulization
110+
notifyEnvUpdated(this);
111+
% check terminal condition
112+
IsHit = checkCollision(this);
113+
Reward = Reward - IsHit * this.hitPenalty;
114+
this.IsDone = IsHit || this.T+0.5 >= this.scenario.StopTime;
115+
if this.IsDone
116+
break
117+
end
118+
% obtain reward
119+
Reward = Reward + obtainReward(this, cur_phase);
120+
end
121+
end
122+
if this.ObservationSpaceDesign == 1
123+
state = observationSpace1(this, Action);
124+
else
125+
state = observationSpace2(this, Action);
126+
end
127+
this.state = state;
128+
IsDone = this.IsDone;
129+
LoggedSignals = [];
130+
end
131+
132+
133+
function InitialState = reset(this)
134+
% flag for record simulation
135+
this.recordVid = false;
136+
% Initialize scenario
137+
this.scenario = createTJunctionScenario();
138+
this.scenario.StopTime = 100;
139+
this.scenario.SampleTime = 0.05;
140+
this.T = 0;
141+
% initialize network
142+
this.network = createTJunctionNetwork(this.scenario);
143+
this.traffic = trafficControl.TrafficController(this.network(7:12));
144+
% car parameters
145+
this.InjectionRate = [250, 250, 250]; % veh/hour
146+
this.TurnRatio = [50, 50];
147+
this.cars = createVehiclesForTJunction(this.scenario, this.network, this.InjectionRate, this.TurnRatio);
148+
this.vehicleEnterJunction = [];
149+
% obtain state from traffic and network
150+
if this.ObservationSpaceDesign == 1
151+
InitialState = observationSpace1(this, 0);
152+
else
153+
InitialState = observationSpace2(this, 0);
154+
end
155+
% visulization
156+
notifyEnvUpdated(this);
157+
end
158+
end
159+
160+
methods
161+
function force = getForce(this,action)
162+
if ~ismember(action,this.ActionInfo.Elements)
163+
error('Action must be integer from 1 to numAction');
164+
end
165+
force = action;
166+
end
167+
% update the action info based on max force
168+
function updateActionInfo(this)
169+
this.ActionInfo.Elements = this.discrete_action;
170+
end
171+
end
172+
173+
methods (Access = protected)
174+
function envUpdatedCallback(this)
175+
if this.T == 0
176+
close all;
177+
plot(this.scenario)
178+
set(gcf,'Visible','On');
179+
if this.recordVid
180+
this.vid = VideoWriter('baseRLlearningProcess33');
181+
this.vid.FrameRate=20;
182+
open(this.vid)
183+
end
184+
end
185+
if this.recordVid
186+
frame = getframe(gcf);
187+
writeVideo(this.vid,frame);
188+
end
189+
this.traffic.plotOpenPaths()
190+
drawnow
191+
end
192+
end
193+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function [acc,v_b,v_a] = gippsDriverModel(spacing,speed,speedDiff,varargin)
2+
3+
desiredSpeed = 10; %Desired speed
4+
maxSpeed = 20; %max. speed
5+
minSpeed = 0; % min. speed
6+
minAcc = -3; %min. acceleration
7+
maxAcc = 3; %max. acceleration
8+
minAccEstimate = minAcc;
9+
reactionTime = 0.8;
10+
S0 = 2;
11+
12+
v_now = speed;
13+
v_infront = speed+speedDiff;
14+
15+
v_a = v_now + 2.5*maxAcc*reactionTime*(1-v_now/desiredSpeed)*sqrt(0.025+v_now/desiredSpeed);
16+
v_b = minAcc*reactionTime + sqrt((minAcc*reactionTime)^2-minAcc*(2*(spacing-S0)-v_now*reactionTime-v_infront^2/minAccEstimate));
17+
18+
v_b = max(v_b,minSpeed);
19+
v_a = min(v_a,maxSpeed);
20+
v_new = min([v_a,v_b]);
21+
22+
acc = (v_new-v_now)/reactionTime;
23+
24+
end
25+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function [acc] = intelligentDriverModel(spacing,speed,speedDiff,varargin)
2+
% Parameters
3+
% NAME REALISTIC BOUNDS DEFAULT UNITS
4+
% desiredSpeed [0,11] 33 m/s
5+
% safeHeadway [1,3] 1.6 s
6+
% accelMax [0.5,2] 0.73 m/s^2
7+
% decelConf [0.5,2] 1.67 m/s^2
8+
% beta 4
9+
% minJamSpacing [0,5] 2 m
10+
% nonLinJamSpacing [0,5] 3 m
11+
12+
% Need to program input parse to pass new parameters
13+
desiredSpeed = 10;
14+
safeHeadway = 1.6;
15+
accelMax = 0.73;
16+
decelConf = 1.67;
17+
beta = 4;
18+
minJamSpacing = 2;
19+
nonLinJamSpacing = 0;
20+
21+
desiredSpacing = minJamSpacing + nonLinJamSpacing*sqrt(speed/desiredSpeed)...
22+
+speed*safeHeadway-speed*speedDiff/2/sqrt(accelMax*decelConf);
23+
24+
acc = accelMax*(1-(speed/desiredSpeed)^beta-(desiredSpacing/spacing)^2);
25+
26+
if acc<-10 || acc>3 || isnan(acc)
27+
acc;
28+
end
29+
end
30+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
classdef TrafficController < driving.scenario.MotionStrategy...
2+
& driving.scenario.mixin.PropertiesInitializableInConstructor
3+
4+
5+
properties
6+
Scenario
7+
Nodes = Node.empty % List of nodes the traffic controller manages
8+
IsOpen % Boolean list indicating wether the node can be entered
9+
PlotHandles = plot3([],[],[]);
10+
end
11+
12+
methods
13+
function obj = TrafficController(nodes,varargin)
14+
15+
obj@driving.scenario.MotionStrategy(nodes(1).Scenario.actor);
16+
obj@driving.scenario.mixin.PropertiesInitializableInConstructor(varargin{:});
17+
18+
obj.EgoActor.MotionStrategy = obj;
19+
obj.EgoActor.IsVisible = false;
20+
%obj.EgoActor.Position = nodes(1).getRoadCenterFromStation(10);
21+
22+
obj.Scenario = nodes(1).Scenario;
23+
obj.Nodes = nodes;
24+
obj.IsOpen = false(size(nodes));
25+
26+
end
27+
28+
function set.Nodes(obj,nodes)
29+
for node = nodes
30+
if ~any(obj.Nodes==node)
31+
obj.Nodes(end+1)=node;
32+
node.TrafficController=obj;
33+
end
34+
end
35+
end
36+
37+
function running = move(obj,SimulationTime)
38+
39+
running = true;
40+
end
41+
42+
function running = restart(obj,inputArg)
43+
%METHOD1 Summary of this method goes here
44+
% Detailed explanation goes here
45+
outputArg = obj.Property1 + inputArg;
46+
end
47+
end
48+
49+
methods
50+
function state = getNodeState(obj,node)
51+
state = obj.IsOpen(obj.Nodes==node);
52+
end
53+
end
54+
55+
methods % plot methods
56+
function plotOpenPaths(obj,ax)
57+
green = [0.4660 0.6740 0.1880];
58+
red = [0.6350 0.0780 0.1840];
59+
yellow = [0.9290 0.6940 0.1250];
60+
if nargin<2
61+
ax=gca;
62+
end
63+
hold on
64+
if isempty(obj.PlotHandles)
65+
for node=obj.Nodes
66+
obj.PlotHandles(end+1) = plot3(ax,node.Mapping(:,2),node.Mapping(:,3),node.Mapping(:,4)+10);
67+
end
68+
end
69+
for idx = 1:length(obj.Nodes)
70+
p = obj.PlotHandles(idx);
71+
node = obj.Nodes(idx);
72+
p.XData = node.Mapping(:,2);
73+
p.YData = node.Mapping(:,3);
74+
p.ZData = node.Mapping(:,4)+0;
75+
76+
p.LineWidth = 2;
77+
if obj.IsOpen(idx)==true
78+
p.Color = [green,1];
79+
else
80+
p.Color = [red,0.2];
81+
end
82+
end
83+
end
84+
end
85+
end
86+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
classdef TrafficLight < trafficControl.TrafficController
2+
3+
properties
4+
Cliques
5+
Cycle
6+
Phase
7+
end
8+
9+
methods
10+
function obj = TrafficLight(nodes,varargin)
11+
obj@trafficControl.TrafficController(nodes,varargin{:});
12+
end
13+
14+
function running = move(obj,SimulationTime)
15+
obj.IsOpen = false(size(obj.Nodes));
16+
t = mod(SimulationTime,obj.Cycle(end));
17+
phase = discretize(t,obj.Cycle);
18+
numPhases = max(obj.Cliques);
19+
for i =1:numPhases
20+
if phase==i
21+
obj.IsOpen(obj.Cliques==i)=true;
22+
end
23+
end
24+
running = true;
25+
end
26+
end
27+
end
28+

0 commit comments

Comments
 (0)