-
Notifications
You must be signed in to change notification settings - Fork 0
/
milp_reynicost_niacs.m
220 lines (208 loc) · 7.99 KB
/
milp_reynicost_niacs.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
function [x,exitflag] = milp_reynicost_niacs(J, K, X, cond_prob, prob_act, joint_prob, state_prob, beta)
%MILP_GENERALCOST Summary of this function goes here
% %Normalize the joint_prob
% for k=1:K
% joint_prob((1:X*J)+(k-1)*X*J) = joint_prob((1:X*J)+(k-1)*X*J)/sum(joint_prob((1:X*J)+(k-1)*X*J));
% end
% for i=1:X
% for k=1:K
% state_prob(i+(k-1)*K) = sum(joint_prob((i+(k-1)*X*J):(i+J+(k-1)*X*J)));
% end
% end
%Rational inattention test
M = 10000;
%A and b are reserved for Ax <= b
A = sparse(J*(4*K*K-J-K)+1, X*J*K+2*J(K-1)+2*(J^2)*(K-1)+2+2);
rowcnt = 1;
%Aeq and beq reserved for Aeqx = beq
Aeq = sparse(J*K, X*J*K+2*J(K-1)+2*(J^2)*(K-1)+2+2);
rowcnteq = 1;
%Binary indicies
intcon = [(1+2*J*(K-1)+X*J*K):(2*(J^2)*(K-1)+2*J*(K-1)+X*J*K)];
%Constraint 1 NIAS
for j = 1:J
for l=1:J
for k=1:K
if(j ~= l)
s_jindx = 1+(j-1)*X+(k-1)*X*J;
e_jindx = X+(j-1)*X+(k-1)*X*J;
s_lindx = 1+(l-1)*X+(k-1)*X*J;
e_lindx = X+(l-1)*X+(k-1)*X*J;
A(rowcnt, s_jindx:e_jindx) = -cond_prob(s_jindx:e_jindx);
A(rowcnt, s_lindx:e_lindx) = cond_prob(s_jindx:e_jindx);
b(rowcnt) = -1e-3; %small negative to force strict inequality for atleast one
rowcnt = rowcnt+1;
end
end
end
end
%Constraint 2 m_k >=
for j=1:J
for l=1:J
for k=1:(K-1)
A(rowcnt, (j+(k-1)*J+X*J*K)) = -1;
A(rowcnt, (1+(l-1)*X+(k-1)*X*J):(X+(l-1)*X+(k-1)*X*J)) = cond_prob((1+(j-1)*X+(k-1)*X*J):(X+(j-1)*X+(k-1)*X*J));
b(rowcnt) = 0;
rowcnt = rowcnt+1;
end
end
end
%Constraint 3 M (1) (mk)
for j=1:J
for l=1:J
for k=1:(K-1)
A(rowcnt, (1+(l-1)*X+(k-1)*X*J):(X+(l-1)*X+(k-1)*X*J)) = -cond_prob((1+(j-1)*X+(k-1)*X*J):(X+(j-1)*X+(k-1)*X*J));
A(rowcnt, l+(j-1)*J+(k-1)*(J^2)+2*J*(K-1)+X*J*K) = M;
A(rowcnt, j+(k-1)*J+X*J*K) = 1;
b(rowcnt) = M;
rowcnt = rowcnt+1;
end
end
end
%Constraint 4 (equality) sum = 1
for j=1:J
for k=1:(K-1)
Aeq(rowcnteq, (1+(j-1)*J+(k-1)*(J^2)+2*J(K-1)+X*J*K):(J+(j-1)*J+(k-1)*(J^2)+2*J(K-1)+X*J*K)) = 1;
beq(rowcnteq) = 1;
rowcnteq = rowcnteq + 1;
end
end
%Constraint 5
for j=1:J
for l=1:J
for k=1:(K-1)
if(k <= (K-2))
A(rowcnt, (j+k*J+X*J*K+J*(K-1))) = -1;
A(rowcnt, (1+(l-1)*X+(k-1)*X*J):(X+(l-1)*X+(k-1)*X*J)) = cond_prob((1+(j-1)*X+k*X*J):(X+(j-1)*X+k*X*J));
else
A(rowcnt, (j+X*J*K+J*(K-1))) = -1;
A(rowcnt, (1+(l-1)*X+(k-1)*X*J):(X+(l-1)*X+(k-1)*X*J)) = cond_prob((1+(j-1)*X):(X+(j-1)*X));
end
b(rowcnt) = 0;
rowcnt = rowcnt+1;
end
end
end
%Constraint 6 M (2) n_k
for j=1:J
for l=1:J
for k=1:(K-1)
if(k <= (K-2))
sl_indx = 1+(l-1)*X+(k-1)*X*J;
el_indx = X+(l-1)*X+(k-1)*X*J;
sj_indx = 1+(j-1)*X+k*X*J;
ej_indx = X+(j-1)*X+k*X*J;
zeta_indx = l+(j-1)*J+k*(J^2)+(K-1)*(J^2)+2*J*(K-1)+X*J*K;
A(rowcnt, j+k*J+X*J*K+J*(K-1)) = 1;
A(rowcnt, sl_indx:el_indx) = -cond_prob(sj_indx:ej_indx);
A(rowcnt, zeta_indx) = M;
else
sl_indx = 1+(l-1)*X+(k-1)*X*J;
el_indx = X+(l-1)*X+(k-1)*X*J;
sj_indx = 1+(j-1)*X;
ej_indx = X+(j-1)*X;
zeta_indx = l+(j-1)*J+(K-1)*(J^2)+2*J*(K-1)+X*J*K;
A(rowcnt, j+X*J*K+J*(K-1)) = 1;
A(rowcnt, sl_indx:el_indx) = -cond_prob(sj_indx:ej_indx);
A(rowcnt, zeta_indx) = M;
end
b(rowcnt) = M;
rowcnt = rowcnt+1;
end
end
end
%Constraint 7 (equality) sum = 1
for j=1:J
for k=1:(K-1)
if(k <= (K-2))
sl_indx = (1+(j-1)*J+k*(J^2)+(J^2)*(K-1)+2*J*(K-1)+X*J*K);
el_indx = (J+(j-1)*J+k*(J^2)+(J^2)*(K-1)+2*J*(K-1)+X*J*K);
Aeq(rowcnteq, sl_indx:el_indx) = 1;
else
sl_indx = (1+(j-1)*J+(J^2)*(K-1)+2*J*(K-1)+X*J*K);
el_indx = (J+(j-1)*J+(J^2)*(K-1)+2*J*(K-1)+X*J*K);
Aeq(rowcnteq, sl_indx:el_indx) = 1;
end
beq(rowcnteq) = 1;
rowcnteq = rowcnteq+1;
end
end
%Constraint 8 NIAC
for k=1:(K-1)
if(k <= (K-2))
A(rowcnt, (1+(k-1)*J+X*J*K):(J+(k-1)*J+X*J*K)) = -prob_act((1+(k-1)*J):(J+(k-1)*J));
A(rowcnt, (1+k*J+X*J*K+J*(K-1)):(J+k*J+X*J*K+J*(K-1))) = -prob_act((1+k*J):(J+k*J));
else
A(rowcnt, (1+(k-1)*J+X*J*K):(J+(k-1)*J+X*J*K)) = -prob_act((1+(k-1)*J):(J+(k-1)*J));
A(rowcnt, (1+X*J*K+J*(K-1)):(J+X*J*K+J*(K-1))) = -prob_act(1:J);
end
b(rowcnt) = 0;
end
% dumm1=0;
% dumm2=0;
% %Constraint 9 (equality) KKT
% for i=1:X
% for j=1:J
% for k=1:K
% coeff = 0;
% for si=1:X
% for sj=1:J
% %coeff = coeff+joint_prob(si+(sj-1)*X+(k-1)*X*J)*((cond_prob(si+(sj-1)*X+(k-1)*X*J)/state_prob(si+(k-1)*K))^(beta-1));
% coeff = coeff+(joint_prob(si+(sj-1)*X+(k-1)*X*J)^(beta))/[(state_prob(si+(k-1)*K)^(beta-1))*(prob_act((k-1)*J + sj)^(beta-1))];
% end
% end
% %rectify KKT for renyi case
% %coeff = coeff*((cond_prob(i+(j-1)*X+(k-1)*X*J)/state_prob(i+(k-1)*K)^(beta-1)))/(beta-1);
% %coeff=coeff*(beta-1)*(prob_act( j+(k-1)*J ))^(2*(beta-1));
% if joint_prob(1 +(j-1)*X+(k-1)*X*J) == 0
% joint_prob(1 +(j-1)*X+(k-1)*X*J) = 0.000001;
% end
% if joint_prob(2 +(j-1)*X+(k-1)*X*J) == 0
% joint_prob(2 +(j-1)*X+(k-1)*X*J) = 0.000001;
% end
% if prob_act(j+(k-1)*J) == 0
% prob_act(j+(k-1)*J) = 0.000001;
% end
% dumm1= [(beta)*(prob_act(j+(k-1)*J)^(beta-1))*(joint_prob(i+(j-1)*X+(k-1)*X*J)^(beta-1)) - (beta-1)*(prob_act(j+(k-1)*J)^(beta-2))*(joint_prob(i+(j-1)*X+(k-1)*X*J)^(beta))]/(state_prob(i+(k-1)*X)^(beta-1));
% dumm2= [(beta-1)*(prob_act(j+(k-1)*J)^(beta-2))* (joint_prob(mod(i,2)+1 +(j-1)*X+(k-1)*X*J)^(beta))]/(state_prob(mod(i,2)+1 +(k-1)*X)^(beta-1)); % can use the mod trick just because we have two states 1 and 2
%
% if ~isnan(coeff)
% coeff=(dumm1 - dumm2)/[coeff*(beta-1)*(prob_act( j+(k-1)*J ))^(2*(beta-1))];
% else
% if dumm1>=dumm2
% coeff=Inf;
% else
% coeff= -Inf;
% end
% end
% %coeff
% Aeq(rowcnteq, i+(j-1)*X+(k-1)*X*J) = 1;
% %if ~(isnan(coeff))
% Aeq(rowcnteq, (2*(k-1))+1+2*J*(K-1)+2*(J^2)*(K-1)+X*J*K) = -coeff;
% %else
% % Aeq(rowcnteq, 1+2*J*(K-1)+2*(J^2)*(K-1)+X*J*K) = (-1)*M;
% %end
% %if cond_prob(i+(j-1)*X+(k-1)*X*J) ~= 0
% % Aeq(rowcnteq, 1+2*J*(K-1)+2*(J^2)*(K-1)+X*J*K) = -log(cond_prob(i+(j-1)*X+(k-1)*X*J));
% %else
% % Aeq(rowcnteq, 1+2*J*(K-1)+2*(J^2)*(K-1)+X*J*K) = 0;
% %end
% Aeq(rowcnteq, (2*(k-1))+2+2*J*(K-1)+2*(J^2)*(K-1)+X*J*K) = 1;
% beq(rowcnteq) = 0;
% rowcnteq = rowcnteq+1;
% end
% end
% end
%Solve MILP to construct utility function
lb = zeros(X*J*K+2*J(K-1)+2*(J^2)*(K-1)+2+2,1);
lb(X*J*K+2*J(K-1)+2*(J^2)*(K-1)+2) = -Inf;
lb(X*J*K+2*J(K-1)+2*(J^2)*(K-1)+2+2) = -Inf;
%lb(1:(X*J*K)) = 1e-3;
ub = ones(X*J*K+2*J(K-1)+2*(J^2)*(K-1)+2+2,1);
ub(X*J*K+2*J(K-1)+2*(J^2)*(K-1)+1:X*J*K+2*J(K-1)+2*(J^2)*(K-1)+2+2) = [Inf Inf Inf Inf];
%ub(1:(X*J*K)) = 100;
f = zeros(X*J*K+2*J(K-1)+2*(J^2)*(K-1)+2+2,1); % no objective function
%options = optimoptions('intlinprog','Display','iter', 'IntegerTolerance', 1e-6);
[x,~,exitflag] = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub);
%Aeq
end