Ejemplo n.º 1
0
 /// <summary>
 /// konštruktor
 /// </summary>
 /// <param name="function">kriteriálna funkcia</param>
 /// <param name="conditions">ohraničenia</param>
 /// <param name="type">typ úlohy lineárneho programovania</param>
 public LPTreeBody(Equation function, EquationsCollection conditions, LinearTypes type)
 {
     myRoot = new LPTreeItem(function, type, conditions);
     myUnprocessedItems.Add(myRoot);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// konštuktor
        /// </summary>
        /// <param name="function">kriteriálna funkcia</param>
        /// <param name="type">typ úlohy lineárneho programovaina</param>
        /// <param name="conditions">ohraničenia</param>
        public LPTreeItem(Equation function, LinearTypes type, EquationsCollection conditions)
        {
            myConditions = conditions;
            myFunction = function;
            myType = type;

            myFunctionBackup = (Equation)function.Clone();
            foreach (Equation item in conditions)
            {
                myConditionsBackup.Add((Equation)item.Clone());
            }

            Simplex simplex = new Simplex(myType, myFunction);
            simplex.InsertEquations(myConditions);
            Solution solution = simplex.Solve();
            myNodeSolution = solution;

            if (solution != null)
            {
                myHasSolution = true;
                double val = double.MinValue;
                int index = -1;
                for (int i = 0; i < solution.Count; i++)
                {
                    if (Math.Abs(Math.Round(solution[i]) - solution[i]) > 0.000005)
                    {
                        val = solution[i];
                        index = i;
                        break;
                    }
                }

                // Ak nie je nájdené riešenie celočíselné:
                if (val > double.MinValue + 0.000005)
                {
                    int left = (int)Math.Floor(val);
                    int right = (int)Math.Ceiling(val);

                    // príprava ľavej strany ohraničenia
                    List<double> leftSide = new List<double>();
                    for (int i = 0; i < myFunctionBackup.LeftSide.Count; i++)
                    {
                        if (i == index)
                        {
                            leftSide.Add(1);
                        }
                        else
                        {
                            leftSide.Add(0);
                        }
                    }

                    // príprava ohraničenia pre ľavého syna
                    EquationsCollection leftConditions = (EquationsCollection)myConditionsBackup.Clone();
                    leftConditions.Add(new Equation(leftSide, "<=", left));

                    // príprava ohraničenia pre pravého syna
                    EquationsCollection rightConditions = (EquationsCollection)myConditionsBackup.Clone();
                    rightConditions.Add(new Equation(leftSide, ">=", right));

                    // vytvorenie potomkov
                    myLeftChild = new LPTreeItem((Equation)myFunctionBackup.Clone(), myType, (EquationsCollection)leftConditions.Clone());
                    myRightChild = new LPTreeItem((Equation)myFunctionBackup.Clone(), myType, (EquationsCollection)rightConditions.Clone());

                    myUpperBound = myFunction.RightSide;
                }
                else
                {
                    // Ak je nájdené riešenie celočíselné:
                    myLowerBound = (int)myFunction.RightSide;
                    myUpperBound = (int)myFunction.RightSide;
                    myIsFinalNode = true;
                }
            }
        }