forked from KaranJagdale/Variable-Agent-NYU
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReward_t_nf.m
More file actions
131 lines (117 loc) · 4.65 KB
/
Reward_t_nf.m
File metadata and controls
131 lines (117 loc) · 4.65 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
function timeGain = Reward_t_nf(bus, state, action, a_par,arr_par, dis_stp, v_pas, ...
hw,cap_bus,lpass,lsact, lapass, phead, t_bo, t_al, pphead, hwt, count, v_bus, gencount, pos_code, actn, fPas1, fPas2)
%In main code, need to calculate acap, lpass, lapass
% 0 = "Stop", 1 = "Skip", 2 = "Split", 3 = "Join"
n_b = size(state,2);
ipl = iplus(bus,n_b); %bus behind the current bus
imin = iminus(bus, n_b);
iminm = iminus(imin, n_b);
stateff = state(:,iminm);
statef = state(:,imin);
stateb = state(:,ipl); %State is state of all the modules
state = state(:,bus);
n_s = size(dis_stp,2);
fixdt = 30;
if state(3) == 1
cap_bus = cap_bus*2;
end
%hw = hw(bus);
%hwt = 150; %target headway
hwg = 10*0; %headway gain
fg = 0.5;
%nxt_stp is the next stop to the stop to which the bus is approaching.
nxt_stp = iplus(state(1), n_s);
%nnext_stp is next to next stop to the stop which bus is approaching
nnxt_stp = iplus(nxt_stp,n_s);
pds = a_par(state(1))*(state(2)-lapass) + lapass;
pbst = arr_par(state(1))*hw + lpass;
pbs = min(pbst,cap_bus-(state(2)-pds)); %this is approximation
pdsplus = a_par(nxt_stp)*(state(2)-pds + pbs);
pbsplus = min(arr_par(nxt_stp)*hw + lpass,cap_bus - (state(2)+pbs - pds - pdsplus)); %assuming deboarding people = boarding people
w_wait = 2.1; w_walk = 2.2;
if count == 1
% bstf = state(1);
% st_sep = floor(n_s/n_b);
% for i = 1:st_sep
% bstf = iminus(bstf,n_b);
% e
% fprintf('bstf : %i, state(1) : %i \n', bstf, state(1))
% lambs = lambsum(state(1), bstf, arr_par, n_s);
lambs = 0;
else
lambs =lambsum(state(1), statef(1), arr_par, n_s); %lambs accountss the number of passengers arriving at the stops between two successive buses
end
stop_time = (pds*t_al + pbs*t_bo + fixdt);
wst = dis_stp(state(1))/v_pas;
wstp = dis_stp(nxt_stp)/v_pas;
% fPas1 = 1; %1 is nomina value
% fPas2 = 1; %0.2 is nominal value
%fprintf('hw : %f, st : %f, phead : %f \n', hw, stop_time, phead)
rskip = phead*pbs*w_wait + pds*wst*w_walk + lambs*(stop_time*(phead - hw) + hw^2)*fPas1;
rstop = ((state(2)-pds)*stop_time + lambs*hw^2/2*fPas2);
rsplit = 0;
rnb = 0;
if action == 3 || action == 4
stbkbus = [];
if stateb(1) < state(1) || stateb(1) == state(1)
for i=stateb(1):state(1)
stbkbus = [stbkbus i];
end
else
stbkbus = 1:size(a_par,2);
for i=state(1)+1:stateb(1)
stbkbus(stbkbus == i) = [];
end
end
rjskipc = 0;
phead_pred = phead;
% if gencount == 26 && (pos_code(1) == '3' && pos_code(2) == '4') && actn == 2
% pphead = 93;
% end
% if gencount == 26 && ((pos_code(1) == '4' && pos_code(2) == '4') || (pos_code(1) == '3' && pos_code(2) == '4')) && actn ==2 %&& actn ==1
% fprintf('phead : %f, pphead : %f \n', phead, pphead)
% end
% if gencount > 10 && gencount < 20
% disp('stbkbus')
% disp(stbkbus)
% fprintf('bus: %i, buap : %i \n', state(1), stateb(1))
% end
% for i=stbkbus
% p_ds = a_par(i)*stateb(2);
% l_i= stateb(2) - p_ds; %intermediate load
% p_bs = min(phead*arr_par(i),cap_bus - l_i);
% %stateb(2) = stateb(2) + p_bs;
% nxt_stp = iplus(i,size(a_par,2));
% wst = dis_stp(nxt_stp)/v_pas;
% st_time = (p_ds*t_al + p_bs*t_bo + fixdt);
% %lambs =lambsum(i, state(1), arr_par, n_s);
%
% rjskip = pphead*p_bs*w_wait + p_ds*wst*w_walk;% + 10*(phead - hwt);
% pphead = pphead + st_time;
% if phead_pred - dis_stp(i)/v_bus- st_time > 0
% phead_pred = phead_pred - dis_stp(i)/v_bus -st_time;
% else
% phead_pred = 0;
% end
% % if gencount == 26 && pos_code(1) == '4' && pos_code(2) == '4' && actn ==2
% % fprintf('rjskip : %f \n', rjskip)
% % end
% rjskipc = rjskipc + rjskip;
% %disp(rjskip)
% end
lambsb =lambsum(stateb(1), state(1), arr_par, n_s);
rjoin = state(2)*phead + lambsb*(phead+pphead)^2/2 + hw*lambs*w_wait*phead/2;
end
switch action
case 0
timeGain = rstop;
case 1
timeGain = rskip;
case 2
timeGain = rsplit;
case 3
timeGain = rjoin;
case 4
timeGain = rnb;
end
end