Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit e5025fd

Browse files
committed
Fix coding style issues
1 parent 00de02b commit e5025fd

File tree

4 files changed

+314
-318
lines changed

4 files changed

+314
-318
lines changed

src/free_refill.m

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
% MIT License
22
%
33
% Copyright (c) 2017 Guilherme Tadashi Maeoka
4-
% https://github.com/g117126unicamp/SimplexTwoPhase
4+
% https://github.com/g117126unicamp/SimplexTwoPhase
55
%
66
% Permission is hereby granted, free of charge, to any person obtaining a copy
77
% of this software and associated documentation files (the "Software"), to deal
@@ -18,68 +18,68 @@
1818
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
% SOFTWARE.
2020

21-
%%
22-
% Harvest identiy columns from A to B and fill with artificial variables.
2321
%%
22+
% Harvest identiy columns from A to B and fill with artificial variables.
23+
%%
2424
function [B, N, c_B, c_N, J, hasArtificial] = free_refill(A)
25-
[m, n] = size(A);
26-
mPn = m + n;
27-
28-
B = eye(m);
29-
N = [ ];
30-
c_B = zeros(1, m);
31-
c_N = [ ];
32-
J = [ zeros(1, mPn) ];
33-
34-
hasArtificial = false;
35-
36-
37-
c = 0;
38-
i = n;
39-
while(i >= 1 && c < m)
40-
[r, k] = recursive_is_identity_array(transpose(A(:, i)), m, false);
41-
42-
if(r && ~J(k))
43-
J(k) = i;
44-
c = c + 1;
45-
else
46-
J(m+i) = i;
47-
N = [A(:, i) N];
48-
end
49-
50-
i = i - 1;
51-
end
52-
53-
for i = i:-1:1
54-
J(m+i) = i;
55-
N = [A(:, i) N];
56-
end
57-
58-
59-
k = n;
60-
for i = 1:1:m
61-
if ~J(i)
62-
c_B(i) = 1;
63-
k = k + 1;
64-
J(i) = k;
65-
end
66-
end
67-
68-
% Test if artificial variables were introduced
69-
if k > n
70-
hasArtificial = true;
71-
end
72-
73-
74-
n_ = mPn; i = i + 1;
75-
while i <= n_
76-
if ~J(i)
77-
J(i) = [ ];
78-
n_ = n_ - 1;
79-
else
80-
i = i + 1;
81-
end
82-
end
83-
84-
c_N = zeros(1, n_);
25+
[m, n] = size(A);
26+
mPn = m + n;
27+
28+
B = eye(m);
29+
N = [ ];
30+
c_B = zeros(1, m);
31+
c_N = [ ];
32+
J = [ zeros(1, mPn) ];
33+
34+
hasArtificial = false;
35+
36+
37+
c = 0;
38+
i = n;
39+
while(i >= 1 && c < m)
40+
[r, k] = recursive_is_identity_array(transpose(A(:, i)), m, false);
41+
42+
if(r && ~J(k))
43+
J(k) = i;
44+
c = c + 1;
45+
else
46+
J(m+i) = i;
47+
N = [A(:, i) N];
48+
end
49+
50+
i = i - 1;
51+
end
52+
53+
for i = i:-1:1
54+
J(m+i) = i;
55+
N = [A(:, i) N];
56+
end
57+
58+
59+
k = n;
60+
for i = 1:1:m
61+
if ~J(i)
62+
c_B(i) = 1;
63+
k = k + 1;
64+
J(i) = k;
65+
end
66+
end
67+
68+
% Test if artificial variables were introduced
69+
if k > n
70+
hasArtificial = true;
71+
end
72+
73+
74+
n_ = mPn; i = i + 1;
75+
while i <= n_
76+
if ~J(i)
77+
J(i) = [ ];
78+
n_ = n_ - 1;
79+
else
80+
i = i + 1;
81+
end
82+
end
83+
84+
c_N = zeros(1, n_);
8585
end

src/recursive_is_identity_array.m

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
%%
2-
% Recursively verify if V is an identity array.
31
%%
2+
% Recursively verify if V is an identity array.
3+
%%
44
function [r, k] = recursive_is_identity_array(V, index, t)
5-
i = index;
6-
while(i > 0 && V(i) == 0)
7-
i = i - 1;
8-
end
9-
10-
k = i;
11-
if i == 0
12-
r = t;
13-
elseif V(i) ~= 1
14-
r = false;
15-
else
16-
if t
17-
r = false;
18-
else
19-
i = i - 1;
20-
r = recursive_is_identity_array(V, i, true);
21-
end
22-
end
5+
i = index;
6+
while(i > 0 && V(i) == 0)
7+
i = i - 1;
8+
end
9+
10+
k = i;
11+
if i == 0
12+
r = t;
13+
elseif V(i) ~= 1
14+
r = false;
15+
else
16+
if t
17+
r = false;
18+
else
19+
i = i - 1;
20+
r = recursive_is_identity_array(V, i, true);
21+
end
22+
end
2323
end

src/simplex_core.m

Lines changed: 87 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
% MIT License
22
%
33
% Copyright (c) 2017 Guilherme Tadashi Maeoka
4-
% https://github.com/g117126unicamp/SimplexTwoPhase
4+
% https://github.com/g117126unicamp/SimplexTwoPhase
55
%
66
% Permission is hereby granted, free of charge, to any person obtaining a copy
77
% of this software and associated documentation files (the "Software"), to deal
@@ -18,98 +18,93 @@
1818
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
% SOFTWARE.
2020

21-
%%
22-
% Dantzig's Simplex Algorithm
2321
%%
22+
% Dantzig's Simplex Algorithm
23+
%%
2424
function [B, N, J, x_B, chk] = simplex_core(B, N, b, c_B, c_N, J, v)
25+
chk = 0;
26+
[m, n] = size(N);
27+
28+
k = 1;
29+
while true
30+
% Basic feasible solution
31+
x_B = B \ b;
32+
33+
% Simplex multiplier
34+
w = c_B / B;
35+
36+
% Pricing operation
37+
zMc = [ ]; % zMc = z_k - c_k = wa_j - c_j
38+
for i = 1:1:n
39+
zMc(i) = w * N(:, i) - c_N(i);
40+
end
41+
42+
[c_k k_in] = max(zMc);
43+
44+
if c_k <= 0 % Test for optimality
45+
if v
46+
fprintf('\n');
47+
fprintf('Simplex complete\n');
48+
49+
if c_k == 0
50+
chk = 1;
51+
fprintf('Alternative optimal solutions found.\n\n\n');
52+
else
53+
fprintf('Optimal solution found.\n\n\n');
54+
end
55+
end
56+
return
57+
else
58+
y = B \ N(:, k_in); % Simplex ray
59+
60+
r = [ ]; j = 1;
61+
for i = 1:1:m
62+
if y(i) > 0
63+
r(j, 1) = x_B(i) / y(i); % Ratio
64+
r(j, 2) = i;
65+
j = j + 1;
66+
end
67+
end
68+
69+
70+
% Test unboundness
71+
if isempty(r)
72+
chk = 10;
73+
x_B = [ ];
74+
if v
75+
fprintf('\n');
76+
fprintf('Simplex stopped\n');
77+
fprintf('Unbounded optimal objective value.\n\n\n');
78+
end
79+
return
80+
else
81+
[r_k, i] = min(r(:, 1)); % Minimum ratio test
82+
k_out = r(i, 2);
83+
end
84+
end
85+
86+
87+
88+
% Switch columns
89+
if v
90+
fprintf('%d. z = %f; %d <-> %d\n', k, c_B*x_B, J(k_out), J(m+k_in));
91+
end
92+
93+
% B <-> N
94+
t_ = B(:, k_out);
95+
B(:, k_out) = N(:, k_in);
96+
N(:, k_in) = t_;
97+
98+
% c_B <-> c_N
99+
t_ = c_B(:, k_out);
100+
c_B(:, k_out) = c_N(:, k_in);
101+
c_N(:, k_in) = t_;
102+
103+
% J_B <-> J_N
104+
t_ = J(k_out);
105+
J(k_out) = J(m+k_in);
106+
J(m+k_in) = t_;
25107

26-
chk = 0;
27-
[m, n] = size(N);
28-
29-
30-
k = 1;
31-
while true
32-
% Basic feasible solution
33-
x_B = B \ b;
34-
35-
36-
% Simplex multiplier
37-
w = c_B / B;
38-
39-
40-
% Pricing operation
41-
zMc = [ ]; % zMc = z_k - c_k = wa_j - c_j
42-
for i = 1:1:n
43-
zMc(i) = w * N(:, i) - c_N(i);
44-
end
45-
46-
47-
[c_k k_in] = max(zMc);
48-
49-
if c_k <= 0 % Test for optimality
50-
if v
51-
fprintf('\n');
52-
fprintf('Simplex complete\n');
53-
54-
if c_k == 0
55-
chk = 1;
56-
fprintf('Alternative optimal solutions found.\n\n\n');
57-
else
58-
fprintf('Optimal solution found.\n\n\n');
59-
end
60-
end
61-
return
62-
else
63-
y = B \ N(:, k_in); % Simplex ray
64-
65-
r = [ ]; j = 1;
66-
for i = 1:1:m
67-
if y(i) > 0
68-
r(j, 1) = x_B(i) / y(i); % Ratio
69-
r(j, 2) = i;
70-
j = j + 1;
71-
end
72-
end
73-
74-
75-
% Test unboundness
76-
if isempty(r)
77-
chk = 10;
78-
x_B = [ ];
79-
if v
80-
fprintf('\n');
81-
fprintf('Simplex stopped\n');
82-
fprintf('Unbounded optimal objective value.\n\n\n');
83-
end
84-
return
85-
else
86-
[r_k, i] = min(r(:, 1)); % Minimum ratio test
87-
k_out = r(i, 2);
88-
end
89-
end
90-
91-
92-
93-
% Switch columns
94-
if v
95-
fprintf('%d. z = %f; %d <-> %d\n', k, c_B*x_B, J(k_out), J(m+k_in));
96-
end
97-
98-
% B <-> N
99-
t_ = B(:, k_out);
100-
B(:, k_out) = N(:, k_in);
101-
N(:, k_in) = t_;
102-
103-
% c_B <-> c_N
104-
t_ = c_B(:, k_out);
105-
c_B(:, k_out) = c_N(:, k_in);
106-
c_N(:, k_in) = t_;
107-
108-
% J_B <-> J_N
109-
t_ = J(k_out);
110-
J(k_out) = J(m+k_in);
111-
J(m+k_in) = t_;
112-
113-
k = k + 1;
114-
end
108+
k = k + 1;
109+
end
115110
end

0 commit comments

Comments
 (0)