2016-04-01 21:40:22 +01:00
|
|
|
/*
|
|
|
|
* NloptMinimizer.cpp, part of LatAnalyze 3
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 - 2016 Antonin Portelli
|
|
|
|
*
|
|
|
|
* LatAnalyze 3 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* LatAnalyze 3 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-02-10 00:23:36 +00:00
|
|
|
#include <LatAnalyze/Numerical/NloptMinimizer.hpp>
|
2016-04-01 21:40:22 +01:00
|
|
|
#include <LatAnalyze/includes.hpp>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Latan;
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* NloptMinimizer implementation *
|
|
|
|
******************************************************************************/
|
|
|
|
// constructors ////////////////////////////////////////////////////////////////
|
|
|
|
NloptMinimizer::NloptMinimizer(const Algorithm algorithm)
|
|
|
|
{
|
|
|
|
setAlgorithm(algorithm);
|
2016-04-04 14:56:21 +01:00
|
|
|
der_.setOrder(1, 1);
|
2016-04-01 21:40:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// access //////////////////////////////////////////////////////////////////////
|
|
|
|
NloptMinimizer::Algorithm NloptMinimizer::getAlgorithm(void) const
|
|
|
|
{
|
|
|
|
return algorithm_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NloptMinimizer::setAlgorithm(const Algorithm algorithm)
|
|
|
|
{
|
|
|
|
algorithm_ = algorithm;
|
|
|
|
}
|
|
|
|
|
2016-04-12 20:10:37 +01:00
|
|
|
bool NloptMinimizer::supportLimits(void) const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-01 21:40:22 +01:00
|
|
|
// minimization ////////////////////////////////////////////////////////////////
|
|
|
|
const DVec & NloptMinimizer::operator()(const DoubleFunction &f)
|
|
|
|
{
|
|
|
|
DVec &x = getState();
|
|
|
|
|
|
|
|
// resize minimizer state to match function number of arguments
|
|
|
|
if (f.getNArg() != x.size())
|
|
|
|
{
|
|
|
|
resize(f.getNArg());
|
|
|
|
}
|
|
|
|
|
|
|
|
// create and set minimizer
|
|
|
|
nlopt::opt min(getAlgorithm(), x.size());
|
|
|
|
NloptFuncData data;
|
|
|
|
vector<double> lb(x.size()), hb(x.size());
|
|
|
|
|
|
|
|
min.set_maxeval(getMaxIteration());
|
|
|
|
min.set_xtol_rel(getPrecision());
|
2016-04-04 14:56:21 +01:00
|
|
|
min.set_ftol_rel(-1.);
|
|
|
|
der_.setFunction(f);
|
2016-04-01 21:40:22 +01:00
|
|
|
data.f = &f;
|
2016-04-04 14:56:21 +01:00
|
|
|
data.d = &der_;
|
2016-04-01 21:40:22 +01:00
|
|
|
min.set_min_objective(&funcWrapper, &data);
|
|
|
|
for (Index i = 0; i < x.size(); ++i)
|
|
|
|
{
|
|
|
|
lb[i] = hasLowLimit(i) ? getLowLimit(i) : -HUGE_VAL;
|
|
|
|
hb[i] = hasHighLimit(i) ? getHighLimit(i) : HUGE_VAL;
|
|
|
|
}
|
|
|
|
min.set_lower_bounds(lb);
|
|
|
|
min.set_upper_bounds(hb);
|
|
|
|
|
2016-04-12 20:10:37 +01:00
|
|
|
// minimize
|
2016-04-01 21:40:22 +01:00
|
|
|
double res;
|
|
|
|
vector<double> vx(x.size());
|
|
|
|
nlopt::result status;
|
|
|
|
unsigned int n = 0;
|
|
|
|
|
|
|
|
for (Index i = 0; i < x.size(); ++i)
|
|
|
|
{
|
|
|
|
vx[i] = x(i);
|
|
|
|
}
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (getVerbosity() >= Verbosity::Normal)
|
|
|
|
{
|
|
|
|
cout << "========== NLopt minimization, pass #" << n + 1;
|
|
|
|
cout << " ==========" << endl;
|
|
|
|
cout << "Algorithm: " << min.get_algorithm_name() << endl;
|
|
|
|
cout << "Max eval.= " << min.get_maxeval();
|
|
|
|
cout << " -- Precision= " << min.get_xtol_rel() << endl;
|
2016-04-05 11:50:45 +01:00
|
|
|
printf("Starting f(x)= %.10e\n", f(x));
|
2016-04-01 21:40:22 +01:00
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
status = min.optimize(vx, res);
|
|
|
|
}
|
|
|
|
catch (invalid_argument &e)
|
|
|
|
{
|
|
|
|
LATAN_ERROR(Runtime, "NLopt has reported receving invalid "
|
|
|
|
"arguments (if you are using a global minimizer, did "
|
|
|
|
"you specify limits for all variables?)");
|
|
|
|
}
|
|
|
|
if (getVerbosity() >= Verbosity::Normal)
|
|
|
|
{
|
2016-04-05 11:50:45 +01:00
|
|
|
printf("Found minimum %.10e at:\n", res);
|
2016-04-01 21:40:22 +01:00
|
|
|
for (Index i = 0; i < x.size(); ++i)
|
|
|
|
{
|
2016-04-05 11:50:45 +01:00
|
|
|
printf("%8s= %.10e\n", f.varName().getName(i).c_str(), vx[i]);
|
2016-04-01 21:40:22 +01:00
|
|
|
}
|
|
|
|
cout << "after " << data.evalCount << " evaluations" << endl;
|
|
|
|
cout << "Minimization ended with code " << status;
|
|
|
|
cout << " (" << returnMessage(status) << ")";
|
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
data.evalCount = 0;
|
|
|
|
for (Index i = 0; i < x.size(); ++i)
|
|
|
|
{
|
|
|
|
x(i) = vx[i];
|
|
|
|
}
|
|
|
|
n++;
|
2016-04-04 14:56:21 +01:00
|
|
|
} while (!minSuccess(status) and (n < getMaxPass()));
|
2016-04-01 21:40:22 +01:00
|
|
|
if (getVerbosity() >= Verbosity::Normal)
|
|
|
|
{
|
|
|
|
cout << "=================================================" << endl;
|
|
|
|
}
|
2016-04-04 14:56:21 +01:00
|
|
|
if (!minSuccess(status))
|
2016-04-01 21:40:22 +01:00
|
|
|
{
|
|
|
|
LATAN_WARNING("invalid minimum: " + returnMessage(status));
|
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NLopt return code parser ////////////////////////////////////////////////////
|
|
|
|
string NloptMinimizer::returnMessage(const nlopt::result status)
|
|
|
|
{
|
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case nlopt::SUCCESS:
|
|
|
|
return "success";
|
|
|
|
case nlopt::STOPVAL_REACHED:
|
|
|
|
return "stopping value reached";
|
|
|
|
case nlopt::FTOL_REACHED:
|
|
|
|
return "tolerance on function reached";
|
|
|
|
case nlopt::XTOL_REACHED:
|
|
|
|
return "tolerance on variable reached";
|
|
|
|
case nlopt::MAXEVAL_REACHED:
|
|
|
|
return "maximum function evaluation reached";
|
|
|
|
case nlopt::MAXTIME_REACHED:
|
|
|
|
return "maximum time reached";
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NLopt function wrapper //////////////////////////////////////////////////////
|
2016-04-04 14:56:21 +01:00
|
|
|
double NloptMinimizer::funcWrapper(unsigned int n, const double *arg,
|
2016-04-01 21:40:22 +01:00
|
|
|
double *grad , void *vdata)
|
|
|
|
{
|
|
|
|
NloptFuncData &data = *static_cast<NloptFuncData *>(vdata);
|
|
|
|
|
2016-04-04 14:56:21 +01:00
|
|
|
if (grad)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
data.d->setDir(i);
|
|
|
|
grad[i] = (*(data.d))(arg);
|
|
|
|
}
|
|
|
|
data.evalCount += data.d->getNPoint()*n;
|
|
|
|
}
|
2016-04-01 21:40:22 +01:00
|
|
|
data.evalCount++;
|
|
|
|
|
|
|
|
return (*data.f)(arg);
|
|
|
|
}
|
2016-04-04 14:56:21 +01:00
|
|
|
|
|
|
|
// NLopt return status parser //////////////////////////////////////////////////
|
|
|
|
bool NloptMinimizer::minSuccess(const nlopt::result status)
|
|
|
|
{
|
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case nlopt::SUCCESS:
|
|
|
|
case nlopt::FTOL_REACHED:
|
|
|
|
case nlopt::XTOL_REACHED:
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|