-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathrdsa.m
More file actions
770 lines (769 loc) · 26.7 KB
/
Copy pathrdsa.m
File metadata and controls
770 lines (769 loc) · 26.7 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
function [xsol, fval, exitflag, iterations] = ...
rdsa(A, c, b, Eqin, MinMaxLP, c0, reinv, tole1, ...
tole2, tole3, scalingTechnique, basisUpdateMethod)
% Filename: rdsa.m
% Description: the function is an implementation of the
% revised dual simplex algorithm
% Authors: Ploskas, N., & Samaras, N.
%
% Syntax: [xsol, fval, exitflag, iterations] = ...
% rdsa(A, c, b, Eqin, MinMaxLP, c0, reinv, tole1, ...
% tole2, tole3, scalingTechnique, basisUpdateMethod)
%
% Input:
% -- A: matrix of coefficients of the constraints
% (size m x n)
% -- c: vector of coefficients of the objective function
% (size n x 1)
% -- b: vector of the right-hand side of the constraints
% (size m x 1)
% -- Eqin: vector of the type of the constraints
% (size m x 1)
% -- MinMaxLP: the type of optimization (optional:
% default value -1 - minimization)
% -- c0: constant term of the objective function
% (optional: default value 0)
% -- reinv: every reinv number of iterations, the basis
% inverse is re-computed from scratch (optional:
% default value 80)
% -- tole1: tolerance for the basic solution (optional:
% default value 1e-07)
% -- tole2: tolerance for the reduced costs (optional:
% default value 1e-09)
% -- tole3: tolerance for the pivoting column (optional:
% default value 1e-09)
% -- scalingTechnique: the scaling method to be used
% (0: no scaling, 1: arithmetic mean, 2: de Buchet for
% the case p = 1, 3: de Buchet for the case p = 2,
% 4: de Buchet for the case p = Inf, 5: entropy,
% 6: equilibration, 7: geometric mean, 8: IBM MPSX,
% 9: LP-norm for the case p = 1, 10: LP-norm for
% the case p = 2, 11: LP-norm for the case p = Inf)
% (optional: default value 6)
% -- basisUpdateMethod: the basis update method to be used
% (1: PFI, 2: MPFI) (optional: default value 1)
%
% Output:
% -- xsol: the solution found (size m x 1)
% -- fval: the value of the objective function at the
% solution x
% -- exitflag: the reason that the algorithm terminated
% (0: optimal solution found, 1: the LP problem is
% infeasible, 2: the LP problem is unbounded, -1: the
% input data is not logically or numerically correct)
% -- iterations: the number of iterations
% initialize output variables
xsol = [];
fval = 0;
exitflag = 0;
iterations = 0;
% set default values to missing inputs
if ~exist('MinMaxLP')
MinMaxLP = -1;
end
if ~exist('c0')
c0 = 0;
end
if ~exist('reinv')
reinv = 80;
end
if ~exist('tole1')
tole1 = 1e-7;
end
if ~exist('tole2')
tole2 = 1e-9;
end
if ~exist('tole3')
tole3 = 1e-9;
end
if ~exist('scalingTechnique')
scalingTechnique = 6;
end
if ~exist('basisUpdateMethod')
basisUpdateMethod = 1;
end
[m, n] = size(A); % find the size of matrix A
[m2, n2] = size(c); % find the size of vector c
[m3, n3] = size(Eqin); % find the size of vector Eqin
[m4, n4] = size(b); % find the size of vector b
% check if input data is logically correct
if n2 ~= 1
disp('Vector c is not a column vector.')
exitflag = -1;
return
end
if n ~= m2
disp(['The number of columns in matrix A and ' ...
'the number of rows in vector c do not match.'])
exitflag = -1;
return
end
if m4 ~= m
disp(['The number of the right-hand side values ' ...
'is not equal to the number of constraints.'])
exitflag = -1;
return
end
if n3 ~= 1
disp('Vector Eqin is not a column vector')
exitflag = -1;
return
end
if n4 ~= 1
disp('Vector b is not a column vector')
exitflag = -1;
return
end
if m4 ~= m3
disp('The size of vectors Eqin and b does not match')
exitflag = -1;
return
end
% if the type of optimization is maximization, then multiply
% vector c and constant c0 by -1
if MinMaxLP == 1
c = -c;
c0 = -c0;
end
% perform the presolve analysis
disp('---- P R E S O L V E A N A L Y S I S ----')
[A, c, b, Eqin, c0, infeasible, unbounded] = ...
presolve(A, c, b, Eqin, c0);
if infeasible == 1 % the LP problem is infeasible
disp('The LP problem is infeasible')
exitflag = 1;
return
end
if unbounded == 1 % the LP problem is unbounded
disp('The LP problem is unbounded')
exitflag = 2;
return
end
[m, n] = size(A); % find the new size of matrix A
[m2, ~] = size(c); % find the new size of vector c
[m3, ~] = size(Eqin); % find the size of vector Eqin
% scale the LP problem using the selected scaling
% technique
disp('---- S C A L I N G ----')
if scalingTechnique == 1 % arithmetic mean
[A, c, b, ~, ~] = arithmeticMean(A, c, b);
% de buchet for the case p = 1
elseif scalingTechnique == 2
[A, c, b, ~, ~] = debuchet1(A, c, b);
% de buchet for the case p = 2
elseif scalingTechnique == 3
[A, c, b, ~, ~] = debuchet2(A, c, b);
% de buchet for the case p = Inf
elseif scalingTechnique == 4
[A, c, b, ~, ~] = debuchetinf(A, c, b);
elseif scalingTechnique == 5 % entropy
[A, c, b, ~, ~] = entropy(A, c, b);
elseif scalingTechnique == 6 % equilibration
[A, c, b, ~, ~] = equilibration(A, c, b);
elseif scalingTechnique == 7 % geometric mean
[A, c, b, ~, ~] = geometricMean(A, c, b);
elseif scalingTechnique == 8 % IBM MPSX
[A, c, b, ~, ~, ~, ~] = ibmmpsx(A, c, b);
% LP-norm for the case p = 1
elseif scalingTechnique == 9
[A, c, b, ~, ~] = lpnorm1(A, c, b);
% LP-norm for the case p = 2
elseif scalingTechnique == 10
[A, c, b, ~, ~] = lpnorm2(A, c, b);
% LP-norm for the case p = Inf
elseif scalingTechnique == 11
[A, c, b, ~, ~] = lpnorminf(A, c, b);
end
% find an invertible basis
disp('---- I N I T I A L B A S I S ----')
flag = isequal(Eqin, zeros(m3, 1));
if flag == 1 % all constraints are equalities
% select an initial invertible basis using lprref
% function
[~, ~, jb, out, ~, exitflag] = lprref(A, ...
b, Eqin, 1e-10);
if exitflag == 1 % the LP problem is infeasible
disp('The LP problem is infeasible')
return
end
% create the basic and nonbasic lists
BasicList = jb;
NonBasicList = setdiff(1:n, BasicList);
% delete redundant constraints found by lprref
A(out, :) = [];
b(out, :) = [];
Eqin(out, :) = [];
else % some or all constraints are inequalities
% add slack variables
axm = nnz(Eqin);
c(m2 + 1:m2 + axm, :) = 0;
A(:, n + 1:n + axm) = sparse(m, axm);
curcol = 1;
for i = 1:m3
% 'greater than or equal to' inequality constraint
if Eqin(i, 1) == 1
A(i, n + curcol) = -1;
curcol = curcol + 1;
% 'less than or equal to' inequality constraint
elseif Eqin(i, 1) == -1
A(i, n + curcol) = 1;
curcol = curcol + 1;
% unrecognized type of constraint
elseif Eqin(i,1) ~= 0
disp('Vector Eqin is not numerically correct.')
exitflag = -1;
return
end
end
% select an initial invertible basis using lprref
% function
[~, ~, jb, out, ~, exitflag] = lprref(A, ...
b, Eqin, 1e-10);
if exitflag == 1 % the LP problem is infeasible
disp('The LP problem is infeasible')
return
end
% create the basic and nonbasic lists
[~, y1] = size(A);
temp = n + 1:y1;
for i = 1:length(temp)
jb(length(jb) + 1) = temp(i);
end
BasicList = sort(jb);
NonBasicList = setdiff(1:y1, BasicList);
% delete redundant constraints found by lprref
A(out, :) = [];
b(out, :) = [];
Eqin(out, :) = [];
end
flag = 0;
[m1, n1] = size(A); % new size of matrix A
% calculate the density of matrix A
density = (nnz(A) / (m1 * n1)) * 100;
% if the density of matrix A is less than 20%, then
% use sparse algebra for faster computations
if density < 20
A = sparse(A);
c = sparse(c);
b = sparse(b);
end
% preallocate memory for variables that hold large
% amounts of data
Xb = spalloc(m1, 1, m1); % basic solution
h_l = spalloc(m1, 1, m1); % pivoting column
% the matrix of the basic variables
Basis = spalloc(m1, length(BasicList), m1 ...
* length(BasicList));
% the matrix of the nonbasic variables
N = spalloc(m1, length(NonBasicList), m1 ...
* length(NonBasicList));
w = spalloc(1, n1, n1); % simplex multiplier
Sn = spalloc(1, n1, n1); % reduced costs
% initialize data
% the matrix of the basic variables
Basis = A(:, BasicList);
% the coefficients of the objective function for the
% basic variables
cb = c(BasicList);
% the matrix of the nonbasic variables
N = A(:, NonBasicList);
% the coefficients of the objective function for the
% nonbasic variables
cn = c(NonBasicList);
% calculate the basis inverse
BasisInv = inv(Basis);
Xb = BasisInv * b; % basic solution
% set to zero, the values of Xb that are less than or
% equal to tole1
toler = abs(Xb) <= tole1;
Xb(toler == 1) = 0;
w = cb' * BasisInv; % calculate the simplex multiplier
% calculate the reduced costs
Sn = sparse(cn' - w * N);
% set to zero, the values of Sn that are less than or
% equal to tole2
toler = abs(Sn) <= tole2;
Sn(toler == 1) = 0;
% check if the current basis is dual feasible
if all(Sn >= 0) % the solution is dual feasible, skip big-M
% method and proceed to the dual simplex algorithm
flag = 1;
else % the solution is not dual feasible, apply big-M method
flag = 0;
end
counter = 1;
if flag == 0 % modified big-M method
disp(['---- D U A L W I T H B I G - M ' ...
'M E T H O D ----'])
% find the entering variable
[p, t] = min(Sn);
rr = find(Sn == p);
% break the ties in order to avoid stalling
if length(rr) > 1
l = NonBasicList(rr);
[l, ~] = max(l);
else
l = NonBasicList(t);
end
% add a constraint and an artificial variable
A(m1 + 1, :) = sparse(1, n1);
A(m1 + 1, NonBasicList) = 1;
A = [A [sparse(m1, 1); 1]];
c = [c; 0];
b = [b; 0];
% the right-hand side of the big-M method
bM = [sparse(m1, 1); 1];
% compute the new basic and nonbasic variables
BasicList(m1 + 1) = l;
NonBasicList(NonBasicList == l) = n1 + 1;
% the artificial variable is in the nonbasic list
artificialVariableInN = 1;
Basis = A(:, BasicList); % the new Basis
% the matrix of the nonbasic variables
N = A(:, NonBasicList);
% re-compute the inverse of the matrix
BasisInv = inv(Basis);
% compute the basic solution for the original problem
% and the big-M problem
Xb = BasisInv * b;
% set to zero, the values of Xb that are less than or
% equal to tole1
toler = abs(Xb) <= tole1;
Xb(toler == 1) = 0;
XbM = BasisInv * bM;
% set to zero, the values of XbM that are less than or
% equal to tole1
toler = abs(XbM) <= tole1;
XbM(toler == 1) = 0;
% compute the simplex multiplier
w = c(BasicList)' * BasisInv;
HRN = w * N;
% set to zero, the values of HRN that are less than or
% equal to tole2
toler = abs(HRN) <= tole2;
HRN(toler == 1) = 0;
% compute the reduced costs
Sn = c(NonBasicList)' - HRN;
% set to zero, the values of Sn that are less than or
% equal to tole2
toler = abs(Sn) <= tole2;
Sn(toler == 1) = 0;
while flag == 0 % iterate in big-M method
% optimality test
if all(XbM > 0)
col = find(BasicList == (n1 + 1), 1);
if ~isempty(col)
% if the artificial variable is in the
% basic list, the problem is optimal
% calculate the value of the objective
% function
if MinMaxLP == 1 % maximization
fval = full(-((c(BasicList))' * Xb ...
+ c0));
else % minimization
fval = full((c(BasicList))' * Xb ...
+ c0);
end
exitflag = 0;
xsol = Xb;
iterations = iterations + 1;
disp('The LP problem is optimal')
return
else
% if the artificial variable is not in
% the basic list, check if the reduced
% cost of the artificial variable is
% equal to zero
% compute the simplex multiplier
w = c(BasicList)' * BasisInv;
HRN = w * A;
% set to zero, the values of HRN that are
% less than or equal to tole2
toler = abs(HRN) <= tole2;
HRN(toler == 1) = 0;
% compute the reduced costs
S = c' - HRN;
% set to zero, the values of S that are
% less than or equal to tole2
toler = abs(S) <= tole2;
S(toler == 1) = 0;
if S(n1 + 1) == 0 % if the reduced cost of
% the artificial variable is equal to
% zero, then the problem is optimal
% calculate the value of the objective
% function
if MinMaxLP == 1 % maximization
fval = full(-((c(BasicList))' * Xb ...
+ c0));
else % minimization
fval = full((c(BasicList))' * Xb ...
+ c0);
end
exitflag = 0;
xsol = Xb;
iterations = iterations + 1;
disp('The LP problem is optimal')
return
else % the problem is unbounded
disp('The LP problem is unbounded')
exitflag = 2;
iterations = iterations + 1;
return
end
end
elseif all(XbM >= 0) % optimality test
row = find(XbM == 0);
if ~isempty(row)
if all(Xb(row) >= 0)
col = find(BasicList == (n1 + 1));
if (~isempty(col) && XbM(col) == 0 && ...
Xb(col) == 0) || isempty(col)
% if the artificial variable is in
% the basic list, check if the reduced
% cost of the artificial variable is
% equal to zero
% compute the simplex multiplier
w = c(BasicList)' * BasisInv;
HRN = w * A;
% set to zero, the values of HRN that
% are less than or equal to tole2
toler = abs(HRN) <= tole2;
HRN(toler == 1) = 0;
% compute the reduced costs
S = c' - HRN;
% set to zero, the values of S that
% are less than or equal to tole2
toler = abs(S) <= tole2;
S(toler == 1) = 0;
if S(n1 + 1) == 0 % if the reduced
% cost of the artificial variable
% is equal to zero, then the
% problem is optimal
% calculate the value of the
% objective function
if MinMaxLP == 1 % maximization
fval = full(-((c(BasicList))' ...
* Xb + c0));
else % minimization
fval = full((c(BasicList))' ...
* Xb + c0);
end
exitflag = 0;
xsol = Xb;
iterations = iterations + 1;
disp('The LP problem is optimal')
return
else % the problem is unbounded
disp('The LP problem is unbounded')
exitflag = 2;
iterations = iterations + 1;
return
end
elseif (~isempty(col) && XbM(col) > 0) ...
|| Xb(col) > 0
% the problem is optimal
% calculate the value of the objective
% function
if MinMaxLP == 1 % maximization
fval = full(-((c(BasicList))' * Xb ...
+ c0));
else % minimization
fval = full((c(BasicList))' * Xb ...
+ c0);
end
exitflag = 0;
xsol = Xb;
iterations = iterations + 1;
disp('The LP problem is optimal')
return
end
else % the artificial variable left the
% nonbasic list
artificialVariableInN = 0;
end
end
end
% if the artificial variable is in the nonbasic list,
% use XbM to select the leaving variable
if artificialVariableInN == 1
% find the leaving variable
mrt = find(XbM < 0);
[a, r] = min(XbM(mrt));
rr = find((XbM(mrt)) == a);
% break the ties in order to avoid stalling
if length(rr) > 1
r = mrt(rr);
% index of the leaving variable
k = BasicList(r);
[k, ~] = max(k);
r = find(BasicList == k);
else
r = mrt(r);
% index of the leaving variable
k = BasicList(r);
end
else % if the artificial variable is not in the
% nonbasic list, use Xb to select the
% leaving variable
mrt = Xb(row) < 0;
mrt = row(mrt);
[a, r] = min(Xb(mrt));
rr = find((Xb(mrt)) == a);
% break the ties in order to avoid stalling
if length(rr) > 1
r = mrt(rr);
k = BasicList(r);
[k, ~] = max(k);
% index of the leaving variable
r = find(BasicList == k);
else
r = mrt(r);
% index of the leaving variable
k = BasicList(r);
end
end
% compute HRN vector
HRN = BasisInv(r, :) * N;
% set to zero, the values of HRN that are less than
% orequal to tole2
toler = abs(HRN) <= tole2;
HRN(toler == 1) = 0;
mrt = find(HRN < 0);
if isempty(mrt) % if there is not any candidate
% to enter the basic list, then the problem
% is infeasible
exitflag = 1;
disp('The problem is infeasible')
return;
end
% perform the minimum ratio test to select
% the entering variable
[a, t] = min(-Sn(mrt) ./ HRN(mrt));
rr = find(-Sn(mrt) ./ HRN(mrt) == a);
% break the ties in order to avoid stalling
if length(rr)>1
l = NonBasicList(mrt(rr));
% index of the entering variable
[l, ~] = max(l);
else
% index of the entering variable
l = NonBasicList(mrt(t));
end
% calculate the pivoting column
h_l = BasisInv * A(:, l);
% set to zero, the values of h_l that are less than
% or equal to tole3
toler = abs(h_l) <= tole3;
h_l(toler == 1) = 0;
% check if the problem is unbounded
if all(h_l <= 0)
disp('The LP problem is unbounded')
exitflag = 2;
iterations = iterations + 1;
return
end
% pivoting and update vectors and matrices
f = Xb(r);
fM = XbM(r);
t = find(NonBasicList == l);
g = h_l(r);
v = Sn(t);
% update the basic and the nonbasic lists
BasicList(r) = l;
NonBasicList(t) = k;
% update the matrix of the nonbasic variables
N = A(:, NonBasicList);
iterations = iterations + 1;
% calculate the new basic solution
Xb(r) = 0;
XbM(r) = 0;
h_l2 = h_l;
h_l2(r) = -1;
if artificialVariableInN == 1 % the artificial
% variable is in the nonbasic list, so
% update XbM
XbM = XbM - (fM / g) * h_l2;
% set to zero, the values of XbM that are less
% than or equal to tole1
toler = abs(XbM) <= tole1;
XbM(toler == 1) = 0;
end
Xb = Xb - (f / g) * h_l2;
% set to zero, the values of Xb that are less than
% or equal to tole1
toler = abs(Xb) <= tole1;
Xb(toler == 1) = 0;
% update the reduced costs
Sn(t) = 0;
HRN(t) = 1;
Sn = Sn - (v / g) * HRN;
% set to zero, the values of Sn that are less than
% or equal to tole2
toler = abs(Sn) <= tole2;
Sn(toler == 1) = 0;
% basis inverse
if iterations == counter * reinv
% recompute the inverse of the basis
% from scratch every reinv iterations
BasisInv = inv(A(:, BasicList));
counter = counter + 1;
h_l(r) = g;
else
% basis update according to the
% selected basis update method
if basisUpdateMethod == 1 % pfi
BasisInv = pfi(BasisInv, h_l, r);
else % mpfi
BasisInv = mpfi(BasisInv, h_l, r);
end
h_l(r) = -1;
end
% print intermediate results every 100
% iterations
if mod(iterations, 100) == 0
% calculate the value of the objective
% function
if MinMaxLP == 1 % maximization
fval = full(-(c(BasicList)' * Xb) + c0);
else % minimization
fval = full(c(BasicList)' * Xb + c0);
end
fprintf(['Iteration %i - objective value: ' ...
'%f\n'], iterations, fval);
end
end
end
if flag == 1 % dual simplex method
disp('---- D U A L ----')
while flag == 1
% optimality test
if all(Xb >= 0) % the problem is optimal
% calculate the value of the objective
% function
if MinMaxLP == 1 % maximization
fval = full(-((c(BasicList))' * Xb + c0));
else % minimization
fval = full((c(BasicList))' * Xb + c0);
end
exitflag = 0;
xsol = Xb;
iterations = iterations + 1;
disp('The LP problem is optimal')
return
end
% find the leaving variable
mrt = find(Xb < 0);
[a, r] = min(Xb(mrt));
rr = find(Xb(mrt) == a);
% break the ties in order to avoid stalling
if length(rr) > 1
r = mrt(rr);
% index of the leaving variable
k = BasicList(r);
[k, ~] = max(k);
r = find(BasicList == k);
else
r = mrt(r);
% index of the leaving variable
k = BasicList(r);
end
% compute HRN vector
HRN = BasisInv(r, :) * N;
% set to zero, the values of HRN that are less than
% or equal to tole3
toler = abs(HRN) <= tole2;
HRN(toler == 1) = 0;
mrt = find(HRN < 0);
if isempty(mrt) % if there is not any candidate
% to enter the basic list, then the problem
% is infeasible
exitflag = 1;
disp('The problem is infeasible')
return;
end
% perform the minimum ratio test to select
% the entering variable
[a, t] = min(-Sn(mrt) ./ HRN(mrt));
rr = find(-Sn(mrt) ./ HRN(mrt) == a);
% break the ties in order to avoid stalling
if length(rr)>1
l = NonBasicList(mrt(rr));
% index of the entering variable
[l, ~] = max(l);
else
% index of the entering variable
l = NonBasicList(mrt(t));
end
% calculate the pivoting column
h_l = BasisInv * A(:, l);
% set to zero, the values of h_l that are less than
% or equal to tole3
toler = abs(h_l) <= tole3;
h_l(toler == 1) = 0;
% check if the problem is unbounded
if all(h_l <= 0)
disp('The LP problem is unbounded')
exitflag = 2;
iterations = iterations + 1;
return
end
% pivoting and update vectors and matrices
f = Xb(r);
t = find(NonBasicList == l);
g = h_l(r);
% update the basic and the nonbasic lists
BasicList(r) = l;
NonBasicList(t) = k;
% update the matrix of the nonbasic variables
N = A(:, NonBasicList);
iterations = iterations + 1;
% calculate the new basic solution
Xb(r) = 0;
h_l2 = h_l;
h_l2(r) = -1;
Xb = Xb - (f / g) * h_l2;
% set to zero, the values of Xb that are less than
% or equal to tole1
toler = abs(Xb) <= tole1;
Xb(toler == 1) = 0;
% update the reduced costs
Sn(t) = 0;
HRN(t) = 1;
Sn = Sn + a * HRN;
% set to zero, the values of Sn that are less than
% or equal to tole2
toler = abs(Sn) <= tole2;
Sn(toler == 1) = 0;
% basic inverse
if iterations == counter * reinv
% recompute the inverse of the basis
% from scratch every reinv iterations
BasisInv = inv(A(:, BasicList));
counter = counter + 1;
h_l(r) = g;
else
% basis update according to the
% selected basis update method
if basisUpdateMethod == 1 % pfi
BasisInv = pfi(BasisInv, h_l, r);
else % mpfi
BasisInv = mpfi(BasisInv, h_l, r);
end
h_l(r) = -1;
end
% print intermediate results every 100
% iterations
if mod(iterations, 100) == 0
% calculate the value of the objective
% function
if MinMaxLP == 1 % maximization
fval = full(-(c(BasicList)' * Xb) + c0);
else % minimization
fval = full(c(BasicList)' * Xb + c0);
end
fprintf(['Iteration %i - objective value: ' ...
'%f\n'], iterations, fval);
end
end
end
end