%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% FILE: example_ll.m
%%
%% Laila Laihian's fall: a minimal m-file for Assignment "Your Fall".
%% Adding report to these kind of calculations will give you an accepted
%% assignment with grade 1.
%%
%% Laila Laihian lives with her mother in Laihia and contemplates what to do
%% in the next fall.
%%
%% Laila considers the following alternatives:
%%
%% a1 = Move to Vaasa into a VOAS flat.
%% a2 = Commute daily between Laihia and Vaasa.
%% a3 = Distant studies from Laihia.
%%
%% The scenarios are:
%%
%% s1 = Koala Ebola is contained.
%% s2 = Koala Ebola is pandemic, Campus is open, teaching is partly distant.
%% s3 = Koala Ebola is pandemic, Campus is closed, teaching is distant.
%%
%% The scenario probabilities are
%%
%% p0 = Koala Ebola pandemic probability(p0 = p2+p3)
%% q0 = 1-p0
%%
%% p1 = q0
%% p2 = p0/2
%% p3 = p0/2
%%
%% Laila has two decision matrices and she uses the expected value rule.
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Decition matrices (multi-objective decision problem)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Monthly expenses
R1 = [
100+40   100+50  100+100;   %% VOAS flat  (rent+food)
   120  0.5*120    0*120;   %% Driving to Vaasa (100%-50%-0% on site teaching)
     0        0        0    %% Studying from Laihia  (baseline)
];

R1 = -R1;     %% Maximun form!


%% Study credit points
R2 = [
30  25  20;   %% VOAS flat
25  25  20;   %% Driving to Vaasa
10  15  20    %% Studying from Laihia
];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Sensitivity calculations and visuatization
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Samplin frequency.  For N=500 the calculations take 3 seconds.
N = 500;
p0samples = linspace(0,1,N);
wsamples = linspace(0,1,N);

%% Decision problem's constant fields.
dp1.drule = "ev";
dp1.dmat = R1;
dp2.drule = "ev";
dp2.dmat = R2;

%% Calculating the sensitivity matrix z.
for i=1:N
    p0 = p0samples(i);
    q0 = 1-p0;
    dp1.prob = [q0  p0/2  p0/2]';
    dp2.prob = [q0  p0/2  p0/2]';
    dr1 = dmatrix(dp1);
    dr2 = dmatrix(dp2);
    V1 = dr1.dvalues;
    V1 = scaling(V1);
    V2 = dr2.dvalues;
    V2 = scaling(V2);
    for j=1:N
        w = wsamples(j);
        V = w*V1 + (1-w)*V2;
        [tmp, z(j,i)] = max(V);
    end
end

%% Drawing the sensitivity matrix z.
contourf(p0samples, wsamples, z);
%% Uncomment the following lines for PDF file output.
%orient("landscape")
%print("ll_picture.pdf")
