-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulationDriver.m
More file actions
254 lines (216 loc) · 7.79 KB
/
SimulationDriver.m
File metadata and controls
254 lines (216 loc) · 7.79 KB
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
27
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
% Script Name : SimulationDriver.m
% Version : Managed by SVN online
% Date : 2011/11/27
% Author : John McAninley
% Description : THis script is the top level script for the HSTC
% simulation
%
%
%
% Ver Date Author Description
% Initialization
close all %close all previously opended figures
clc
clear classes
global CaravanControllerSetup
global SimulationSetup
global StatisticsSetup
global guiHandle
cc = CaravanController.getInstance();
vm = VehicleMgr.getInstance(5);
% Initialize variables
turnNumber = 1;
selection = 0;
simulationOver = false;
startSimulation = false;
%load default simulation setups
CaravanControllerSetup.VehicleSpacing = 3;
CaravanControllerSetup.MaxCaravanSize = 33;
CaravanControllerSetup.MaxCaravanSpeed = 133;
CaravanControllerSetup.MinCaravanDistance = 10;
CaravanControllerSetup.MaxCaravanDistance = 100;
SimulationSetup.TrafficDensityModel = 'normal';
SimulationSetup.SimulationRunLength = 180;
SimulationSetup.SimulationRunUnits = 'forever';
SimulationSetup.SimTimeStep = 2;
SimulationSetup.SlowLoop = 0;
SimulationSetup.focusId = 0;
SimulationSetup.Pause = false;
SimulationSetup.End = false;
SimulationSetup.ShowGUI = true;
SimulationSetup.TimeElapsed = 0;
StatisticsSetup.DisplayKpp1 = false;
StatisticsSetup.DisplayKpp2 = false;
StatisticsSetup.DisplayKpp5 = false;
SimParams.genTraffic = false;
SimParams.numLanes = 5;
SimParams.trafficRate = 5;
while ~startSimulation
selection = menu('Main Menu',...
'Run KPP1 Scenario',...
'Run KPP2 Scenario',...
'Run KPP 3 & 4 Scenario',...
'Run KPP5 Scenario',...
'Run Interactive Simulation',...
'Run Batched Simulations',...
'Edit Simulation Parameters',...
'Exit');
switch selection
case 1
StatisticsSetup.DisplayKpp1 = true;
Kpp1_Generate;
SimulationSetup.SlowLoop = 0;
startSimulation = true;
SimulationSetup.focusId = 1;
case 2
StatisticsSetup.DisplayKpp2 = true;
Kpp2_Generate;
SimulationSetup.SlowLoop = 1;
SimulationSetup.SimTimeStep = 0.100;
startSimulation = true;
SimulationSetup.focusId = 1;
case 3
KPP4();
startSimulation = true;
SimulationSetup.ShowGUI = false;
case 4
StatisticsSetup.DisplayKpp5 = true;
Kpp5_Generate;
SimulationSetup.SlowLoop = 1;
SimulationSetup.SimTimeStep = 0.100;
startSimulation = true;
SimulationSetup.focusId = 4;
SimulationSetup.ShowGUI = true;
case 5
startSimulation = true;
SimParams.genTraffic = true;
SimulationSetup.SimulationRunUnits = 'Seconds';
case 6
startSimulation = true;
SimulationSetup.ShowGUI = false;
SimParams.genTraffic = true;
SimulationSetup.SlowLoop = 1;
SimulationSetup.SimulationRunUnits = 'Seconds';
case 7
% EditSimulationParameters;
prompt= {'Number of lanes:' ...
'Traffic Arrival Rate (vehicles/hr/lane):' ...
'Simulation Run Length (s):' ...
'Simulation Time Step (s):'};
defaultanswer={num2str(SimParams.numLanes),...
num2str(SimParams.trafficRate),...
num2str(SimulationSetup.SimulationRunLength),...
num2str(SimulationSetup.SimTimeStep)};
answer = inputdlg(prompt, 'Edit Simulation Parameters',1,...
defaultanswer);
SimParams.numLanes = str2num(answer{1});
SimParams.trafficRate = str2num(answer{2});
SimulationSetup.SimulationRunLength = str2num(answer{3});
SimulationSetup.SimTimeStep = str2num(answer{4});
SimParams.genTraffic = true;
vm = VehicleMgr.getInstance(SimParams.numLanes);
case 8
close all
return
end
end
close all;
%do some setups
if SimParams.genTraffic
tg = TrafficGen;
tg = InitTraffic (tg, SimParams.numLanes, SimParams.trafficRate, ...
SimulationSetup.SimulationRunLength, SimulationSetup.SimTimeStep, true);
end
simulationOver = false;
% Note - if the user selected Play game from the GUI Menu,
% the flow of the program will fall through to here. Once
% in this loop, we will stay in this loop until the user chooses to
% exit the game
%capture start time
startTime = clock;
t = 0;
tinc = SimulationSetup.SimTimeStep; %seconds
if SimulationSetup.ShowGUI
guiHandle = GUI;
setappdata(guiHandle,'vm',vm);
GUI('updateGUI');
gh=guihandles(guiHandle);
if SimulationSetup.focusId > 0
set(gh.focusCheckbox, 'Value', 1);
set(gh.focusSelector, 'Value', SimulationSetup.focusId);
end
end
isStartedKpp5 = false;
while ~simulationOver
tic
if SimulationSetup.End == true
break;
end
if SimulationSetup.Pause == true
pause(1);
if SimulatioNSetup.ShowGUI
GUI('updateGUI');
end
continue;
end
if StatisticsSetup.DisplayKpp5
if isStartedKpp5 == false
isStartedKpp5 = true;
fprintf('Kpp5 Start = %f\n', SimulationSetup.TimeElapsed);
end
end
if SimParams.genTraffic
[tg, newVehicles] = TimeStep(tg);
vm = AddVehicles(vm, newVehicles);
end
vm.TimeStep(tinc);
cc.Update();
if StatisticsSetup.DisplayKpp5
numStopped = length(find([vm.currentVehicles.velocity] <= 0.5));
if( numStopped == 30)
fprintf('Kpp5 Stop = %f\n', SimulationSetup.TimeElapsed);
SimulationSetup.End = true;
end
end
if sum([vm.currentVehicles.gapMode]) > 0
tinc=tinc;
% SimulationSetup.SlowLoop = 1;
else
% tinc=SimulationSetup.SimTimeStep;
end
% % fprintf('%d, ',sum([vm.currentVehicles.gapMode]) );
% % for i= 1:10
% % fprintf('%f, ', vm.currentVehicles(i).posY);
% % end
% % fprintf('\n');
%check for exit conditions
if strcmp(SimulationSetup.SimulationRunUnits, 'Seconds')
if etime(clock, startTime) > SimulationSetup.SimulationRunLength
disp('Time Expired...');
simulationOver = true;
end
else
end
turnNumber = turnNumber+ 1;
t = t + tinc;
%maintain global time elapsed
SimulationSetup.TimeElapsed = t;
if SimulationSetup.ShowGUI
setappdata(guiHandle,'vm',vm);
GUI('updateGUI');
end
elapsedTime=toc;
if SimulationSetup.SlowLoop
pause(tinc-elapsedTime);
end
end
vehicles = vm.currentVehicles;
numInCaravan = sum([vehicles.caravanNumber] > 0)
numNotInCaravan = sum([vehicles.caravanNumber] <= 0)
caravanAvgMpg = mean(nonzeros(([vehicles.caravanNumber] > 0) .* [vehicles.avgMPG]))
nonCaravanAvgMPG = mean(nonzeros(([vehicles.caravanNumber] <= 0) .* [vehicles.avgMPG]))
% caravanDist = sum(nonzeros(([vehicles.caravanNumber] > 0) .* [vehicles.distanceTraveled]))
% nonCaravanDist = sum(nonzeros(([vehicles.caravanNumber] <= 0) .* [vehicles.distanceTraveled]))
% caravanTime = sum(nonzeros(([vehicles.caravanNumber] > 0) .* [vehicles.driveTime]))
% caravanTime = sum(nonzeros(([vehicles.caravanNumber] <= 0) .* [vehicles.driveTime]))
disp('End of simulation');