Esempio n. 1
0
    private static void powell_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    POWELL_TEST works with the Powell function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    05 January 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        double    fx = 0;
        int       k  = 0;
        const int m  = 4;

        double[] x0 = new double[m];
        Console.WriteLine("");
        Console.WriteLine("POWELL_TEST:");
        Console.WriteLine("  Test COMPASS_SEARCH with the Powell function.");
        const double delta_tol = 0.00001;
        const double delta     = 0.3;
        const int    k_max     = 20000;

        x0[0] = 3.0;
        x0[1] = -1.0;
        x0[2] = 0.0;
        x0[3] = 1.0;
        typeMethods.r8vec_print(m, x0, "  Initial point X0:");
        Console.WriteLine("");
        Console.WriteLine("  F(X0) = " + powell(m, x0) + "");

        double[] x = CompassSearch.compass_search(powell, m, x0, delta_tol, delta, k_max, ref fx, ref k);
        typeMethods.r8vec_print(m, x, "  Estimated minimizer X1:");
        Console.WriteLine("");
        Console.WriteLine("  F(X1) = " + fx + " number of steps = " + k + "");
        //
        //  Demonstrate correct minimizer.
        //
        x    = new double[m];
        x[0] = 0.0;
        x[1] = 0.0;
        x[2] = 0.0;
        x[3] = 0.0;
        typeMethods.r8vec_print(m, x, "  Correct minimizer X*:");
        Console.WriteLine("");
        Console.WriteLine("  F(X*) = " + powell(m, x) + "");
    }
Esempio n. 2
0
    private static void broyden_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BROYDEN_TEST works with the Broyden function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    05 January 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        double    fx = 0;
        int       k  = 0;
        const int m  = 2;

        double[] x0 = new double[m];
        Console.WriteLine("");
        Console.WriteLine("BROYDEN_TEST:");
        Console.WriteLine("  Test COMPASS_SEARCH with the Broyden function.");
        const double delta_tol = 0.00001;
        const double delta     = 0.3;
        const int    k_max     = 20000;

        x0[0] = -0.9;
        x0[1] = -1.0;
        typeMethods.r8vec_print(m, x0, "  Initial point X0:");
        Console.WriteLine("");
        Console.WriteLine("  F(X0) = " + broyden(m, x0) + "");

        double[] x = CompassSearch.compass_search(broyden, m, x0, delta_tol, delta, k_max, ref fx, ref k);
        typeMethods.r8vec_print(m, x, "  Estimated minimizer X1:");
        Console.WriteLine("");
        Console.WriteLine("  F(X1) = " + fx + " number of steps = " + k + "");
        //
        //  Demonstrate correct minimizer.
        //
        x    = new double[m];
        x[0] = -0.511547141775014;
        x[1] = -0.398160951772036;
        typeMethods.r8vec_print(m, x, "  Correct minimizer X*:");
        Console.WriteLine("");
        Console.WriteLine("  F(X*) = " + broyden(m, x) + "");
    }
Esempio n. 3
0
    private static void mckinnon_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    MCKINNON_TEST works with the McKinnon function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    05 January 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        double    fx = 0;
        int       k  = 0;
        const int m  = 2;

        double[] x0 = new double[m];
        Console.WriteLine("");
        Console.WriteLine("MCKINNON_TEST:");
        Console.WriteLine("  Test COMPASS_SEARCH with the McKinnon function.");
        const double delta_tol = 0.00001;
        const double delta     = 0.3;
        const int    k_max     = 20000;
        //
        //  Test 1
        //
        double a = (1.0 + Math.Sqrt(33.0)) / 8.0;
        double b = (1.0 - Math.Sqrt(33.0)) / 8.0;

        double phi   = 10.0;
        double tau   = 1.0;
        double theta = 15.0;

        mckinnon_parameters("set", ref phi, ref tau, ref theta);

        x0[0] = a;
        x0[1] = b;
        typeMethods.r8vec_print(m, x0, "  Initial point X0:");
        Console.WriteLine("  PHI = " + phi + " TAU = " + tau + " THETA = " + theta + "");
        Console.WriteLine("");
        Console.WriteLine("  F(X0) = " + mckinnon(m, x0) + "");

        double[] x = CompassSearch.compass_search(mckinnon, m, x0, delta_tol, delta, k_max, ref fx, ref k);
        typeMethods.r8vec_print(m, x, "  Estimated minimizer X1:");
        Console.WriteLine("");
        Console.WriteLine("  F(X1) = " + fx + " number of steps = " + k + "");

        x    = new double[m];
        x[0] = 0.0;
        x[1] = -0.5;
        typeMethods.r8vec_print(m, x, "  Correct minimizer X*:");
        Console.WriteLine("");
        Console.WriteLine("  F(X*) = " + mckinnon(m, x) + "");
        //
        //  Test 2
        //
        a = (1.0 + Math.Sqrt(33.0)) / 8.0;
        b = (1.0 - Math.Sqrt(33.0)) / 8.0;

        phi   = 60.0;
        tau   = 2.0;
        theta = 6.0;

        mckinnon_parameters("set", ref phi, ref tau, ref theta);

        x0[0] = a;
        x0[1] = b;
        typeMethods.r8vec_print(m, x0, "  Initial point X0:");
        Console.WriteLine("  PHI = " + phi + " TAU = " + tau + " THETA = " + theta + "");
        Console.WriteLine("");
        Console.WriteLine("  F(X0) = " + mckinnon(m, x0) + "");

        x = CompassSearch.compass_search(mckinnon, m, x0, delta_tol, delta, k_max, ref fx, ref k);
        typeMethods.r8vec_print(m, x, "  Estimated minimizer X1:");
        Console.WriteLine("");
        Console.WriteLine("  F(X1) = " + fx + " number of steps = " + k + "");

        x    = new double[m];
        x[0] = 0.0;
        x[1] = -0.5;
        typeMethods.r8vec_print(m, x, "  Correct minimizer X*:");
        Console.WriteLine("");
        Console.WriteLine("  F(X*) = " + mckinnon(m, x) + "");
        //
        //  Test 3
        //
        a = (1.0 + Math.Sqrt(33.0)) / 8.0;
        b = (1.0 - Math.Sqrt(33.0)) / 8.0;

        phi   = 4000.0;
        tau   = 3.0;
        theta = 6.0;

        mckinnon_parameters("set", ref phi, ref tau, ref theta);

        x0[0] = a;
        x0[1] = b;
        typeMethods.r8vec_print(m, x0, "  Initial point X0:");
        Console.WriteLine("  PHI = " + phi + " TAU = " + tau + " THETA = " + theta + "");
        Console.WriteLine("");
        Console.WriteLine("  F(X0) = " + mckinnon(m, x0) + "");

        x = CompassSearch.compass_search(mckinnon, m, x0, delta_tol, delta, k_max, ref fx, ref k);
        typeMethods.r8vec_print(m, x, "  Estimated minimizer X1:");
        Console.WriteLine("");
        Console.WriteLine("  F(X1) = " + fx + " number of steps = " + k + "");

        x    = new double[m];
        x[0] = 0.0;
        x[1] = -0.5;
        typeMethods.r8vec_print(m, x, "  Correct minimizer X*:");
        Console.WriteLine("");
        Console.WriteLine("  F(X*) = " + mckinnon(m, x) + "");
    }
Esempio n. 4
0
    private static void local_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    LOCAL_TEST works with the Local function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    05 January 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        double    fx = 0;
        int       k  = 0;
        const int m  = 2;

        double[] x0 = new double[m];
        Console.WriteLine("");
        Console.WriteLine("LOCAL_TEST:");
        Console.WriteLine("  Test COMPASS_SEARCH with the Local function.");
        const double delta_tol = 0.00001;
        const double delta     = 0.3;
        const int    k_max     = 20000;

        x0[0] = 1.0;
        x0[1] = 1.0;
        typeMethods.r8vec_print(m, x0, "  Initial point X0:");
        Console.WriteLine("");
        Console.WriteLine("  F(X0) = " + local(m, x0) + "");

        double[] x = CompassSearch.compass_search(local, m, x0, delta_tol, delta, k_max, ref fx, ref k);
        typeMethods.r8vec_print(m, x, "  Estimated minimizer X1:");
        Console.WriteLine("");
        Console.WriteLine("  F(X1) = " + fx + " number of steps = " + k + "");
        //
        //  Demonstrate local minimizer.
        //
        x    = new double[m];
        x[0] = 0.2858054412;
        x[1] = 0.2793263206;
        typeMethods.r8vec_print(m, x, "  Correct local minimizer X*:");
        Console.WriteLine("");
        Console.WriteLine("  F(X*) = " + local(m, x) + "");
        //
        //  Try for global minimizer.
        //
        x0[0] = -15.0;
        x0[1] = -35.0;
        typeMethods.r8vec_print(m, x0, "  Initial point X0:");
        Console.WriteLine("");
        Console.WriteLine("  F(X0) = " + local(m, x0) + "");

        x = CompassSearch.compass_search(local, m, x0, delta_tol, delta, k_max, ref fx, ref k);
        typeMethods.r8vec_print(m, x, "  Estimated minimizer X1:");
        Console.WriteLine("");
        Console.WriteLine("  F(X1) = " + fx + " number of steps = " + k + "");
        //
        //  Demonstrate global minimizer.
        //
        x    = new double[m];
        x[0] = -21.02667179;
        x[1] = -36.75997872;
        typeMethods.r8vec_print(m, x, "  Correct global minimizer X*:");
        Console.WriteLine("");
        Console.WriteLine("  F(X*) = " + local(m, x) + "");
    }
Esempio n. 5
0
    private static void himmelblau_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    HIMMELBLAU_TEST works with the Himmelblau function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    05 January 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        double    fx = 0;
        int       k  = 0;
        const int m  = 2;

        double[] x0 = new double[m];
        Console.WriteLine("");
        Console.WriteLine("HIMMELBLAU_TEST:");
        Console.WriteLine("  Test COMPASS_SEARCH with the Himmelblau function.");
        const double delta_tol = 0.00001;
        const double delta     = 0.3;
        const int    k_max     = 20000;

        x0[0] = 1.0;
        x0[1] = 1.0;
        typeMethods.r8vec_print(m, x0, "  Initial point X0:");
        Console.WriteLine("");
        Console.WriteLine("  F(X0) = " + himmelblau(m, x0) + "");

        double[] x = CompassSearch.compass_search(himmelblau, m, x0, delta_tol, delta, k_max, ref fx, ref k);
        typeMethods.r8vec_print(m, x, "  Estimated minimizer X1:");
        Console.WriteLine("");
        Console.WriteLine("  F(X1) = " + fx + " number of steps = " + k + "");

        x0[0] = -1.0;
        x0[1] = 1.0;
        typeMethods.r8vec_print(m, x0, "  Initial point X0:");
        Console.WriteLine("");
        Console.WriteLine("  F(X0) = " + himmelblau(m, x0) + "");

        x = CompassSearch.compass_search(himmelblau, m, x0, delta_tol, delta, k_max, ref fx, ref k);
        typeMethods.r8vec_print(m, x, "  Estimated minimizer X1:");
        Console.WriteLine("");
        Console.WriteLine("  F(X1) = " + fx + " number of steps = " + k + "");

        x0[0] = -1.0;
        x0[1] = -1.0;
        typeMethods.r8vec_print(m, x0, "  Initial point X0:");
        Console.WriteLine("");
        Console.WriteLine("  F(X0) = " + himmelblau(m, x0) + "");

        x = CompassSearch.compass_search(himmelblau, m, x0, delta_tol, delta, k_max, ref fx, ref k);
        typeMethods.r8vec_print(m, x, "  Estimated minimizer X1:");
        Console.WriteLine("");
        Console.WriteLine("  F(X1) = " + fx + " number of steps = " + k + "");

        x0[0] = 1.0;
        x0[1] = -1.0;
        typeMethods.r8vec_print(m, x0, "  Initial point X0:");
        Console.WriteLine("");
        Console.WriteLine("  F(X0) = " + himmelblau(m, x0) + "");

        x = CompassSearch.compass_search(himmelblau, m, x0, delta_tol, delta, k_max, ref fx, ref k);
        typeMethods.r8vec_print(m, x, "  Estimated minimizer X1:");
        Console.WriteLine("");
        Console.WriteLine("  F(X1) = " + fx + " number of steps = " + k + "");
        //
        //  Demonstrate Himmelblau minimizers.
        //
        x    = new double[m];
        x[0] = 3.0;
        x[1] = 2.0;
        typeMethods.r8vec_print(m, x, "  Correct Himmelblau minimizer X1*:");
        Console.WriteLine("");
        Console.WriteLine("  F(X*) = " + himmelblau(m, x) + "");

        x    = new double[m];
        x[0] = 3.58439;
        x[1] = -1.84813;
        typeMethods.r8vec_print(m, x, "  Correct Himmelblau minimizer X2*:");
        Console.WriteLine("");
        Console.WriteLine("  F(X*) = " + himmelblau(m, x) + "");

        x    = new double[m];
        x[0] = -3.77934;
        x[1] = -3.28317;
        typeMethods.r8vec_print(m, x, "  Correct Himmelblau minimizer X3*:");
        Console.WriteLine("");
        Console.WriteLine("  F(X*) = " + himmelblau(m, x) + "");

        x    = new double[m];
        x[0] = -2.80512;
        x[1] = 3.13134;
        typeMethods.r8vec_print(m, x, "  Correct Himmelblau minimizer X4*:");
        Console.WriteLine("");
        Console.WriteLine("  F(X*) = " + himmelblau(m, x) + "");
    }
Esempio n. 6
0
    private static void goldstein_price_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    GOLDSTEIN_PRICE_TEST works with the Goldstein-Price function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    05 January 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        double    fx = 0;
        int       k  = 0;
        const int m  = 2;

        double[] x0 = new double[m];
        Console.WriteLine("");
        Console.WriteLine("GOLDSTEIN_PRICE_TEST:");
        Console.WriteLine("  Test COMPASS_SEARCH with the Goldstein-Price function.");
        const double delta_tol = 0.00001;
        const double delta     = 0.3;
        const int    k_max     = 20000;

        x0[0] = -0.5;
        x0[1] = 0.25;
        typeMethods.r8vec_print(m, x0, "  Initial point X0:");
        Console.WriteLine("");
        Console.WriteLine("  F(X0) = " + goldstein_price(m, x0) + "");

        double[] x = CompassSearch.compass_search(goldstein_price, m, x0, delta_tol, delta, k_max, ref fx, ref k);
        typeMethods.r8vec_print(m, x, "  Estimated minimizer X1:");
        Console.WriteLine("");
        Console.WriteLine("  F(X1) = " + fx + " number of steps = " + k + "");
        //
        //  Repeat with more difficult start.
        //
        x0[0] = -4.0;
        x0[1] = 5.0;
        typeMethods.r8vec_print(m, x0, "  Initial point X0:");
        Console.WriteLine("");
        Console.WriteLine("  F(X0) = " + goldstein_price(m, x0) + "");

        x = CompassSearch.compass_search(goldstein_price, m, x0, delta_tol, delta, k_max, ref fx, ref k);
        typeMethods.r8vec_print(m, x, "  Estimated minimizer X1:");
        Console.WriteLine("");
        Console.WriteLine("  F(X1) = " + fx + " number of steps = " + k + "");
        //
        //  Demonstrate correct minimizer.
        //
        x    = new double[m];
        x[0] = 0.0;
        x[1] = -1.0;
        typeMethods.r8vec_print(m, x, "  Correct minimizer X*:");
        Console.WriteLine("");
        Console.WriteLine("  F(X*) = " + goldstein_price(m, x) + "");
    }