123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using System;
- using System.Collections.Generic;
- namespace Опорные_планы
- {
- internal class MethodOfPotentials
- {
- public static bool CalculatePotentials(int?[,] opornyPlan, int[,] postavki)
- {
- double?[] potenciali_u = new double?[opornyPlan.GetLength(0)];
- double?[] potenciali_v = new double?[opornyPlan.GetLength(1)];
- potenciali_u[0] = 0;
- bool potentialsFound = false;
- while (!potentialsFound)
- {
- potentialsFound = true;
- for (int i = 0; i < opornyPlan.GetLength(0); i++)
- {
- for (int j = 0; j < opornyPlan.GetLength(1); j++)
- {
- if (opornyPlan[i, j] != 0)
- {
- if (potenciali_u[i] != null && potenciali_v[j] == null)
- {
- potenciali_v[j] = postavki[i, j] - potenciali_u[i];
- Console.WriteLine(postavki[i, j]);
- Console.WriteLine(opornyPlan[i, j]);
- Console.WriteLine($"{postavki[i, j]} - {potenciali_u[i]} = {potenciali_v[j]}");
- }
- else if (potenciali_v[j] != null && potenciali_u[i] == null)
- {
- potenciali_u[i] = postavki[i, j] - potenciali_v[j];
- Console.WriteLine(postavki[i, j]);
- Console.WriteLine(opornyPlan[i, j]);
- Console.WriteLine($"{postavki[i, j]} - {potenciali_v[j]} = {potenciali_u[i]}");
- }
- }
- }
- }
- for (int i = 0; i < potenciali_u.Length; i++)
- {
- if (potenciali_u[i] == null)
- {
- potentialsFound = false;
- break;
- }
- }
- for (int i = 0; i < potenciali_v.Length; i++)
- {
- if (potenciali_v[i] == null)
- {
- potentialsFound = false;
- break;
- }
- }
- }
- bool result = true;
- Console.WriteLine("Потенциалы u: ");
- for (int i = 0; i < potenciali_u.Length; i++)
- {
- Console.Write(potenciali_u[i] + "; ");
- }
- Console.WriteLine("\nПотенциалы v:");
- for (int i = 0; i < potenciali_v.Length; i++)
- {
- Console.Write(potenciali_v[i] + "; ");
- }
- Console.WriteLine("\n");
- double coeff = 0;
- for (int i = 0; i < opornyPlan.GetLength(0); i++)
- {
- for (int j = 0; j < opornyPlan.GetLength(1); j++)
- {
- if (opornyPlan[i, j] == 0)
- {
- coeff = (double)potenciali_u[i] + (double)potenciali_v[j] - postavki[i, j];
- Console.WriteLine("Коффициент ячейки [" + i + ", " + j + "] равен " + coeff);
- if (coeff > 0)
- Console.WriteLine("\nИмеется положительный коэффицент!");
- result = false;
- }
- }
- }
- if (Plans.IsOptimal(opornyPlan, potenciali_u, potenciali_v, postavki))
- {
- Console.WriteLine("\nПлан оптимален\n");
- }
- else
- {
- Console.WriteLine("\nПлан не оптимален\n");
- }
- return result;
- }
- }
- }
|