public void GoldfarbIdnaniParseTest()
        {
            var s = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;

            String strObjective = "0" + s + "5x² + 0" + s + "2y² + 0" + s + "3xy";

            String[] strConstraints =
            {
                "0" + s + "01x + 0" + s + "02y - 0" + s + "03 = 0",
                "x + y = 100"
            };

            // Now we can start creating our function:
            QuadraticObjectiveFunction function = new QuadraticObjectiveFunction(strObjective, CultureInfo.CurrentCulture);
            LinearConstraintCollection cst      = new LinearConstraintCollection();

            foreach (var tmpCst in strConstraints)
            {
                cst.Add(new LinearConstraint(function, tmpCst, CultureInfo.CurrentCulture));
            }

            var    classSolver = new Accord.Math.Optimization.GoldfarbIdnani(function, cst);
            bool   status      = classSolver.Minimize();
            double result      = classSolver.Value;

            Assert.IsTrue(status);
            Assert.AreEqual(15553.60, result, 1e-10);
        }
        public void GoldfarbIdnaniParseGlobalizationTest2()
        {
            String strObjective = 0.5.ToString(CultureInfo.InvariantCulture)
                                  + "x² +" + 0.2.ToString(CultureInfo.InvariantCulture) + "y² +"
                                  + 0.3.ToString(CultureInfo.InvariantCulture) + "xy";

            String[] strConstraints =
            {
                0.01.ToString(CultureInfo.InvariantCulture) + "x" + " + " +
                0.02.ToString(CultureInfo.InvariantCulture) + "y - " +
                0.03.ToString(CultureInfo.InvariantCulture) + " = 0",
                "x + y = 100"
            };

            QuadraticObjectiveFunction function = new QuadraticObjectiveFunction(strObjective);
            LinearConstraintCollection cst      = new LinearConstraintCollection();

            foreach (var tmpCst in strConstraints)
            {
                cst.Add(new LinearConstraint(function, tmpCst));
            }

            var    classSolver = new Accord.Math.Optimization.GoldfarbIdnani(function, cst);
            bool   status      = classSolver.Minimize();
            double result      = classSolver.Value;

            Assert.IsTrue(status);
            Assert.AreEqual(15553.60, result, 1e-10);
        }
        public void GoldfarbIdnaniParseGlobalizationTestBase()
        {
            // minimize 0.5x² + 0.2y² + 0.3xy s.t. 0.01x + 0.02y - 0.03 = 0 AND x + y = 100
            // http://www.wolframalpha.com/input/?i=minimize+0.5x%C2%B2+%2B+0.2y%C2%B2+%2B+0.3xy+s.t.+0.01x+%2B+0.02y+-+0.03+%3D+0+AND+x+%2B+y+%3D+100

            String strObjective = "0.5x² + 0.2y² + 0.3xy";

            String[] strConstraints =
            {
                "0.01x + 0.02y - 0.03 = 0",
                "x + y = 100"
            };

            QuadraticObjectiveFunction function = new QuadraticObjectiveFunction(strObjective);
            LinearConstraintCollection cst      = new LinearConstraintCollection();

            foreach (var tmpCst in strConstraints)
            {
                cst.Add(new LinearConstraint(function, tmpCst));
            }

            var    classSolver = new Accord.Math.Optimization.GoldfarbIdnani(function, cst);
            bool   status      = classSolver.Minimize();
            double result      = classSolver.Value;

            Assert.IsTrue(status);
            Assert.AreEqual(15553.60, result, 1e-10);
        }
Example #4
0
        public void GoldfarbIdnaniMinimizeTest1()
        {
            // This test reproduces Issue #33 at Google Code Tracker
            // https://code.google.com/p/accord/issues/detail?id=33

            // Create objective function using the
            // Hessian Q and linear terms vector d.

            double[,] Q =
            {
                {  0.12264004, 0.011579293, 0.103326825, 0.064073439 },
                { 0.011579293,    0.033856, 0.014311947, 0.014732381 },
                { 0.103326825, 0.014311947,  0.17715681, 0.067615114 },
                { 0.064073439, 0.014732381, 0.067615114,  0.11539609 }
            };

            Assert.IsTrue(Q.IsPositiveDefinite());

            double[] d = { 0, 0, 0, 0 };

            var f = new QuadraticObjectiveFunction(Q, d, "a", "b", "c", "d");

            // Now, create the constraints
            var constraints = new LinearConstraintCollection();

            constraints.Add(new LinearConstraint(f, "0.0732 * a + 0.0799 * b + 0.1926 * c + 0.0047 * d = 0.098"));
            constraints.Add(new LinearConstraint(f, "a + b + c + d = 1"));
            constraints.Add(new LinearConstraint(f, "a >= 0"));
            constraints.Add(new LinearConstraint(f, "b >= 0"));
            constraints.Add(new LinearConstraint(f, "c >= 0"));
            constraints.Add(new LinearConstraint(f, "d >= 0"));
            constraints.Add(new LinearConstraint(f, "a >= 0.5"));

            double[] b;
            int      eq;

            double[,] A = constraints.CreateMatrix(4, out b, out eq);

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

            // And attempt to solve it.
            double minValue = solver.Minimize(f);

            double[] expected = { 0.5, 0.336259542, 0.163740458, 0 };
            double[] actual   = solver.Solution;

            for (int i = 0; i < expected.Length; i++)
            {
                double e = expected[i];
                double a = actual[i];
                Assert.AreEqual(e, a);
            }
        }
Example #5
0
        private static void DoSv(Tuple<double[], double>[] classified)
        {
            var stripped = classified.Select(t => new Tuple<double[], double>(t.Item1.Skip(1).ToArray(), t.Item2))
            .ToArray();
              var q = GetQ(stripped);

              //all are 0 or more
              var lcc = new LinearConstraintCollection(
            Enumerable.Range(0, stripped.Length)
              .Select(i => new LinearConstraint(1)
              {
            VariablesAtIndices = new[] { i },
            ShouldBe = ConstraintType.GreaterThanOrEqualTo,
            Value = 0.0,
            CombinedAs= new []{1.0} //???
              }));
              //and they zero out with the classificaiton
              lcc.Add(
            //new LinearConstraint(stripped.Select(t => t.Item2).ToArray()) { Value = 0, ShouldBe = ConstraintType.EqualTo }
            new LinearConstraint(stripped.Length)
            {
              Value = 0,
              ShouldBe = ConstraintType.EqualTo,
              VariablesAtIndices = Enumerable.Range(0, stripped.Length).ToArray(),
              CombinedAs = stripped.Select(t => t.Item2).ToArray()
            }        );

              var solver = new GoldfarbIdnaniQuadraticSolver(stripped.Length, lcc);
              solver.Minimize(q, neg1Array(stripped.Length));
              //b  = 1/y - w_*x_

              var tmp = solver.Solution.Zip(stripped, (alpha, s) => s.Item1.Select(x => x * alpha * s.Item2))
            .Aggregate(VAdd).ToList();
              var support = solver.Solution.Zip(stripped, (alpha, s) => Math.Abs(alpha) > 0.0001 ? s : null).Where(x => x != null).ToArray();
              var offset = GetOffset(support, tmp); //I'm pretty sure this calculation for b is wrong: you get a different one for each of elements...
              tmp.Insert(0, offset);
              var wSupp = Norm( tmp.ToArray());
        }
Example #6
0
        public void GoldfarbIdnaniMinimizeTest1()
        {
            // This test reproduces Issue #33 at Google Code Tracker
            // https://code.google.com/p/accord/issues/detail?id=33

            // Create objective function using the
            // Hessian Q and linear terms vector d.

            double[,] Q =
            {
                { 0.12264004,  0.011579293, 0.103326825, 0.064073439 },
                { 0.011579293, 0.033856,    0.014311947, 0.014732381 },
                { 0.103326825, 0.014311947, 0.17715681,  0.067615114 },
                { 0.064073439, 0.014732381, 0.067615114, 0.11539609 }
            };

            Assert.IsTrue(Q.IsPositiveDefinite());

            double[] d = { 0, 0, 0, 0 };

            var f = new QuadraticObjectiveFunction(Q, d, "a", "b", "c", "d");

            // Now, create the constraints
            var constraints = new LinearConstraintCollection();

            constraints.Add(new LinearConstraint(f, "0.0732 * a + 0.0799 * b + 0.1926 * c + 0.0047 * d = 0.098"));
            constraints.Add(new LinearConstraint(f, "a + b + c + d = 1"));
            constraints.Add(new LinearConstraint(f, "a >= 0"));
            constraints.Add(new LinearConstraint(f, "b >= 0"));
            constraints.Add(new LinearConstraint(f, "c >= 0"));
            constraints.Add(new LinearConstraint(f, "d >= 0"));
            constraints.Add(new LinearConstraint(f, "a >= 0.5"));

            double[] b;
            int eq;
            double[,] A = constraints.CreateMatrix(4, out b, out eq);

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

            // And attempt to solve it.
            double minValue = solver.Minimize(f);

            double[] expected = { 0.5, 0.336259542, 0.163740458, 0 };
            double[] actual = solver.Solution;

            for (int i = 0; i < expected.Length; i++)
            {
                double e = expected[i];
                double a = actual[i];
                Assert.AreEqual(e, a);
            }
        }
        public void GoldfarbIdnaniParseGlobalizationTestBase()
        {
            // minimize 0.5x² + 0.2y² + 0.3xy s.t. 0.01x + 0.02y - 0.03 = 0 AND x + y = 100
            // http://www.wolframalpha.com/input/?i=minimize+0.5x%C2%B2+%2B+0.2y%C2%B2+%2B+0.3xy+s.t.+0.01x+%2B+0.02y+-+0.03+%3D+0+AND+x+%2B+y+%3D+100

            String strObjective = "0.5x² + 0.2y² + 0.3xy";

            String[] strConstraints = 
            { 
                "0.01x + 0.02y - 0.03 = 0", 
                "x + y = 100" 
            };

            QuadraticObjectiveFunction function = new QuadraticObjectiveFunction(strObjective);
            LinearConstraintCollection cst = new LinearConstraintCollection();
            foreach (var tmpCst in strConstraints)
                cst.Add(new LinearConstraint(function, tmpCst));

            var classSolver = new Accord.Math.Optimization.GoldfarbIdnani(function, cst);
            bool status = classSolver.Minimize();
            double result = classSolver.Value;

            Assert.IsTrue(status);
            Assert.AreEqual(15553.60, result, 1e-10);
        }
        public void GoldfarbIdnaniMinimizeLessThanWithEqualityTest()
        {
            // This test reproduces Issue #33 at Google Code Tracker
            // https://code.google.com/p/accord/issues/detail?id=33

            // Create objective function using the
            // Hessian Q and linear terms vector d.

            double[,] Q =
            {
                { 0.12264004,  0.011579293, 0.103326825, 0.064073439 },
                { 0.011579293, 0.033856,    0.014311947, 0.014732381 },
                { 0.103326825, 0.014311947, 0.17715681,  0.067615114 },
                { 0.064073439, 0.014732381, 0.067615114, 0.11539609  }
            };

            Assert.IsTrue(Q.IsPositiveDefinite());

            double[] d = { 0, 0, 0, 0 };

            var f = new QuadraticObjectiveFunction(Q, d, "a", "b", "c", "d");

            // Now, create the constraints
            var constraints = new LinearConstraintCollection();

            constraints.Add(new LinearConstraint(f, "0.0732 * a + 0.0799 * b + 0.1926 * c + 0.0047 * d = 0.098"));
            constraints.Add(new LinearConstraint(f, "a + b + c + d = 1"));
            constraints.Add(new LinearConstraint(f, "-a <= 0"));
            constraints.Add(new LinearConstraint(f, "-b <= 0"));
            constraints.Add(new LinearConstraint(f, "-c <= 0"));
            constraints.Add(new LinearConstraint(f, "-d <= 0"));
            constraints.Add(new LinearConstraint(f, "-a + 0.5 <= 0.0"));

            Assert.AreEqual(-1, constraints[6].CombinedAs[0]);
            Assert.AreEqual(-0.5, constraints[6].Value);
            Assert.AreEqual(0.1, constraints[6].GetViolation(new double[] { 0.6 }), 1e-10);
            Assert.AreEqual(-0.1, constraints[6].GetViolation(new double[] { 0.4 }), 1e-10);

            bool psd = Q.IsPositiveDefinite();

            double[] b;
            int eq;
            double[,] A = constraints.CreateMatrix(4, out b, out eq);

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

            // And attempt to solve it.
            Assert.IsTrue(solver.Minimize());
            double minValue = solver.Value;

            double[] expected = { 0.50000000000000, 0.30967169476486, 0.19032830523514, 0 };
            double[] actual = solver.Solution;

            for (int i = 0; i < constraints.Count; i++)
            {
                double error = constraints[i].GetViolation(actual);
                Assert.IsTrue(error >= 0);
            }

            for (int i = 0; i < expected.Length; i++)
            {
                double e = expected[i];
                double a = actual[i];
                Assert.AreEqual(e, a, 1e-10);
            }
        }
        public void GoldfarbIdnaniParseTest()
        {
            var s = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;

            String strObjective = "0" + s + "5x² + 0" + s + "2y² + 0" + s + "3xy";

            String[] strConstraints = 
            { 
                "0" + s + "01x + 0" + s + "02y - 0" + s + "03 = 0", 
                "x + y = 100" 
            };

            // Now we can start creating our function:
            QuadraticObjectiveFunction function = new QuadraticObjectiveFunction(strObjective, CultureInfo.CurrentCulture);
            LinearConstraintCollection cst = new LinearConstraintCollection();
            foreach (var tmpCst in strConstraints)
                cst.Add(new LinearConstraint(function, tmpCst, CultureInfo.CurrentCulture));

            var classSolver = new Accord.Math.Optimization.GoldfarbIdnani(function, cst);
            bool status = classSolver.Minimize();
            double result = classSolver.Value;

            Assert.IsTrue(status);
            Assert.AreEqual(15553.60, result, 1e-10);
        }
        public void GoldfarbIdnaniParseGlobalizationTest2()
        {
            String strObjective = 0.5.ToString(CultureInfo.InvariantCulture)
                + "x² +" + 0.2.ToString(CultureInfo.InvariantCulture) + "y² +"
                + 0.3.ToString(CultureInfo.InvariantCulture) + "xy";

            String[] strConstraints = 
            { 
                0.01.ToString(CultureInfo.InvariantCulture) + "x" + " + " + 
                0.02.ToString(CultureInfo.InvariantCulture) + "y - " + 
                0.03.ToString(CultureInfo.InvariantCulture) + " = 0", 
                "x + y = 100" 
            };

            QuadraticObjectiveFunction function = new QuadraticObjectiveFunction(strObjective);
            LinearConstraintCollection cst = new LinearConstraintCollection();
            foreach (var tmpCst in strConstraints)
                cst.Add(new LinearConstraint(function, tmpCst));

            var classSolver = new Accord.Math.Optimization.GoldfarbIdnani(function, cst);
            bool status = classSolver.Minimize();
            double result = classSolver.Value;

            Assert.IsTrue(status);
            Assert.AreEqual(15553.60, result, 1e-10);
        }
Example #11
0
        public void GoldfarbIdnaniMinimizeLessThanWithEqualityTest()
        {
            // This test reproduces Issue #33 at Google Code Tracker
            // https://code.google.com/p/accord/issues/detail?id=33

            // Create objective function using the
            // Hessian Q and linear terms vector d.

            double[,] Q =
            {
                {  0.12264004, 0.011579293, 0.103326825, 0.064073439 },
                { 0.011579293,    0.033856, 0.014311947, 0.014732381 },
                { 0.103326825, 0.014311947,  0.17715681, 0.067615114 },
                { 0.064073439, 0.014732381, 0.067615114,  0.11539609 }
            };

            Assert.IsTrue(Q.IsPositiveDefinite());

            double[] d = { 0, 0, 0, 0 };

            var f = new QuadraticObjectiveFunction(Q, d, "a", "b", "c", "d");

            // Now, create the constraints
            var constraints = new LinearConstraintCollection();

            constraints.Add(new LinearConstraint(f, "0.0732 * a + 0.0799 * b + 0.1926 * c + 0.0047 * d = 0.098"));
            constraints.Add(new LinearConstraint(f, "a + b + c + d = 1"));
            constraints.Add(new LinearConstraint(f, "-a <= 0"));
            constraints.Add(new LinearConstraint(f, "-b <= 0"));
            constraints.Add(new LinearConstraint(f, "-c <= 0"));
            constraints.Add(new LinearConstraint(f, "-d <= 0"));
            constraints.Add(new LinearConstraint(f, "-a + 0.5 <= 0.0"));

            Assert.AreEqual(-1, constraints[6].CombinedAs[0]);
            Assert.AreEqual(-0.5, constraints[6].Value);
            Assert.AreEqual(0.1, constraints[6].GetViolation(new double[] { 0.6 }), 1e-10);
            Assert.AreEqual(-0.1, constraints[6].GetViolation(new double[] { 0.4 }), 1e-10);

            bool psd = Q.IsPositiveDefinite();

            double[] b;
            int      eq;

            double[,] A = constraints.CreateMatrix(4, out b, out eq);

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

            // And attempt to solve it.
            Assert.IsTrue(solver.Minimize());
            double minValue = solver.Value;

            double[] expected = { 0.50000000000000, 0.30967169476486, 0.19032830523514, 0 };
            double[] actual   = solver.Solution;

            for (int i = 0; i < constraints.Count; i++)
            {
                double error = constraints[i].GetViolation(actual);
                Assert.IsTrue(error >= 0);
            }

            for (int i = 0; i < expected.Length; i++)
            {
                double e = expected[i];
                double a = actual[i];
                Assert.AreEqual(e, a, 1e-10);
            }
        }