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
| #include "stdafx.h"
#include <winsock2.h>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <sstream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
class user_data {
public:
void get_data_from_user();
void user_data_is_valid();
protected:
double *function;
double *fm;
double **system;
int *sign;
int num_v;
int num_l;
bool way;
};
class simplex : public user_data {
public:
void init();
void gen_plane();
bool plane_is_valid();
bool function_is_undefined();
void print_result_to_file(int it_num);
private:
double func;
double **bv;
double **sv;
double *istr;
double *th;
double alm;
int i_lrow;
int i_lcol;
std::stringstream table;
};
void error(int err_no)
{
switch (err_no) {
case 0:
cout << "\nВы ввели некорректное значение.\n" << endl;
break;
case 1:
cout << "\nВы не можете задать менее двух ограничений.\n" << endl;
break;
case 2:
cout << "\nВы не можете задать больше 500 ограничений.\n" << endl;
break;
case 3:
cout << "\nВы не можете задать менее двух переменных.\n" << endl;
break;
case 4:
cout << "\nВы не можете задать более 500 уравнений.\n" << endl;
break;
}
}
void user_data::get_data_from_user()
{
string num_limits, num_vars, s_var, fr_m, sn, func, w;
int i, j;
bool validator = false;
do {
cout << "Введите количество ограничений в системе: ";
getline(cin, num_limits);
if (atoi(num_limits.c_str()) < 2)
error(1);
else if (atoi(num_limits.c_str()) > 500)
error(2);
else
validator = true;
} while (!validator);
num_l = atoi(num_limits.c_str());
validator = false;
do {
cout << "Введите количество переменных в системе ограничений: ";
getline(cin, num_vars);
if (atoi(num_vars.c_str()) < 2)
error(3);
else if (atoi(num_vars.c_str()) > 500)
error(4);
else
validator = true;
} while (!validator);
num_v = atoi(num_vars.c_str());
validator = false;
function = new double[num_v];
system = new double *[num_l];
for (i = 0; i < num_l; i++)
system[i] = new double[num_v];
fm = new double[num_l];
sign = new int[num_l];
cout << "\nЗаполните коэффициенты при целевой функции.\n" << endl;
for (i = 0; i < num_v; i++) {
do {
cout << "Введите коэффициент целевой фукнции при x" << i + 1 << ": ";
getline(cin, func);
validator = true;
function[i] = atof(func.c_str());
} while (!validator);
validator = false;
}
do {
cout << "Введите направление целевой функции ( min, max ) : ";
getline(cin, w);
if (w == "max" || w == "MAX" || w == "min" || w == "MIN") {
validator = true;
if (w == "max" || w == "MAX")
way = true;
else
way = false;
}
else
error(0);
} while (!validator);
cout << "\nЗаполните систему ограничений.\n" << endl;
for (i = 0; i < num_l; i++) {
cout << "Заполните " << i + 1 << "-е ограничение.\n" << endl;
for (j = 0; j < num_v; j++) {
do {
cout << "Введите коэффициэнт при x" << j + 1 << ": ";
getline(cin, s_var);
validator = true;
} while (!validator);
system[i][j] = atof(s_var.c_str());
validator = false;
}
do {
cout << "Введите знак при " << i + 1 << "-м ограничении ( <=, =, >= ) : ";
getline(cin, sn);
if (sn == "<=" || sn == "=" || sn == ">=") {
validator = true;
if (sn == "<=")
sign[i] = 0;
if (sn == "=")
sign[i] = 1;
if (sn == ">=")
sign[i] = 2;
}
else
error(0);
cout << sign[i] << endl;
} while (!validator);
validator = false;
do {
cout << "Введите свободный член при " << i + 1 << "-м ограничении: ";
getline(cin, fr_m);
if (atof(fr_m.c_str()) == 0)
error(0);
else
validator = true;
} while (!validator);
fm[i] = atof(fr_m.c_str());
validator = false;
cout << endl;
}
}
void simplex::init()
{
int i, j;
func = 0;
sv = new double *[num_l];
for (i = 0; i < num_l; i++)
sv[i] = new double[num_v * 2];
for (i = 0; i < num_l; i++) {
for (j = 0; j < num_v; j++)
sv[i][j] = system[i][j];
for (; j < num_v * 2; j++)
if (i + num_v == j)
if (way)
sv[i][j] = 1;
else
sv[i][j] = -1;
else
sv[i][j] = 0;
}
istr = new double[num_v * 2];
bv = new double *[num_l];
for (i = 0; i < num_l; i++) {
bv[i] = new double[2];
bv[i][0] = i + num_v;
bv[i][1] = fm[i];
}
for (i = 0; i < num_v * 2; i++)
if (i < num_v)
istr[i] = function[i] * -1;
else
istr[i] = 0;
i_lcol = 0;
for (i = 0; i < num_v * 2 - 1; i++) {
if (istr[i] < 0)
if (fabs(istr[i + 1]) > fabs(istr[i]))
i_lcol = i + 1;
}
th = new double[num_l];
for (i = 0; i < num_l; i++)
th[i] = bv[i][1] / sv[i][i_lcol];
i_lrow = 0;
for (i = 0; i < num_l - 1; i++)
if (th[i] > th[i + 1])
i_lrow = i + 1;
alm = sv[i_lrow][i_lcol];
print_result_to_file(0);
}
bool simplex::plane_is_valid()
{
int i;
bool result = true;
if (way)
for (i = 0; i < num_v * 2; i++)
if (istr[i] < 0) {
result = false;
break;
}
if (!way)
for (i = 0; i < num_v * 2; i++)
if (istr[i] >= 0) {
result = false;
break;
}
return result;
}
bool simplex::function_is_undefined()
{
int i;
for (i = 0; i < num_l; i++)
if (th[i] < 0) {
return false;
}
return true;
}
void simplex::gen_plane()
{
int i, j, it_num = 0;
double A, B;
while (!plane_is_valid() && function_is_undefined()) {
A = bv[i_lrow][1];
B = istr[i_lcol];
func -= A * B / alm;
double *tmp_bv = new double[num_l];
bv[i_lrow][0] = i_lcol;
A = bv[i_lrow][1];
for (i = 0; i < num_l; i++) {
B = sv[i][i_lcol];
tmp_bv[i] = bv[i_lrow][1];
if (i != i_lrow)
tmp_bv[i] = bv[i][1] - A * B / alm;
else
tmp_bv[i] /= alm;
}
for (i = 0; i < num_l; i++)
bv[i][1] = tmp_bv[i];
double *tmp_istr = istr;
B = istr[i_lcol];
for (i = 0; i < num_v * 2; i++) {
A = sv[i_lrow][i];
tmp_istr[i] = istr[i] - A * B / alm;
}
istr = tmp_istr;
double **tmp_sv = new double *[num_l];
for (i = 0; i < num_l; i++)
tmp_sv[i] = new double[num_v * 2];
for (i = 0; i < num_l; i++)
for (j = 0; j < num_v * 2; j++) {
tmp_sv[i][j] = sv[i][j];
A = sv[i_lrow][j];
B = sv[i][i_lcol];
if (i == i_lrow)
tmp_sv[i][j] /= alm;
else
tmp_sv[i][j] = sv[i][j] - A * B / alm;
}
sv = tmp_sv;
i_lcol = 0;
for (i = 0; i < num_l; i++)
th[i] = bv[i][1] / sv[i][i_lcol];
i_lrow = 0;
for (i = 0; i < num_l - 1; i++)
if (th[i] > th[i + 1])
i_lrow = i + 1;
alm = sv[i_lrow][i_lcol];
it_num++;
print_result_to_file(it_num);
}
if (!function_is_undefined())
cout << "\nЦелевая фукнция не ограничена, данная задача решений не имеет\n" << endl;
else {
cout << "\nf(x) = " << func << "\n" << endl;
for (i = 0; i < num_l; i++) {
cout << "x" << bv[i][0] + 1 << " = " << bv[i][1] << endl;
}
cout << "\nВсе вычисления были записаны в файл table.txt\n" << endl;
}
}
void simplex::print_result_to_file(int it_num)
{
int i, j;
if (!it_num) {
table << "Задана целевая функция:\n" << endl;
std::stringstream f_x;
f_x << "f(x) = ";
for (i = 0; i < num_v; i++) {
if (!i)
f_x << function[i] << "x" << i + 1 << " ";
else {
if (function[i] < 0)
f_x << "- " << fabs(function[i]) << "x" << i + 1 << " ";
if (function[i] > 0)
f_x << "+ " << function[i] << "x" << i + 1 << " ";
}
}
std::string minmax;
if (way)
minmax = "max";
else
minmax = "min";
f_x << "=> " << minmax << "\n" << endl;
table << f_x.str();
table << "И система ограничений:\n" << endl;
std::stringstream math_sys;
std::string math_sign;
for (i = 0; i < num_l; i++) {
for (j = 0; j < num_v; j++) {
if (!j)
math_sys << system[i][j] << "x" << j + 1 << " ";
else
if (system[i][j] == 1)
if (!j)
math_sys << "x" << j + 1 << " ";
else
math_sys << "+ x" << j + 1 << " ";
else
if (system[i][j] == -1)
if (!j)
math_sys << "-x" << j + 1 << " ";
else
math_sys << "- x" << j + 1 << " ";
else {
if (system[i][j] < 0)
math_sys << "- " << fabs(system[i][j]) << "x" << j + 1 << " ";
else
math_sys << "+ " << system[i][j] << "x" << i + 1 << " ";
if (!sign[i])
math_sign = "<=";
if (sign[i] == 1)
math_sign = "=";
if (sign[i] == 2)
math_sign = ">=";
}
}
math_sys << math_sign << " " << fm[i];
math_sys << endl;
}
std::string min_or_max;
if (way)
min_or_max = "максимум";
else
min_or_max = "минимум";
table << math_sys.str() << endl;
table << "Решим данную задачу на " << min_or_max << " методом симплексных таблиц.\nПостроим первый опорный план:\n" << endl;
}
for (i = 0; i < num_l; i++) {
table << "x" << bv[i][0] + 1 << "\t" << bv[i][1] << "\t";
for (j = 0; j < num_v * 2; j++)
table << sv[i][j] << "\t";
if (!plane_is_valid())
table << th[i];
table << "\n" << endl;
}
table << "f(x)\t" << func << "\t";
for (i = 0; i < num_v * 2; i++)
table << istr[i] << "\t";
table << "\n";
if (plane_is_valid()) {
if (plane_is_valid() && function_is_undefined())
table << "\nДанный план является оптимальным и не требует улучшения. Решение найдено." << endl;
std::ofstream outfile("table.txt");
outfile << table.str();
}
else {
std::string ln_or_gn;
if (way)
ln_or_gn = "неположительные";
else
ln_or_gn = "положительные";
std::stringstream num_of_plane;
if (!it_num)
num_of_plane << "Первый опорный план";
else
num_of_plane << it_num + 1 << "-й план также";
table << "\n" << num_of_plane.str() << " не является оптимальным, поскольку\nв индексной строке присутствуют " << ln_or_gn << " элементы.\nErо необходимо улучшить.\n" << endl;
}
}
int main()
{
setlocale(LC_ALL, "Russian");
simplex *ud = new simplex;
ud->get_data_from_user();
ud->init();
ud->gen_plane();
return 0;
} |