This was my work in developing the plotregion function in Julia
In [4]:
# Pkg.add("QuickHull")
using QuickHull
using Combinatorics
In [10]:
collect(combinations(1:5,3))
Out[10]:
In [11]:
setdiff(1:5,[1,2,3])
Out[11]:
%% Define the LP
% minimize -x1 - 2*x2
% subject to -2*x1 + x2 <= 2
% -x1 + 2*x2 <= 7
% x1 <= 3
% x1, x2 >= 0
%
% Except, we need this in standard form:
% minimize f'*x subject to. A*x <= b
c = [-1 -2];
A = [-2 1; -1 2; 1 0];
b = [2; 7; 3];
lb = [0; 0];
ub = []; % No upper bound
%% Find the solution using linprog
x = linprog(c,A,b,[],[],lb);
%% Define the full LP with all the slacks.
c = [-1 -2 0 0 0];
A1 = [-2 1; -1 2; 1 0];
b = [2; 7; 3];
A = [A1 eye(3)];
%x = linprog(c,[],[],A,b,zeros(5,1));
In [13]:
function basic_feasible_point(A::Matrix,b::Vector,set::Vector)
m,n = size(A)
@assert length(set) == m "need more indices to define a BFP"
binds = set # basic variable indices
ninds = setdiff(1:size(A,1),binds) # non-basic
B = A[:,binds]
N = A[:,ninds]
#cb = c[binds]
#cn = c[ninds]
xb = B\b
x = zeros(eltype(xb),n)
x[binds] = xb
x[ninds] = zero(eltype(xb))
if any(xb .< 0)
return (:Infeasible, x)
else
#lam = B'\cb
#sn = cn - N'*lam
return (:Feasible, x)
end
end
A1 = [-2.0 1; -1 2; 1 0]
b = [2.0; 7; 3]
A = [A1 eye(3)]
basic_feasible_point(A,b,[1,2,3])
In [ ]:
In [ ]:
"""
Plot the feasible polytope
Ax = b, x >= 0
for the first two components of x.
"""
function plotregion(A::Matrix,b::Vector)
m,n = size(A)
for inds in combinations(1:n,m)
bfp=basic_feasible_point(A,b,inds)
%% Look at all vertices
nchoosek(5,3)
sets = nchoosek(1:5,3);
xlim([-10 10]);
ylim([0,8]);
for i=1:size(sets,1)
lam = B'\cb';
sn = cn' - N'*lam;
x = zeros(5,1);
x(bi) = xb;
x(ni) = 0;
fprintf('set %i, %i, %i is feasible %s \n', SB(1), SB(2), SB(3), num2str(x'));
sn
plot(x(1),x(2),'*','MarkerSize',25);
end
end