forum.alglib.net

ALGLIB forum
It is currently Sat Apr 27, 2024 5:33 pm

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: minasa_d_1 example
PostPosted: Fri Nov 05, 2010 6:13 pm 
Offline

Joined: Tue Oct 19, 2010 5:10 pm
Posts: 3
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


Top
 Profile  
 
 Post subject: Re: minasa_d_1 example
PostPosted: Sat Nov 06, 2010 9:32 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 906
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.


Top
 Profile  
 
 Post subject: Re: minasa_d_1 example
PostPosted: Sat Nov 06, 2010 4:30 pm 
Offline

Joined: Tue Oct 19, 2010 5:10 pm
Posts: 3
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


Top
 Profile  
 
 Post subject: Re: minasa_d_1 example
PostPosted: Sat Nov 06, 2010 9:38 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 906
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.


Top
 Profile  
 
 Post subject: Re: minasa_d_1 example
PostPosted: Sun Nov 07, 2010 5:53 pm 
Offline

Joined: Tue Oct 19, 2010 5:10 pm
Posts: 3
I finally got it working - just as you described.

Thanks for all your help,
DAK


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 311 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group