Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <vector>
  5. #include <iomanip>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. int counter = 0;
  11. vector<double> ves_criter = { 2, 0.5, 1.5, 2 };
  12. vector<string> kinds = { "Suzuki","Mitsub","Honda","Toyota" };
  13.  
  14. void Print(vector<vector<double>> & matrix) // Печать таблицы
  15. {
  16. ++counter;
  17. cout << endl;
  18. for (int i = 0; i < 5; i++)
  19. {
  20. if (i == 0) cout << ' ';
  21. else if (i == 1) cout << setw(25) << "Cost";
  22. else if (i == 2) cout << setw(20) << "Service";
  23. else if (i == 3) cout << setw(20) << "Fuel";
  24. else if (i == 4) cout << setw(20) << "Comfort";
  25. }
  26. cout << endl;
  27. for (int i = 0; i < 4; i++)
  28. {
  29. if (i == 0) cout << setw(20) << "Suzuki\t ";
  30. else if (i == 1) cout << setw(20) << "Mitsub\t ";
  31. else if (i == 2) cout << setw(20) << "Honda\t ";
  32. else if (i == 3) cout << setw(20) << "Toyota\t ";
  33. for (int j = 0; j < 4; j++)
  34. {
  35. if (counter == 2) cout << matrix[i][j] << setw(20);
  36. else cout << matrix[j][i] << setw(20);
  37. }
  38. cout << endl;
  39. }
  40. }
  41.  
  42. void Print_2(vector<vector<double>> & matrix)
  43. {
  44. ++counter;
  45. cout << endl;
  46. for (int i = 0; i < 5; i++)
  47. {
  48. if (i == 0) cout << ' ';
  49. else if (i == 1) cout << setw(25) << "Cost";// цена
  50. else if (i == 2) cout << setw(20) << "Service";// обслуживание
  51. else if (i == 3) cout << setw(20) << "Fuel";// бензин
  52. else if (i == 4) cout << setw(20) << "Comfort";// комфорт
  53. }
  54. cout << endl;
  55. for (int i = 0; i < 4; i++)
  56. {
  57. if (i == 0) cout << setw(15) << "Suzuki\t ";// марки машин
  58. else if (i == 1) cout << setw(15) << "Mitsub\t ";
  59. else if (i == 2) cout << setw(15) << "Honda\t ";
  60. else if (i == 3) cout << setw(15) << "Toyota\t ";
  61. for (int j = 0; j < 4; j++)
  62. {
  63. if (counter == 2) cout << matrix[i][j] << setw(20);
  64. else cout << matrix[j][i] << setw(20);
  65. }
  66. cout << endl;
  67. }
  68. }
  69.  
  70. void VyvodOptSol(int str) // Вывод оптим. выбора
  71. {
  72. cout << endl;
  73. if (str == 0)
  74. cout << "optimal: SUZUKI";
  75. else if (str == 1)
  76. cout << "optimal: MITSUBISHI";
  77. else if (str == 2)
  78. cout << "optimal: HONDA";
  79. else if (str == 3)
  80. cout << "optimal: TOYOTA";
  81. cout << endl << endl << endl;
  82. }
  83.  
  84. void NormalVectorCriteriev()
  85. {
  86. cout << setw(50) << "========================== Normal vector criteriev ==========================" << endl << endl;
  87. double sum = 0;
  88. for (int i = 0; i < 4; ++i)
  89. {
  90. sum += ves_criter[i];
  91. }
  92. cout << "Ves Kriteriev: { 2, 0.5, 1.5, 2 }" << endl << endl;
  93. cout << "Summa Vesov:" << sum << endl << endl;
  94. cout << "Norm Vesa Criter : ";
  95. for (int i = 0; i < 4; ++i)
  96. {
  97. ves_criter[i] /= sum;
  98. cout << setw(5) << ves_criter[i] << " ";
  99. }
  100. cout << endl << endl;
  101. }
  102.  
  103. void NormalizSaati(double matrix[4][5], vector<double> & vector)// нормирование
  104. {
  105. double sum = 0;
  106. for (int i = 0; i < 4; ++i)
  107. {
  108. for (int j = 0; j < 4; ++j)
  109. {
  110. matrix[i][4] += matrix[i][j];
  111. }
  112. cout << setw(10) << matrix[i][4];
  113. cout << endl;
  114. }
  115. for (int i = 0; i < 4; ++i)
  116. {
  117. sum += matrix[i][4];
  118. }
  119. cout << endl << " Symma Strok: " << sum << endl;
  120. for (int i = 0; i < 4; ++i)
  121. {
  122. vector.push_back(matrix[i][4] / sum);
  123. cout << "\t" << endl;
  124. }
  125. }
  126. void Saati()
  127. {
  128. cout << "========================== Saati ==========================" << endl;
  129. double matr_criteriy[4][5] = { { 1, 0.2, 4, 3, 0 },{ 5, 1, 7, 6, 0 },{ 0.25, 0.14, 1, 0.5, 0 },{ 0.333, 0.167, 2, 1, 0 } };
  130. double cost[4][5] = { { 1, 0.25, 0.333, 0.125, 0 },{ 4, 1, 2, 0.333, 0 },{ 3, 0.5, 1, 0.25, 0 },{ 8, 3, 4, 1, 0 } };
  131. double service[4][5] = { { 1, 6, 0.333, 2, 0 },{ 0.167, 1, 0.125, 0.2, 0 },{ 3, 8, 1, 4, 0 },{ 0.5, 5, 0.25, 1, 0 } };
  132. double fuel[4][5] = { { 1, 0.2, 0.333, 0.125, 0 },{ 5, 1, 3, 0.25, 0 },{ 3, 0.333, 1, 0.167, 0 },{ 8, 4, 6, 1, 0 } };
  133. double comfort[4][5] = { { 1, 0.333, 0.2, 0.14, 0 },{ 3, 1, 0.333, 0.2, 0 },{ 5, 3, 1, 0.333, 0 },{ 7, 5, 3, 1, 0 } };
  134. vector<double> cost_v, service_v, fuel_v, comfort_v, ves_criter_saati;
  135. NormalizSaati(cost, cost_v);
  136. NormalizSaati(service, service_v);
  137. NormalizSaati(fuel, fuel_v);
  138. NormalizSaati(comfort, comfort_v);
  139. NormalizSaati(matr_criteriy, ves_criter_saati);
  140. vector<vector<double>> normaliz_matrix;
  141. normaliz_matrix.push_back(cost_v); normaliz_matrix.push_back(service_v); normaliz_matrix.push_back(fuel_v); normaliz_matrix.push_back(comfort_v);
  142. Print(normaliz_matrix);
  143. double record = 0;
  144. int str = -1;
  145. for (unsigned i = 0; i < 4; ++i)
  146. {
  147. double sum = 0;
  148. for (unsigned j = 0; j < 4; ++j)
  149. {
  150. sum += (normaliz_matrix[j][i] * ves_criter_saati[j]);
  151. }
  152. cout << endl;
  153. cout << "Sum: " << sum << endl;
  154. if (sum > record)
  155. {
  156. record = sum;
  157. str = i;
  158. }
  159. }
  160. VyvodOptSol(str);
  161. }
  162.  
  163. double manhattan(pair<double, double>& a, pair<double, double>& b)
  164. {
  165. return abs(a.second - b.second) + abs(a.first - b.first);
  166. }
  167.  
  168. using matrix = vector<vector<double>>;
  169. void MethodPareto(const matrix& matrix, size_t k1, size_t k2)
  170. {
  171. vector<pair<double, double>> points;
  172. for (auto i = 0; i < 4; ++i)
  173. {
  174. points.push_back(std::make_pair(matrix[i][k1], matrix[i][k2]));
  175. }
  176. cout << "Tocki: ";
  177. for (auto& it : points)
  178. {
  179. cout << '(' << it.first << ", " << it.second << ")";
  180. }
  181. cout << endl;
  182. auto max_x = max_element(points.begin(), points.end(),
  183. [](pair<double, double> &a, pair<double, double> &b)
  184. {
  185. return a.first < b.first;
  186. });
  187. auto max_y = max_element(points.begin(), points.end(),
  188. [](pair<double, double> &a, pair<double, double> &b)
  189. {
  190. return a.second < b.second;
  191. });
  192. pair<double, double> utopia_point = make_pair(8, 8);
  193. cout << "Utopia tocki: (" << utopia_point.first << ", " << utopia_point.second << ") ";
  194. cout << endl;
  195. vector<double> solution;
  196. for (auto i = 0; i < points.size(); ++i)
  197. solution.push_back(manhattan(points[i], utopia_point));
  198. cout << "Znachanie komb kriteria: ";
  199. for (auto i = 0; i < solution.size(); ++i)
  200. cout << setw(7) << solution[i];
  201. cout << endl;
  202. auto min = std::min_element(solution.begin(), solution.end());
  203. cout << "Best reshenie: " << kinds[distance(solution.begin(), min)] << endl;
  204. }
  205.  
  206. void MainCritery()
  207. {
  208. vector<vector<double>> normaliz_matrix = { { 0,0.6,0,1 },
  209. { 0.286,0,0.833,0.333 },
  210. { 0.714,1, 0.333,0.833 },
  211. { 1,0.4,1, 0 } };
  212. cout << setw(50) << "====================== Method zameni criteria ogranicheniami ==========================" << endl;
  213. Print_2(normaliz_matrix);
  214. double rec = 0;
  215. int str = 0;
  216. for (unsigned i = 0; i < 4; ++i)
  217. {
  218. cout << "Dopystimoe Cost= " << normaliz_matrix[i][0] * 0.2<<endl;
  219. cout << "Dopystimoe Service= " << normaliz_matrix[i][3] * 0.5<<endl;
  220. cout << "Dopystimoe Fuel= " << normaliz_matrix[i][1] * 0.3<<endl;
  221. cout << "==============="<<endl;
  222. if (normaliz_matrix[i][0] >= 0.2 && normaliz_matrix[i][1] >= 0.5 && normaliz_matrix[i][3] >= 0.3 && normaliz_matrix[i][2] > rec)
  223. {
  224. rec = normaliz_matrix[i][0];
  225. str = i;
  226. }
  227. }
  228. VyvodOptSol(str);
  229. }
  230.  
  231. void LinConvolut()
  232. {
  233. vector<vector<double>> normaliz_matrix = { { 2, 6, 2, 8 },
  234. { 4, 3, 7, 4 },
  235. { 7, 8, 4, 7 },
  236. { 9, 5, 8, 2 } };
  237. for (int i = 0; i < 4; ++i)
  238. {
  239. double sum = 0;
  240.  
  241. for (int j = 0; j < 4; ++j)
  242. {
  243. sum += normaliz_matrix[j][i];
  244. }
  245. for (int j = 0; j < 4; ++j)
  246. {
  247. normaliz_matrix[j][i] /= sum;
  248. }
  249. }
  250. cout << endl << setw(50) << "================================== Method Lineynoy svertki =========================" << endl << endl;
  251. Print_2(normaliz_matrix);
  252. cout << endl;
  253. //Ввод матрицы
  254. int str = 0;
  255. double record = 0;
  256. for (int i = 0; i < 4; ++i)
  257. {
  258. double sum = 0;
  259. for (int j = 0; j < 4; ++j)
  260. {
  261. sum += (normaliz_matrix[i][j] * ves_criter[j]);
  262. }
  263. cout << setw(25) << "sum: " << sum << endl;
  264. if (record < sum)
  265. {
  266. record = sum;
  267. str = i;
  268. }
  269. }
  270. VyvodOptSol(str);
  271. cout << endl << endl;
  272. }
  273.  
  274.  
  275.  
  276. int main()
  277. {
  278. cout << "======================================================================================" << endl;
  279. matrix m = { { 2, 6, 2, 8 },
  280. { 4, 3, 7, 4 },
  281. { 7, 8, 4, 7 },
  282. { 9, 5, 8, 2 } };
  283. Print(m);
  284. cout << endl;
  285. MainCritery();
  286. NormalVectorCriteriev();
  287. LinConvolut();
  288. cout << setw(63) << "========================== Pareto ==========================" << endl << endl;
  289. MethodPareto(m, 1, 2);
  290. Saati();
  291. system("pause");
  292. return 0;
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement