Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions doc/sphinx/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@
Changelog
=========

2.21.0 (unreleased)
-------------------

New
~~~

- User-defined problems can now rewrite in place the decision vectors they evaluate
(e.g., to write back into a chromosome the result of a local search, as in the decoders
of the biased random-key genetic algorithms), by implementing the optional
``fitness_rewrite()`` member function. See :cpp:func:`pagmo::problem::fitness_rewrite()`,
:cpp:func:`pagmo::bfe::fitness_rewrite()` and the
:ref:`tutorial <cpp_tutorial_rewriting_decoders>`. Populations and most algorithms
(including the final solutions of :cpp:class:`~pagmo::nlopt` and :cpp:class:`~pagmo::ipopt`,
and the inner algorithm of :cpp:class:`~pagmo::cstrs_self_adaptive`)
store the rewritten decision vectors, and :cpp:class:`~pagmo::thread_bfe` and
:cpp:class:`~pagmo::default_bfe` rewrite them in parallel. Problems which do not implement
``fitness_rewrite()`` are not affected.

Fix
~~~

- Fix the scout bee phase of :cpp:class:`~pagmo::bee_colony`, which did not update
the fitness of the new food source used internally by the algorithm (so that the
subsequent comparisons and onlooker probabilities used the fitness of the abandoned
food source).

2.20.0 (2026-09-24)
--------------------

Expand Down
20 changes: 20 additions & 0 deletions doc/sphinx/docs/cpp/batch_evaluators/default_bfe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ Default BFE
:exception std\:\:invalid_argument: if the input problem *p* does not provide a batch fitness member function and it is does not provide at least the :cpp:enumerator:`~pagmo::thread_safety::basic` thread safety level.
:exception unspecified: any exception raised by the call operator of :cpp:class:`~pagmo::member_bfe` or :cpp:class:`~pagmo::thread_bfe`.

.. cpp:function:: vector_double fitness_rewrite(const problem &p, vector_double &dvs) const

Batch fitness evaluation with in-place rewriting of the decision vectors.

.. versionadded:: 2.21.0

Since the UDP's ``batch_fitness()`` member function cannot rewrite decision vectors, this member function
always delegates to :cpp:func:`pagmo::thread_bfe::fitness_rewrite()`, provided that *p* offers at least the
:cpp:enumerator:`~pagmo::thread_safety::basic` thread safety level.

This member function is usually invoked via :cpp:func:`pagmo::bfe::fitness_rewrite()`.

:param p: the input :cpp:class:`~pagmo::problem`.
:param dvs: the input decision vectors that will be evaluated and rewritten.

:return: the fitness vectors corresponding to the rewritten decision vectors in *dvs*.

:exception std\:\:invalid_argument: if the input problem *p* does not provide at least the :cpp:enumerator:`~pagmo::thread_safety::basic` thread safety level.
:exception unspecified: any exception raised by :cpp:func:`pagmo::thread_bfe::fitness_rewrite()`.

.. cpp:function:: std::string get_name() const

:return: a human-readable name for this :cpp:class:`~pagmo::default_bfe`.
Expand Down
6 changes: 6 additions & 0 deletions doc/sphinx/docs/cpp/batch_evaluators/member_bfe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Member function BFE
:cpp:class:`~pagmo::member_bfe` is a simple wrapper which delegates batch fitness evaluations
to the input problem's :cpp:func:`pagmo::problem::batch_fitness()` member function.

.. note::

:cpp:class:`~pagmo::member_bfe` does not support in-place rewriting of the decision vectors
(see :cpp:func:`pagmo::bfe::fitness_rewrite()`), because the UDP's ``batch_fitness()`` member
function cannot modify its input.

.. cpp:function:: vector_double operator()(const problem &p, const vector_double &dvs) const

Call operator.
Expand Down
20 changes: 20 additions & 0 deletions doc/sphinx/docs/cpp/batch_evaluators/thread_bfe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ Multithreaded BFE
:exception std\:\:overflow_error: in case of (unlikely) internal overflow conditions.
:exception unspecified: any exception raised by memory allocation failures or by the public API of :cpp:class:`~pagmo::problem`.

.. cpp:function:: vector_double fitness_rewrite(problem p, vector_double &dvs) const

Batch fitness evaluation with in-place rewriting of the decision vectors.

.. versionadded:: 2.21.0

This member function behaves like the call operator, the only difference being that
the fitnesses are computed via :cpp:func:`pagmo::problem::fitness_rewrite()`, and that the
decision vectors in *dvs* are replaced with their rewritten versions. Each thread of execution
writes only into the portion of *dvs* containing the decision vectors it evaluates.

This member function is usually invoked via :cpp:func:`pagmo::bfe::fitness_rewrite()`.

:param p: the input :cpp:class:`~pagmo::problem`.
:param dvs: the input decision vectors that will be evaluated and rewritten.

:return: the fitness vectors corresponding to the rewritten decision vectors in *dvs*.

:exception unspecified: any exception raised by the call operator.

.. cpp:function:: std::string get_name() const

:return: a human-readable name for this :cpp:class:`~pagmo::thread_bfe`.
Expand Down
64 changes: 64 additions & 0 deletions doc/sphinx/docs/cpp/bfe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Batch fitness evaluator
std::string get_name() const;
std::string get_extra_info() const;
thread_safety get_thread_safety() const;
vector_double fitness_rewrite(const problem &, vector_double &) const;

See the documentation of the corresponding member functions in this class for details on how the optional
member functions in the UDBFE are used by :cpp:class:`~pagmo::bfe`.
Expand Down Expand Up @@ -211,6 +212,52 @@ Batch fitness evaluator
:exception std\:\:invalid_argument: if *dvs* or the return value produced by the UDBFE are incompatible with the input problem *p*.
:exception unspecified: any exception raised by the invocation of the UDBFE.

.. cpp:function:: vector_double fitness_rewrite(const problem &p, vector_double &dvs) const

Batch fitness evaluation with in-place rewriting of the decision vectors.

.. versionadded:: 2.21.0

This member function is the batch counterpart of :cpp:func:`pagmo::problem::fitness_rewrite()`: it evaluates
the decision vectors stored in *dvs* using the input problem *p* and, if *p* rewrites decision vectors during
the fitness evaluation (see :cpp:func:`pagmo::problem::has_fitness_rewrite()`), it replaces the content of
*dvs* with the rewritten decision vectors. The layout of *dvs* and of the return value is the same as in the
call operator.

The behaviour of this member function is the following:

* if *p* does not rewrite decision vectors, this member function is equivalent to the call operator,
and *dvs* is not modified;
* otherwise, if the UDBFE provides a ``fitness_rewrite()`` member function (as established by
:cpp:func:`~pagmo::bfe::has_fitness_rewrite()`), the UDBFE's ``fitness_rewrite()`` is invoked on a copy
of *dvs*. The rewritten decision vectors and the fitness vectors are then checked, the fitness evaluation
counter in *p* is increased and *dvs* is replaced with the rewritten decision vectors;
* otherwise, an error is raised.

Among the batch fitness evaluators provided by pagmo, :cpp:class:`~pagmo::thread_bfe` and
:cpp:class:`~pagmo::default_bfe` support in-place rewriting, while :cpp:class:`~pagmo::member_bfe` does not.

In case of exceptions, *dvs* is not modified.

:param p: the input :cpp:class:`~pagmo::problem`.
:param dvs: the input decision vectors that will be evaluated (and possibly rewritten) in batch mode.

:return: the fitness vectors corresponding to the (rewritten) decision vectors in *dvs*.

:exception std\:\:invalid_argument: if *dvs*, the rewritten decision vectors or the fitness vectors produced by the UDBFE
are incompatible with the input problem *p* (e.g., if a rewritten decision vector lies outside the bounds of *p*).
:exception not_implemented_error: if *p* rewrites decision vectors, but the UDBFE does not provide a
``fitness_rewrite()`` member function.
:exception unspecified: any exception raised by the invocation of the UDBFE.

.. cpp:function:: bool has_fitness_rewrite() const

Check if the UDBFE supports in-place rewriting of the decision vectors.

.. versionadded:: 2.21.0

:return: ``true`` if the UDBFE satisfies :cpp:class:`pagmo::has_bfe_fitness_rewrite`, ``false`` otherwise.

.. cpp:function:: std::string get_name() const

Get the name of this batch fitness evaluator.
Expand Down Expand Up @@ -326,6 +373,23 @@ Associated type traits

The value of the type trait.

.. cpp:class:: template <typename T> has_bfe_fitness_rewrite

.. versionadded:: 2.21.0

This type trait detects if ``T`` provides a member function whose signature is compatible with

.. code-block:: c++

vector_double fitness_rewrite(const problem &, vector_double &) const;

The ``fitness_rewrite()`` member function is part of the interface for the definition of a
user-defined batch fitness evaluator (see :cpp:func:`pagmo::bfe::fitness_rewrite()` for details).

.. cpp:member:: static const bool value

The value of the type trait.

.. cpp:class:: template <typename T> is_udbfe

This type trait detects if ``T`` is a user-defined batch fitness evaluator (or UDBFE).
Expand Down
36 changes: 36 additions & 0 deletions doc/sphinx/docs/cpp/problem.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,40 @@ Associated type traits

The value of the type trait.

.. cpp:class:: template <typename T> has_fitness_rewrite

.. versionadded:: 2.21.0

This type trait detects if ``T`` provides a member function whose signature
is compatible with

.. code-block:: c++

vector_double fitness_rewrite(vector_double &) const;

The ``fitness_rewrite()`` member function is part of the interface for the definition of a
user-defined problem (see :cpp:func:`pagmo::problem::fitness_rewrite()` for details).

.. cpp:member:: static const bool value

The value of the type trait.

.. cpp:class:: template <typename T> override_has_fitness_rewrite

.. versionadded:: 2.21.0

This type trait detects if ``T`` provides a member function whose signature
is compatible with

.. code-block:: c++

bool has_fitness_rewrite() const;

The ``has_fitness_rewrite()`` member function is part of the interface for the definition of a
user-defined problem (see :cpp:func:`pagmo::problem::has_fitness_rewrite()` for details).

.. cpp:member:: static const bool value

The value of the type trait.

.. cpp:namespace-pop::
8 changes: 8 additions & 0 deletions doc/sphinx/docs/cpp/tutorials/cpp_tut.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ Basics
:maxdepth: 2

evolving_a_population

Advanced topics
^^^^^^^^^^^^^^^

.. toctree::
:maxdepth: 2

rewriting_decoders
112 changes: 112 additions & 0 deletions doc/sphinx/docs/cpp/tutorials/rewriting_decoders.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
.. _cpp_tutorial_rewriting_decoders:

Rewriting decision vectors during the fitness evaluation
========================================================

.. versionadded:: 2.21.0

In some optimisation approaches, the fitness evaluation does more than just
computing objectives and constraints: it also *improves* the solution being evaluated,
and the improved solution should replace the original one. A prominent example are
the biased random-key genetic algorithms (BRKGA), where a *decoder* maps a vector of
random keys (the chromosome) to a solution, possibly improves the solution with a local
search, and then *rewrites* the chromosome so that it encodes the improved solution.
Writing the result of the local search back into the chromosome turns a memetic algorithm
from a Baldwinian one (only the fitness benefits from the local search) into a Lamarckian
one (the improved chromosomes are inherited by the offspring).

In pagmo, a user-defined problem (UDP) can opt in to this behaviour by implementing,
in addition to the mandatory ``fitness()`` member function, the optional member function

.. code-block:: c++

vector_double fitness_rewrite(vector_double &dv) const;

which must return the fitness of the decision vector *dv* **after** having rewritten it in place.
The UDP can also implement

.. code-block:: c++

bool has_fitness_rewrite() const;

in order to enable or disable the rewriting at runtime (see :cpp:func:`pagmo::problem::has_fitness_rewrite()`).

The rewritten decision vector must have the same size as the original one, it must lie within the problem
bounds and its integer part (if any) must contain integral values: pagmo checks these conditions and raises an
error otherwise, leaving the original decision vector untouched. Ideally, rewriting should also be idempotent
(i.e., rewriting an already rewritten decision vector should not change it), so that each stored decision vector
is a fixed point of the decoder.

The ``fitness()`` member function must not modify its argument. A convenient way of implementing both member
functions, used in the example below and borrowed from the
`BRKGA-MP-IPR <https://github.com/ceandrade/brkga_mp_ipr_cpp>`__ library, is to delegate them to a single
``decode(dv, rewrite)`` function.

How rewriting is used by pagmo
------------------------------

Rewriting happens through :cpp:func:`pagmo::problem::fitness_rewrite()` (and its batch counterpart,
:cpp:func:`pagmo::bfe::fitness_rewrite()`). If the UDP does not support rewriting, these functions
behave exactly like :cpp:func:`pagmo::problem::fitness()` (and the call operator of :cpp:class:`pagmo::bfe`),
so that existing problems are not affected in any way.

The :cpp:class:`~pagmo::population` class stores the rewritten decision vectors whenever it evaluates
the fitness itself, that is, in its constructors, in the single-argument overloads of
:cpp:func:`~pagmo::population::push_back()` and in :cpp:func:`~pagmo::population::set_x()`. The
overloads which receive both a decision vector and a fitness vector (such as
:cpp:func:`~pagmo::population::set_xf()`) store their arguments as they are.

The following algorithms store the rewritten decision vectors they generate, both when evaluating
serially and when using a batch fitness evaluator:

* multi-objective: :cpp:class:`~pagmo::nsga2`, :cpp:class:`~pagmo::nsga3`, :cpp:class:`~pagmo::moead`,
:cpp:class:`~pagmo::moead_gen`, :cpp:class:`~pagmo::nspso`, :cpp:class:`~pagmo::maco`;
* single-objective genetic and evolutionary algorithms: :cpp:class:`~pagmo::sga`, :cpp:class:`~pagmo::sea`,
:cpp:class:`~pagmo::de`, :cpp:class:`~pagmo::de1220`, :cpp:class:`~pagmo::sade`;
* swarm and other heuristics: :cpp:class:`~pagmo::pso`, :cpp:class:`~pagmo::pso_gen`, :cpp:class:`~pagmo::gwo`,
:cpp:class:`~pagmo::bee_colony`, :cpp:class:`~pagmo::ihs`, :cpp:class:`~pagmo::gaco`,
:cpp:class:`~pagmo::simulated_annealing`, :cpp:class:`~pagmo::compass_search`;
* local optimisers: :cpp:class:`~pagmo::nlopt` and :cpp:class:`~pagmo::ipopt` rewrite the final solution
they find (but not the points they evaluate during the optimisation), before comparing it with the
selected individual;
* meta-algorithms: :cpp:class:`~pagmo::cstrs_self_adaptive` forwards the rewriting to its inner algorithm
(through the penalized problem it evolves), and :cpp:class:`~pagmo::mbh` rewrites the perturbed decision vectors
(via :cpp:func:`~pagmo::population::set_x()`) as well as whatever its inner algorithm rewrites.

:cpp:class:`~pagmo::cmaes` and :cpp:class:`~pagmo::xnes`, on the other hand, evaluate the fitness via
:cpp:func:`pagmo::problem::fitness()`, and thus never rewrite the decision vectors they sample: their search
distribution update assumes that the evaluated decision vectors are the sampled ones. In all cases,
the stored decision vectors remain consistent with the stored fitness vectors.

Among the batch fitness evaluators provided by pagmo, :cpp:class:`~pagmo::thread_bfe` and
:cpp:class:`~pagmo::default_bfe` rewrite the decision vectors in parallel. Batch fitness evaluators which do not
support rewriting (such as :cpp:class:`~pagmo::member_bfe`, since the UDP's ``batch_fitness()`` member function
cannot modify its input) raise an error when used with a problem that rewrites decision vectors.

.. note::

The fitness evaluation of a rewriting UDP is subject to the same thread safety requirements as
the ``fitness()`` member function (see :cpp:func:`pagmo::problem::get_thread_safety()`).

.. note::

The meta-problems provided by pagmo (e.g., :cpp:class:`~pagmo::decompose`, :cpp:class:`~pagmo::unconstrain`
and :cpp:class:`~pagmo::translate`) do not forward rewriting to the problem they wrap: wrapping a rewriting UDP
in a meta-problem results in plain fitness evaluations.

An example: a random-key bi-objective TSP
-----------------------------------------

In the following example, a bi-objective travelling salesman problem is encoded with random keys:
the decoder sorts the keys to obtain a tour, improves the tour with a 2-opt local search accepting
only the moves which improve one objective without worsening the other, and finally rewrites the keys so
that they encode the improved tour (by reassigning the sorted keys following the improved tour). The problem
is then solved with :cpp:class:`~pagmo::nsga2`, using a :cpp:class:`~pagmo::thread_bfe` to decode the
chromosomes in parallel.

.. literalinclude:: ../../../../../tutorials/rewriting_decoder.cpp
:language: c++
:linenos:

At the end of the evolution, every chromosome in the population is a fixed point of the decoder
(it encodes a 2-opt local optimum) and is consistent with its fitness.
9 changes: 9 additions & 0 deletions include/pagmo/algorithms/cmaes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ namespace pagmo
* reinserted into the population, CMA-ES may not preserve the best individual (not elitist). As a consequence the
* plot of the population best fitness may not be perfectly monotonically decreasing.
*
* .. note::
*
* CMA-ES never rewrites the decision vectors it samples, even if the problem supports
* rewriting (see :cpp:func:`pagmo::problem::fitness_rewrite()`): the fitness is evaluated
* via :cpp:func:`pagmo::problem::fitness()` (or the call operator of :cpp:class:`pagmo::bfe`),
* as the update of the search distribution assumes that the evaluated decision vectors are
* the sampled ones (which, unless ``force_bounds`` is ``true``, may also lie outside the problem bounds).
* The stored decision vectors are thus the sampled ones, and they remain consistent with the stored fitness vectors.
*
* .. seealso::
*
* Hansen, Nikolaus. "The CMA evolution strategy: a comparing review." Towards a new evolutionary computation.
Expand Down
14 changes: 14 additions & 0 deletions include/pagmo/algorithms/cstrs_self_adaptive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ struct PAGMO_DLL_PUBLIC penalized_udp {
// The fitness computation
vector_double fitness(const vector_double &) const;

// The fitness computation, rewriting the decision vector in place
vector_double fitness_rewrite(vector_double &) const;

// Rewriting is enabled only if the problem of the reference population supports it
bool has_fitness_rewrite() const;

// Call to this method updates all the members that are used to penalize the objective function
// As the penalization algorithm depends heavily on the ref population this method takes care of
// updating the necessary information. It also builds the hash map used to avoid unnecessary fitness
Expand Down Expand Up @@ -212,6 +218,14 @@ PAGMO_DLL_PUBLIC std::ostream &operator<<(std::ostream &, const penalized_udp &)
* The performances of :cpp:class:`pagmo::cstrs_self_adaptive` are highly dependent on the particular inner UDA
* employed and in particular to its parameters (generations / iterations).
*
* .. note::
*
* If the problem supports rewriting the decision vectors during the fitness evaluation
* (see :cpp:func:`pagmo::problem::fitness_rewrite()`), the penalized problem evolved by the inner UDA
* forwards the rewriting to the original problem, and the internal cache stores the rewritten decision
* vectors. Hence, if the inner UDA supports rewriting, the evolved population contains the rewritten decision
* vectors, consistently with their fitness vectors.
*
* .. seealso::
*
* Farmani, Raziyeh, and Jonathan A. Wright. "Self-adaptive fitness formulation for constrained optimization." IEEE
Expand Down
Loading
Loading