Exemplo n.º 1
0
        public void ConstructorTest()
        {
            #region doc_example
            // Suppose we were given the function x³ + 2x² - 10x + 1 and
            // we have to find its root, maximum and minimum inside
            // the interval [-4, 2]. First, we express this function
            // as a lambda expression:
            Func <double, double> function = x => x * x * x + 2 * x * x - 10 * x + 1;

            // And now we can create the search algorithm:
            BrentSearch search = new BrentSearch(function, -4, 2);

            // Finally, we can query the information we need
            bool   success1 = search.Maximize(); // should be true
            double max      = search.Solution;   // occurs at -2.61

            bool   success2 = search.Minimize(); // should be true
            double min      = search.Solution;   // occurs at  1.28

            bool   success3 = search.FindRoot(); // should be true
            double root     = search.Solution;   // occurs at  0.10
            double value    = search.Value;      // should be zero
            #endregion

            Assert.IsTrue(success1);
            Assert.IsTrue(success2);
            Assert.IsTrue(success3);
            Assert.AreEqual(-2.6103173073566239, max);
            Assert.AreEqual(1.2769839857480398, min);
            Assert.AreEqual(0.10219566016872624, root);
            Assert.AreEqual(0, value, 1e-5);
        }
Exemplo n.º 2
0
        public void ConstructorTest()
        {
            #region doc_example
            // Suppose we were given the function x³ + 2x² - 10x and
            // we have to find its root, maximum and minimum inside
            // the interval [-4,3]. First, we express this function
            // as a lambda expression:
            Func <double, double> function = x => x * x * x + 2 * x * x - 10 * x;

            // And now we can create the search algorithm:
            BrentSearch search = new BrentSearch(function, -4, 3);

            // Finally, we can query the information we need
            bool   success1 = search.Maximize(); // should be true
            double max      = search.Solution;   // occurs at -2.61

            bool   success2 = search.Minimize(); // should be true
            double min      = search.Solution;   // occurs at  1.27

            bool   success3 = search.FindRoot(); // should be true
            double root     = search.Solution;   // occurs at  0.50
            #endregion

            Assert.IsTrue(success1);
            Assert.IsTrue(success2);
            Assert.IsTrue(success3);
            Assert.AreEqual(-2.6103173042172645, max);
            Assert.AreEqual(1.2769840667540548, min);
            Assert.AreEqual(-0.5, root);
        }
Exemplo n.º 3
0
        public void MinimizeTest()
        {
            Func <double, double> f = x => 2 * x * x - 3 * x + 5;

            double expected = 3 / 4.0;
            double actual   = BrentSearch.Minimize(f, -200, +200);

            Assert.AreEqual(expected, actual, 1e-10);
        }
Exemplo n.º 4
0
        private double optimizing(
            Vector <double> w,
            Vector <double>[] input,
            double[] desiredTrainingOutput,
            int batch,
            int[] rand)
        {
            Func <double, double> function = x => loop(w, input, desiredTrainingOutput, batch, x, rand);
            BrentSearch           search   = new BrentSearch(function, 0, 1);
            bool   success = search.Minimize();
            double min     = search.Solution;

            return(min);
        }
Exemplo n.º 5
0
        public static PointParamWithRayProjection ClosestPointToRay(this ICurve curve, PointDirection3 ray, double tol = 1e-9)
        {
            var bound       = curve.Domain();
            int numOfRadius = 0;

            double[]  radius   = null;
            MathPoint location = null;

            var radiusResult       = curve.FindMinimumRadius();
            var domain             = new RangeDouble(curve.Domain());
            var tessTol            = radiusResult.Radius / 10;
            var closestPointOnEdge = Vector3.Zero;

            for (var i = 0; i < 1; i++)
            {
                var tessPoints = Sequences
                                 .LinSpace(domain.Min, domain.Max, 100)
                                 .Select(curve.PointParamAt).ToList();
                var edges = tessPoints.Buffer(2, 1).Where(buf => buf.Count == 2)
                            .ToList();

                var closestEdge
                    = edges
                      .Select(edge => new { edge, connection = MakeEdge(edge).ShortestEdgeJoining(ray, tol) })
                      .MinBy(o => o.connection.LengthSquared)[0];

                var a = closestEdge.edge[0].T;
                var b = closestEdge.edge[1].T;
                domain  = new RangeDouble(a, b);
                tessTol = tessTol / 10;
            }

            Func <Vector3, Vector3> projectOnRay = p => (p - ray.Point).ProjectOn(ray.Direction) + ray.Point;

            var solver = new BrentSearch(t =>
            {
                var p    = curve.PointAt(t);
                var proj = projectOnRay(p);
                return((p - proj).LengthSquared());
            }, domain.Min, domain.Max);

            solver.Minimize();
            var minT = solver.Solution;

            var pointParam = curve.PointParamAt(minT);

            return(new PointParamWithRayProjection(pointParam, projectOnRay(pointParam.Point)));
        }
Exemplo n.º 6
0
        public void MinimizeTest()
        {
            Func <double, double> f = x => 2 * x * x - 3 * x + 5;

            double expected = 3 / 4d;
            double actual   = BrentSearch.Minimize(f, -200, +200);

            Assert.AreEqual(expected, actual, 1e-10);


            var  search    = new BrentSearch(f, -200, 200);
            bool isSuccess = search.Minimize();

            Assert.IsTrue(isSuccess);
            Assert.AreEqual(BrentSearchStatus.Success, search.Status);
            Assert.AreEqual(expected, search.Solution, 1e-10);
            Assert.AreEqual(f(expected), search.Value, double.Epsilon);
        }
Exemplo n.º 7
0
        private double optimizing(Vector <double>[] input,
                                  Vector <double>[] V,
                                  Matrix <double>[] w,
                                  int M,
                                  Vector <double>[] h,
                                  Vector <double>[] delta,
                                  Vector <double>[] trainingOutput,
                                  Matrix <double>[] deltaW,
                                  int batch,
                                  double error,
                                  int[] rand)
        {
            Func <double, double> function = x => loop(input, V, M, h, delta, trainingOutput, deltaW, batch, error, x, rand, w);
            BrentSearch           search   = new BrentSearch(function, 0, 1);
            bool   success = search.Minimize();
            double min     = search.Solution;

            return(min);
        }
Exemplo n.º 8
0
        public static EdgeDistance ClosestDistanceBetweenTwoCurves(IMathUtility m, ICurve curve0, ICurve curve1)
        {
            var curveDomain = curve1.Domain();

            var solver = new BrentSearch
                             (t =>
            {
                var pt = curve1.PointAt(t);
                return((curve0.ClosestPointOn(pt).Point - pt).Length());
            }
                             , curveDomain[0]
                             , curveDomain[1]
                             );

            solver.Minimize();
            var param = solver.Solution;

            var pt1  = curve1.PointAt(param);
            var pt0  = curve0.ClosestPointOn(pt1).Point;
            var edge = new Edge3(pt1, pt0);

            return(new EdgeDistance(edge, solver.Value));
        }