MTask cpp Defines the entry point for the console application include

  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
771
772
773
774
775
776
777
778
// MTask.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <vector>
#include <sstream>
#include <string>
#include <math.h>
/****************************************************************************
* Îáåðòêà äëÿ âåùåñòâåííûõ ÷èñåë. Íóæíà äëÿ M-çàäà÷è.
* Òàêîå ÷èñëî ïðåäñòàâëÿåò äâå êîìïîíåíòû - âåùåñòâåííóþ ÷àñòü
* è êîýôôèöèåíò ïðè íåêîòîðîì ÷èñëå M.
*/
class number {
private:
// âåùåñòâåííàÿ ÷àñòü
double real;
// êîýôôèöèåò ïðè M
double coef;
// ôëàã "íå ÷èñëî"
bool is_nan;
public:
/**
* Êîíñòðóêòîð.
* r - âåùåñòâåííàÿ êîìïîíåíòà
* c - êîýôôèöèåíò ïðè M
* num - åñëè ýòîò àðãóìåíò áóäåò false, òî ÷èñëî ñ÷èòàåòñÿ êàê NaN
*/
number(double r = 0, double c = 0, bool num = true) {
real = r;
coef = c;
is_nan = ! num;
}
double get_real() {
return real;
}
double get_coef() {
return coef;
}
/**
* Äàëåå èäóò ìåòîäû ïåðåãðóçêè àðèôìåòè÷åñêèõ îïåðàòîðîâ.
*/
number operator+(number num) {
number res;
res.real = real + num.real;
res.coef = coef + num.coef;
return res;
}
void operator+=(number num) {
real += num.real;
coef += num.coef;
}
number operator-(number num) {
number res;
res.real = real - num.real;
res.coef = coef - num.coef;
return res;
}
void operator-=(number num) {
real -= num.real;
coef -= num.coef;
}
number operator*(number num) {
number res;
res.real = real * num.real;
if (real != 0 && coef == 0 && num.coef != 0) {
res.coef = real * num.coef;
} else if (num.real != 0 && num.coef == 0 && coef != 0) {
res.coef = coef * num.real;
}
return res;
}
void operator*=(number num) {
real *= num.real;
if (real != 0 && coef == 0 && num.coef != 0) {
coef = real * num.coef;
} else if (num.real != 0 && num.coef == 0 && coef != 0) {
coef = coef * num.real;
}
}
number operator/(number num) {
number res;
res.real = real / num.real;
return res;
}
void operator/=(number num) {
real /= num.real;
}
bool operator!=(number num) {
return (real != num.real && coef != num.coef);
}
bool operator!=(double num) {
return (real != num);
}
bool operator<(number num) {
if (coef != num.coef) {
return (coef < num.coef);
}
return (real < num.real);
}
bool operator>(number num) {
if (coef != num.coef) {
return (coef > num.coef);
}
return (real > num.real);
}
bool operator==(number num) {
return (real == num.real && coef == num.coef);
}
bool operator>=(number num) {
return this->operator>(num) || this->operator==(num);
}
bool operator<=(number num) {
return this->operator<(num) || this->operator==(num);
}
/**
* Âûâîä â âèäå ñòðîêè â ôîðìàòå
* [R][+,-][C]M
* ãäå R - âåùåñòâåííàÿ ÷àñòü,
* C - êîýôôèöèåíò ïðè M.
*/
std::string str() {
if (is_nan) {
return std::string("NaN");
}
if (real == 0 && coef == 0) {
return std::string("0");
}
std::stringstream ss;
ss << std::setprecision(2);
if (real != 0) {
ss << real;
}
if (coef != 0) {
if (real != 0 && coef > 0) {
ss << '+';
} else if (coef < 0) {
ss << '-';
}
if (coef != 1 && coef != -1) {
ss << abs(coef);
}
ss << 'M';
}
return ss.str();
}
/**
* Âîçâðàøàåò äðîáíóþ ÷àñòü âåùåñòâåííîé êîìïîíåíòû
*/
number fract() {
return (real > 0) ? number(real - floor(real)) : number(abs(floor(real)) + (real));
}
/**
* Ïðîâåðÿåò ÿâëÿåòñÿ ëè ÷èñëî (ïî÷òè) öåëûì
*/
bool is_integer() {
return this->fract() < 0.00001;
}
/**
* Ìîäóëü ÷èñëà
*/
number absolute() {
return number(fabs(real));
}
};
/****************************************************************************
* Íåñêîëüêî òàéïäåôîâ
*/
typedef std::vector<number> vector_nums;
typedef std::vector<int> vector_ints;
typedef std::vector<std::vector<number> > matrix_nums;
/****************************************************************************
* Íåñêîëüêî ôóíêöèé äëÿ âûâîäà â êîíñîëü
*/
void print_line() {
std::cout << std::endl;
for (int i = 0; i < 80; ++i) {
std::cout << (char) 0xCD;
}
std::cout << std::endl;
}
/**
* Ïå÷àòü ñòðîêè
*/
void print_str(char* msg) {
std::cout << std::endl << msg << std::endl;
}
/**
* Ïå÷àòàåò ÷èñëî
*/
void print_num(char* msg, number n) {
std::cout << std::endl << msg << n.str() << std::endl;
}
/**
* Ïå÷àòàåò âåùåñòâåííîå ÷èñëî
*/
void print_num(char* msg, double n) {
std::cout << std::endl << msg << n << std::endl;
}
/**
* Ïå÷àòàåò öåëîå ÷èñëî
*/
void print_num(char* msg, int n) {
std::cout << std::endl << msg << n << std::endl;
}
/**
* Ïå÷àåò âåêòîð
*/
void print_vector(char* msg, vector_nums v) {
int i, j;
int width = 8;
std::cout << std::endl << msg << std::endl << (char) 0xDA;
for (i = 0; i < v.size(); ++i) {
for (j = 0; j < width - 1; ++j) {
std::cout << (char) 0xC4;
}
if (i < v.size() - 1) {
std::cout << (char) 0xC2;
}
}
std::cout << (char) 0xBF << std::endl << (char) 0xB3;
for (i = 0; i < v.size(); ++i) {
std::cout << std::setw(width - 1) << v[i].str() << (char) 0xB3;
}
std::cout << std::endl << (char) 0xC0;
for (i = 0; i < v.size(); ++i) {
for (j = 0; j < width - 1; ++j) {
std::cout << (char) 0xC4;
}
if (i < v.size() - 1) {
std::cout << (char) 0xC1;
}
}
std::cout << (char) 0xD9 << std::endl;
}
/**
* Ïå÷àòàåò âåêòîð öåëûõ ÷èñåë
*/
void print_vector(char* msg, std::vector<int> v) {
std::cout << std::endl << msg << std::endl << (char) 0xDA;
int i, j;
int width = 8;
for (i = 0; i < v.size(); ++i) {
for (j = 0; j < width - 1; ++j) {
std::cout << (char) 0xC4;
}
if (i < v.size() - 1) {
std::cout << (char) 0xC2;
}
}
std::cout << (char) 0xBF << std::endl << (char) 0xB3;
for (i = 0; i < v.size(); ++i) {
std::cout << std::setw(width - 1) << v[i] << (char) 0xB3;
}
std::cout << std::endl << (char) 0xC0;
for (i = 0; i < v.size(); ++i) {
for (j = 0; j < width - 1; ++j) {
std::cout << (char) 0xC4;
}
if (i < v.size() - 1) {
std::cout << (char) 0xC1;
}
}
std::cout << (char) 0xD9 << std::endl;
}
/**
* Ïå÷àòàåò ìàòðèöó
*/
void print_matrix(char* msg, matrix_nums m) {
int i, j, k;
int width = 8;
std::cout << std::endl << msg << std::endl << (char) 0xDA;
for (i = 0; i < m[0].size(); ++i) {
for (j = 0; j < width - 1; ++j) {
std::cout << (char) 0xC4;
}
if (i < m[0].size() - 1) {
std::cout << (char) 0xC2;
}
}
std::cout << (char) 0xBF << std::endl;
for (i = 0; i < m.size(); ++i) {
std::cout << (char) 0xB3;
for (j = 0; j < m[i].size(); ++j) {
std::cout << std::setw(width - 1) << std::right << m[i][j].str() << (char) 0xB3;
}
std::cout << std::endl;
if (i < m.size() - 1) {
std::cout << (char) 0xC3;
for (j = 0; j < m[i].size(); ++j) {
for (k = 0; k < width - 1; ++k) {
std::cout << (char) 0xC4;
}
if (j < m[i].size() - 1) {
std::cout << (char) 0xC5;
} else {
std::cout << (char) 0xB4;
}
}
std::cout << std::endl;
}
}
std::cout << (char) 0xC0;
for (i = 0; i < m[0].size(); ++i) {
for (j = 0; j < width - 1; ++j) {
std::cout << (char) 0xC4;
}
if (i < m[0].size() - 1) {
std::cout << (char) 0xC1;
}
}
std::cout << (char) 0xD9 << std::endl;
std::cout << std::endl;
}
// ìàêñèìàëüíîå êîëè÷åñòâî èòåðàöèé ìåòîäà
#define DEBUG_MAX_ITER_NUM 10
/****************************************************************************
* Èñêëþ÷åíèå
*/
class simplex_error {
public:
std::string msg;
simplex_error(char* m) {
msg = std::string(m);
}
};
/****************************************************************************
* ñòðóêòóðà òàáëèöû ñèìïëåêñ-ìåòîäà
*/
struct opt_table {
vector_nums cib;
vector_ints bp;
vector_nums br;
vector_nums c;
matrix_nums a;
vector_nums z;
vector_nums delta;
};
/****************************************************************************
* Ïðîâåðêà öåëî÷èñëåííîñòè êîìïîíåíò âåêòîðà
*/
bool vector_is_integer(vector_nums v) {
for (int i = 0; i < v.size(); ++i) {
if (! v[i].is_integer()) {
return false;
}
}
return true;
}
/****************************************************************************
* Ñèìïëåêñ ìåòîä ïîèñêà ìàêñèìóìà
*/
opt_table simplex_max(opt_table task, bool negative = false, bool int_break = false) {
int i, j;
int n = task.c.size();
int m = task.bp.size();
vector_nums cib(m);
vector_nums z(n);
vector_nums delta(n);
vector_nums min(m);
matrix_nums tmp_a(m, vector_nums(n));
vector_nums tmp_br(m);
for (i = 0; i < m; ++i) {
if (task.bp[i] == -1) {
continue;
}
cib[i] = task.c[ task.bp[i] ];
}
for (int k = 0; k < DEBUG_MAX_ITER_NUM; ++k) {
print_line();
// îïðåäåëÿåì ðàçðåøàþùèé ñòîëáåö
number deltaExtr;
if (negative) {
deltaExtr = 1000000;
} else {
deltaExtr = delta[0];
}
int r = -1;
for (j = 0; j < n; ++j) {
z[j] = number();
for (i = 0; i < m; ++i) {
if (task.bp[i] == -1) {
continue;
}
z[j] += cib[i] * task.a[i][j];
}
delta[j] = task.c[j] - z[j];
// íàèìåíüøàÿ ïî ìîäóëþ îòðèöàòåëüíàÿ îöåíêà
if (negative) {
if (delta[j] < number(0) && delta[j].absolute() < deltaExtr) {
deltaExtr = delta[j].absolute();
r = j;
}
// íàèáîëüøàÿ îöåíêà
} else {
if (deltaExtr < delta[j]) {
deltaExtr = delta[j];
r = j;
}
}
}
print_vector("Coefs of variables (c)", task.c);
print_vector("Coefs of basic variables (cib)", cib);
print_vector("Basic variables numbers (bp)", task.bp);
print_vector("Basic variables values (br)", task.br);
print_matrix("Coefs of system (a)", task.a);
print_vector("Values (z)", z);
print_vector("Relative valuations (delta)", delta);
print_num("Permitted column number (r): ", r);
if (r == -1) {
break;
}
// îïðåäåëÿåì ðàçðåøàþùóþ ñòðîêó
number min_row = number(100000);
int s = -1;
for (i = 0; i < m; ++i) {
if (task.bp[i] == -1) {
s = i;
break;
}
if (task.a[i][r] != 0) {
min[i] = task.br[i] / task.a[i][r];
} else {
min[i] = number(0, 0, false);
continue;
}
if (min[i] < 0) {
min[i] = number(0, 0, false);
continue;
}
if (min_row > min[i]) {
min_row = min[i];
s = i;
}
}
//print_vector("Relations x/a (br[i]/a[i][r], min): ", min);
if (s == -1) {
throw simplex_error("Permitted row not found");
}
print_num("Permitted row number (s): ", s);
number element = task.a[s][r];
print_num("Permitted value (element): ", element);
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
tmp_a[i][j] = task.a[i][j];
}
tmp_br[i] = task.br[i];
}
// âíîñèì ïåðåìåííóþ â áàçèñ
task.bp[s] = r;
cib[s] = task.c[r];
for (j = 0; j < n; ++j) {
task.a[s][j] /= element;
}
task.br[s] /= element;
for (i = 0; i < m; ++i) {
if (i == s) {
continue;
}
number air = tmp_a[i][r];
for (j = 0; j < n; ++j) {
task.a[i][j] -= (air * tmp_a[s][j]) / element;
}
task.br[i] -= (air * tmp_br[s]) / element;
}
if (int_break && vector_is_integer(task.br)) {
break;
}
}
print_line();
print_vector("Result: ", task.br);
opt_table tab;
tab.cib = cib;
tab.bp = task.bp;
tab.br = task.br;
tab.c = task.c;
tab.a = task.a;
tab.z = z;
tab.delta = delta;
return tab;
}
/****************************************************************************
* Ñðàâíåíèå ïî ìîäóëþ 1
*/
bool cmp_mod_one(number a, number b) {
return (a - b).is_integer();
}
/****************************************************************************
* Ïðîâåðêà çíà÷åíèÿ ýëåìåíòà â ìàññèâå
*/
bool in_vector(int a, vector_ints v) {
for (int i = 0; i < v.size(); ++i) {
if (a == v[i]) {
return true;
}
}
return false;
}
/****************************************************************************
* Ìåòîä Ãîìîðè
*/
opt_table gomory(opt_table tab) {
tab = simplex_max(tab, false, false);
int i, j;
// îáùåå êîëè÷åñòâî ïåðåìåííûõ
int n = tab.c.size();
// êîëè÷åñòâî áàçèñíûõ ïåðåìåííûõ
int m = tab.bp.size();
for (int k = 0; k < DEBUG_MAX_ITER_NUM; ++k) {
print_line();
std::cout << "Gomory iteration #" << k << std::endl;
print_line();
// óñëîâèå âûõîäà
if (vector_is_integer(tab.br)) {
return tab;
}
// ìàêñèìàëüíàÿ äðîáíàÿ ÷àñòü
number max_fract = 0;
// íîìåð ñòðîêè ñ ìàêñèìàëüíîé äðîáíîé ÷àñòüþ
int r = -1;
// ïîèñê ïåðåìåííîé ñ ìàêñèìàëüíîé äðîáíîé ÷àñòüþ
for (i = 0; i < m; ++i) {
number fract = tab.br[i].fract();
if (fract >= max_fract) {
max_fract = fract;
r = i;
}
}
// ñîñòàâëÿåì íîâóþ òàáëèöó, äîáàâëÿÿ îäèí ñòîëáåö è îäíó ñòðîêó
++n;
++m;
opt_table ext_tab;
// âåêòîð êîýôôèöèåíòîâ âñåõ ïåðåìåííûõ
ext_tab.c = vector_nums(n);
for (j = 0; j < n - 1; ++j) {
ext_tab.c[j] = tab.c[j];
}
ext_tab.c[n - 1] = 0;
// ìàòðèöà a
ext_tab.a = matrix_nums(m, vector_nums(n));
for (i = 0; i < m - 1; ++i) {
for (j = 0; j < n - 1; ++j) {
ext_tab.a[i][j] = tab.a[i][j];
}
ext_tab.a[i][n - 1] = 0;
}
for (j = 0; j < n - 1; ++j) {
if (in_vector(j, tab.bp)) {
ext_tab.a[m - 1][j] = 0;
} else {
ext_tab.a[m - 1][j] = tab.a[r][j].fract();
}
}
ext_tab.a[m - 1][n - 1] = -1;
// âåêòîð-ðåøåíèå
ext_tab.bp = vector_ints(m);
ext_tab.br = vector_nums(m);
for (i = 0; i < m - 1; ++i) {
//ext_tab.cib[i] = tab.cib[i];
ext_tab.bp[i] = tab.bp[i];
ext_tab.br[i] = tab.br[i];
}
// ext_tab.cib[m - 1] = ?
ext_tab.bp[m - 1] = -1; // ?
ext_tab.br[m - 1] = tab.br[r].fract();
tab = simplex_max(ext_tab, true, true);
}
return tab;
}
/****************************************************************************
* Ìåéí, àãà
*/
int main(int argc, char* argv[]) {
int n = 5;
int m = 2;
vector_nums c(n);
c[0] = number(1);
c[1] = number(-1);
c[2] = number(0);
c[3] = number(0);
c[4] = number(0, -1);
matrix_nums a(m, vector_nums(n));
a[0][0] = number(-1);
a[0][1] = number(2);
a[0][2] = number(-1);
a[0][3] = number(0);
a[0][4] = number(1);
a[1][0] = number(3);
a[1][1] = number(2);
a[1][2] = number(0);
a[1][3] = number(1);
a[1][4] = number(0);
vector_ints bp(m);
bp[0] = 4;
bp[1] = 3;
vector_nums br(m);
br[0] = number(4);
br[1] = number(14);
opt_table task;
task.a = a;
task.bp = bp;
task.br = br;
task.c = c;
/*opt_table task;
task.c = vector_nums(n);
task.c[0] = number(2);
task.c[1] = number(1);
task.c[2] = number(0);
task.a = matrix_nums(m, vector_nums(n));
task.a[0][0] = number(15);
task.a[0][1] = number(30);
task.a[0][2] = number(1);
task.bp = vector_ints(m);
task.bp[0] = 2;
task.br = vector_nums(m);
task.br[0] = number(96);*/
/*opt_table task;
task.c = vector_nums(n);
task.c[0] = number(1);
task.c[1] = number(0);
task.c[2] = number(0);
task.c[3] = number(0);
task.c[4] = number(0);
task.c[5] = number(0, -1);
task.a = matrix_nums(m, vector_nums(n));
task.a[0][0] = number(1);
task.a[0][1] = number(-2);
task.a[0][2] = number(1);
task.a[0][3] = number(0);
task.a[0][4] = number(0);
task.a[0][5] = number(0);
task.a[1][0] = number(1);
task.a[1][1] = number(-1);
task.a[1][2] = number(0);
task.a[1][3] = number(-1);
task.a[1][4] = number(0);
task.a[1][5] = number(1);
task.a[2][0] = number(1);
task.a[2][1] = number(1);
task.a[2][2] = number(0);
task.a[2][3] = number(0);
task.a[2][4] = number(1);
task.a[2][5] = number(0);
task.bp = vector_ints(m);
task.bp[0] = 2;
task.bp[1] = 5;
task.bp[2] = 4;
task.br = vector_nums(m);
task.br[0] = number(4);
task.br[1] = number(-1);
task.br[2] = number(2);*/
gomory(task);
return 0;
}