-
Notifications
You must be signed in to change notification settings - Fork 1
/
stochActivity.m
73 lines (68 loc) · 2.14 KB
/
stochActivity.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
function [opt_util passengers1 flows1] = stochActivity(THE, ALP, DISC, population0, num_act_util, T)
% this is an implementation of the MDP model for departure time choice
% time 13:00 -- 23:59
% 1: home, 2:work
theta = THE;
alpha = ALP;
act_util = num_act_util;
times = T;
opt_util = NaN(13,2);
opt_util(13,1) = 0.0;
passengers1 = zeros(13,2);
passengers1(1,2) = population0;
flows1 = zeros(12,2,2);
this_util = [0,0];
discount = DISC;
for t = 12:-1:1
for s = 1:2
opt_util(t,s) = 0;
for a = 1:2
if a<=s && t+int32(times(s,a,t))+1<=13
if ~isnan(opt_util(t+int32(times(s,a,t))+1,a))
this_util(a) = theta*(act_util(t,s) - ...
alpha*times(s,a,t) + discount*opt_util(t+times(s,a,t)+1,a) );
opt_util(t,s) = opt_util(t,s) + exp(this_util(a));
end
end
end
if opt_util(t,s) ~= 0
opt_util(t,s) = log(opt_util(t,s) )/theta;
else
opt_util(t,s) = NaN;
end
end
end
for t = 1:12
for s = 1:2
for a = 1:2
if a<=s && t+times(s,a,t)+1<=13
if ~isnan(opt_util(t+int32(times(s,a,t))+1,a))
this_util(a) = theta*(act_util(t,s) - ...
alpha*times(s,a,t) + discount*opt_util(t+times(s,a,t)+1,a));
flows1(t,s,a) = exp(this_util(a) ) / exp(opt_util(t,s)*theta );
end
end
end
for a = 1:2
if a<=s && t+times(s,a,t)+1<=13
if ~isnan(opt_util(t+int32(times(s,a,t))+1,a))
flows1(t,s,a) = passengers1(t,s)*flows1(t,s,a);
passengers1(t+times(s,a,t)+1,a) = ...
passengers1(t+times(s,a,t)+1,a) + flows1(t,s,a);
end
end
end
end
end
save MDP_results1 flows1 passengers1 opt_util
disp('dynamic programming')
disp('optimal utilty')
disp(opt_util)
disp('flows')
disp(flows1)
disp('passengers')
disp(passengers1)
% figure
% hold on
% plot(flows1(:,2,1), '--')
% plot(passengers1)