-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimplex.m
77 lines (76 loc) · 1.9 KB
/
Simplex.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function [x,y,B,flg,loop] = Simplex(A,b,c,B,loop)
%
% this function solves the Phase II LP
% with the simplex method
%
% first set up the simplex tableau
%
format short g;
f = ['Starting Phase II Simplex Iteration... '];
flg = nan;
%
%
[T,x,y] = GetT(A,b,c,B);
obj = transpose(c)*x;
[m,n] = size(A);
disp(['Initial Objective = ', num2str(obj)]);
%
ITER = 0;
simplex = 1;
%
while (simplex == 1)
%
% determine the next s and r values.
%
loop = loop + 1;
y = transpose(T(end,2:end));
[zmin,s] = min(c-transpose(A)*y);
%
% check for convergence.
%
if (abs(zmin) < 1e-14)
disp('Simplex Method has converged');
simplex = 0;
% disp('Displaying Optimal Basis');
% disp(transpose(B));
x = zeros(n,1);
x(B) = T(1:end-1,1);
obj = transpose(c)*x;
disp(['Optimal Objective = ', num2str(obj),' after ', num2str(ITER), ' iterations']);
% disp('Displaying Optimal solution x, c-A^T*y and their componentwise product');
% disp([x c-transpose(A)*y x.*(c-transpose(A)*y)]);
continue;
end
t = T(1:end-1,2:end)*A(:,s);
[flg,r] = Revisedgetr(n,s,B,T,t);
if (flg == 1)
disp('LP is degenerate');
simplex = 0;
continue;
end
if (r < 1)
disp('LP has no lower bound');
simplex = 0;
flg = - 1;
continue;
end
x = zeros(n,1);
x(B) = T(1:end-1,1);
ITER = ITER + 1;
% f = ['Iteration ', num2str(ITER), ' Obj ', num2str(transpose(c)*x), '. Smallest component in c-A^T*y: ', ...
% num2str(zmin), ' at s =', num2str(s), '. Component r = ', num2str(r), ' out of basis'];
%
% disp('Current solution index set')
% disp(transpose(B))
% disp(f);
%
% update the revised simplex tableau.
%
[T,B1,flg]=RevisedSimplexTableau(B,r,s,t,zmin,T);
if (flg == 1)
disp('LP is degenerate');
simplex = 0;
continue;
end
B = B1;
end