MinimalEl.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace MinEl
  7. {
  8. internal class MinimalEl
  9. {
  10. double[][] arrayTarif;
  11. int[] suppliers, customers;
  12. int[][] arraydelivery;
  13. bool[][] checkCell;
  14. double targetFunction;
  15. public MinimalEl(double[][] arrayTarif, int[][] supliersAndCustomers)
  16. {
  17. this.arrayTarif = arrayTarif;
  18. suppliers = supliersAndCustomers[0];
  19. customers = supliersAndCustomers[1];
  20. arraydelivery = new int[arrayTarif.Count()][];
  21. checkCell = new bool[arrayTarif.Count()][];
  22. for (int i = 0; i < arrayTarif.Count(); i++)
  23. {
  24. arraydelivery[i] = new int[arrayTarif[i].Count()];
  25. checkCell[i] = new bool[arrayTarif[i].Count()];
  26. }
  27. for (int i = 0; i < arraydelivery.Count(); i++)
  28. {
  29. for (int j = 0; j < arraydelivery[i].Count(); j++)
  30. checkCell[i][j] = false;
  31. }
  32. }
  33. void InsertCelslAfterDelivery(int delivery, int indexRow, int indexCol)
  34. {
  35. arraydelivery[indexRow][indexCol] = delivery;
  36. checkCell[indexRow][indexCol] = true;
  37. if (suppliers[indexRow] == 0)
  38. {
  39. for (int i = 0; i < arrayTarif[0].Count(); i++)
  40. {
  41. if (arraydelivery[indexRow][i] != delivery & arraydelivery[indexRow][i]==0)
  42. {
  43. arraydelivery[indexRow][i] = 0;
  44. checkCell[indexRow][i] = true;
  45. }
  46. }
  47. }
  48. if (customers[indexCol] == 0)
  49. {
  50. for (int i = 0; i < arrayTarif.Count(); i++)
  51. {
  52. if (arraydelivery[i][indexCol] != delivery & arraydelivery[i][indexCol] == 0)
  53. {
  54. arraydelivery[i][indexCol] = 0;
  55. checkCell[i][indexCol] = true;
  56. }
  57. }
  58. }
  59. }
  60. public void TargetFunction()
  61. {
  62. for (int i = 0; i < arraydelivery.Count(); i++)
  63. {
  64. for (int j = 0; j < arraydelivery[i].Count(); j++)
  65. if (arraydelivery[i][j]!=0)
  66. targetFunction += arraydelivery[i][j]*arrayTarif[i][j];
  67. }
  68. }
  69. public void RealizationAlg()
  70. {
  71. while (true)
  72. {
  73. if (checkCell.All(row => row.All(el => el == true)))
  74. break;
  75. double minTarif = int.MaxValue;
  76. int indexRow = int.MaxValue;
  77. int indexCol = int.MaxValue;
  78. for (int i = 0; i < arrayTarif.Count(); i++)
  79. {
  80. for (int j = 0; j < arrayTarif[i].Count(); j++)
  81. {
  82. if (minTarif > arrayTarif[i][j] & !checkCell[i][j])
  83. {
  84. minTarif = arrayTarif[i][j];
  85. indexRow = i; indexCol = j;
  86. }
  87. }
  88. }
  89. int delivery = Math.Min(suppliers[indexRow], customers[indexCol]);
  90. suppliers[indexRow]-=delivery;
  91. customers[indexCol]-=delivery;
  92. InsertCelslAfterDelivery(delivery, indexRow, indexCol);
  93. }
  94. TargetFunction();
  95. }
  96. public override string ToString()
  97. {
  98. string optPlan = string.Empty;
  99. foreach (int[] line in arraydelivery)
  100. {
  101. foreach (int item in line)
  102. optPlan += $"{item} ";
  103. optPlan +="\n";
  104. }
  105. return $"{optPlan}\n{targetFunction}";
  106. }
  107. }
  108. }