forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplexcode.m
28 lines (24 loc) · 1.03 KB
/
simplexcode.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
%8.6 simplexcode.m
function [x,y,cost] = simplex(A,b,c,basis)
disp(basis);
B=A(:,basis); x(basis) = B\b; % xpos at starting corner
if any (x(basis) < -.000001)
error('Starting x has a component < 0')
end
cost = c(basis)'*x(basis); % cost at starting corner
for iter = 1:100
y = B'\c(basis); % this y may not be feasible
[rmin,in] = min(c - A'*y); % minimum r and its index in
if rmin >= -.00000001 % optimality is reached, r>=0
break; % current x and y are optimal
end
v(basis) = B\A(:,in); % decrease in x from 1 unit of xin
[minratio,out] = min(x(basis)./max(v(basis),.000001));
if v(out) == .000001 % out = index of first x to reach 0
break; % break when that edge is extremely short
end
cost = cost + minratio*rmin; % lower cost at end of step
x(basis) = x(basis) - minratio*v(basis); % update old x
x(in) = minratio; % find new positive component of x
basis(out) = in; % replace old index by new in basis
end