%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% FILE: example_sosu.m
%%
%% Sophie Sophomore's and Susan Suit's compromise solution
%%
%% Alternatives:
%%
%% a1 = Student exchange to China.
%% a2 = Concentrate on the studies full-time.
%% a3 = Mostly studies and working for a food delivery company in the evenings.
%% a4 = Little studies and working full-time for a multinational corporation.
%%
%% Scenarios:
%%
%% s1 = Koala Ebola is pandemic, the university is in distant teaching,
%%      and the world economy is in recess.
%% s2 = Koala Ebola is pandemic, the university is in distant teaching,
%%      and the world economy is recovering.
%% s3 = Koala Ebola is pandemic, the university is in normal teaching,
%%      and the world economy is in recess.
%% s4 = Koala Ebola is pandemic, the university is in normal teaching,
%%      and the world economy is recovering.
%% s5 = Koala Ebola has been defeated, and the world economy is in recess.
%% s6 = Koala Ebola has been defeated, and the world economy is recovering.
%%
%% The probabilities for the scenarios are
%%
%% p1 = p0/4
%% p2 = p0/4
%% p3 = p0/4
%% p4 = p0/4
%% p5 = (1-p0)/2
%% p6 = (1-p0)/2
%%
%% The Koala Ebola pandemic paramter p0 is a sensitivity parameter.  The weight
%% w combining Sophie's and Susan's value functions is a sentitivity parameter.
%%
%% The file uses the functions dmatrix and scaling.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Decision matrices
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Sophie's matrix 1

sa = 12*252.76;      %% Yearly student allowance.
cs = 5000;           %% China scholarship.
cm = 2000;           %% China moving costs.
cf = 12*500;         %% Yearly living costs in Finland.
cc = 12*250;         %% Yearly living costs in China.

sc = sa+cs-cm;       %% Yearly income in China.
sf = sa;             %% Yearly income if Finland.

fd = 12*200*0.9;     %% Yearly food delivery salary.
ft = 12*2500*0.75;   %% Yearly full-time salary in a multinational corporation.

RSo1 = [
sc-0.8*cc      sc-cc       sc-0.8*cc      sc-cc       sc-0.9*cc     sc-cc   ;
sf-0.6*cf      sf-cf       sf-0.6*cf      sf-cf       sf-0.8*cf     sf-cf   ;
sf+4*fd-0.6*cf sf+4*fd-cf  sf+2*fd-0.6*cf sf+2*fd-cf  sf+fd-0.8*cf  sf+fd-cf;
0.3*ft-1.6*cf  0.7*ft-2*cf 0.3*ft-1.6*cf  0.8*ft-2*cf 0.5*ft-1.8*cf ft-2*cf
];

%% Sophie's matrix 2

RSo2 = [
2  2  2  2  2  2;
2  2  1  1  1  1;
2  2  2  2  2  2;
3  4  3  4  3  5
];

RSo2 = -RSo2;          %% Maximization form!

%% Susan's decision matrix

RSu = [
3000  3500  2800  3500  2800  3500;
2500  2700  3300  3800  3500  4000;
3000  3200  2000  2500  2000  2500;
3000  3200  2500  4000  3000  3500
];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Visualization.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Decision problems' constant fields (independent of p0 and w)
dpSo1.dmat = RSo1;
dpSo1.drule = "ev";
dpSo2.dmat = RSo2;
dpSo2.drule = "ev";

dpSu1.dmat = RSu;
dpSu1.drule = "ev";
dpSu2.dmat = RSu;
dpSu2.drule = "regret";

%% Decision problem dpSu2 can be calculated completely, since it is independent
%% pf p0 and w.
drSu2 = dmatrix(dpSu2);
VSu2 = drSu2.dvalues;
VSu2 = -VSu2;                       %% Regret is a minimization form.
VSu2 = scaling(VSu2);


%% Sampling frequeny.  The bigge the better picure, but the longer the
%% calculations take.  For N=500 the calculations take 5 seconds.
N = 500;

%% Sample points for x (p0samples) and y (wsamples) axes.
p0samples = linspace(0,1,N);
wsamples = linspace(0,1,N);

for i=1:length(p0samples)
    p0 = p0samples(i);
    q0 = 1-p0;
    dpSo1.prob = [p0/4 p0/4 p0/4 p0/4 q0/2 q0/2]';
    dpSo2.prob = [p0/4 p0/4 p0/4 p0/4 q0/2 q0/2]';
    dpSu1.prob = [p0/4 p0/4 p0/4 p0/4 q0/2 q0/2]';
    drSo1 = dmatrix(dpSo1);
    drSo2 = dmatrix(dpSo2);
    drSu1 = dmatrix(dpSu1);
    %% drSu2 has been calculated in the line 103.
    VSo1 = scaling(drSo1.dvalues);
    VSo2 = scaling(drSo2.dvalues);
    VSo = 0.5*VSo1 + 0.5*VSo2;
    VSo = scaling(VSo);
    VSu1 = scaling(drSu1.dvalues);
    VSu = 0.5*VSu1 + 0.5*VSu2;
    VSu = scaling(VSu);
    for j=1:length(wsamples)
        w = wsamples(j);
        V = w*VSo + (1-w)*VSu;
       [roskaa, z(j,i)] = max(V);
    end
end

%% Draw the picture. See the m-file example_so.m for comments.
contourf(p0samples, wsamples, z)
set(gca, "fontsize", 24);
xlabel("p_0")
ylabel("w")
red     = [1 0 0];
yellow  = [1 1 0];
green   = [0 1 0];
colormap([red; yellow; green])
text(0.70,0.50, "a_1", "fontsize",24)
text(0.08,0.08, "a_2", "fontsize",24)
text(0.92,0.92, "a_3", "fontsize",24)
%% Uncommet the following lines to draw the picture in a landcape paper in
%% PDF format to the file sosu_picture.pdf.  Your system may not support the
%% PDF format format.  In that case you can try, e.g., the JPG format. This
%% means naming the print file as so_picture.jpg.  You may also want to leave
%% the orient line commented.
%orient("landscape")
%print("sosu_picture.pdf")
