whosfsd

C#

Apr 6th, 2021
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8.  
  9. namespace Transport
  10. {//северно-западный угол
  11. class NorthWest
  12. {
  13. public static double[,] goods;
  14. public static double[,] cost;
  15. public static double[] need;
  16. public static double[] stock;
  17. public static double[] Vpotent;
  18. public static double[] Upotent;
  19. public static DataGridView dataGridView;
  20. public static DataGridView dataGridView2;
  21. public static Label summa;
  22. public static double CurrentSum = 0;
  23. public NorthWest(DataGridView _dataGridView,DataGridView _dataGridView2, Label label)
  24. {
  25. summa = label;
  26. dataGridView = _dataGridView;
  27. dataGridView2 = _dataGridView2;
  28. cost = new double[dataGridView.RowCount - 1, dataGridView.ColumnCount - 1];
  29. for (int i = 0; i < dataGridView.RowCount - 1; i++)
  30. {
  31. for (int j = 0; j < dataGridView.ColumnCount - 1; j++)
  32. {
  33. cost[i, j] = Double.Parse((String)dataGridView.Rows[i].Cells[j].Value);
  34. }
  35. }
  36. need = new double[dataGridView.RowCount - 1];
  37. for (int i = 0; i < dataGridView.RowCount - 1; i++)
  38. {
  39. need[i] = Double.Parse((String)dataGridView.Rows[i].Cells[dataGridView.ColumnCount - 1].Value);
  40. dataGridView2.Rows[i].Cells[dataGridView.ColumnCount - 1].Value = need[i];
  41. }
  42. stock = new double[dataGridView.ColumnCount - 1];
  43. for (int i = 0; i < dataGridView.ColumnCount - 1; i++)
  44. {
  45. stock[i] = Double.Parse((String)dataGridView.Rows[dataGridView.RowCount - 1].Cells[i].Value);
  46. dataGridView2.Rows[dataGridView.RowCount - 1].Cells[i].Value = stock[i];
  47. }
  48. }
  49.  
  50. public void Show()
  51. {
  52. for (int i = 0; i < dataGridView.RowCount - 1; i++)
  53. {
  54. for (int j = 0; j < dataGridView.ColumnCount - 1; j++)
  55. {
  56. MessageBox.Show((cost[i, j]).ToString());
  57. }
  58. }
  59. }
  60.  
  61. public void Showneed()
  62. {
  63. foreach (var item in need)
  64. {
  65. MessageBox.Show(item.ToString());
  66. }
  67. }
  68.  
  69. public void Showstock()
  70. {
  71. foreach (var item in stock)
  72. {
  73. MessageBox.Show(item.ToString());
  74. }
  75. }
  76.  
  77. public void FormPlan()
  78. {
  79. int rawsInCost = cost.GetLength(0);
  80. int columnsInCost = cost.GetLength(1);
  81. goods = new double[rawsInCost, columnsInCost];
  82. for(int i=0; i< rawsInCost; i++)
  83. {
  84. while (need[i] > 0)
  85. {
  86. for(int j = 0; j < columnsInCost; j++)
  87. {
  88. if (need[i] >= stock[j])
  89. {
  90. goods[i, j] = stock[j];
  91. need[i] -= stock[j];
  92. stock[j] = 0;
  93. }
  94. else
  95. {
  96. goods[i, j] = need[i];
  97. stock[j] -= need[i];
  98. need[i] = 0;
  99. }
  100. }
  101. }
  102. }
  103. for (int i = 0; i < dataGridView2.RowCount - 1; i++)
  104. {
  105. for (int j = 0; j < dataGridView2.ColumnCount - 1; j++)
  106. {
  107. dataGridView2.Rows[i].Cells[j].Value = goods[i, j].ToString() + "(" + cost[i, j] + ")";
  108. }
  109. }
  110. //общ. стоимость перевозки
  111. double Sum = 0;
  112. for (int i = 0; i < rawsInCost; i++)
  113. {
  114. for (int j = 0; j < columnsInCost; j++)
  115. {
  116. if (goods[i, j] > 0)
  117. Sum += goods[i, j] * cost[i, j];
  118. }
  119. }
  120. summa.Text += Sum.ToString();
  121. summa.Visible = true;
  122. }
  123. //оптимизация методом потенциалов
  124. public static void FindSolution()
  125. {
  126.  
  127. bool isOptimal = false;
  128. double[] Vpotent = new double[goods.GetLength(0)];
  129. for (int i = 0; i < Vpotent.Length; i++)
  130. {
  131. Vpotent[i] = 99999;
  132. }
  133. Upotent = new double[dataGridView.ColumnCount - 1];
  134. for (int i = 0; i < Upotent.Length; i++)
  135. {
  136. Upotent[i] = 99999;
  137. }
  138.  
  139. //макс. тариф в заполненных ячейках
  140. double MaxCost = 0;
  141. int maxV = 0;
  142. int maxU = 0;
  143. for (int i = 0; i < cost.GetLength(0); i++)
  144. {
  145. for (int j = 0; j < cost.GetLength(1); j++)
  146. {
  147. if (goods[i, j] != 0 && cost[i, j] > MaxCost)
  148. {
  149. MaxCost = cost[i, j];
  150. maxV = i;
  151. maxU = j;
  152. }
  153. }
  154. }
  155. //расчет потенциалов
  156. Vpotent[maxV] = 0;
  157. Upotent[maxU] = cost[maxV, maxU] - Vpotent[maxV];
  158. for (int sania = 0; sania < cost.GetLength(1); sania++)
  159. {
  160. for (int i = 0; i < cost.GetLength(0); i++)
  161. {
  162. for (int j = 0; j < cost.GetLength(1); j++)
  163. {
  164. if (goods[i, j] != 0 && (Vpotent[i] == 99999 || Upotent[j] == 99999))
  165. {
  166. if (Vpotent[i] == 99999 & Upotent[j] == 99999)
  167. continue;
  168. if (Vpotent[i] != 99999)
  169. {
  170. for (int k = 0; k < cost.GetLength(1); k++)
  171. {
  172. if (goods[i, k] != 0)
  173. {
  174. Upotent[k] = cost[i, k] - Vpotent[i];
  175. }
  176. }
  177. }
  178. if (Upotent[j] != 99999)
  179. {
  180. for (int k = 0; k < cost.GetLength(0); k++)
  181. {
  182. if (goods[k, j] != 0)
  183. {
  184. Vpotent[k] = cost[k, j] - Upotent[j];
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }
  192.  
  193. //анализ данного плана
  194. //матрица дельта
  195. double[,] delta = new double[cost.GetLength(0), cost.GetLength(1)];
  196. double Maxdelta = 0;
  197. int maxi = int.MaxValue, maxj = int.MaxValue;
  198. //
  199. for (int i = 0; i < cost.GetLength(0); i++)
  200. {
  201. for (int j = 0; j < cost.GetLength(1); j++)
  202. {
  203. if (goods[i, j] == 0)
  204. {
  205. delta[i, j] = Vpotent[i] + Upotent[j] - cost[i, j];
  206. if (delta[i, j] > 0)
  207. {
  208. isOptimal = false;
  209. if (delta[i, j] > Maxdelta)
  210. {
  211. Maxdelta = delta[i, j];
  212. maxi = i;
  213. maxj = j;
  214. }
  215. }
  216. }
  217. }
  218. }
  219. if (maxi == int.MaxValue && maxj == int.MaxValue)
  220. {
  221. MessageBox.Show("План оптимален. ");
  222. isOptimal = true;
  223. summa.Text = "План оптимален. Стоимость перевозки:" + CurrentSum;
  224. }
  225. else
  226. {
  227. double MaxCostInRowWithDelta = 0;
  228. int MaxCostInRowWithDeltaI = 0, MaxCostInRowWithDeltaJ = 0;
  229. //находим ячейку с товаром и макс. тарифом в строчке с макс. дельта
  230. for (int j = 0; j < goods.GetLength(1); j++)
  231. {
  232. if (goods[maxi, j] != 0 && cost[maxi, j] > MaxCostInRowWithDelta)
  233. {
  234. MaxCostInRowWithDelta = cost[maxi, j];
  235. MaxCostInRowWithDeltaI = maxi;
  236. MaxCostInRowWithDeltaJ = j;
  237. }
  238. }
  239. //находим ячейку с товаром и макс. тарифом в столбце с макс. дельта
  240. double MaxCostInCOLUMNWithDelta = 0;
  241. int MaxCostInColumnWithDeltaI = 0, MaxCostInColumnWithDeltaJ = 0;
  242.  
  243. for (int i = 0; i < goods.GetLength(0); i++)
  244. {
  245. if (goods[i, maxj] != 0 && cost[i, maxj] > MaxCostInCOLUMNWithDelta)
  246. {
  247. MaxCostInCOLUMNWithDelta = cost[i, maxj];
  248. MaxCostInColumnWithDeltaI = i;
  249. MaxCostInColumnWithDeltaJ = maxj;
  250. }
  251. }
  252. //находим, сколько товара мы можем переместить
  253. double MaxAmountWeCanAfford ;
  254. if (goods[MaxCostInColumnWithDeltaI, MaxCostInColumnWithDeltaJ] > goods[MaxCostInRowWithDeltaI, MaxCostInRowWithDeltaJ])
  255. {
  256. MaxAmountWeCanAfford = goods[MaxCostInRowWithDeltaI, MaxCostInRowWithDeltaJ];
  257. }
  258. else
  259. {
  260. MaxAmountWeCanAfford = goods[MaxCostInColumnWithDeltaI, MaxCostInColumnWithDeltaJ];
  261. }
  262. //перемещаем товар
  263. goods[MaxCostInRowWithDeltaI, MaxCostInRowWithDeltaJ] -= MaxAmountWeCanAfford;
  264. goods[maxi, maxj] += MaxAmountWeCanAfford;
  265. goods[MaxCostInColumnWithDeltaI, MaxCostInColumnWithDeltaJ] -= MaxAmountWeCanAfford;
  266. goods[MaxCostInColumnWithDeltaI, MaxCostInRowWithDeltaJ] += MaxAmountWeCanAfford;
  267.  
  268. //вывод результата
  269. for (int i = 0; i < dataGridView2.RowCount - 1; i++)
  270. {
  271. for (int j = 0; j < dataGridView2.ColumnCount - 1; j++)
  272. {
  273. dataGridView2.Rows[i].Cells[j].Value = goods[i, j].ToString() + "(" + cost[i, j] + ")";
  274. }
  275. }
  276.  
  277.  
  278. CurrentSum = 0;
  279. for (int i = 0; i < goods.GetLength(0); i++)
  280. for (int j = 0; j < goods.GetLength(1); j++)
  281. if (goods[i, j] > 0)
  282. CurrentSum += goods[i, j] * cost[i, j];
  283.  
  284. summa.Text = "План приближен к оптимальному. Стоимость перевозки: " + CurrentSum;
  285.  
  286. }
  287.  
  288.  
  289. }
  290. }
  291. }
Add Comment
Please, Sign In to add comment