forum.alglib.net

ALGLIB forum
It is currently Thu Mar 28, 2024 12:10 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  [ 4 posts ] 
Author Message
 Post subject: minlmoptimize question
PostPosted: Mon Feb 20, 2017 4:34 pm 
Offline

Joined: Mon Feb 20, 2017 4:28 pm
Posts: 3
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!

_________________
~Guido~


Last edited by gbartoli80 on Tue Feb 21, 2017 1:38 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: minlmoptimize question
PostPosted: Tue Feb 21, 2017 8:33 am 
Offline

Joined: Mon Feb 20, 2017 4:28 pm
Posts: 3
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

_________________
~Guido~


Top
 Profile  
 
 Post subject: Re: minlmoptimize question
PostPosted: Tue Feb 21, 2017 8:50 am 
Offline

Joined: Mon Feb 20, 2017 4:28 pm
Posts: 3
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

_________________
~Guido~


Top
 Profile  
 
 Post subject: Re: minlmoptimize question
PostPosted: Wed Feb 22, 2017 8:32 pm 
Offline
Site Admin

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


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

All times are UTC


Who is online

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