-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSGAS5.m
75 lines (64 loc) · 2.2 KB
/
SGAS5.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
function [rate, stop] = SGAS5(highway, stoppingEq, ...
alpha, beta, percentCalc, percentSecretary)
%New solution separtes range of car into 3 sections: Density Observation,
%Secretary Solution, and Critical. 1) Density Observation: Uses TCP RTO
%calculations to estimate how many k gast stations will be seen in the
%Secretary section of the car's range. 2) Secretary Solution: Runs the
%Secretary Solution assuming k gas stations 3) Critical: Stops at the first
%available Gas Station.
len = length(highway);
stopCalc = floor(len * percentCalc);
stopSecretary = floor(len * percentSecretary);
gasStations = 0;
lastStation = 0;
dev = 0;
est = 0;
%Calculate density for predicting k
for position = 1:stopCalc
if highway(position) ~= 0
gasStations = gasStations + 1;
if(gasStations == 1)
est = position;
else
est = alpha*est + (1-alpha)*(position - lastStation);
dev = beta*dev + (1-beta)*abs(est - (position - lastStation));
end
lastStation = position;
end
end
if(stopCalc > 0); k = round((gasStations*(stopSecretary - stopCalc))/(stopCalc)); else; k = 1; end
%Uncomment these two lines for perfect prediction
%temp = highway(stopCalc:stopSecretary);
%k = length(temp(temp > 0));
stationsToPass = stoppingEq(k);
stationRates = [];
%Run the secretary problem
for position = stopCalc:stopSecretary
if highway(position) ~= 0
stationRates = [stationRates highway(position)];
%Program seems to perform better if we don't stop at last one
%Instead it keeps going until critical section is reached
% if length(stationRates) == k
% stop = position / len;
% rate = highway(position);
% return
% end
if highway(position) <= min(stationRates) && ...
length(stationRates) >= stationsToPass
stop = position / len;
rate = highway(position);
return
end
end
end
%In critical section. Stop at first available station
for position = stopSecretary:len
if highway(position) ~= 0
stop = position / len;
rate = highway(position);
return
end
end
stop = -1;
rate = -1;
return