forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 8:57 am

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  [ 15 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Minasa usage question
PostPosted: Sun Oct 31, 2010 12:55 pm 
Offline

Joined: Sun Oct 31, 2010 12:38 pm
Posts: 10
Hello,

I have aquestion regarding the usage of minasa functions. What I would like to do basically is to find the optimal angles (alpha, beta, gamma) that a segment has to be rotated in order to minimize a distance from a given point. So i have tried to modify the code from the examples as follow:

Code:
alglib.minasa.minasastate state = new alglib.minasa.minasastate();
alglib.minasa.minasareport report = new alglib.minasa.minasareport();

double[] initialAngles = new double[3];
double[] lowerBounds = new double[3];
double[] upperBounds = new double[3];         

for(int i=0; i<3; i++)
{
   initialAngles[i] = 0.0;
   lowerBounds[i] = -Math.PI;
   upperBounds[i] = Math.PI;
}

alglib.minasa.minasacreate(3, initialAngles, lowerBounds, upperBounds, state);
alglib.minasa.minasasetcond(state, 0.0, 0.0, 0.00001, 0);
alglib.minasa.minasasetxrep(state, true);
alglib.minasa.minasasetstpmax(state, 0.2);

while(alglib.minasa.minasaiteration(state))
{
   if(state.needfg)
   {   
                // Estimate the new position of the segment tip                           
      Matrix3f rotation =  Matrix3f.FromEulerAnglesXYZ(state.x[0], state.x[1], state.x[2]);
      Vector3f length = new Vector3f(segments[0].length, 0, 0);
      point2 = bodyRotation * length + point1;

                // Calculate distance between the point we want to reach and the current point
                double distance = Vector3f.euclideanDistance(coordinates, point2);

                // Here is the problem how to estimate the values of these parameters??????               
      state.f = distance; // Needs fixing?
      state.g[0] = ?
      state.g[1] = ?
           state.g[2] = ?            
   }
}
// Get the results
alglib.minasa.minasaresults(state, initialAngles, report);

double a = initialAngles[0];
double b = initialAngles[1];
double c = initialAngles[2];


Any help whould be appreciated. I have managed to get the results with MATLAB's lsqnonlin function (which does not require gradients), but I would like to do the same thing in C#. Is it possible to achieve that with the minasa functions?


Top
 Profile  
 
 Post subject: Re: Minasa usage question
PostPosted: Sun Oct 31, 2010 4:28 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
I recommend you to wait for ALGLIB 3.1 - it will be released in several hours (hours, not days) and will support least squares optimization without gradients (similar to lsqnonlin). However, you will have to use minlm unit and to represent your problem as sum of squares (as minimization of squared distance).


Top
 Profile  
 
 Post subject: Re: Minasa usage question
PostPosted: Sun Oct 31, 2010 5:07 pm 
Offline

Joined: Sun Oct 31, 2010 12:38 pm
Posts: 10
Wow nice timing!!! I guess I can wait for a few hours. :)

I though though that minlm could not be used with bounds. I would like to have the angle values to be from -180 to 180. Will that be possible?


Top
 Profile  
 
 Post subject: Re: Minasa usage question
PostPosted: Sun Oct 31, 2010 5:13 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
In current version - not. Bound constraints will be introduced in 3.2 or 3.3 (not decided yet).

But angles are periodic - nothing prevents you from normalizing them by adding/subtracting 2pi until they will be in [-pi,+pi]. You can do it after optimization.


Top
 Profile  
 
 Post subject: Re: Minasa usage question
PostPosted: Sun Oct 31, 2010 7:19 pm 
Offline

Joined: Sun Oct 31, 2010 12:38 pm
Posts: 10
Actually I was not clear. In my example all the angles can be from [-180, 180], but in general I would like to change their limits. For example alpha could be from [-60, 60].


Top
 Profile  
 
 Post subject: Re: Minasa usage question
PostPosted: Sun Oct 31, 2010 8:11 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
The best solution for your problem is to use MinASA - and to implement numerical differentiation to provide gradient (in current version of ALGLIB you have to differentiate your function yourself). I also recommend to optimize "f=distance^2", not "f=distance" because distance is non-smooth at zero.

As I said, boundary constraints will be implemented somewhere in 3.2-3.3 - just several months from now. But not in the current release. If you are commercial user, you can pay for such functionality being implemented now, not several months later - more information is available at http://www.alglib.net/commercial.php But I think that you can just write numerical differentiation code (I recommend to use 4-point central formula) and be happy with MinASA :)


Top
 Profile  
 
 Post subject: Re: Minasa usage question
PostPosted: Mon Nov 01, 2010 8:03 am 
Offline

Joined: Sun Oct 31, 2010 12:38 pm
Posts: 10
I am a student, not a commercial user. In the lsqnonlin I am actually using the trust region reflective algorithm which is closer to MinASA implementation I guess. However, I am quite confused on how to implement the 4-point central formula. The problem is that I try to optimize the angles (and not the coordinates) which are used from the matrices and the vectors and then the distance is computed from their results. Do you have any ideas on how to address that problem?


Top
 Profile  
 
 Post subject: Re: Minasa usage question
PostPosted: Mon Nov 01, 2010 8:21 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
At this forum, it is not bad being a noncommercial user :)

Now about gradient. For this algorithm, everything is coordinates. You have three angles - you have three coordinates in the space of all possible angles. So you can easily differentiate squared distance function with respect to angles: if you have f(angle1,angle2,angle3), then central two-point formula will look like df/dangle1=(f(angle1+h,angle2,angle3)-f(angle1-h,angle2,angle3))/(2*h).


Top
 Profile  
 
 Post subject: Re: Minasa usage question
PostPosted: Mon Nov 01, 2010 8:22 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
BTW, 3.1 was released half an hour ago...


Top
 Profile  
 
 Post subject: Re: Minasa usage question
PostPosted: Mon Nov 01, 2010 8:20 pm 
Offline

Joined: Sun Oct 31, 2010 12:38 pm
Posts: 10
I used the 4-point formula of the form (f(angle1 - 2h, angle2, angle3) - 8 * f(angle1 - h, angle2, angle3) + 8 * f(angle1 + h, angle2, angle3) - f(angle1 + 2h, angle2, angle3)) / 12 * h and it seems to be working quite well. I will test it now with more parameters for extra vectors.

I used 0.1 as the value for the h parameter, although as far as I remember from the numerical recipes the value should be something like Sqrt(alglib.math.machineepsilon) * angle1. Do you have any suggestions on this matter?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 47 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