Ejemplo n.º 1
0
 public void Alloc(PZMath_multimin_fminimizer_type T, int n)
 {
     type = T;
     x = new PZMath_vector(n);
     state = new nmsimplex_state_t();
     T.alloc(state, n);
 }
Ejemplo n.º 2
0
        public static void TestMultiminNMSimplex()
        {
            int np = 2;
            TestMultiminNMSimplexParms parms = new TestMultiminNMSimplexParms(1.0, 2.0);

            PZMath_multimin_fminimizer_type T = new PZMath_multimin_fminimizer_type();
            T.Init("PZMath_multimin_fminimizer_nmsimplex");

            PZMath_multimin_fminimizer s = new PZMath_multimin_fminimizer();

            /* Initial vertex size vector */
            PZMath_vector ss = new PZMath_vector(np);
            PZMath_vector x;

            PZMath_multimin_function minex_func = new PZMath_multimin_function();

            int iter = 0, i;
            int status;
            double size;

            /* Set all step sizes to 1 */
            ss.SetAll(1.0);

            /* Starting point */
            x = new PZMath_vector(np);
            x[0] = 5.0;
            x[1] = 7.0;

            /* Initialize method and iterate */
            minex_func.f = new multimin_function(my_f);
            minex_func.n = np;
            minex_func.parms = parms;
            s.Alloc(T, np);
            s.Init(minex_func, x, ss);

            do
            {
                iter++;
                status = s.Iterate();

                if (status > 0)
                    break;

                size = s.Size();
                status = s.TestSize (size, 1e-2);

                if (status == 0)
                {
                    System.Console.WriteLine  ("converged to minimum at");
                }

                System.Console.Write (iter + " ");

                for (i = 0; i < np; i++)
                {
                    System.Console.Write (s.x[i] + " ");
                }
                System.Console.WriteLine ("f() = " + s.fval + "  size = " + size);
            }
            while (status == -2 && iter < 100);
            System.Console.ReadLine();
        }