Esempio n. 1
0
        public void GoldfarbIdnaniMaximizeTest1()
        {
            // Solve the following optimization problem:
            //
            //  max f(x) = -2x² + xy - y² + 5y
            //
            //  s.t.   x + y  <= 0
            //             y  >= 0
            //

            // Create our objective function using a text string
            var f = new QuadraticObjectiveFunction("-2x² + xy - y² + 5y");

            // Now, create the constraints
            List <LinearConstraint> constraints = new List <LinearConstraint>();

            constraints.Add(new LinearConstraint(f, "x + y <= 0"));
            constraints.Add(new LinearConstraint(f, "    y >= 0"));

            // Now we create the quadratic programming solver for 2 variables, using the constraints.
            GoldfarbIdnaniQuadraticSolver solver = new GoldfarbIdnaniQuadraticSolver(2, constraints);

            // And attempt to solve it.
            double maxValue = solver.Maximize(f);

            Assert.AreEqual(25 / 16.0, maxValue);

            Assert.AreEqual(-5 / 8.0, solver.Solution[0]);
            Assert.AreEqual(5 / 8.0, solver.Solution[1]);
        }
Esempio n. 2
0
        /// <summary>
        ///   Computes the optimization algorithm when the user
        ///   presses the "Compute" button in the main interface.
        /// </summary>
        ///
        private void btnCompute_Click(object sender, EventArgs e)
        {
            // First, get what the user entered on screen:
            String strObjective = tbObjective.Text;

            String[] strConstraints = tbConstraints.Lines;

            // Check if this is a minimization or maximization task
            bool minimize = (string)comboBox1.SelectedItem == "min";

            // Now we can start creating our function:
            QuadraticObjectiveFunction function;

            LinearConstraint[] constraints = new LinearConstraint[strConstraints.Length];


            // Attempt to parse the string and create the objective function
            if (!QuadraticObjectiveFunction.TryParse(strObjective, out function))
            {
                tbSolution.Text = "Invalid objective function.";
                return;
            }

            // Create list of constraints
            for (int i = 0; i < constraints.Length; i++)
            {
                if (!LinearConstraint.TryParse(strConstraints[i], function, out constraints[i]))
                {
                    tbSolution.Text = "Invalid constraint at line " + i + ".";
                    return;
                }
            }


            // Now, after the text has been parsed into actual objects, finally create the solver
            var solver = new GoldfarbIdnaniQuadraticSolver(function.NumberOfVariables, constraints);

            double optimumValue;

            try
            {
                // Solve the optimization problem:
                optimumValue = (minimize) ?
                               solver.Minimize(function) : // the user wants to minimize it
                               solver.Maximize(function);  // the user wants to maximize it
            }
            catch (NonPositiveDefiniteMatrixException)
            {
                tbSolution.Text = "Function is not positive definite.";
                return;
            }
            catch (ConvergenceException)
            {
                tbSolution.Text = "No possible solution could be attained.";
                return;
            }


            // Retrieve the computed solution
            double[] solution = solver.Solution;

            // And let's format and display it:
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Solution:");
            sb.AppendLine();
            sb.AppendLine(" " + strObjective + " = " + optimumValue);
            sb.AppendLine();

            for (int i = 0; i < solution.Length; i++)
            {
                string variableName = function.Indices[i];
                sb.AppendLine(" " + variableName + " = " + solution[i]);
            }

            tbSolution.Text = sb.ToString();
        }
Esempio n. 3
0
        private void button1_Click(object sender, EventArgs e)
        {
            String objectiveString = tbObjective.Text;

            String[] constraintStrings = tbConstraints.Lines;
            bool     minimize          = (string)comboBox1.SelectedItem == "min";

            QuadraticObjectiveFunction function;

            LinearConstraint[] constraints = new LinearConstraint[constraintStrings.Length];

            try
            {
                // Create objective function
                function = new QuadraticObjectiveFunction(objectiveString);
            }
            catch (FormatException)
            {
                tbSolution.Text = "Invalid objective function.";
                return;
            }

            // Create list of constraints
            for (int i = 0; i < constraints.Length; i++)
            {
                try
                {
                    constraints[i] = new LinearConstraint(function, constraintStrings[i]);
                }
                catch (FormatException)
                {
                    tbSolution.Text = "Invalid constraint at line " + i + ".";
                    return;
                }
            }

            // Create solver
            var solver = new GoldfarbIdnaniQuadraticSolver(function.NumberOfVariables, constraints);

            try
            {
                // Solve the minimization or maximization problem
                double value = (minimize) ? solver.Minimize(function) : solver.Maximize(function);

                // Grab the solution found
                double[] solution = solver.Solution;

                // Format and display solution
                StringBuilder sb = new StringBuilder();

                sb.AppendLine("Solution:");
                sb.AppendLine();
                sb.AppendLine(" " + objectiveString + " = " + value);
                sb.AppendLine();
                for (int i = 0; i < solution.Length; i++)
                {
                    string variableName = function.Indices[i];
                    sb.AppendLine(" " + variableName + " = " + solution[i]);
                }

                tbSolution.Text = sb.ToString();
            }
            catch (NonPositiveDefiniteMatrixException)
            {
                tbSolution.Text = "Function is not positive definite.";
            }
            catch (ConvergenceException)
            {
                tbSolution.Text = "No possible solution could be attained.";
            }
        }