
% Demonstrate the use of lp_solve.mex4

% Get help message

clc;
lp_solve

disp('Press any key to continue.');
pause

echo on;
clc
% Example 1 from the lp_solve distribution

f = [-1 2]';
A = [2 1;-4 4];
b = [5 5];
e = -[1 1];
xint = [1 2];

[v,x] = lp_solve(f,sparse(A),b,e,[],[],xint)

disp('Press any key to continue.');
pause

clc
% Example 2

f = [50 100];
A = sparse([10 5;4 10; 1 1.5]);
b = [2500 2000 450];
e = [-1 -1 -1];

[v,x] = lp_solve(f,A,b,e)

disp('Press any key to continue.');
pause

clc
% Example 3

f = -[40 36];
vub = [8 10];
A = sparse([5 3]);
b = [45];
e = 1;

[v,x] = lp_solve(f,A,b,e,[],vub)

disp('Press any key to continue.');
pause

clc
% Example 4

clc
f = [10 6 4];
A = [1 1 1;10 4 5;2 2 6];
A = sparse(A);
b = [100 600 300];
e = [-1 -1 -1];
xint = [2];

[v,x] = lp_solve(f,A,b,e,[],[],xint)

disp('Press any key to continue.');
pause

clc
% Integer programming example, page 218 of Ecker & Kupferschmid

f = -[3 -7 -12];
a = [-3 6 8;6 -3 7;-6 3 3];
a = sparse(a);
b = [12 8 5];
e = [-1 -1 -1];
xint = [1 2 3];

[v,x] = lp_solve(f,a,b,e,[],[],xint)

disp('Press any key to continue.');
pause

clc
% 0-1 programming example, page 228 of Ecker & Kupferschmid

f = -[2 3 7 7];
a = [1 1 -2 -5;-1 2 1 4];
a = sparse(a);
b = [2 -3];
e = [1 1];
xint = [1 2 3 4];
vub = [1 1 1 1];

[v,x] = lp_solve(f,a,b,e,[],vub,xint)

disp('Press any key to continue.');
pause

clc
% 0-1 programming example, page 233 of Ecker & Kupferschmid

f = -[2 3 7 7];
a = [1 1 -2 5;-1 2 1 4];
a = sparse(a);
b = [2 -3];
e = [1 1];
xint = [1 2 3 4];
vub = [1 1 1 1];

[v,x] = lp_solve(f,a,b,e,[],vub,xint)

disp('Press any key to continue.');
pause


clc
% 0-1 programming example, page 238 of Ecker & Kupferschmid

f = -[1 2 3 7 8 8];
a = [5 -3 2 -3 -1 2; -1 0 2 1 3 -3;1 2 -1 0 5 -1];
b = [-5 -1 3];
e = [1 1 1];
xint = [1 2 3 4 5 6];
vub = [1 1 1 1 1 1];

[v,x] = lp_solve(f,sparse(a),b,e,[],vub,xint)

disp('Press any key to continue.');
pause

clc
% A knapsack problem

% Number of objects

n = 5;

% The problem is to maximize the sum of objects

f = ones(n,1);

% subject to a constraint not to fill up the bag

a = rand(1,n);
b = [1];
e = [-1];
xint = 1:n;
vub = ones(n,1);

[v,x] = lp_solve(f,sparse(a),b,e,[],vub,xint)

disp('Press any key to continue.');
pause


clc
% L1 Data fitting example with integer constraint on the intercept

% Generate data

n = 40;
t = (0:n-1)';
y = 3.5 -.2*t;
y = y + 0.5*randn(size(y));

m = [ones(n,1),t(:)];
a = [m,-m,speye(n)];
f = -[sum(m),sum(-m),2*ones(1,n)];
e = ones(n,1);

vub = [10 10, 10 10, 5*ones(1,n)];

[v,x] = lp_solve(f,sparse(a),y,e,[],vub,[1,3]);
p = x(1:2)-x(3:4);
err = y-m*p;

plot(t,y,'o',t,m*p);
xlabel('t');
ylabel('y');

disp('Press any key to continue.');
pause

clc;
% Now solve bigger problem

n = 200;
m = 100;
a = rand(m,n);
idx = find(a<0.8);
a(idx) = zeros(length(idx),1);
a = sparse(a);
z = rand(n,1);
b = a*z;

[v,x] = lp_solve(-ones(1,n),a,b,zeros(m,1));

plot(a*x-b);
title('Residuals');
xlabel('Equation Number');

echo off
