Skip to content

Algorithms

Every algorithm is a class with the same interface: you set it up once with its settings, then call solve(problem, seed) to get a Result.

Choosing an algorithm

All 34 algorithms, with their settings, defaults and papers, are listed in Algorithms. No algorithm wins everywhere, so compare a few on your problem with an experiment. Good starting points:

  • Continuous, 10 000 evaluations per dimension or more: L-SHADE and IPOP-CMA-ES, with JADE and SHADE close behind.
  • Smooth or badly scaled, or a small budget: CMA-ES. Nelder-Mead for a fast local search on a few variables.
  • A simple first try: DE or PSO.
  • Tours, assignments and other combinatorial problems: a local search such as Tabu Search, ILS or VNS, or MAX-MIN Ant System with a heuristic. They move through the problem's own moves (swaps, insertions, inversions), and on tours they do far better than population methods on codes.
  • Permutations, selections and binary problems with a population method: GA or SA with space="native" (see Native operators). Every other algorithm also works through the [0, 1] codes.
  • Global search, then refinement: combine methods with Sequence, or give a population method a local search with local_search=.
  • Several objectives: NSGA-II for two or three, NSGA-III for three or more (see Multiple objectives).
  • A baseline: every method should beat Random Search on the same budget.

How a run works

A run creates a first set of solutions, then repeats one iteration (a step that creates and evaluates new solutions) until a stopping rule is met. Callbacks run after every iteration.

%%{init: {"flowchart": {"curve": "monotoneY"}}}%%
flowchart TB
    S("solve(problem, seed)") --> I("initialize: create and evaluate<br>the first solutions")
    I --> T{"stopping rule met?<br>max_iter, max_nfe, target,<br>max_time, callback"}
    T -- no --> IT("iterate: create new solutions,<br>evaluate them, select")
    IT --> CB("callbacks")
    CB --> T
    T -- yes --> R("Result")

Common settings

Every algorithm accepts these, next to its own settings:

Setting Default Meaning
max_iter None maximum number of iterations
pop_size 100 population size (some algorithms use another default, see their pages)
max_nfe None maximum number of objective function evaluations
target None stop when a feasible solution at least this good is found
max_time None stop after this many seconds
display False print the best value after every iteration
x0 None starting solutions: codes, decoded solutions, Individual objects or a Result. They become the first members of the initial population (the starting point of a single-solution method, the initial mean of CMA-ES), and the rest is random. Restarts stay random
local_search None an algorithm, for example HillClimbing(), that improves some new solutions (memetic runs). Population methods only
local_search_prob 0.1 the share of new solutions improved by local_search
local_search_nfe 100 the most evaluations of one local search run

Settings are plain attributes that you can read and change. They are checked when the algorithm is made and again when solve starts. Printing an algorithm shows all its settings, its own first and then the common ones:

import ypea

de = ypea.DE(max_nfe=20_000, pop_size=50, crossover_prob=0.9)
de.crossover_prob = 0.5  # settings are plain attributes
print(de)
DE(strategy='DE/rand/1/bin', base_vector='rand', diff_vectors_count=1, crossover_method='bin', crossover_prob=0.5, beta_min=0.5, beta_max=0.5, max_iter=None, pop_size=50, max_nfe=20000, target=None, max_time=None, display=False, x0=None, local_search=None, local_search_prob=0.1, local_search_nfe=100)

Differs from Python: disp shows every property, including the run state, where Python prints the settings in one line.

de = ypea.DE(max_nfe=20000, pop_size=50, crossover_prob=0.9);
de.crossover_prob = 0.5;  % settings are plain properties
disp(de)
  DE with properties:

                  name: "Differential Evolution"
            short_name: "DE"
           base_vector: "rand"
    diff_vectors_count: 1
      crossover_method: "bin"
        crossover_prob: 0.5000
              beta_min: 0.5000
              beta_max: 0.5000
              strategy: "DE/rand/1/bin"
              max_iter: []
              pop_size: 50
               max_nfe: 20000
                target: []
              max_time: []
               display: 0
                    x0: []
          local_search: []
     local_search_prob: 0.1000
      local_search_nfe: 100
               problem: []
                  seed: []
                   rng: []
                  iter: 0
                   pop: []
                   nfe: 0
                  best: []
                 front: []

de.params() returns them as a dict. For more control over printing than display, use the Print callback.

Stopping rules

A run stops at the first limit it reaches:

  • max_nfe: never exceeded, even in the middle of an iteration.
  • max_iter: iteration 0 is the initialization.
  • target: a feasible solution at least as good as target.
  • max_time: checked between iterations, so the run may end a little later.
  • A callback that returns True (see Callbacks).

With none of max_iter, max_nfe and max_time given, a run stops after 100 iterations. Otherwise only the limits you set apply. Schedules that end with the run, such as the temperature of SA, follow whichever limit binds first.

result.stop_reason names the rule: "max_nfe", "max_iter", "target", "max_time" or "callback". It can also be "no_evaluations" (an iteration created no new solutions, for example GA with a population of 1) or "max_restarts" (IPOP-CMA-ES).

This example minimizes the sum of squares of 10 variables (the sphere function, best value 0) with a budget of 50 000 evaluations and a target of 1e-6. PSO reaches the target first, so the run stops early:

import numpy as np
import ypea

problem = ypea.Problem(
    ypea.Real("x", shape=10, lb=-5, ub=5),
    lambda sol: np.sum(sol.x**2),
)
result = ypea.PSO(max_nfe=50_000, target=1e-6).solve(problem, seed=1)
print(result.stop_reason, result.nfe, result.best.obj_value)
target 32400 8.719740113894366e-07

Differs from Python: MATLAB draws its own random numbers, so the counts and values differ from Python's.

problem = ypea.Problem( ...
    vars=ypea.Real("x", shape=10, lb=-5, ub=5), ...
    objective=@(sol) sum(sol.x.^2));
result = ypea.PSO(max_nfe=50000, target=1e-6).solve(problem, seed=1);
fprintf("%s %d %g\n", result.stop_reason, result.nfe, result.best.obj_value);
target 32900 9.50396e-07

The output shows the stop reason, the evaluations used and the best value found.

Reproducibility and seeds

solve(problem, seed=...) takes an integer, a NumPy Generator or SeedSequence, a ypea.StudioRng, or None (the default) for a fresh random run. Every random number comes from the run's own generator, and each run starts from a clean state, so the same object and seed always give the same result.

Algorithms pickle with their settings only, so they are cheap to send to worker processes.

The Studio generator

ypea.StudioRng is the random generator of YPEA Studio, the companion app. With it, a run in Python draws exactly the same numbers as the app with the same seed, so you can repeat a Studio run in Python and get the same result:

import numpy as np
import ypea

problem = ypea.Problem(
    ypea.Real("x", shape=10, lb=-5, ub=5),
    lambda sol: np.sum(sol.x**2),
)
rng = ypea.StudioRng(123)
result = ypea.DE(max_nfe=5_000).solve(problem, seed=rng)
print(result.best.obj_value)
0.40688034728081757

The seed is an integer from 0 to 4294967295. ypea.derive_seed(base, i) gives the seed of run i of a Studio experiment, and Experiment(..., seeds="studio") uses these seeds (see Experiments).