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

Minasa usage question
http://forum.alglib.net/viewtopic.php?f=2&t=108
Page 1 of 2

Author:  Georgios [ Sun Oct 31, 2010 12:55 pm ]
Post subject:  Minasa usage question

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?

Author:  Sergey.Bochkanov [ Sun Oct 31, 2010 4:28 pm ]
Post subject:  Re: Minasa usage question

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).

Author:  Georgios [ Sun Oct 31, 2010 5:07 pm ]
Post subject:  Re: Minasa usage question

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?

Author:  Sergey.Bochkanov [ Sun Oct 31, 2010 5:13 pm ]
Post subject:  Re: Minasa usage question

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.

Author:  Georgios [ Sun Oct 31, 2010 7:19 pm ]
Post subject:  Re: Minasa usage question

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].

Author:  Sergey.Bochkanov [ Sun Oct 31, 2010 8:11 pm ]
Post subject:  Re: Minasa usage question

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 :)

Author:  Georgios [ Mon Nov 01, 2010 8:03 am ]
Post subject:  Re: Minasa usage question

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?

Author:  Sergey.Bochkanov [ Mon Nov 01, 2010 8:21 am ]
Post subject:  Re: Minasa usage question

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).

Author:  Sergey.Bochkanov [ Mon Nov 01, 2010 8:22 am ]
Post subject:  Re: Minasa usage question

BTW, 3.1 was released half an hour ago...

Author:  Georgios [ Mon Nov 01, 2010 8:20 pm ]
Post subject:  Re: Minasa usage question

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?

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