Skip to content
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

Merged
merged 16 commits into from
Apr 7, 2017
1 change: 1 addition & 0 deletions doc/sphinx/docs/cpp/cpp_docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Various optimization utilities.
utils/constrained
utils/discrepancy
utils/hypervolume
utils/gradient_and_hessians

Miscellanea
^^^^^^^^^^^
Expand Down
18 changes: 18 additions & 0 deletions doc/sphinx/docs/cpp/utils/gradient_and_hessians.rst
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
2 changes: 1 addition & 1 deletion doc/sphinx/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ After following the :ref:`install` you will be able to compile and run your firs
:language: c++
:linenos:

Place it into a getting_started.cpp text file and compile it (for eaxmple) with:
Place it into a getting_started.cpp text file and compile it (for example) with:

.. code-block:: bash

Expand Down
228 changes: 228 additions & 0 deletions include/pagmo/problems/luksan_vlcek1.hpp
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>
Copy link
Member

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 need stdexcept, which is included below).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#include <cmath>

#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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just from instead of from the.

/**
* 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
/**
*
Copy link
Member

Choose a reason for hiding this comment

The 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});
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Loading