% Simple Recourse Model

% Looking at an example given in [Sen, Higle, 1999]
% Min = -x_2
% s.t.  x_1 + x_2 + x_3 = 2
%       -a*x_1 + a*x_2 + x_4 = 2
%       -1 =< x_1 => 1
%        x_j => 0, j = 2,3,4.

% Where a_21 and a_22 are not know with certainty, and the joint
% distribution is given as: {(1,3/4) with probability 0.5
%                           {(-3,5/4) with probability 0.5

% Solution

g = 5; %Penalty cost parameter
f = [0 -1 0 0 g/2 g/2 g/2 g/2]; % cost function

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

beq = [2;
    2;
    2]; % Constraint value vector

A = zeros(1,8); % Inequality constraint matrix

b = zeros(1,1); % Inequality value vector

LB = [-1;0;0;0;0;0;0;0]; % Lower bound vector

UB = [1;Inf;Inf;Inf;Inf;Inf;Inf;Inf]; % upper bound vector

% Solve the problem stated above.
[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 %1.4f] \n ' ...
'the cost function is %2.4f \n '];

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