forum.alglib.net
http://forum.alglib.net/

minasa_d_1 example
http://forum.alglib.net/viewtopic.php?f=2&t=111
Page 1 of 1

Author:  dak [ Fri Nov 05, 2010 6:13 pm ]
Post subject:  minasa_d_1 example

I'm new to C# but learned Fortran in the 80s and Visual Basic in the 90s. Can you expand on the example given in the documentation for MinASA? Specifically I'm having difficulty with usage of pointer(s) in the line

alglib.minasaoptimize(state, function1_grad, null, null);

that are 'null'. I'd like to monitor the current function value as the optimization is progressing. In prior versions, I was able to monitor the current function value inside the alglib.minasaiteration loop. Any help would be greatly appreciated.

Thanks,
DAK

Author:  Sergey.Bochkanov [ Sat Nov 06, 2010 9:32 am ]
Post subject:  Re: minasa_d_1 example

minasaoptimize accepts 4 parameters: state, gradient, report callback (null in the code snippet you've posted) and additional parameter passed by algorithm to all functions (null). If you want to monitor current function value, you can pass report callback (read ap.cs for definition of ndimensional_rep type). This function will be called after each iteration.

Author:  dak [ Sat Nov 06, 2010 4:30 pm ]
Post subject:  Re: minasa_d_1 example

I'm still missing something on how this works. Here's the code I'm trying to use (with error "Use of unassigned local variable 'ndrep' "). The code I added is bold.

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

namespace MinASA_Test_csharp_31_1
{
class Program
{
public static void function1_grad(double[] x, ref double func, double[] grad, object obj)
{
// this callback calculates f(x0,x1) = 100*(x0+3)^4 + (x1-3)^4
// and its derivatives df/d0 and df/dx1
func = 100 * System.Math.Pow(x[0] + 3, 4) + System.Math.Pow(x[1] - 3, 4);
grad[0] = 400 * System.Math.Pow(x[0] + 3, 3);
grad[1] = 4 * System.Math.Pow(x[1] - 3, 3);
}

public static int Main(string[] args)
{
//
// This example demonstrates minimization of f(x,y) = 100*(x+3)^4+(y-3)^4
// subject to bound constraints -1<=x0<=+1, -1<=x1<=+1, using ASA.
//
double[] x = new double[] { 0, 0 };
double[] bndl = new double[] { -1, -1 };
double[] bndu = new double[] { +1, +1 };
double epsg = 0.0000000001;
double epsf = 0;
double epsx = 0;
int maxits = 0;
alglib.minasastate state;
alglib.minasareport rep;
alglib.ndimensional_rep ndrep;

alglib.minasacreate(x, bndl, bndu, out state);
alglib.minasasetcond(state, epsg, epsf, epsx, maxits);
alglib.minasasetxrep(state, true);
alglib.minasaoptimize(state, function1_grad, ndrep, null);
alglib.minasaresults(state, out x, out rep);

System.Console.WriteLine("{0}", rep.terminationtype); // EXPECTED: 4
System.Console.WriteLine("{0}", alglib.ap.format(x, 2)); // EXPECTED: [-1,1]
System.Console.ReadLine();
return 0;
}
}
}


Any thoughts would be greatly appreciated.

Thanks,
DAK

Author:  Sergey.Bochkanov [ Sat Nov 06, 2010 9:38 pm ]
Post subject:  Re: minasa_d_1 example

You don't have to declare ndrep variable.

Look at definition of alglib.ndimensional_rep - it is delegate type; declare method with same set of parameters and pass it instead of ndrep variable, just as function1_grad is passed.

Author:  dak [ Sun Nov 07, 2010 5:53 pm ]
Post subject:  Re: minasa_d_1 example

I finally got it working - just as you described.

Thanks for all your help,
DAK

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/