-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_decision.m
116 lines (100 loc) · 4.12 KB
/
sim_decision.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
function bestAction = sim_decision(dbn, ex, isNightOwl, startTime, travel, priority)
% function prNeedPrepTime = sim_decision(dbn, ex, isNightOwl,
% startTime, travel, priority)
% ARGS: dbn = dynamic bayes net model specified by BNT syntax
% ex = setting used to generate evidence
% (1=random, 2=fixed observable evidence)
% isNightOwl = if user is nightowl (1=false, 2=true)
% startTime = time of day the event begins (1=morn, 2=day, 3=night)
% travel = if user must travel to event (1=false, 2=true)
% priority = if event is a high priority (1=false, 2=true)
engine = bk_inf_engine(dbn); % set up inference engine
T = 50; % number of time steps to simulate
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% generate a series of evidence in advance
% To change the fixed variables, look for !! in comments below !!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ex == 1 % For flag 1, create random evidence
disp('Creating random evidence...')
ev = sample_dbn( dbn, T ); % similar to randi
evidence = cell(7,T); % 3 nodes per slice, 50 time steps
onodes = dbn.observed;
evidence(onodes,:) = ev( onodes, : ); % all cells besides onodes are empty
elseif ex == 2 % For flag 2, create fixed observable evidence
fprintf(['Creating evidence with isNightOwl=%d, StartTime=%d, ' ...
'Travel=%d, Priority=%d\n'], ...
isNightOwl, startTime, travel, priority)
evidence = cell(7,T); % 7 nodes, T time steps
for ii = 1:T % Iterate through evidence and fix values to event info
evidence{1,ii} = isNightOwl;
evidence{2,ii} = startTime;
evidence{5,ii} = travel;
evidence{6,ii} = priority;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% inference process: infer if user needs help over T time steps
% keep track of results and plot as we go
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% setup results to be stored
belief = []; % will concatenate and plot this variable
exputil = [];
subplot(1, 2, 1); % setup plot for graph
% get the probability of need prep time at t=0
prNeedPrepTime = get_field(dbn.CPD{dbn.names('NeedPrepTime')},'cpt');
% Start plotting prNeedPrepTime
belief = [belief, prNeedPrepTime(2)];
subplot(1, 2, 1);
plot(belief, 'o-', LineWidth=1)
% log & plot best action
[bestA, eu] = get_meu(prNeedPrepTime(2));
exputil = [exputil, eu];
fprintf('\tAt t=%d: best action = set %d reminder(s), EU = %f\n', ...
0, bestA, eu);
subplot( 1, 2, 2 );
plot( exputil, '*-', LineWidth=1);
% At t=1, initialize the belief state
[engine, ll(1)] = dbn_update_bel1(engine, evidence(:,1));
% get the probability of NeedPrepTime for this time step
marg = dbn_marginal_from_bel(engine, 7);
prNeedPrepTime = marg.T;
% plot prNeedPrepTime
belief = [belief, prNeedPrepTime(2)];
subplot( 1, 2, 1 );
plot( belief, 'o-', LineWidth=1);
% log best decision
[bestA, eu] = get_meu(prNeedPrepTime(2));
exputil = [exputil, eu];
fprintf('\tAt t=%d: best action = set %d reminder(s), EU = %f\n', ...
1, bestA, eu);
subplot( 1, 2, 2 );
plot( exputil, '*-' );
% Repeat inference steps for each time step
for t=2:T
% update belief with evidence at current time step
[engine, ll(t)] = dbn_update_bel(engine, evidence(:,t-1:t));
% extract marginals for 'needPrepTime' in current belief state
marg = dbn_marginal_from_bel(engine, 7);
prNeedPrepTime = marg.T;
% log best decision
[bestA, eu] = get_meu( prNeedPrepTime(2) );
exputil = [exputil, eu];
fprintf('\tAt t=%d: best action = set %d reminder(s), EU = %f\n', ...
t, bestA, eu);
subplot( 1, 2, 2 );
plot( exputil, '*-', LineWidth=1);
xlabel( 'Time Steps' );
ylabel( 'EU(setReminder)' );
axis( [ 0 T -5 5] );
% keep track of results and plot
belief = [belief, prNeedPrepTime(2)];
subplot( 1, 2, 1 );
plot( belief, 'o-' );
xlabel( 'Time Steps' );
ylabel( 'Pr(NeedPrepTime)' );
axis( [ 0 T 0 1] );
pause(0.01);
end
% Return the best Action from the last simulated time step
bestAction = bestA;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%