Пример #1
0
    private static void local_min_example(double a, double b, Func <double, double> f,
                                          string title)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    local_min_example() tests local_min() on one test function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    30 May 2021
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Input:
    //
    //    double A, B, the endpoints of the interval.
    //
    //    double F ( double x ), the name of a user-supplied
    //    function, whose local minimum is being sought.
    //
    //    string TITLE, a title for the problem.
    //
    {
        int    calls = 0;
        double x     = 0;

        double t = Math.Sqrt(typeMethods.r8_epsilon());

        double fx = LocalMinimum.local_min(a, b, t, f, ref x, ref calls);
        double fa = f(a);
        double fb = f(b);

        Console.WriteLine("");
        Console.WriteLine("  " + title + "");
        Console.WriteLine("");
        Console.WriteLine("           A                 X             B");
        Console.WriteLine("         F(A)              F(X)          F(B)");
        Console.WriteLine("  " + a.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                          + "  " + x.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                          + "  " + b.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
        Console.WriteLine("  " + fa
                          + "  " + fx.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                          + "  " + fb.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
        Console.WriteLine("  Number of calls to F = " + calls + "");
    }
        public void LocalSearchTest()
        {
            float weigth;
            var   result          = LocalMinimum.LocalSearch(testGraph, out weigth);
            var   fullCycleWeight = testGraph.CountWeight(testGraph.Vertexes);

            Console.WriteLine("Full cycle weight is " + fullCycleWeight);
            Console.WriteLine("Locmin cycle weight is " + weigth);
            Console.WriteLine("Result cycle vertexies: ");
            for (int i = 0; i < result.Count; i++)
            {
                Console.Write(result[i] + " ");
            }
        }
Пример #3
0
    public static void test047()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST047 tests MINQUAD.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    17 January 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        double xmin = 0;
        double ymin = 0;

        Console.WriteLine("");
        Console.WriteLine("TEST047");
        Console.WriteLine("  MINQUAD finds the minimum of a function");
        Console.WriteLine("    F(X) = A * X^2 + B * X + C");
        Console.WriteLine("  within an interval, given three data points.");
        //
        //  Case 1: a minimum is in the interval.
        //  y = ( x - 1 )**2 + 4
        //
        double x1 = 0.0;
        double y1 = (x1 - 1.0) * (x1 - 1.0) + 4.0;

        double x2 = 2.0;
        double y2 = (x2 - 1.0) * (x2 - 1.0) + 4.0;

        double x3 = 3.0;
        double y3 = (x3 - 1.0) * (x3 - 1.0) + 4.0;

        bool result = LocalMinimum.minquad(x1, y1, x2, y2, x3, y3, ref xmin, ref ymin);

        switch (result)
        {
        case false:
            Console.WriteLine("");
            Console.WriteLine("Warning");
            Console.WriteLine("  MINQUAD returned an error code.");
            break;

        default:
            Console.WriteLine("");
            Console.WriteLine("  The minimum lies in the interval.");
            Console.WriteLine("  X1, Y1 = " + x1 + "  " + y1 + "");
            Console.WriteLine("  X2, Y2 = " + x2 + "  " + y2 + "");
            Console.WriteLine("  X3, Y3 = " + x3 + "  " + y3 + "");
            Console.WriteLine("  XMIN = " + xmin + " YMIN = " + ymin + "");
            break;
        }

        //
        //  Case 2: the minimum is to the left of the interval.
        //  y = ( x - 1 )**2 + 4
        //
        x1 = 2.0;
        y1 = (x1 - 1.0) * (x1 - 1.0) + 4.0;

        x2 = 4.0;
        y2 = (x2 - 1.0) * (x2 - 1.0) + 4.0;

        x3 = 5.0;
        y3 = (x3 - 1.0) * (x3 - 1.0) + 4.0;

        result = LocalMinimum.minquad(x1, y1, x2, y2, x3, y3, ref xmin, ref ymin);

        switch (result)
        {
        case false:
            Console.WriteLine("");
            Console.WriteLine("Warning");
            Console.WriteLine("  MINQUAD returned an error code.");
            break;

        default:
            Console.WriteLine("");
            Console.WriteLine("  The minimum is to the left of the interval");
            Console.WriteLine("  X1, Y1 = " + x1 + "  " + y1 + "");
            Console.WriteLine("  X2, Y2 = " + x2 + "  " + y2 + "");
            Console.WriteLine("  X3, Y3 = " + x3 + "  " + y3 + "");
            Console.WriteLine("  XMIN = " + xmin + " YMIN = " + ymin + "");
            break;
        }

        //
        //  Case 3: the function is flat.
        //
        x1 = 11.0;
        y1 = 6.0;

        x2 = 6.0;
        y2 = 6.0;

        x3 = 2.0;
        y3 = 6.0;

        result = LocalMinimum.minquad(x1, y1, x2, y2, x3, y3, ref xmin, ref ymin);

        switch (result)
        {
        case false:
            Console.WriteLine("");
            Console.WriteLine("Warning");
            Console.WriteLine("  MINQUAD returned an error code.");
            break;

        default:
            Console.WriteLine("");
            Console.WriteLine("  The function is flat.");
            Console.WriteLine("  X1, Y1 = " + x1 + "  " + y1 + "");
            Console.WriteLine("  X2, Y2 = " + x2 + "  " + y2 + "");
            Console.WriteLine("  X3, Y3 = " + x3 + "  " + y3 + "");
            Console.WriteLine("  XMIN = " + xmin + " YMIN = " + ymin + "");
            break;
        }

        //
        //  Case 4: the function has a maximum.
        //  y = - ( x - 1 )**2 + 4
        //
        x1 = 0.0;
        y1 = -(x1 - 1.0) * (x1 - 1.0) + 4.0;

        x2 = 2.0;
        y2 = -(x2 - 1.0) * (x2 - 1.0) + 4.0;

        x3 = 3.0;
        y3 = -(x3 - 1.0) * (x3 - 1.0) + 4.0;

        result = LocalMinimum.minquad(x1, y1, x2, y2, x3, y3, ref xmin, ref ymin);

        switch (result)
        {
        case false:
            Console.WriteLine("");
            Console.WriteLine("Warning");
            Console.WriteLine("  MINQUAD returned an error code.");
            break;

        default:
            Console.WriteLine("");
            Console.WriteLine("  The function has a maximum.");
            Console.WriteLine("  X1, Y1 = " + x1 + "  " + y1 + "");
            Console.WriteLine("  X2, Y2 = " + x2 + "  " + y2 + "");
            Console.WriteLine("  X3, Y3 = " + x3 + "  " + y3 + "");
            Console.WriteLine("  XMIN = " + xmin + " YMIN = " + ymin + "");
            break;
        }
    }
Пример #4
0
    public static void test046()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST046 tests MINABS.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    17 July 2005
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        double xmin = 0;
        double ymin = 0;

        Console.WriteLine("");
        Console.WriteLine("TEST046");
        Console.WriteLine("  MINABS finds the minimum of a function");
        Console.WriteLine("    F(X) = a * ABS ( X ) + B");
        Console.WriteLine("  within an interval, given three data points.");
        //
        //  Case 1: the three points lie on a straight line.
        //  (XMIN=9,YMIN=2).
        //
        double x1 = 14.0;
        double y1 = 7.0;

        double x2 = 9.0;
        double y2 = 2.0;

        double x3 = 12.0;
        double y3 = 5.0;

        LocalMinimum.minabs(x1, y1, x2, y2, x3, y3, ref xmin, ref ymin);

        Console.WriteLine("");
        Console.WriteLine("  The points lie on a straight line.");
        Console.WriteLine("  XMIN = " + xmin + "  YMIN = " + ymin + "");
        //
        //  Case 2: the three points straddle a minimum.
        //  (XMIN=7, YMIN=2).
        //
        x1 = 3.0;
        y1 = 6.0;

        x2 = 12.0;
        y2 = 7.0;

        x3 = 9.0;
        y3 = 4.0;

        LocalMinimum.minabs(x1, y1, x2, y2, x3, y3, ref xmin, ref ymin);

        Console.WriteLine("");
        Console.WriteLine("  The points straddle a minimum.");
        Console.WriteLine("  XMIN = " + xmin + "  YMIN = " + ymin + "");
        //
        //  Case 3: the three points straddle a maximum.
        //  (XMIN=2, YMIN=5).
        //
        x1 = 11.0;
        y1 = 6.0;

        x2 = 6.0;
        y2 = 9.0;

        x3 = 2.0;
        y3 = 5.0;

        LocalMinimum.minabs(x1, y1, x2, y2, x3, y3, ref xmin, ref ymin);

        Console.WriteLine("");
        Console.WriteLine("  The points straddle a maximum.");
        Console.WriteLine("  XMIN = " + xmin + "  YMIN = " + ymin + "");
    }
Пример #5
0
    private static void example_test(double a, double b, Func <double, double> f, string title)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    example_test() tests local_min_rc() on one test function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    16 April 2008
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Input:
    //
    //    double A, B, the endpoints of the interval.
    //
    //    double F ( double x ), the name of a user-supplied
    //    function, whose local minimum is being sought.
    //
    //    string TITLE, a title for the problem.
    //
    {
        Console.WriteLine("");
        Console.WriteLine("  " + title + "");
        Console.WriteLine("");
        Console.WriteLine("  Step      X                          F(X)");
        Console.WriteLine("");
        int step = 0;

        double arg   = a;
        double value = f(arg);

        Console.WriteLine("  " + step.ToString().PadLeft(4)
                          + "  " + arg.ToString("0.################").PadLeft(24)
                          + "  " + value.ToString("0.################").PadLeft(24) + "");

        arg   = b;
        value = f(arg);
        Console.WriteLine("  " + step.ToString().PadLeft(4)
                          + "  " + arg.ToString("0.################").PadLeft(24)
                          + "  " + value.ToString("0.################").PadLeft(24) + "");

        double a2     = a;
        double b2     = b;
        int    status = 0;

        LocalMinimum.LocalMinimumData data = new();

        for (;;)
        {
            arg = LocalMinimum.local_min_rc(ref data, ref a2, ref b2, ref status, value);

            if (status < 0)
            {
                Console.WriteLine("");
                Console.WriteLine("example_test(): Fatal error!");
                Console.WriteLine("  LOCAL_MIN_RC returned negative status.");
                break;
            }

            value = f(arg);

            step += 1;
            Console.WriteLine("  " + step.ToString().PadLeft(4)
                              + "  " + arg.ToString("0.################").PadLeft(24)
                              + "  " + value.ToString("0.################").PadLeft(24) + "");

            if (50 < step)
            {
                Console.WriteLine("");
                Console.WriteLine("example_test() - Fatal error!");
                Console.WriteLine("  Too many steps!");
                break;
            }

            if (status == 0)
            {
                break;
            }
        }
    }