Advertisement
Guest User

Ku Pe v1.1

a guest
Dec 15th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. //for this task matrix D is 4x4, c 4x1, A 2x4, b 2x1, delta 4x4
  6. //knowing that the size of simplex is defined as 6x17 (last column for matrices b and c)
  7.  
  8. static float D[4][4];
  9. static float c[4];
  10. static float A[2][4];
  11. static float b[2];
  12. static float delta[4][4];
  13. static float simplex[6][17];
  14.  
  15. //function creating matrix D basing on entered coefficients staying near x1^2, x2^2 and x1x2
  16. void D_initializer()
  17. {
  18. std::cout << "Filling the D matrix." << std::endl;
  19. for (int i = 0; i < 4;i++)//initially filling with 0's
  20. {
  21. for (int j = 0;j < 4;j++)
  22. {
  23. D[i][j] = 0.0;
  24. }
  25. }
  26. std::cout << "Enter coefficient of x1 square: ";
  27. std::cin >> D[0][0];
  28. std::cout << "Enter coefficient of x2 square: ";
  29. std::cin >> D[1][1];
  30. std::cout << "Enter coefficient of product x1*x2: ";
  31. std::cin >> D[0][1];
  32. D[0][1] = D[0][1] / 2;//the value of x1*x2 multiplication happen twice, so it has to be divided by two
  33. D[1][0] = D[0][1];
  34. //in other positions are 0's
  35. }
  36.  
  37. //function showing on the content of the matrix D on the console window
  38. void D_printer()
  39. {
  40. std::cout << "Matrix D" << std::endl;
  41. for (int i = 0;i < 4;i++)
  42. {
  43. for (int j = 0;j < 4;j++)
  44. {
  45. std::cout << D[i][j] << "\t";
  46. }
  47. std::cout << std::endl;
  48. }
  49. }
  50.  
  51. //function D checks if matrix is symmetric and nonnegatively defined; if it is returns 1, otherwise returns 0;
  52. bool D_checker()
  53. {
  54. for (int i = 0;i < 4;i++)//is it symmetrical?
  55. {
  56. for (int j = i;j < 4; j++)
  57. {
  58. if(D[i][j]!=D[j][i])
  59. {
  60. std::cout << "Matrix D is not simmetrical" << std::endl;
  61. return 0;
  62. }
  63. }
  64. }
  65. if ((D[0][0] * D[1][1] - D[0][1] * D[1][0]) < 0)//to check if it is nonnegatively defined in this case, it is necessary to check if 2x2
  66. {
  67. std::cout << "Matrix D is negatively defined" << std::endl;
  68. return 0;
  69. }
  70. return 1;
  71. }
  72.  
  73. void C_initializer()
  74. {
  75. std::cout << "Enter coefficient of x1: ";
  76. std::cin >> c[0];
  77. std::cout << "Enter coefficient of x2: ";
  78. std::cin >> c[1];
  79. c[3] = 0;
  80. c[4] = 0;
  81. }
  82.  
  83. //function showing on the content of the matrix c on the console window
  84. void C_printer()
  85. {
  86. std::cout << "Matrix c" << std::endl;
  87. for (int i = 0; i < 4;i++)
  88. {
  89. std::cout << c[i] << std::endl;
  90. }
  91. }
  92.  
  93. void Ab_initializer()
  94. {
  95. bool inequality;
  96. for (int i = 0;i < 2;i++)
  97. {
  98. std::cout << "Does the inequality sign in "<<i+1<<". constraint is =<? (1-yes, 0-no): ";
  99. std::cin >> inequality;
  100. if (inequality)
  101. {
  102. A[i][i+2] = 1;
  103. }
  104. else
  105. {
  106. A[i][i+2] = -1;
  107. }
  108. A[i][3 - i] = 0;
  109. std::cout << "Enter coefficient of x1 in " << i+1 << ". constraint: ";
  110. std::cin >> A[i][0];
  111. std::cout << "Enter coefficient of x2 in " << i+1 << ". constraint: ";
  112. std::cin >> A[i][1];
  113. std::cout << "Enter value of b in " << i+1 << ". constraint: ";
  114. std::cin >> b[i];
  115. }
  116. }
  117.  
  118. //function showing on the content of the matrix A on the console window
  119. void A_printer()
  120. {
  121. std::cout << "Matrix A" << std::endl;
  122. for (int i = 0;i < 2;i++)
  123. {
  124. for (int j = 0;j < 4;j++)
  125. {
  126. std::cout << A[i][j] << "\t";
  127. }
  128. std::cout<<std::endl;
  129. }
  130. }
  131.  
  132. //function showing on the content of the matrix b on the console window
  133. void b_printer()
  134. {
  135. std::cout << "Matrix b" << std::endl;
  136. std::cout << b[0] << std::endl;
  137. std::cout << b[1] << std::endl;
  138. }
  139.  
  140. void delta_setter()
  141. {
  142. for (int i = 0;i < 4;i++)
  143. {
  144. for (int j = i;j < 4;j++)
  145. {
  146. if (i == j)
  147. {
  148. if (c[i] < 0)
  149. {
  150. delta[i][j] = -1;
  151. }
  152. else
  153. {
  154. delta[i][j] = 1;
  155. }
  156. }
  157. else
  158. {
  159. delta[i][j] = delta[j][i] = 0;
  160. }
  161. }
  162. }
  163. }
  164.  
  165. //function showing on the content of the delta matrix on the console window
  166. void delta_printer()
  167. {
  168. std::cout << "Delta" << std::endl;
  169. for (int i = 0;i < 4;i++)
  170. {
  171. for (int j = 0;j < 4;j++)
  172. {
  173. std::cout << delta[i][j] << "\t";
  174. }
  175. std::cout << std::endl;
  176. }
  177. }
  178.  
  179. void simplex_setter()
  180. {
  181. for (int i = 0;i < 2;i++)
  182. {
  183. for (int j = 0;j < 4;j++)
  184. {
  185. simplex[i][j] = A[i][j];
  186. }
  187. }
  188.  
  189. for (int i = 2;i < 6;i++)
  190. {
  191. for (int j = 0;j < 4; j++)
  192. {
  193. simplex[i][j] = 2 * D[i - 2][j];
  194. }
  195. }
  196.  
  197. for (int i = 2;i < 6;i++)
  198. {
  199. for (int j = 4; j < 6;j++)
  200. {
  201. simplex[i][j] = -A[j - 4][i - 2];
  202. simplex[i][j+2] = A[j - 4][i - 2];
  203. }
  204. }
  205.  
  206. for (int i = 2;i < 6;i++)
  207. {
  208. for (int j = 8;j < 12;j++)
  209. {
  210. if (j == (i+6))
  211. {
  212. simplex[i][j] = -1;
  213. }
  214. else
  215. {
  216. simplex[i][j] = 0;
  217. }
  218. }
  219. }
  220.  
  221. for (int i = 2;i < 6;i++)
  222. {
  223. for (int j = 12;j < 16;j++)
  224. {
  225. simplex[i][j] = delta[i - 2][j - 12];
  226. }
  227. }
  228.  
  229. for (int i = 0;i < 2;i++)
  230. {
  231. simplex[i][16] = b[i];
  232. }
  233.  
  234. for (int i = 2;i < 6;i++)
  235. {
  236. if (c[i - 2] != 0)
  237. {
  238. simplex[i][16] = -c[i - 2];
  239. }
  240. else
  241. {
  242. simplex[i][16] = c[i - 2];
  243. }
  244. }
  245.  
  246. for (int j = 4;j < 16;j++)
  247. {
  248. simplex[0][j] = 0;
  249. simplex[1][j] = 0;
  250. }
  251. }
  252.  
  253. //function showing on the content of the simplex tableaux on the console window
  254. void simplex_printer()
  255. {
  256. std::cout << "Simplex" << std::endl;
  257. for (int i = 0;i < 6;i++)
  258. {
  259. for (int j = 0;j < 17;j++)
  260. {
  261. std::cout << simplex[i][j] << " ";
  262. }
  263. std::cout<<std::endl;
  264. }
  265. }
  266.  
  267. //function writing on the content of the simplex tableaux into "input.txt" file
  268. void output_creator()
  269. {
  270. std::ofstream output;
  271. output.open("input.txt");
  272. for (int i = 0;i < 6;i++)
  273. {
  274. for (int j = 0;j < 17;j++)
  275. {
  276. output << simplex[i][j] << " ";
  277. }
  278. output << std::endl;
  279. }
  280. output.close();
  281. }
  282.  
  283. int main()
  284. {
  285. std::cout << "PROBLEM OF MINIMIZATION" << std::endl;
  286. D_initializer();
  287. if (D_checker())
  288. {
  289. D_printer();
  290. C_initializer();
  291. C_printer();
  292. Ab_initializer();
  293. A_printer();
  294. b_printer();
  295. delta_setter();
  296. delta_printer();
  297. simplex_setter();
  298. simplex_printer();
  299. output_creator();
  300. std::cout << "Simplex succesfully created. Result is written in \"input.txt\" file." << std::endl;
  301. }
  302. else
  303. {
  304. std::cout << "Unnable to create simplex. Check wether given quadratic problem is solvable by aim of constructing matrix D or all data was entered correctly." << std::endl;
  305. }
  306. system("pause");
  307. return 0;
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement