Beispiel #1
0
        public double RunTestProblem(CalcfcDelegate calcfc, int n, int m, double rhoend, double[] xopt)
        {
            var timer = new Stopwatch();

            timer.Restart();
            var optimizer = new Cobyla(n, m, calcfc)
            {
                MaximumFunctionCalls   = maxfun,
                TrustRegionRadiusStart = rhobeg,
                TrustRegionRadiusEnd   = rhoend
            };
            var result = optimizer.FindMinimum(Enumerable.Repeat(1.0, n).ToArray());

            timer.Stop();

            Assert.That(result.Status,
                        Is.EqualTo(OptimizationStatus.Normal).Or.EqualTo(OptimizationStatus.MAXFUN_Reached));

            var error = xopt.Select((xo, i) => Math.Pow(xo - result.X[i], 2.0)).Sum();

            Console.WriteLine("{0}Least squares error in variables = {1,16:E6}", Environment.NewLine, error);
            Console.WriteLine("Elapsed time for optimization = {0} ms", timer.ElapsedMilliseconds);

            return(error);
        }
Beispiel #2
0
        public void FindMinimum_LogOutputToConsole_VisualInspection()
        {
            var optimizer = new Cobyla(9, 14, calcfc10)
            {
                PrintLevel = 2, Logger = Console.Out
            };

            optimizer.FindMinimum(Enumerable.Repeat(1.0, 9).ToArray());
        }
Beispiel #3
0
        public void Cobyla_FindMinimum_ConstraintsShouldBeSatisfied()
        {
            var x0 = new[] { 0.705, 0.0075, 0.075, 0, 0.0375, 0, 0.05, 0.125 };
            var cobyla = new Cobyla(x0.Length, 3 + x0.Length, Calcfc)
                             {
                                 MaximumFunctionCalls = 100000,
                                 TrustRegionRadiusStart = 1.0,
                                 TrustRegionRadiusEnd = 1.0e-6,
                                 PrintLevel = 1,
                                 Logger = Console.Out
                             };

            var summary = cobyla.FindMinimum(x0);
            Assert.AreEqual(OptimizationStatus.Normal, summary.Status);
            Assert.IsTrue(summary.G.All(c => c >= -1.0e-6));
        }
Beispiel #4
0
        public void Cobyla_FindMinimum_ConstraintsShouldBeSatisfied()
        {
            var x0     = new[] { 0.705, 0.0075, 0.075, 0, 0.0375, 0, 0.05, 0.125 };
            var cobyla = new Cobyla(x0.Length, 3 + x0.Length, Calcfc)
            {
                MaximumFunctionCalls   = 100000,
                TrustRegionRadiusStart = 1.0,
                TrustRegionRadiusEnd   = 1.0e-6,
                PrintLevel             = 1,
                Logger = Console.Out
            };

            var summary = cobyla.FindMinimum(x0);

            Assert.AreEqual(OptimizationStatus.Normal, summary.Status);
            Assert.IsTrue(summary.G.All(c => c >= -1.0e-6));
        }
Beispiel #5
0
        public double RunTestProblem(CalcfcDelegate calcfc, int n, int m, double rhoend, double[] xopt)
        {
            var x = Enumerable.Repeat(1.0, n).ToArray();

            var timer = new Stopwatch();

            timer.Restart();
            Assert.That(Cobyla.FindMinimum(calcfc, n, m, x, rhobeg, rhoend, iprint, maxfun),
                        Is.EqualTo(CobylaExitStatus.Normal).Or.EqualTo(CobylaExitStatus.MaxIterationsReached));
            timer.Stop();

            var error = xopt.Select((xo, i) => Math.Pow(xo - x[i], 2.0)).Sum();

            Console.WriteLine("{0}Least squares error in variables = {1,16:E6}", Environment.NewLine, error);
            Console.WriteLine("Elapsed time for optimization = {0} ms", timer.ElapsedMilliseconds);

            return(error);
        }
Beispiel #6
0
        /// <summary>
        ///     Finds central point of all lines with COBYLA using weight
        ///     <param name="list">Array of lines</param>
        ///     <returns>Returns central Point or Vector3D(NaN,NaN,NaN) on error</returns>
        /// </summary>
        private static Point3D cobylaCentralPointWithWeight(Ray3D[] lines)
        {
            gLines = lines;

            var xyz = new[] { 0.0, 0.0, 0.0 };

            var status = Cobyla.FindMinimum(calfunCobylaWeight, 3, 3, xyz, 0.5, 1.0e-6, 0, 3500, Console.Out);

            if (status == CobylaExitStatus.MaxIterationsReached)
            {
                status = Cobyla.FindMinimum(calfunCobylaWeight, 3, 3, xyz, 0.5, 1.0e-6, 1, 10000, Console.Out);
            }

            if (status == CobylaExitStatus.Normal)
            {
                return(new Point3D(xyz[0], xyz[1], xyz[2]));
            }
            else
            {
                return(new Point3D(Double.NaN, Double.NaN, Double.NaN));
            }
        }
Beispiel #7
0
 public void FindMinimum_LogOutputToConsole_VisualInspection()
 {
     var x      = Enumerable.Repeat(1.0, 9).ToArray();
     var actual = Cobyla.FindMinimum(calcfc10, 9, 14, x, rhobeg, rhoend1, 2, maxfun, Console.Out);
 }