public static void TransformForSimplex(SimplexTable simplexTable, int startRow = 0)
 {
     for (int i = startRow; i < simplexTable.nRows; i++)
     {
         if (simplexTable.B[i] < 0)
         {
             for (int j = 0; j < simplexTable.nColumns; j++)
             {
                 simplexTable.A[i][j] *= -1;
             }
             simplexTable.B[i] *= -1;
             if (simplexTable.Sign[i] == "<=")
             {
                 simplexTable.Sign[i] = ">=";
             }
             else
             {
                 if (simplexTable.Sign[i] == ">=")
                 {
                     simplexTable.Sign[i] = "<=";
                 }
             }
         }
     }
     Transform(simplexTable, startRow);
 }
Exemplo n.º 2
0
        private void AddLimitation(SimplexTable simplexTable, int nOfDouble, Fraction value, string sign)
        {
            simplexTable.nRows++;
            simplexTable.A.Add(new List <Fraction>());
            for (int j = 0; j < simplexTable.nColumns; j++)
            {
                if (j != nOfDouble)
                {
                    simplexTable.A[simplexTable.A.Count - 1].Add(0);
                }
                else
                {
                    simplexTable.A[simplexTable.A.Count - 1].Add(1);
                }
            }
            simplexTable.Sign.Add((string)sign.Clone());
            Fraction intPart;

            intPart = (int)value;
            if (value < 0)
            {
                intPart = intPart - 1;
            }
            if (sign == ">=")
            {
                simplexTable.B.Add(intPart + 1);
            }
            else
            {
                simplexTable.B.Add(intPart);
            }
        }
        public static void Transform(SimplexTable simplexTable, int startRow = 0)
        {
            Fraction M = FindM(simplexTable);

            for (int i = startRow; i < simplexTable.nRows; i++)
            {
                simplexTable.nColumns++;
                AddNewColumn(simplexTable.A);
                simplexTable.A[i][simplexTable.A[i].Count - 1] = 1;
                if (simplexTable.Sign[i] == "<=")
                {
                    simplexTable.C.Add(0);
                    simplexTable.TypeOfVariable.Add(2);
                }
                else
                {
                    if (simplexTable.Sign[i] == ">=")
                    {
                        simplexTable.C.Add(M);
                        simplexTable.TypeOfVariable.Add(3);
                        simplexTable.nColumns++;
                        AddNewColumn(simplexTable.A);
                        simplexTable.A[i][simplexTable.A[i].Count - 1] = -1;
                        simplexTable.C.Add(0);
                        simplexTable.TypeOfVariable.Add(2);
                    }
                    else
                    {
                        simplexTable.C.Add(M);
                        simplexTable.TypeOfVariable.Add(3);
                    }
                }
                simplexTable.Sign[i] = "=";
            }
        }
Exemplo n.º 4
0
 public SimplexAlgorithm(SimplexTable simplexTable, bool taskForMax)
 {
     this.simplexTable = (SimplexTable)simplexTable.Clone();
     d          = new Fraction[simplexTable.nColumns];
     basis      = new int[simplexTable.nRows];
     TaskForMax = taskForMax;
 }
Exemplo n.º 5
0
 public SimplexAlgorithm GetResult(SimplexTable simplexTable, bool taskForMax)
 {
     if (!taskForMax)
     {
         simplexTable.ChangTargetFunction();
     }
     return(GetResult(simplexTable, 0));
 }
Exemplo n.º 6
0
        private SimplexAlgorithm GetResult(SimplexTable simplexTable, int startRowForTransform)
        {
            SimplexTable simplexT = (SimplexTable)simplexTable.Clone();

            CanonicalTransformation.TransformForSimplex(simplexT, startRowForTransform);
            Console.WriteLine(simplexT.ToString());
            SimplexAlgorithm smpAlg = new SimplexAlgorithm(simplexT, true);

            smpAlg.GetResultForSimplex();
            if (smpAlg.Result == null)
            {
                return(smpAlg);
            }
            int nOfDouble = IsDouble(smpAlg.Result);

            if (nOfDouble == -1)
            {
                return(smpAlg);
            }
            Console.WriteLine("double result({0})={1}", nOfDouble, smpAlg.Result[nOfDouble]);
            //Branch 1
            Console.WriteLine("start branch 1");
            SimplexTable simplexT1 = (SimplexTable)simplexT.Clone();

            AddLimitation(simplexT1, nOfDouble, smpAlg.Result[nOfDouble], "<=");
            SimplexAlgorithm smpAlg1;

            smpAlg1 = GetResult(simplexT1, simplexT1.nRows - 1);

            //Brackh 2
            Console.WriteLine("start branch 2");
            SimplexTable simplexT2 = (SimplexTable)simplexT.Clone();

            AddLimitation(simplexT2, nOfDouble, smpAlg.Result[nOfDouble], ">=");
            SimplexAlgorithm smpAlg2;

            smpAlg2 = GetResult(simplexT2, simplexT2.nRows - 1);
            if (smpAlg1.Result == null)
            {
                return(smpAlg2);
            }
            if (smpAlg2.Result == null)
            {
                return(smpAlg1);
            }
            if (smpAlg1.FunctionValue > smpAlg2.FunctionValue)
            {
                return(smpAlg1);
            }
            else
            {
                return(smpAlg2);
            }
        }
        private static Fraction FindM(SimplexTable st)
        {
            Fraction max = st.C[0].Abs();

            for (int j = 0; j < st.nColumns; j++)
            {
                if ((st.C[j].Abs() > max) && (st.TypeOfVariable[j] == 1))
                {
                    max = st.C[j].Abs();
                }
            }
            return(max * (-10));
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            //new changes
            Stopwatch sw = new Stopwatch();

            sw.Start();
            SimplexTable st = MatrixReader.ReadSimplexTable("MatrixA.txt", "VectorB.txt", "VectorC.txt", "Signs.txt");
            //CanonicalTransformation.TransformForSimplex(st);
            //SimplexAlgorithm sa = new SimplexAlgorithm(st, true);
            //sa.GetResultForSimplex();
            BranchAndBoundAlgorithm bb = new BranchAndBoundAlgorithm();
            SimplexAlgorithm        sa = bb.GetResult(st, true);

            Console.Write("\nResult\n{\t");
            for (int i = 0; i < sa.Result.Length; i++)
            {
                Console.Write(sa.Result[i] + "\t");
            }
            Console.WriteLine("}");
            Console.Write("Function value {0} ", sa.FunctionValue);
            sw.Stop();
            Console.WriteLine("{0} ms", sw.ElapsedMilliseconds);
            Console.ReadLine();
        }
Exemplo n.º 9
0
        public static SimplexTable ReadSimplexTable(string matrixA, string vectorB, string vectorC, string sign)
        {
            SimplexTable st = new SimplexTable(ReadMatrix(matrixA), ReadVector(vectorB), ReadVector(vectorC), ReadStringVector(sign));

            return(st);
        }