Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. using ElectronicRelayManufacturing.Data.Info;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ElectronicRelayManufacturing.Server.Methods
  9. {
  10. class SimplexMethod : IMethod
  11. {
  12. private decimal[,] _matrix;
  13. private decimal[] _res;
  14. private decimal[] _att;
  15. private string[] _rows;
  16. private int _xCount = 3;
  17. private int _restCount;
  18.  
  19.  
  20. public SimplexMethod(FunctionCoefficients funCoefficients, List<Limit> limits)
  21. {
  22. _matrix = CreateMatrix(funCoefficients, limits);
  23. _restCount = limits.Count;
  24. _rows = CreateRows(funCoefficients, limits);
  25. _res = CreateRes(funCoefficients, limits);
  26. _att = new decimal[_res.Length];
  27.  
  28. }
  29.  
  30. public Result GetResult()
  31. {
  32. var stepCount = 0;
  33. while(IsFine())
  34. {
  35. PrintMatrix();
  36. Step();
  37. stepCount++;
  38. }
  39.  
  40. var result = new decimal[_xCount];
  41. for (var i = 0; i < _xCount; i++)
  42. {
  43. result[i] = 0;
  44. }
  45. for (var i = 0; i < _restCount + 1; i++)
  46. {
  47. if (_rows[i][0] == 'x')
  48. {
  49. result[int.Parse(_rows[i][1].ToString()) - 1] = _res[i];
  50. }
  51. }
  52. return new Result
  53. {
  54. Profit = (double)_res[0],
  55. RelayCounts = new List<int>
  56. {
  57. (int)result[0],
  58. (int)result[1],
  59. (int)result[2],
  60. }
  61. };
  62. }
  63.  
  64. private void Step()
  65. {
  66. var min = 0M;
  67. var colIndex = -1;
  68. for (var i = 0; i < _xCount + _restCount; i++)
  69. {
  70. if (min > _matrix[0, i])
  71. {
  72. min = _matrix[0, i];
  73. colIndex = i;
  74. }
  75. }
  76.  
  77. for (var i = 1; i < _restCount + 1; i++)
  78. {
  79. if (_matrix[i, colIndex] == 0)
  80. {
  81. _att[i] = decimal.MaxValue;
  82. }
  83. else
  84. {
  85. _att[i] = _res[i] / _matrix[i, colIndex];
  86. }
  87. }
  88.  
  89. var rowIndex = -1;
  90. min = decimal.MaxValue;
  91. for (var i = 1; i < _restCount + 1; i++)
  92. {
  93. if (_att[i] > 0 && _att[i] < min)
  94. {
  95. min = _att[i];
  96. rowIndex = i;
  97. }
  98. }
  99.  
  100. var element = _matrix[rowIndex, colIndex];
  101. for (var i = 0; i < _xCount + _restCount; i++)
  102. {
  103. _matrix[rowIndex, i] = Math.Round(_matrix[rowIndex, i] / element, 2);
  104. }
  105. _res[rowIndex] = Math.Round(_res[rowIndex] / element, 2);
  106.  
  107. var symbol = colIndex < _xCount ? 'x' : 's';
  108. var number = colIndex < _xCount ? colIndex + 1 : colIndex - _restCount + 2;
  109. _rows[rowIndex] = symbol + number.ToString();
  110.  
  111.  
  112.  
  113.  
  114. for (var j = 0; j < _restCount + 1; j++)
  115. {
  116. if (j != rowIndex)
  117. {
  118. var lineForPlus = new decimal[_xCount + _restCount];
  119. for (var i = 0; i < _xCount + _restCount; i++)
  120. {
  121. lineForPlus[i] = Math.Round(_matrix[rowIndex, i] * _matrix[j, colIndex], 2);
  122. }
  123.  
  124. var resForPlus = Math.Round(_res[rowIndex] * _matrix[j, colIndex], 2);
  125.  
  126. _res[j] -= resForPlus;
  127.  
  128. for (var i = 0; i < _xCount + _restCount; i++)
  129. {
  130. _matrix[j, i] -= lineForPlus[i];
  131. }
  132. }
  133. }
  134. }
  135.  
  136. private void PrintMatrix()
  137. {
  138. for (int i = 0; i < 1 + _restCount; i++)
  139. {
  140. for (int j = 0; j < _xCount + _restCount; j++)
  141. {
  142. Console.Write(_matrix[i, j] + " ");
  143.  
  144. }
  145.  
  146. Console.WriteLine();
  147. }
  148. Console.WriteLine();
  149. }
  150.  
  151. private decimal[,] CreateMatrix(FunctionCoefficients funCoefficients, List<Limit> limits)
  152. {
  153. var columnCount = 3 + limits.Count;
  154. var rowCount = 1 + limits.Count;
  155.  
  156. var matrix = new decimal[rowCount, columnCount];
  157.  
  158. for (int i = 0; i < rowCount; i++)
  159. for (int j = 0; j < columnCount; j++)
  160. matrix[i, j] = 0;
  161.  
  162. matrix[0, 0] = (decimal)-funCoefficients.X1;
  163. matrix[0, 1] = (decimal)-funCoefficients.X2;
  164. matrix[0, 2] = (decimal)-funCoefficients.X3;
  165.  
  166. for(int i = 1; i < rowCount; i++)
  167. {
  168. for(int j = 0; j < 3; j++)
  169. {
  170. matrix[i, j] = (decimal)Math.Abs(limits[i - 1].Coefficients[j]);
  171. }
  172. }
  173.  
  174. int rowIndexToOne = 0;
  175. var values = new decimal[]
  176. {
  177. 1, 1, -1, -1, -1, -1
  178. };
  179. for(int i = 1; i < rowCount; i++)
  180. {
  181. for(int j = 3; j < columnCount; j++)
  182. {
  183. if(j - 3 == rowIndexToOne)
  184. {
  185. matrix[i, j] = values[rowIndexToOne];
  186. }
  187. }
  188. rowIndexToOne += 1;
  189. }
  190.  
  191. return matrix;
  192. }
  193.  
  194. private string[] CreateRows(FunctionCoefficients funCoefficients, List<Limit> limits)
  195. {
  196. var rows = new string[_restCount + 1];
  197.  
  198. rows[0] = "z";
  199. for(int i = 1; i < _restCount + 1; i++)
  200. {
  201. rows[i] = $"s{i}";
  202. }
  203.  
  204. return rows;
  205. }
  206.  
  207. private decimal[] CreateRes(FunctionCoefficients funCoefficients, List<Limit> limits)
  208. {
  209. var res = new decimal[_restCount + 1];
  210.  
  211. res[0] = 0;
  212.  
  213. int resIndex = 1;
  214. foreach(var limit in limits)
  215. {
  216. res[resIndex++] = (decimal)Math.Abs(limit.Value);
  217. }
  218.  
  219. return res;
  220. }
  221.  
  222. private bool IsFine()
  223. {
  224. for (int j = 0; j < _xCount + _restCount; j++)
  225. {
  226. if (_matrix[0, j] < 0) return true;
  227. }
  228.  
  229. return false;
  230. }
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement