Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <math.h>
  5. #include <sstream>
  6.  
  7. using namespace std;
  8.  
  9. class Strategy {
  10.  
  11. vector<vector<double>> Matrix;
  12. vector<vector<double >> MatrixB;
  13. public:
  14. const vector<vector<double>> &getMatrixForB() const;
  15.  
  16. private:
  17. int size_x,size_y;
  18. vector<double> vector_x, vector_y;
  19. double min_x, max_y;
  20. bool saddle_point = false;
  21.  
  22.  
  23. public:
  24. Strategy (vector<vector<double>> M){
  25. Matrix = M;
  26. size_y = M.size();
  27. size_x = M.at(0).size();
  28. minimax_x();
  29. maximin_y();
  30. isSaddlePoint();
  31. createMatrixForB();
  32. }
  33.  
  34. double getMinX() const;
  35.  
  36. double getMaxY() const;
  37.  
  38.  
  39. bool isSaddlePoint() {
  40. if(max_y == min_x){
  41. saddle_point = true;
  42. return saddle_point;
  43. }
  44. else {
  45. saddle_point = false;
  46. }
  47. return saddle_point;
  48. }
  49.  
  50. // СТРАТЕГИЯ ИГРОКА А
  51. void maximin_y(){
  52.  
  53. for (int i = 0; i < size_y; ++i) {
  54. vector<double> v = Matrix.at(i);
  55. auto result = min_element(v.begin(), v.end());
  56. double a = v.at(std::distance(v.begin(), result));
  57. vector_y.emplace_back(a);
  58. }
  59.  
  60. auto max_result = max_element(vector_y.begin(), vector_y.end());
  61. double a = vector_y.at(std::distance(vector_y.begin(), max_result));
  62. max_y = a;
  63. }
  64.  
  65.  
  66. //СТРАТЕГИЯ ИГРОКА B
  67. void minimax_x(){
  68.  
  69. for (int j = 0; j < size_x; ++j) {
  70. vector<double> v;
  71. for (int i = 0; i < size_y; ++i) {
  72. v.emplace_back(Matrix.at(i).at(j));
  73. }
  74. auto result = max_element(v.begin(), v.end());
  75. double a = v.at(std::distance(v.begin(), result));
  76. vector_x.emplace_back(a);
  77. }
  78.  
  79. vector<double> v = vector_x;
  80.  
  81. auto min_result = std::min_element(v.begin(), v.end());
  82. double a = v.at(std::distance(v.begin(), min_result));
  83. min_x = a;
  84. }
  85.  
  86.  
  87. void print();
  88.  
  89. const vector<vector<double>> &getMatrixForA() const;
  90. vector<vector<double>> &createMatrixForB();
  91.  
  92.  
  93. };
  94.  
  95. void Strategy::print() {
  96.  
  97. for (int i = 0; i < size_y; ++i) {
  98. cout << "+";
  99. for (int j = 0; j < size_x + 1; ++j) {
  100. cout << "-------+";
  101. }
  102. cout << endl;
  103. for (int j = 0; j < size_x; ++j) {
  104. std::cout << "| " << Matrix.at(i).at(j) << "\t";
  105. }
  106. cout << "| " << vector_y.at(i) << "\t|\n";
  107. }
  108. cout << "+";
  109. for (int j = 0; j < size_x + 1; ++j) {
  110.  
  111. cout << "-------+";
  112. }
  113. cout << endl;
  114. for (int k = 0; k < size_x; ++k) {
  115. std::cout << "| " << vector_x.at(k) << "\t";
  116. }
  117. cout << "|\n";
  118.  
  119. cout << "+";
  120. for (int j = 0; j < size_x ; ++j) {
  121.  
  122. cout << "-------+";
  123. }
  124. cout << endl;
  125.  
  126. cout << "min of column is: " << min_x << endl;
  127. cout << "max of row is: " << max_y << endl;
  128.  
  129. cout << "saddle point is ";
  130. if(isSaddlePoint()){
  131. cout << "exist.\n";
  132. }
  133. else{
  134. cout << "not exist.\n";
  135. }
  136.  
  137. cout << "================================\n";
  138. }
  139.  
  140. const vector<vector<double>> &Strategy::getMatrixForA() const {
  141. return Matrix;
  142. }
  143.  
  144. // pivot operation
  145. vector<vector<double>>& Strategy::createMatrixForB() {
  146.  
  147. vector<vector<double>> result(Matrix.at(0).size(),vector<double>(Matrix.size()));
  148. for (size_t i = 0; i < Matrix.size(); ++i)
  149. for (size_t j = 0; j < Matrix.at(0).size(); ++j)
  150. result[j][i] = Matrix[i][j];
  151.  
  152. MatrixB = result;
  153. }
  154.  
  155. const vector<vector<double>> &Strategy::getMatrixForB() const {
  156. return MatrixB;
  157. }
  158.  
  159. double Strategy::getMinX() const {
  160. return min_x;
  161. }
  162.  
  163. double Strategy::getMaxY() const {
  164. return max_y;
  165. }
  166.  
  167.  
  168. class Simplex {
  169.  
  170. vector<vector<double>> A;
  171. vector<double> B;
  172. vector<double> C;
  173. int size_x, size_y;
  174. double g;
  175. char gambler;
  176.  
  177.  
  178. vector<string> VARIABLES_Y;
  179. vector<string> VARIABLES_X;
  180.  
  181. vector<vector<double>> full_matrix;
  182.  
  183.  
  184. public:
  185. void print() {
  186.  
  187. for (int i = 0; i < size_y; ++i) {
  188. cout << "+";
  189. for (int j = 0; j < size_x + 1; ++j) {
  190. cout << "-------+";
  191. }
  192. cout << endl;
  193. for (int j = 0; j < size_x; ++j) {
  194. std::cout << "| " << roundf(A.at(i).at(j)*100)/100 << "\t";
  195. }
  196. cout << "| " << roundf(B.at(i)*100)/100 << "\t|\n";
  197. }
  198. cout << "+";
  199. for (int j = 0; j < size_x + 1; ++j) {
  200.  
  201. cout << "-------+";
  202. }
  203. cout << endl;
  204. for (int k = 0; k < size_x; ++k) {
  205. std::cout << "| " << roundf(C.at(k)*100)/100 << "\t";
  206. }
  207. cout << "|\n";
  208.  
  209. cout << "+";
  210. for (int j = 0; j < size_x; ++j) {
  211.  
  212. cout << "-------+";
  213. }
  214. cout << endl;
  215. }
  216. void printFullMatrix(int y_size, int x_size) {
  217.  
  218. cout << "FULL MATRIX IS:\n";
  219.  
  220. for (int i = 0; i < y_size; ++i) {
  221. cout << "+";
  222. for (int j = 0; j < x_size; ++j) {
  223. cout << "-------+";
  224. }
  225. cout << endl;
  226. for (int j = 0; j < x_size; ++j) {
  227. std::cout << "| " << roundf(full_matrix.at(i).at(j)*100)/100 << "\t";
  228. }
  229. cout << "|\n";
  230. }
  231.  
  232. cout << "+";
  233. for (int j = 0; j < x_size; ++j) {
  234. cout << "-------+";
  235. }
  236. cout << endl;
  237. }
  238. void Solve() {
  239. if(gambler == 'A') {
  240. first_step_solve();
  241. }
  242.  
  243. for (int i = 0; i < size_y; ++i) {
  244. printMatrixWithVariables();
  245. second_step_solve();
  246. }
  247.  
  248. third_step_solve();
  249.  
  250. }
  251. void first_step_solve() {
  252.  
  253. for (int j = 0; j < size_y; ++j) {
  254.  
  255. cout << "divide string at value in basics X" << j+5 << endl;
  256.  
  257. double string_value_divider = full_matrix.at(j).at(j+size_x);
  258. for (int i = 0; i < full_matrix.at(0).size(); ++i) {
  259. if(full_matrix.at(j).at(i) != 0) {
  260. full_matrix.at(j).at(i) = full_matrix.at(j).at(i) / string_value_divider;
  261. }
  262. }
  263. printFullMatrix(size_y,size_x+size_y+1);
  264. }
  265. }
  266. bool second_step_solve() {
  267.  
  268. int column_index = find_max_or_min_index(); // find max or min from L(C) vector
  269.  
  270. //get true string (divide B by a at index and then compare to choose min)
  271. int row_index = get_MIN_INDEX_BY_DIVIDING_AT_B(column_index);
  272.  
  273. change_variables_index(column_index,row_index);
  274.  
  275.  
  276. divide_row_by_main_element(column_index,row_index);
  277.  
  278. minus_main_row_from_all_other_rows(column_index,row_index);
  279.  
  280. for (int i = 0; i < size_x; ++i) {
  281. C.at(i) = full_matrix.at(size_y).at(i);
  282. }
  283. cout << endl;
  284.  
  285. }
  286.  
  287. void printMatrix() {
  288. if(gambler == 'A') {
  289. printFullMatrix(size_y, size_x + size_y + 1);
  290. }
  291. else if (gambler == 'B') {
  292. printFullMatrix(size_y, size_x + size_y);
  293. }
  294. }
  295.  
  296.  
  297. void printMatrixWithVariables() {
  298. if(gambler == 'A') {
  299. printFullMatrixWithVariables(size_y +1, size_x + size_y + 1);
  300. }
  301. else if (gambler == 'B') {
  302. printFullMatrixWithVariables(size_y+1, size_x + size_y+1);
  303. }
  304. }
  305.  
  306.  
  307. void printFullMatrixWithVariables(int y_size, int x_size) {
  308. cout << "FULL MATRIX WITH VARIABLES IS:\n";
  309.  
  310. cout << "+-------+";
  311. for (int j = 0; j < x_size ; ++j) {
  312. cout << "-----------+";
  313. }
  314. cout << endl;
  315. cout << "| ";
  316.  
  317. for (int d = 0; d < x_size-1; ++d){
  318. cout << "| " << VARIABLES_X.at(d) << "\t\t";
  319. }
  320. cout << "| " << "B" << "\t\t";
  321. cout << "|\n";
  322.  
  323. for (int i = 0; i < y_size; ++i) {
  324.  
  325. cout << "+-------+";
  326. for (int j = 0; j < x_size; ++j) {
  327. cout << "-----------+";
  328. }
  329. cout << endl;
  330.  
  331. if(VARIABLES_Y.size() > i) {
  332. std::cout << "| " << VARIABLES_Y.at(i) << "\t";
  333. } else {
  334. std::cout << "| " << "L" << "\t";
  335. }
  336.  
  337.  
  338. for (int j = 0; j < x_size; ++j) {
  339.  
  340.  
  341.  
  342. ostringstream strs;
  343. double a = roundf(full_matrix.at(i).at(j) * 100) / 100;
  344. strs << a;
  345. string str = strs.str();
  346.  
  347. if (str.length() > 4) {
  348. std::cout << "| " << str << "\t";
  349. } else {
  350. std::cout << "| " << str << "\t\t";
  351. }
  352.  
  353. }
  354.  
  355.  
  356. cout << "|\n";
  357. }
  358.  
  359. cout << "+-------+";
  360. for (int j = 0; j < x_size; ++j) {
  361. cout << "-----------+";
  362. }
  363. cout << endl;
  364. }
  365.  
  366. int find_max_or_min_index() {
  367.  
  368. if(gambler == 'A'){
  369. //find max from L(C) vector
  370. int maxElementIndex = max_element(C.begin(),C.end()) - C.begin();
  371. return maxElementIndex;
  372.  
  373. } else if (gambler == 'B') {
  374. //find min from L(C) vector
  375. int minElementIndex = min_element(C.begin(),C.end()) - C.begin();
  376. return minElementIndex;
  377. }
  378. }
  379.  
  380.  
  381. int get_MIN_INDEX_BY_DIVIDING_AT_B(int column) {
  382. vector<double> values;
  383. for (int i = 0; i < size_y; ++i) {
  384. values.emplace_back(abs(full_matrix.at(i).at(size_y+size_x)/full_matrix.at(i).at(column)));
  385. }
  386.  
  387. cout << endl;
  388.  
  389. int minElementIndex = std::min_element(values.begin(),values.end()) - values.begin();
  390. return minElementIndex;
  391. }
  392.  
  393.  
  394. void change_variables_index(int column, int row) {
  395. VARIABLES_Y.at(row) = VARIABLES_X.at(column);
  396. }
  397.  
  398.  
  399. void divide_row_by_main_element(int column, int row) {
  400.  
  401. double element = full_matrix.at(row).at(column);
  402. for (int i = 0; i < size_x+size_y+1; ++i) {
  403. full_matrix.at(row).at(i) = full_matrix.at(row).at(i) / element;
  404. }
  405. }
  406.  
  407.  
  408. void minus_main_row_from_all_other_rows(int main_column, int main_row) {
  409.  
  410. for (int i = 0; i < size_y+1; ++i) {
  411. if( i == main_row){
  412. continue;
  413. } else {
  414.  
  415. double x_times = full_matrix.at(i).at(main_column) / full_matrix.at(main_row).at(main_column);
  416.  
  417. for (int j = 0; j < size_x + size_y + 1; ++j) {
  418. full_matrix.at(i).at(j) = full_matrix.at(i).at(j) - full_matrix.at(main_row).at(j) * x_times;
  419. }
  420. }
  421. }
  422. }
  423.  
  424. void third_step_solve() {
  425. double sum = 0;
  426. for (int i = 0; i < VARIABLES_Y.size(); ++i) {
  427. int n = atoi(VARIABLES_Y.at(i).substr(1, 1).c_str());
  428. if(n < 5){
  429. sum += full_matrix.at(i).at(size_y+size_x);
  430. cout << "X" <<n <<": "<< full_matrix.at(i).at(size_y+size_x) << endl;
  431. }
  432. }
  433. if(gambler == 'A'){
  434. cout << "max win is " << 1/sum << endl;
  435. }
  436. else if (gambler == 'B') {
  437. cout << "min lose is " << 1/sum << endl;
  438. }
  439. }
  440.  
  441. Simplex(vector<vector<double>> M, bool is_min, double min_win, double max_lose) {
  442.  
  443.  
  444. // FOR A = search min, FOR B = search max
  445. if (is_min) {
  446. gambler = 'A';
  447. } else {
  448. gambler = 'B';
  449. }
  450.  
  451. A = M;
  452. size_y = M.size();
  453. size_x = M.at(0).size();
  454.  
  455. for (int m = 0; m < size_y; ++m) {
  456. VARIABLES_Y.emplace_back("X" + to_string(m+1+size_x));
  457. }
  458.  
  459. for (int l = 0; l < size_y+size_x; ++l) {
  460. VARIABLES_X.emplace_back("X" + to_string(l+1));
  461. }
  462.  
  463. int c = 0;
  464.  
  465. if (gambler == 'A') {
  466. c = -1;
  467. } else if (gambler == 'B') {
  468. c = 1;
  469. }
  470.  
  471.  
  472. // <= или >= система
  473.  
  474. for (int i = 0; i < size_y; ++i) {
  475. B.emplace_back(c);
  476. }
  477.  
  478.  
  479. for (int j = 0; j < size_x; ++j) {
  480. C.emplace_back(-1);
  481. }
  482.  
  483. if (gambler == 'A') {
  484. g = (1 / min_win);
  485. } else {
  486. g = (1 / max_lose);
  487. }
  488.  
  489. if (gambler == 'A') {
  490. vector<vector<double>> v(size_y + 1, vector<double>(size_x + size_y + 1, 0));
  491. full_matrix = v;
  492. } else
  493. if (gambler == 'B') {
  494. vector<vector<double>> v(size_y + 1, vector<double>(size_x + size_y+1, 0));
  495. full_matrix = v;
  496. }
  497.  
  498.  
  499.  
  500. {
  501. for (int k = 0; k < size_y; ++k) {
  502. for (int i = 0; i < size_x; ++i) {
  503. full_matrix.at(k).at(i) = M.at(k).at(i);
  504. }
  505. }
  506.  
  507.  
  508. for (int j = 0; j < size_y; ++j) {
  509. full_matrix.at(j).at(j+size_x) = B.at(j);
  510. if(gambler == 'A') {
  511. full_matrix.at(j).at(size_x + size_y) = -1;
  512. }
  513. }
  514.  
  515. for (int l = 0; l < size_x; ++l) {
  516. full_matrix.at(size_y).at(l) = C.at(l);
  517. }
  518.  
  519. for(int i = 0; i < size_y; ++i) {
  520. if(gambler == 'A') {
  521. full_matrix.at(i).at(size_x + size_y) = -B.at(i);
  522. } else if (gambler == 'B') {
  523. full_matrix.at(i).at(size_x + size_y) = B.at(i);
  524. }
  525. }
  526. }
  527. }
  528. };
  529.  
  530.  
  531. int main() {
  532.  
  533. vector<vector<double>> strategy_a1;
  534. strategy_a1.push_back({{ 6, 18, 6, 15}});
  535. strategy_a1.push_back({{17, 8, 13, 14}});
  536. strategy_a1.push_back({{16, 16, 18, 2}});
  537. strategy_a1.push_back({{18, 8, 4, 18}});
  538. strategy_a1.push_back({{15, 8, 3, 19}});
  539.  
  540. Strategy A(strategy_a1);
  541. A.print();
  542. auto M = A.getMatrixForA();
  543. Simplex K(M, true,A.getMaxY(),A.getMinX());
  544. K.print();
  545. K.printMatrix();
  546. K.printMatrixWithVariables();
  547. K.Solve();
  548. auto M1 = A.getMatrixForB();
  549. Simplex K1(M1, false, A.getMaxY(), A.getMinX());
  550. K1.print();
  551. K1.printMatrix();
  552. K1.printMatrixWithVariables();
  553. K1.Solve();
  554.  
  555. return 0;
  556. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement