-
Notifications
You must be signed in to change notification settings - Fork 4
/
RoadAvgFifoCTM.m
125 lines (109 loc) · 4.77 KB
/
RoadAvgFifoCTM.m
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
% This file is part of Traffic MacroSimulator.
%
% Traffic MacroSimulator is free software: you can redistribute it and/or
% modify it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% You should have received a copy of the GNU General Public License
% along with Traffic MacroSimulator. If not, see http://www.gnu.org/licenses/.
% @author: P. Grandinetti
% This class is a modelization of a traffic road system with averaged values
% traffic lights and dynamic model similar to the CTM
% See "Control of large scale traffic networks" by the author, Chapter 3
% Available at https://www.dropbox.com/s/9m2ldonki5x6lfq/ControlLargeScaleTraffic.pdf
classdef RoadAvgFifoCTM < handle
properties (SetAccess = public)
myNet;
L;
maxSpeed; %v
congSpeed; %w
currentDensity;
id;
maxDensity;
maxFlow;
criticalDensity;
computedOutFlow; %-1 when the outflow has not been computed yes by this road
end
methods
function R = RoadAvgFifoCTM (length, maxSpeed, congSpeed, currentDensity, maxDensity, maxFlow, id)
R.L = length;
R.maxSpeed = maxSpeed;
R.congSpeed = congSpeed;
R.currentDensity = currentDensity;
R.id = id;
R.maxDensity = maxDensity;
R.maxFlow = maxFlow;
R.myNet = [];
R.criticalDensity = [];
R.computedOutFlow = -1;
end
function supply = getSupply(self)
supply = min( self.maxFlow, self.congSpeed * (self.maxDensity - self.currentDensity) );
end
function demand = getDemand(self)
demand = min (self.maxSpeed * self.currentDensity , self.maxFlow);
end
% Given the outflows computed by each road and the external demand,
% the road can update its density (i.e., computing its state at the
% next time instant)
function updateDensity (self, fOut, time, Din)
inRoads = self.myNet.neighborsIn(self.id);
myLight = find (self.myNet.lM == self.id);
if isempty(myLight)
myDuty = 1;
else
myDuty = self.myNet.lights(myLight).dutyCycle();
end
if isempty(inRoads)
inF = min (self.getSupply, Din(self.id, time));
else
sem = self.myNet.lights(inRoads);
duties = zeros(1,length(sem));
for k = 1 : length(duties)
duties(k) = sem(k).dutyCycle();
end
inF = duties*(fOut(inRoads).*self.myNet.turnings(inRoads,self.id))...
+ min (self.getSupply, Din(self.id, time));
end
self.currentDensity = self.currentDensity + ...
self.myNet.sampleTime * ...
(inF - fOut(self.id)*myDuty)/ self.L;
if self.currentDensity < 0
error('Something went wrong in the simulation: you have a road with negative density. Remember that in the discrete CTM you need to have Ts*v/L < 1');
end
if self.currentDensity > self.maxDensity
error('Something went wrong in the simulation: you have a road with density above threshold');
end
self.computedOutFlow = -1;
end
% Given the current network's state, each road is able to
% compute its own outflow of vehicles.
function f = ownOutFlow (self, Sout, time)
if self.computedOutFlow ~= -1
f = self.computedOutFlow;
else
outRoads = self.myNet.neighborsOut(self.id);
toRemove=[];
for i = 1 : length(outRoads)
if self.myNet.turnings(self.id,outRoads(i)) == 0
toRemove = [i toRemove];
end
end
outRoads(toRemove)=[];
minimum = 1e10;
if length(outRoads)>=1
minimum = min( minimum, self.myNet.roads(outRoads(1)).getSupply/self.myNet.turnings(self.id, outRoads(1)));
for k = 2 : length(outRoads)
minimum = min (minimum , self.myNet.roads(outRoads(k)).getSupply/self.myNet.turnings(self.id, outRoads(k)));
end
end
if minimum == 1e10
f = min( self.getDemand, Sout(self.id, time));
else
f = min (minimum(:), self.getDemand);
end
self.computedOutFlow = f;
end
end
end
end