% Multistage recourse model

% Use same example but with x_2 as a third stage variable, and x_3 and x_4
% as second stage variables.

% Formulation of cost function and constraints

f = [0 0 0 0 0 -1/4 -1/4 -1/4 -1/4]; % cost function

A = zeros(1,length(f)); % inequality constraint matrix

b = zeros(1,1); %

Aeq = [1 1 0 0 0 1 0 0 0;
    1 0 1 0 0 3/4 0 0 0;
    1 0 0 1 0 0 1 0 0;
    -3 0 0 0 -1 0 5/4 0 0;
    1 1 0 0 0 0 0 1 0;
    1 0 1 0 0 0 0 3/4 0;
    1 0 0 1 0 0 0 0 1;
    -3 0 0 0 -1 0 0 0 5/4;];

beq = [2 2 2 2 2 2 2 2]';

lb = [-1 0 0 0 0 0 0 0 0]';
ub = [1 inf inf inf inf inf inf inf inf]';

[x, fval, EF] = linprog(f,A,b,Aeq,beq,lb,ub);

% EF is the existflag and if it is 1, the function linprog found a feasible
% soultion to the problem.
% Check if the solution is feasible 
if EF > 0
    fprintf('Feasbile solution \n ');
else
    fprintf('Unfeasible solution \n ');
end

% Prints results 
string = ['The solution is [%1.4f %1.4f %1.4f %1.4f %1.4f %1.4f %1.4f \n' ...
   '%1.4f %1.4f ] the cost function is %2.4f \n '];

fprintf(string, x(:,1),fval );
