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

minlmoptimize question
http://forum.alglib.net/viewtopic.php?f=2&t=3791
Page 1 of 1

Author:  gbartoli80 [ Mon Feb 20, 2017 4:34 pm ]
Post subject:  minlmoptimize question

Hi,
I'm trying a small toy example to gain confidence with Non Linear Optimization with Levenberg-Marquardt in ALGLib.
This is my simple code where the objective function computes the sum of the distance between a point and each vertex of a fixed triangle.

Code:
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "optimization.h"

using namespace alglib;

const double XA = 1, YA = 5;
const double XB = 7, YB = 2;
const double XC = 7, YC = 8;

double pointDist(double x1, double y1, double x2, double y2)
{
    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}

void func(const real_1d_array &x, real_1d_array &fi, void *ptr)
{
    fi[0] = pointDist(x[0], x[1], XA, YA);
    fi[1] = pointDist(x[0], x[1], XB, YB);
    fi[2] = pointDist(x[0], x[1], XC, YC);
}

int main(int argc, char *argv[])
{
    real_1d_array x = "[0,0]";
    double epsg = 0.0000000001;
    double epsf = 0;
    double epsx = 0;
    ae_int_t maxits = 0;
    minlmstate state;
    minlmreport rep;

    minlmcreatev(2, x, 0.0001, state);
    minlmsetcond(state, epsg, epsf, epsx, maxits);
    minlmoptimize(state, func);
    minlmresults(state, x, rep);

    printf("%d\n", int(rep.terminationtype));
    printf("%s\n", x.tostring(2).c_str());
    return 0;
}


Minimizing this function should give the coordinates of the triangle center of mass, right?
With the provided input points, the result should be: [5,5]
But when I compile and run it gives me: [4.00,3.50]
Do you know what am I missing or doing wrong here?

Many thanks!

Author:  gbartoli80 [ Tue Feb 21, 2017 8:33 am ]
Post subject:  Re: minlmoptimize question

Another issue with the simple multivariate function (x*y - 3)^2+1:
Code:
void func(const real_1d_array &x, real_1d_array &fi, void *ptr)
{
    fi[0] = x[0] * x[1] - 3;
    fi[1] = 1;
}
...
double epsg = 1e-9;
double epsf = 1e-6;
double epsx = 1e-6;
ae_int_t maxits = 0;

The real minimum is [-1,-3], but ALGLib always gives very distant solutions:
Code:
Termination  = Relative function improvement is no more than EpsF
Solution     = [-1.3551,-2.2138]
Iterations   = 4
Calculations = 14
Jacobians    = 2

I even tried to start from points close to the real minimum, but the result is the same:
Code:
real_1d_array x = "[-0.5,-2]";

Do you have any hints on this?

Thanks

Author:  gbartoli80 [ Tue Feb 21, 2017 8:50 am ]
Post subject:  Re: minlmoptimize question

Last update: I also tried with minbleicoptimize, but I still get inconsistent results:
Code:
void func2(const real_1d_array &x, double &func, void *ptr)
{
    func = pow(x[0]*x[1] - 3, 2) + 1;
}

Code:
real_1d_array x = "[1,1]";
real_1d_array bndl = "[0,0]";
real_1d_array bndu = "[10,10]";
minbleicstate state;
minbleicreport rep;

double epsg = 1e-9;
double epsf = 1e-6;
double epsx = 1e-6;
ae_int_t maxits = 0;
double diffstep = 1.0e-6;

Code:
Termination  = Relative function improvement is no more than EpsF
Solution     = [1.7321,1.7321]
Iterations   = 4

Author:  Sergey.Bochkanov [ Wed Feb 22, 2017 8:32 pm ]
Post subject:  Re: minlmoptimize question

Hi!

Your problem with first task is that you call minlmcreatev(2, x, 0.0001, state), telling optimizer that you have 2 target functions to optimize. You should specify 3 because you have three points to fit.

As for the second problem, it has non-unique solution. Any pair (x0,x1) will work as long as x0*x1=3, and optimizer correctly returns one of such solutions.

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