.. index:: single: differential_evolution(Problem,Random)
.. _differential_evolution/2:

.. rst-class:: right

**object**

``differential_evolution(Problem,Random)``
==========================================

* ``Problem`` - Problem object implementing ``differential_evolution_problem_protocol``.
* ``Random`` - Random number generator object providing ``between/3``, ``random/1``, and ``randomize/1`` predicates.


Differential Evolution (DE/rand/1/bin, DE/rand/1/exp, DE/best/1/bin, and DE/current-to-best/1/bin) metaheuristic for continuous bounded optimization.

| **Availability:** 
|    ``logtalk_load(differential_evolution(loader))``

| **Author:** Paulo Moura
| **Version:** 2:0:0
| **Date:** 2026-10-19

| **Compilation flags:**
|    ``static, context_switching_calls``


| **Imports:**
|    ``public`` :ref:`options <options/0>`
| **Uses:**
|    :ref:`linear_algebra <linear_algebra/0>`
|    :ref:`list <list/0>`
|    :ref:`numberlist <numberlist/0>`
|    :ref:`type <type/0>`

| **Remarks:**

   - Strategies: The ``strategy(rand/1/bin)`` (default), ``strategy(rand/1/exp)``, ``strategy(best/1/bin)``, and ``strategy(current-to-best/1/bin)`` options select the classic mutation strategies.
   - Optimization objective: The ``objective(minimize|maximize)`` option selects the fitness ordering.
   - Boundary handling: Trial vectors that leave the bounds are clamped to the nearest bound.
   - Seed control: The ``seed(S)`` option initializes the random number generator for reproducible runs.

| **Inherited public predicates:**
|     :ref:`options_protocol/0::check_option/1`  :ref:`options_protocol/0::check_options/1`  :ref:`options_protocol/0::default_option/1`  :ref:`options_protocol/0::default_options/1`  :ref:`options_protocol/0::option/2`  :ref:`options_protocol/0::option/3`  :ref:`options_protocol/0::valid_option/1`  :ref:`options_protocol/0::valid_options/1`  

.. contents::
   :local:
   :backlinks: top

Public predicates
-----------------

.. index:: run/2
.. _differential_evolution/2::run/2:

``run/2``
^^^^^^^^^

Runs Differential Evolution with default options and returns the best position and fitness found.

| **Compilation flags:**
|    ``static``

| **Template:**
|    ``run(BestPosition,BestFitness)``
| **Mode and number of proofs:**
|    ``run(-list(number),-number)`` - ``one_or_error``

| **Exceptions:**
|    ``Bounds`` returned by ``Problem::position_bounds/1`` is a variable:
|        ``instantiation_error``
|    ``Positions`` returned by ``Problem::initial_positions/1`` is a variable:
|        ``instantiation_error``
|    ``Fitness`` returned by ``Problem::fitness/2`` is a variable:
|        ``instantiation_error``
|    ``Bounds`` returned by ``Problem::position_bounds/1`` is not a valid list of position bounds:
|        ``domain_error(position_bounds,Bounds)``
|    ``Positions`` returned by ``Problem::initial_positions/1`` is not a valid initial population:
|        ``domain_error(initial_positions,Positions)``
|    ``Fitness`` returned by ``Problem::fitness/2`` is not a number:
|        ``domain_error(fitness,Fitness)``


------------

.. index:: run/3
.. _differential_evolution/2::run/3:

``run/3``
^^^^^^^^^

Runs Differential Evolution with the given options and returns the best position and fitness found.

| **Compilation flags:**
|    ``static``

| **Template:**
|    ``run(BestPosition,BestFitness,Options)``
| **Mode and number of proofs:**
|    ``run(-list(number),-number,+list(compound))`` - ``one_or_error``

| **Exceptions:**
|    ``Options`` is a variable:
|        ``instantiation_error``
|    An element ``Option`` of the list ``Options`` is a variable:
|        ``instantiation_error``
|    ``Bounds`` returned by ``Problem::position_bounds/1`` is a variable:
|        ``instantiation_error``
|    ``Positions`` returned by ``Problem::initial_positions/1`` is a variable:
|        ``instantiation_error``
|    ``Fitness`` returned by ``Problem::fitness/2`` is a variable:
|        ``instantiation_error``
|    ``Options`` is neither a variable nor a list:
|        ``type_error(list,Options)``
|    An element ``Option`` of the list ``Options`` is neither a variable nor a compound term:
|        ``type_error(compound,Option)``
|    An element ``Option`` of the list ``Options`` is a compound term but not a valid option:
|        ``domain_error(option,Option)``
|    ``Bounds`` returned by ``Problem::position_bounds/1`` is not a valid list of position bounds:
|        ``domain_error(position_bounds,Bounds)``
|    ``Positions`` returned by ``Problem::initial_positions/1`` is not a valid initial population:
|        ``domain_error(initial_positions,Positions)``
|    ``Fitness`` returned by ``Problem::fitness/2`` is not a number:
|        ``domain_error(fitness,Fitness)``


------------

.. index:: run/4
.. _differential_evolution/2::run/4:

``run/4``
^^^^^^^^^

Runs Differential Evolution with the given options and returns the best position, best fitness, and run statistics.

| **Compilation flags:**
|    ``static``

| **Template:**
|    ``run(BestPosition,BestFitness,Statistics,Options)``
| **Mode and number of proofs:**
|    ``run(-list(number),-number,-list(compound),+list(compound))`` - ``one_or_error``

| **Exceptions:**
|    ``Options`` is a variable:
|        ``instantiation_error``
|    An element ``Option`` of the list ``Options`` is a variable:
|        ``instantiation_error``
|    ``Bounds`` returned by ``Problem::position_bounds/1`` is a variable:
|        ``instantiation_error``
|    ``Positions`` returned by ``Problem::initial_positions/1`` is a variable:
|        ``instantiation_error``
|    ``Fitness`` returned by ``Problem::fitness/2`` is a variable:
|        ``instantiation_error``
|    ``Options`` is neither a variable nor a list:
|        ``type_error(list,Options)``
|    An element ``Option`` of the list ``Options`` is neither a variable nor a compound term:
|        ``type_error(compound,Option)``
|    An element ``Option`` of the list ``Options`` is a compound term but not a valid option:
|        ``domain_error(option,Option)``
|    ``Bounds`` returned by ``Problem::position_bounds/1`` is not a valid list of position bounds:
|        ``domain_error(position_bounds,Bounds)``
|    ``Positions`` returned by ``Problem::initial_positions/1`` is not a valid initial population:
|        ``domain_error(initial_positions,Positions)``
|    ``Fitness`` returned by ``Problem::fitness/2`` is not a number:
|        ``domain_error(fitness,Fitness)``

| **Remarks:**

    - Statistics list: Contains ``generations(N)``, ``evaluations(E)``, ``improvements(I)``, ``final_mean_fitness(M)``, and ``final_diversity(D)``.


------------

Protected predicates
--------------------

(no local declarations; see entity ancestors if any)

Private predicates
------------------

(no local declarations; see entity ancestors if any)

Operators
---------

(none)

.. seealso::

   :ref:`differential_evolution(Problem) <differential_evolution/1>`, :ref:`differential_evolution_problem_protocol <differential_evolution_problem_protocol/0>`

