Optimisation Algorithm

Writing an Optimisation Algorithm

An optimisation algorithm in EvoAl is a template of steps after the unified meta-algorithm model: the steps of the initialisation (iteration 0) and the steps of one iteration, e.g. sample candidates, evaluate them, accept them. A shared run kernel executes the template. It numbers the iterations, checks the stopping criteria, handles an exhausted evaluation budget, records the memory model and passes the run events to the statistics writers. The algorithm only describes its search.

The full guide, including the search state, candidates and lineage, budget handling and the run events, is the README of de.evoal.optimisation.api. The plugin de.evoal.optimisation.random_search is the smallest complete example.

DL: the configuration

Declare a type that extends 'algorithm' in a .dl file of your plugin. The type inherits 'initialisation', 'optimisation-function', 'comparator' and 'stopping-criteria'; add the options of your algorithm:

import "definitions" from de.evoal.optimisation.core;

module com.example.optimisation {
  type 'new-algorithm' extends 'algorithm' {
    @LowerBoundary { boundary := 1; }
    'samples-per-iteration' : int;
  }
}

Java: the template

Extend TemplateAlgorithm and name the bean after the DL module and type. configure reads the options, template() assembles the steps for a run:

@Dependent
@Named("com.example.optimisation.new-algorithm")
public class NewAlgorithm extends TemplateAlgorithm {
    @Inject @Dependent @Component
    private OptimisationValueComparator comparator;

    @Inject
    private EvaluationService evaluationService;

    @Inject
    private AttributeHelper helper;

    @Inject @Named("initial")
    private InitialCandidatesProvider provider;

    private int samplesPerIteration;

    @Override
    protected void configure(final EObject configuration) {
        samplesPerIteration = helper.<Integer>lookup(configuration, "samples-per-iteration");
    }

    @Override
    protected Template template() {
        final List<Step> steps = List.of(new SampleCandidates(provider, samplesPerIteration),
                                         new EvaluateCandidates(evaluationService, comparator),
                                         new ReplacePopulation());

        return new Template(steps, steps);
    }
}

TemplateAlgorithm creates the stopping criteria before configure is called. Throw an InitializationException in configure if the configuration cannot be used.

Steps

Every step has one kind of the meta-algorithm model, given by the interface it implements: Initialization, Selection, Adaption, FitnessCalculation, Acceptance or ParameterAdjustment. Reusable steps are SampleCandidates, EvaluateCandidates (also EvaluateCandidates.untilAccepted for one candidate after the other) and ReplacePopulation. Your own steps implement a kind interface and work on the SearchState.

Always evaluate candidates through the EvaluationService: it counts the evaluations and enforces the evaluation budget.

What else you can inject

Injection Content
@Inject @Named("initial") InitialCandidatesProvider the configured 'initialisation'; create() returns a stream of candidates
@Inject @Dependent @Component OptimisationValueComparator the configured 'comparator'; turns an evaluation result into an optimisation value
@Inject EvaluationService evaluates candidates with the configured 'optimisation-function'
@Inject @Named("search-space") Space the search variables of the problem
@Inject AttributeHelper reads attributes of the configuration (lookup(configuration, "name"))
@Inject RandomService named, seeded random streams: random("my-plugin/purpose") for decisions of the algorithm, taskRandom("my-plugin/purpose") for values drawn while a candidate is evaluated (a new generator per evaluation, derived from the evaluation index, so the values do not depend on the order or thread of the evaluations)

Parallel evaluation

Evaluator.evaluateAll (used by EvaluateCandidates in batch mode) evaluates a batch of candidates on the threads of the run; -Bcore:evaluation-threads/EVOAL_THREADS set their number. The result of a run must not depend on it, so:

  • Evaluate a batch where the candidates are independent, and handle a batch cut by the budget (BatchEvaluation) like a sequential run would.
  • Other parallel work of your algorithm (e.g. moving particles) runs on the EvaluationExecutor and uses one random stream per entity, never a shared one.
  • An optimisation function declares with isThreadSafe() (default false) whether evaluate may run on several threads: no shared mutable state in evaluate, no new EClasses during a run, random numbers only from taskRandom. Functions that are not thread-safe are evaluated one after the other.

The README of de.evoal.optimisation.api explains the details.

The plugin

Register the plugin in plugins/pom.xml, the regression and JaCoCo modules, and RELEASE_PLUGINS in ci/create-release.sh. Add golden-output regressions under src/test/regressions/<name>/. The README lists the details.