MethodOfPotentials.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Опорные_планы
  4. {
  5. internal class MethodOfPotentials
  6. {
  7. public static bool CalculatePotentials(int?[,] opornyPlan, int[,] postavki)
  8. {
  9. double?[] potenciali_u = new double?[opornyPlan.GetLength(0)];
  10. double?[] potenciali_v = new double?[opornyPlan.GetLength(1)];
  11. potenciali_u[0] = 0;
  12. bool potentialsFound = false;
  13. while (!potentialsFound)
  14. {
  15. potentialsFound = true;
  16. for (int i = 0; i < opornyPlan.GetLength(0); i++)
  17. {
  18. for (int j = 0; j < opornyPlan.GetLength(1); j++)
  19. {
  20. if (opornyPlan[i, j] != 0)
  21. {
  22. if (potenciali_u[i] != null && potenciali_v[j] == null)
  23. {
  24. potenciali_v[j] = postavki[i, j] - potenciali_u[i];
  25. Console.WriteLine(postavki[i, j]);
  26. Console.WriteLine(opornyPlan[i, j]);
  27. Console.WriteLine($"{postavki[i, j]} - {potenciali_u[i]} = {potenciali_v[j]}");
  28. }
  29. else if (potenciali_v[j] != null && potenciali_u[i] == null)
  30. {
  31. potenciali_u[i] = postavki[i, j] - potenciali_v[j];
  32. Console.WriteLine(postavki[i, j]);
  33. Console.WriteLine(opornyPlan[i, j]);
  34. Console.WriteLine($"{postavki[i, j]} - {potenciali_v[j]} = {potenciali_u[i]}");
  35. }
  36. }
  37. }
  38. }
  39. for (int i = 0; i < potenciali_u.Length; i++)
  40. {
  41. if (potenciali_u[i] == null)
  42. {
  43. potentialsFound = false;
  44. break;
  45. }
  46. }
  47. for (int i = 0; i < potenciali_v.Length; i++)
  48. {
  49. if (potenciali_v[i] == null)
  50. {
  51. potentialsFound = false;
  52. break;
  53. }
  54. }
  55. }
  56. bool result = true;
  57. Console.WriteLine("Потенциалы u: ");
  58. for (int i = 0; i < potenciali_u.Length; i++)
  59. {
  60. Console.Write(potenciali_u[i] + "; ");
  61. }
  62. Console.WriteLine("\nПотенциалы v:");
  63. for (int i = 0; i < potenciali_v.Length; i++)
  64. {
  65. Console.Write(potenciali_v[i] + "; ");
  66. }
  67. Console.WriteLine("\n");
  68. double coeff = 0;
  69. for (int i = 0; i < opornyPlan.GetLength(0); i++)
  70. {
  71. for (int j = 0; j < opornyPlan.GetLength(1); j++)
  72. {
  73. if (opornyPlan[i, j] == 0)
  74. {
  75. coeff = (double)potenciali_u[i] + (double)potenciali_v[j] - postavki[i, j];
  76. Console.WriteLine("Коффициент ячейки [" + i + ", " + j + "] равен " + coeff);
  77. if (coeff > 0)
  78. Console.WriteLine("\nИмеется положительный коэффицент!");
  79. result = false;
  80. }
  81. }
  82. }
  83. if (Plans.IsOptimal(opornyPlan, potenciali_u, potenciali_v, postavki))
  84. {
  85. Console.WriteLine("\nПлан оптимален\n");
  86. }
  87. else
  88. {
  89. Console.WriteLine("\nПлан не оптимален\n");
  90. }
  91. return result;
  92. }
  93. }
  94. }