-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmincon_robust.m
72 lines (58 loc) · 2.6 KB
/
fmincon_robust.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
function [r1,x] = fmincon_general_easy_NIAC(K, cond_prob, prob_act,joint_prob)
%Rational inattention test - general cost, NIAS in A matrix, NIAC in
%nonlcon arguments in c matrix.
% OUTPUT: SOLUTION WITH LARGEST ROBUSTNESS METRIC R(D) (Eq. 13 in paper)
small = 1e-3;
NIAS = (10*9)*K; %number of constraints
A = zeros(NIAS,100*K+K+1); % last variable contains the robustness metric
%NIAS
nias_count=1;
for k=1:K % for all decision problems
for a=1:10 % primary action in NIAS (higher utility)
for b = 1:10 % secondary action in NIAS (lower utility)
if a~=b
% NIAS : \sum_{x} p(x|a) u(x,b) - p(x|a) u(x,a) <= 0.
A(nias_count,(k-1)*100 + (b-1)*10 + 1: (k-1)*100 + (b-1)*10 + 10) = cond_prob((k-1)*100 + (a-1)*10 + 1: (k-1)*100 + (a-1)*10 + 10);
A(nias_count,(k-1)*100 + (a-1)*10 + 1: (k-1)*100 + (a-1)*10 + 10) = -cond_prob((k-1)*100 + (a-1)*10 + 1: (k-1)*100 + (a-1)*10 + 10);
A(nias_count,100*K + K + 1) = 1; %only for robustness test
nias_count=nias_count+1;
end
end
end
end
%NIAC-under non-linear constraints
function [c,ceq] = NIAC(x)
c=K*(K-1); %array of NIAC constraints
ceq=[];
niac_count = 1;
for main=1:K
for side=1:K
if main~=side
main_u = sum( joint_prob( (main-1)*100 + 1 : main*100 )'.* x((main-1)*100 + 1 : main*100) );
side_u = 0;
for act = 1:10
vec_act = zeros(1,10);
for cand=1:10
vec_act(cand) = sum( cond_prob( (side-1)*100 + (act-1)*10 + 1 :...
(side-1)*100 + act*10 ).* x( (main-1)*100 + (cand-1)*10 + 1 : (main-1)*100 + cand*10 )' );
end
side_u = side_u + prob_act( (side-1)*10 + act )*max(vec_act);
end
%c(niac_count) = side_u - x(100*K + side) - (main_u - x(100*K + main));
c(niac_count) = side_u - x(100*K + side) - (main_u - x(100*K + main)) + x(100*K + K +1); %add last term for robustness test only
niac_count=niac_count+1;
end
end
end
ceq = [];
end
%Can constrain the utility values to be within [0,1] WLOG
lb = 1e-4*ones(100*K+K+1,1);
ub = ones(100*K+K+1,1);
ub(100*K+K+1) = 0.1;
function f = objfun(x)
f = -x(100*K + K + 1);% use this for robustness testing
end % Compute function value at x
options = optimoptions('fmincon','Algorithm','active-set','MaxFunctionEvaluations',1000000,'MaxIterations',100000);%,'StepTolerance',1e-14,'OptimalityTolerance',0.5*1e-3);
[r1 r2 x r3] = fmincon(@objfun,(2*lb + ub)/3,A,-small*ones(NIAS,1),[],[],lb,ub,@NIAC,options);
end