freegsnke.optimiser module

Differential Evolution optimisation

Implemented generically, to optimise opaque objects

The optimiser copies objects using deepcopy, and manipulates objects using supplied control objects.

Copyright 2019 Ben Dudson, University of York. Email: benjamin.dudson@york.ac.uk

This file is part of FreeGSNKE.

FreeGSNKE is free software: you can redistribute it and/or modify it under the terms of 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.

FreeGSNKE 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with FreeGSNKE. If not, see <http://www.gnu.org/licenses/>.

freegsnke.optimiser.mutate(obj: object, controls: list[object]) → object[source]

Create a new object by randomly perturbing the controls of an object.

Each control value is scaled by a random factor in the range 0.95 to 1.05.

Parameters:
  • obj (object) – Object to copy. It is deep-copied, not modified.

  • controls (list[object]) – Control objects, each providing get(obj) and set(obj, value).

Returns:

The perturbed deep copy of obj.

Return type:

object

freegsnke.optimiser.optimise(obj: object, controls: list[object], measure: Callable[[object], float], maxgen: int = 10, N: int = 10, CR: float = 0.3, F: float = 1.0, monitor: Callable[[int, tuple[float, object], list[tuple[float, object]]], None] | None = None) → object[source]

Use Differential Evolution to optimise an object.

See https://en.wikipedia.org/wiki/Differential_evolution

Parameters:
  • obj (object) – Starting solution. Objects are deep-copied and passed to controls and measure.

  • controls (list[object]) – Control objects, each with methods set(object, value), which modifies the given object, and get(object), which returns the value from the object.

  • measure (Callable[[object], float]) – Returns a score for a given object; the optimiser minimises it.

  • maxgen (int, optional) – Maximum number of generations.

  • N (int, optional) – Population size (must be >= 4).

  • CR (float, optional) – Crossover probability, in [0, 1].

  • F (float, optional) – Differential weight, in [0, 2].

  • monitor (Callable or None, optional) – Called each generation as monitor(generation, best, population), where generation is an int, best is a (score, object) tuple and population is a list of (score, object) tuples.

Returns:

The object with the lowest score.

Return type:

object

freegsnke.optimiser.pickUnique(N: int, m: int, e: list[int]) → list[int][source]

Pick random distinct integers from 0 ... N-1, excluding some values.

The returned values are in a random order (not sorted).

Parameters:
  • N (int) – Size of the range to pick from.

  • m (int) – Number of values to pick; must satisfy m <= N - len(e).

  • e (list[int]) – Values to exclude.

Returns:

The m picked values.

Return type:

list[int]