|
@@ -139,8 +139,122 @@ namespace MinElKuzminEkz
|
|
|
return kolvo_polozh_komponent != n + m - 1;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ public static double CalculateCelevayaFunction(double[,] costs, int[,] supply_plan)
|
|
|
+ {
|
|
|
+ double result = 0;
|
|
|
|
|
|
+ for (int i = 0; i < costs.GetLength(0); i++)
|
|
|
+ {
|
|
|
+ for (int j = 0; j < costs.GetLength(1); j++)
|
|
|
+ {
|
|
|
+ if (supply_plan[i, j] > 0)
|
|
|
+ {
|
|
|
+ result += costs[i, j] * supply_plan[i, j];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Debug.WriteLine($"Рассчет целевой фунции = {result}");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static int[,] MinElementMethod(int[] a, int[] b, double[,] costs)
|
|
|
+ {
|
|
|
+ int n = a.Length;
|
|
|
+ int m = b.Length;
|
|
|
+ int[,] supply_plan = new int[n, m]; // матрица для опорного плана
|
|
|
+ bool[,] bools_suply_plan = new bool[n, m];
|
|
|
+
|
|
|
+ while (!CheckIfMatrixAllTrue(bools_suply_plan))
|
|
|
+ {
|
|
|
+ List<(int, int)> positions = FindMinElementPositions(costs, bools_suply_plan);
|
|
|
+ int min_j = int.MaxValue;
|
|
|
+ int index_of_min_j = -1;
|
|
|
+
|
|
|
+ for (int k = 0; k < positions.Count; k++)
|
|
|
+ {
|
|
|
+ if (positions[k].Item2 < min_j)
|
|
|
+ {
|
|
|
+ min_j = positions[k].Item2;
|
|
|
+ index_of_min_j = k;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ int min_i = positions.ElementAt(index_of_min_j).Item1;
|
|
|
+ int value = Math.Min(a[min_i], b[min_j]);
|
|
|
+
|
|
|
+ a[min_i] -= value;
|
|
|
+ b[min_j] -= value;
|
|
|
+ supply_plan[min_i, min_j] = value;
|
|
|
+ bools_suply_plan[min_i, min_j] = true;
|
|
|
+
|
|
|
+ if (a[min_i] == 0)
|
|
|
+ {
|
|
|
+ for (int j = 0; j < m; j++)
|
|
|
+ {
|
|
|
+ if (bools_suply_plan[min_i, j] == false)
|
|
|
+ {
|
|
|
+ bools_suply_plan[min_i, j] = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (b[min_j] == 0)
|
|
|
+ {
|
|
|
+ for (int i = 0; i < n; i++)
|
|
|
+ {
|
|
|
+ if (bools_suply_plan[i, min_j] == false)
|
|
|
+ {
|
|
|
+ bools_suply_plan[i, min_j] = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Debug.WriteLine($"Метод минимального элемента : опорный план инициализирован");
|
|
|
+ return supply_plan;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static bool CheckIfMatrixAllTrue(bool[,] bools)
|
|
|
+ {
|
|
|
+ for (int i = 0; i < bools.GetLength(0); i++)
|
|
|
+ {
|
|
|
+ for (int j = 0; j < bools.GetLength(1); j++)
|
|
|
+ {
|
|
|
+ if (bools[i, j] == false)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<(int, int)> FindMinElementPositions(double[,] costs, bool[,] bools)
|
|
|
+ {
|
|
|
+ List<(int, int)> positions = new List<(int, int)>();
|
|
|
+ double min = double.MaxValue;
|
|
|
+
|
|
|
+ for (int i = 0; i < costs.GetLength(0); i++)
|
|
|
+ {
|
|
|
+ for (int j = 0; j < costs.GetLength(1); j++)
|
|
|
+ {
|
|
|
+ if (bools[i, j] == false && costs[i, j] < min)
|
|
|
+ {
|
|
|
+ min = costs[i, j];
|
|
|
+ positions.Clear();
|
|
|
+ positions.Add((i, j));
|
|
|
+ }
|
|
|
+ else if (bools[i, j] == false && costs[i, j] == min)
|
|
|
+ {
|
|
|
+ positions.Add((i, j));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return positions;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|