-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Luksan vlcek1 and gradient utils #69
Changes from 13 commits
0acc726
f7bf2b2
db744bd
cd0ccd2
3066a46
2c6ee0f
3fdf7b2
fad6813
9e5eef8
db5af50
34d1a41
0a29f0b
77ff092
a5dc30a
9f3acbb
4639490
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.. _cpp_gradient_utils: | ||
|
||
Utilities for gradient and hessians | ||
=================================== | ||
|
||
A number of utilities to help with problems with gradient and hessians. | ||
|
||
-------------------------------------------------------------------------- | ||
|
||
.. doxygenfunction:: pagmo::estimate_sparsity | ||
|
||
-------------------------------------------------------------------------- | ||
|
||
.. doxygenfunction:: pagmo::estimate_gradient | ||
|
||
-------------------------------------------------------------------------- | ||
|
||
.. doxygenfunction:: pagmo::estimate_gradient_h |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
/* Copyright 2017 PaGMO development team | ||
|
||
This file is part of the PaGMO library. | ||
|
||
The PaGMO library is free software; you can redistribute it and/or modify | ||
it under the terms of either: | ||
|
||
* the GNU Lesser General Public License as published by the Free | ||
Software Foundation; either version 3 of the License, or (at your | ||
option) any later version. | ||
|
||
or | ||
|
||
* 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. | ||
|
||
or both in parallel, as here. | ||
|
||
The PaGMO library 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 copies of the GNU General Public License and the | ||
GNU Lesser General Public License along with the PaGMO library. If not, | ||
see https://www.gnu.org/licenses/. */ | ||
|
||
#ifndef PAGMO_PROBLEM_LUKSANVLCECK_HPP | ||
#define PAGMO_PROBLEM_LUKSANVLCECK_HPP | ||
|
||
#include <cassert> | ||
#include <exception> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
#include <iostream> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "../detail/constants.hpp" | ||
#include "../exceptions.hpp" | ||
#include "../problem.hpp" // needed for cereal registration macro | ||
#include "../types.hpp" | ||
|
||
namespace pagmo | ||
{ | ||
|
||
/// Test problem from the Luksan and Vlcek | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe just |
||
/** | ||
* Implementation of Example 5.1 in the report from Luksan and Vlcek. | ||
* | ||
* The problem is the Chanied Rosenbrock function with trigonometric-exponential | ||
* constraints. | ||
* | ||
* Its formulation in pagmo can be written as: | ||
* | ||
* \f[ | ||
* \begin{array}{rl} | ||
* \mbox{find:} & -5 \le \mathbf x_i \le 5, \forall i=1..n\\ | ||
* \mbox{to minimize: } & \sum_{i=1}^{n-1}\left[100\left(x_i^2-x_{i+1}\right)^2 + \left(x_i-1\right)^2\right]\\ | ||
* \mbox{subject to:} & 3x_{k+1}^3+2x_{k+2}-5+\sin(x_{k+1}-x_{k+2}})\sin(x_{k+1}+x_{k+2}}) | ||
* +4x_{k+1}-x_k\exp(x_k-x_{k+1})-3 \le UB, \forall k=1..n-2 \\ | ||
* & 3x_{k+1}^3+2x_{k+2}-5+\sin(x_{k+1}-x_{k+2}})\sin(x_{k+1}+x_{k+2}}) | ||
* +4x_{k+1}-x_k\exp(x_k-x_{k+1})-3 \ge LB, \forall k=1..n-2 \\ | ||
* \end{array} | ||
* \f] | ||
* | ||
* See: Lukšan, L., and Jan Vlcek. "Sparse and partially separable test problems for unconstrained and equality | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would check if the "š" character survives intact in the doxygen -> sphinx transition. If it does, I'd use it consistently (elsewhere a normal "s" is used). |
||
* constrained optimization." (1999). http://folk.uib.no/ssu029/Pdf_file/Luksan99.ps | ||
* | ||
*/ | ||
struct luksan_vlcek1 { | ||
/// Constructor from dimension and bounds | ||
/** | ||
* Constructs the luksan_vlcek1 problem. | ||
* | ||
* @param dim the problem dimensions. | ||
* | ||
* @throw std::invalid_argument if \p dim is < 3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing period |
||
*/ | ||
luksan_vlcek1(unsigned dim = 3u) : m_dim(dim) | ||
{ | ||
if (dim < 3u) { | ||
pagmo_throw(std::invalid_argument, | ||
"luksan_vlcek1 must have minimum 3 dimension, " + std::to_string(dim) + " requested"); | ||
} | ||
}; | ||
/// Fitness computation | ||
/** | ||
* Computes the fitness for this UDP | ||
* | ||
* @param x the decision vector. | ||
* | ||
* @return the fitness of \p x. | ||
*/ | ||
vector_double fitness(const vector_double &x) const | ||
{ | ||
assert(x.size() == m_dim); | ||
auto n = x.size(); | ||
// 1 objective and (n-2) equalities | ||
vector_double f(1 + (n - 2), 0.); | ||
f[0] = 0.; | ||
for (decltype(n) i = 0u; i < n - 1u; ++i) { | ||
double a1 = x[i] * x[i] - x[i + 1]; | ||
double a2 = x[i] - 1.; | ||
f[0] += 100. * a1 * a1 + a2 * a2; | ||
} | ||
for (decltype(n) i = 0u; i < n - 2u; ++i) { | ||
f[i + 1] = (3. * std::pow(x[i + 1], 3.) + 2. * x[i + 2] - 5. | ||
+ std::sin(x[i + 1] - x[i + 2]) * std::sin(x[i + 1] + x[i + 2]) + 4. * x[i + 1] | ||
- x[i] * std::exp(x[i] - x[i + 1]) - 3.); | ||
} | ||
return f; | ||
} | ||
/// Box-bounds | ||
/** | ||
* | ||
* It returns the box-bounds for this UDP. | ||
* | ||
* @return the lower and upper bounds for each of the decision vector components | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MP |
||
*/ | ||
std::pair<vector_double, vector_double> get_bounds() const | ||
{ | ||
vector_double lb(m_dim, -5.); | ||
vector_double ub(m_dim, 5.); | ||
return {lb, ub}; | ||
} | ||
/// Inequality constraint dimension | ||
/** | ||
* | ||
* It returns the number of inequality constraints | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MP, MP |
||
* | ||
* @return the number of inequality constraints | ||
*/ | ||
vector_double::size_type get_nec() const | ||
{ | ||
return (m_dim - 2); | ||
} | ||
/// Gradients | ||
/** | ||
* It returns the fitness gradient. | ||
* | ||
* The gradient is represented in a sparse form as required by | ||
* problem::gradient(). | ||
* | ||
* @param x the decision vector. | ||
* | ||
* @return the gradient of the fitness function | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MP |
||
*/ | ||
vector_double gradient(const vector_double &x) const | ||
{ | ||
assert(x.size() == m_dim); | ||
auto n = x.size(); | ||
vector_double grad(n + 3 * (n - 2), 0.); | ||
for (decltype(n) i = 0u; i < n - 1; ++i) { | ||
grad[i] += 400. * x[i] * (x[i] * x[i] - x[i + 1]) + 2. * (x[i] - 1.); | ||
grad[i + 1] = -200. * (x[i] * x[i] - x[i + 1]); | ||
} | ||
for (decltype(n) i = 0u; i < n - 2; ++i) { | ||
// x[i] | ||
grad[n + 3 * i] = -(1. + x[i]) * std::exp(x[i] - x[i + 1]); | ||
// x[i+1] | ||
grad[n + 1 + 3 * i] = 9. * x[i + 1] * x[i + 1] | ||
+ std::cos(x[i + 1] - x[i + 2]) * std::sin(x[i + 1] + x[i + 2]) | ||
+ std::sin(x[i + 1] - x[i + 2]) * std::cos(x[i + 1] + x[i + 2]) + 4. | ||
+ x[i] * std::exp(x[i] - x[i + 1]); | ||
// x[i+2] | ||
grad[n + 2 + 3 * i] = 2. - std::cos(x[i + 1] - x[i + 2]) * std::sin(x[i + 1] + x[i + 2]) | ||
+ std::sin(x[i + 1] - x[i + 2]) * std::cos(x[i + 1] + x[i + 2]); | ||
} | ||
return grad; | ||
} | ||
/// Gradients sparsity | ||
/** | ||
* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra line, MP. |
||
* It returns the gradent sparisty structure for the Luksan Vlcek 1 problem | ||
* | ||
* The gradients sparisty is represented in the form required by | ||
* problem::gradient_sparsity(). | ||
* | ||
* @return the gradient sparsity structure of the fitness function | ||
*/ | ||
sparsity_pattern gradient_sparsity() const | ||
{ | ||
sparsity_pattern retval; | ||
// The part relative to the objective function is dense | ||
for (decltype(m_dim) i = 0u; i < m_dim; ++i) { | ||
retval.push_back({0, i}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want you can write these push_backs as emplace_back without the need of curly brackets. |
||
} | ||
// The part relative to the inequality constraints is sparse as each | ||
// constraint c_k depends on x_k, x_{k+1} and x_{k+2} | ||
for (decltype(m_dim) i = 0u; i < m_dim - 2u; ++i) { | ||
retval.push_back({i + 1, i}); | ||
retval.push_back({i + 1, i + 1}); | ||
retval.push_back({i + 1, i + 2}); | ||
} | ||
return retval; | ||
} | ||
/// Problem name | ||
/** | ||
* @return a string containing the problem name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MP |
||
*/ | ||
std::string get_name() const | ||
{ | ||
return "luksan_vlcek1"; | ||
} | ||
/// Object serialization | ||
/** | ||
* This method will save/load \p this into the archive \p ar. | ||
* | ||
* @param ar target archive. | ||
* | ||
* @throws unspecified any exception thrown by the serialization of the UDP and of primitive types. | ||
*/ | ||
template <typename Archive> | ||
void serialize(Archive &ar) | ||
{ | ||
ar(m_dim); | ||
} | ||
/// Problem dimensions | ||
unsigned int m_dim; | ||
}; | ||
|
||
} // namespace pagmo | ||
|
||
PAGMO_REGISTER_PROBLEM(pagmo::luksan_vlcek1) | ||
|
||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this header is not used (for
std::invalid_argument
you needstdexcept
, which is included below).