-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSimplexMatlab.m
More file actions
82 lines (76 loc) · 2.48 KB
/
SimplexMatlab.m
File metadata and controls
82 lines (76 loc) · 2.48 KB
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
78
79
80
81
82
function [fval,x] =SimplexMatlab(c, A, b )
% % ----------- Simplex Implementation in Matlab ------------
%=====================================
%% ---LP Problem --------
%-------- Max c'x ---------
% ------- S.t. Ax = b
% ------- x>=0
% --------- c and b are column Vectors and A is Matrix-----
%% ----- Intial Basic Feasible Solution --------------
[m,n] = size (A);
% -------- Set Indices of Initial basic variable B default to last m columns of A
IB = n-m+1:n;
% ------ initialize x vector
x = zeros (n,1);
%-------- find first basic solution solution
B = A(:,IB); % Intial Basic variables
x(IB) = B\b; % Intial basic solution of x
%-------- calculate the function value
fval = c'*x ;
%% ========== condition check for optimality and next iteration
% ----- ---cost reduction calculation with current basic --------
cbar = c' - c(IB)'/B*A ;
% find the entering varble
[cbarval, ex] = max(cbar);
% ex ----- entering basic variable -----
if (cbarval <=0)
disp('Optimality rached')
end
%% ------ If optimality condition has not reached then subsequent steps
iter = 0;
while (cbarval >0)
iter = iter+1;
disp('-------------------------------------------------------------');
disp('Iteration:');
disp(iter);
disp('entering varible: x');
disp(ex);
% calculate the direction 'D'
% intialize the direction first
D = zeros(n,1);
% set the direction of basic variables
D(IB) = -B\A(:,ex);
% set the direction of entering variable
D(ex) =1;
disp('DIrection:');
disp(D);
% ---------- finding leaving variable ------------------------------
theta = -x(IB)./D(IB);
% --- Discarding theta with negative value ----------------
for i =1:m
if theta(i) <= 0
theta (i) = inf;
end
end
% --- Taking minimum theta to abide non- negativity constrained of x
% --- lx is the leaving basic variables -----
[theta,lx] = min(theta);
disp('leaving variable is: x');
disp(IB(lx));
%---- update basic variable indices i.e. repace lx with ex
IB(lx) = ex;
%---- Update X--------------
x = x+ theta*D
% ----- Update Basic Matrix ------
B= A(:,IB);
% --- Calculate function value ----------
fval = c'*x;
cbar = c' - c(IB)'/B*A
% find the entering varble
[cbarval,ex] = max(cbar);
%ex = IB(z1);
% if (cbar< 0)
% optim =1;
% end
% %disp ('entering variable :x',ex);
end