-
Notifications
You must be signed in to change notification settings - Fork 0
/
aStarKMOMDP_values.m
145 lines (78 loc) · 3.2 KB
/
aStarKMOMDP_values.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
function [Values, Policies, PK, RK, S2K, K2S, time] = aStarKMOMDP_values(K, precision, Gamma, Beliefs, P, R, GammaAttributes, num_full_obs_vars)
%Given a the set of alpha-vectors Gamma and the Belief space,
%aStarKMOMDP_values compute and state abstraction on the fully observable
%state space
N_A = size(P, 4);
N_X = size(P, 1);
N_Y = size(P, 3);
%R = normalize(R, 'range');
Gammas = cell(num_full_obs_vars, 1);
GammasActions = cell(num_full_obs_vars, 1);
Values = zeros(num_full_obs_vars, length(Beliefs));
Policies = zeros(num_full_obs_vars, length(Beliefs));
for gs = 1:num_full_obs_vars
Gammas{gs} = {};
GammasActions{gs} = {};
end
for g = 1:length(Gamma)
x = GammaAttributes(g, 2)+1;
Gammas{x} = [Gammas{x}; Gamma(g,:)];
GammasActions{x} = [GammasActions{x}; GammaAttributes(g,1)];
end
for x = 1:num_full_obs_vars
max_val = -1000;
for b = 1:length(Beliefs)
max_val = -1000;
for a = 1:length(Gammas{x})
tmp_val = dot(Gammas{x}{a}, Beliefs(b, :));
if tmp_val > max_val
max_val = tmp_val;
Values(x,b) = max_val;
Optimal_actions(x,b) = GammasActions{x}{a};
end
end
end
end
%Once we have all the possible Values for pairs x,b, we abstract them
VMAX_row = max(Values);
VMAX = max(VMAX_row);
VMAX = 10;
number_abstractions = 0;
d_lower = 0;
d_upper = VMAX;
p = d_upper - d_lower;
% bindings = zeros(num_full_obs_vars, length(Beliefs));
bindings = zeros(num_full_obs_vars, length(Beliefs) + length(Beliefs));
tic;
while p > 0.000001
p = d_upper - d_lower; %To refine precision
d = d_lower + (d_upper - d_lower)/2; %Binary search on d
for x = 1:num_full_obs_vars
for b = 1:length(Beliefs)
bindings(x,b) = ceil(Values(x,b)/d);
bindings(x, length(Beliefs)+b) = Optimal_actions(x,b);
end
end
[B,ia,ic] = unique(bindings,'rows');
tmp_number_abstractions = length(ia);
tmp_number_abstractions
if tmp_number_abstractions <= K
%Here K must be the number of clusters in L. In this case is the size of ic;
L= ic';
number_abstractions = tmp_number_abstractions;
d_upper = d;
else
d_lower = d;
end
end
%Build the K-MOMDP with the abstracted state space
PK = zeros(number_abstractions, number_abstractions, N_Y, N_A);
%L
number_abstractions
for y = 1:N_Y
%PK must be a matrix of the form PK(K, K, N_Y, N_A)
[PK_y,RK,S2K,K2S]=buildKMOMDP_R(P(:,:,y,:),R,L,N_A,N_X,number_abstractions); %Build the KMOMDP with k states and state space L
PK(:,:,y,:) = PK_y;
end
time = toc;
end