Sample about GradientBasedOptimization?

Nov 24, 2011 at 6:10 AM

Hi,

Thank you very much for your OOOT. I found it very useful for my work. I have one question about the framework. Do you have an easy to start example show how I can use the GradientBasedoptimization?

Below are my code to use the library. It is built without error. But at runtime it throws an error. I don't know how to fix the problem.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StarMathLib;

using OptimizationToolbox;

namespace testLib
{
    public class testOOOT
    {
        public static void Main(string[] args)
        {
           // Build the model

            var optMethod = new GradientBasedOptimization();
            optMethod.Add(new ArithmeticMean(0.5,0.5,20));
            optMethod.Add(new FletcherReevesDirection());
         
          //set the object function

            optMethod.Add(new myOpt());

         // set the convergence criteria

            optMethod.Add(new ToKnownBestFConvergence(0, 0.0001));
    

        // Set the initial value

            double[] xInit = new double[20];
            for (int i = 0; i < 20; ++i)
            {
                xInit[i] = i;
            }

            double[] xStar;
            var fStar = optMethod.Run(out xStar, xInit);

        // output the result

            Console.WriteLine("Convergence Declared by " + optMethod.ConvergenceDeclaredByTypeString);

            for (int i = 0; i < 20; ++i)
            {
                Console.WriteLine("X* = " + xStar[i]);
            }

            Console.WriteLine("F* = " + fStar, 1);

            Console.WriteLine("NumEvals = " + optMethod.numEvals);


        }
    }

    public class myOpt
        : IObjectiveFunction
    {

        #region Implementation of IOptFunction

        public double calculate(double[] x)
        {
            double result = 0;
            for (int i = 0; i < 20; ++i)
            {
                result += (x[i] - i + 10.0) * (x[i] - i + 10.0);
            }
            return result;
        }

        #endregion

    }
}

Regards,

Cheng

Coordinator
Feb 21, 2012 at 6:19 PM

Sorry for the very late reply. You found a bug! One I'm surprised hadn't been hit before. I have fixed the code (see release 809dc18f9523 or later).

Might I suggest when you run it that you change the parameters of Arithmetic Mean.

I wrote : optMethod.Add(new ArithmeticMean(0.0001, 0.1, 20));

and it ran quite fast.

Feb 22, 2012 at 1:27 AM

Thx. I am rereading my codes and see the problem. Besides, much thanks to your wonderful optimization sofeware.


From: mattica [email removed]
Sent: 2012年2月22日 2:20
To: Li, Cheng-B
Subject: Re: Sample about GradientBasedOptimization? [ooot:280671]

From: mattica

Sorry for the very late reply. You found a bug! One I'm surprised hadn't been hit before. I have fixed the code (see release 809dc18f9523 or later).

Might I suggest when you run it that you change the parameters of Arithmetic Mean.

I wrote : optMethod.Add(new ArithmeticMean(0.0001, 0.1, 20));

and it ran quite fast.