Definitions

Every component that a configuration can name, e.g. an optimisation function, an operator or a statistics writer, has a type in the Definition Language (DL). The type declares the attributes of the component and their checks; the configurations in the other languages instantiate it. This page describes how a plugin connects its DL types to its Java classes; the language itself (types, attributes, annotations, functions) is described on the Definition Language page.

The DL file

The DL file of a plugin lies in its resources, in the folder of the module name: the module com.example.functions is the file src/main/resources/com/example/functions.dl (the last segment of the name is the file name). EvoAl reads the DL files from the plugin jars when a configuration imports them (import "definitions" from 'com.example.functions';), so the package of the folder has to be open in the module-info.java of the plugin:

module com.example.plugin {
    // ...
    opens com.example; // the DL files
}

A type extends the base type of its kind, e.g. 'fitness-function' for an optimisation function, mutation for a mutation of the evolutionary algorithm, 'statistics-writer' for a statistics writer, 'candidate-check' for a candidate check, or 'algorithm' for an optimisation algorithm (see Writing an Optimisation Algorithm). Its documentation comments describe the component for the users.

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

module com.example.functions {
  /**
   * The sum of the squares of all search variables.
   */
  type 'sum-of-squares' extends 'fitness-function' {
    /**
     * The objective that gets the sum.
     */
    objective : data;
  }
}

The Java class

The component is a CDI bean named after the module and the type, <module>.<type>. EvoAl creates it when a configuration names the type and passes the configuration, the instance of the DL type, to init. AttributeHelper.lookup reads an attribute; an attribute of type data gives the feature of the data, which the class looks up in its space:

@Dependent
@Named("com.example.functions.sum-of-squares")
public class SumOfSquares extends OptimisationFunctionImpl {
    @Inject
    private AttributeHelper attributes;

    @Inject @Named("optimisation-space")
    private Space optimisationSpace;

    private EStructuralFeature objective;

    @Override
    public OptimisationFunction init(final EObject configuration) {
        final EStructuralFeature data = attributes.lookup(configuration, "objective");
        objective = optimisationSpace.find(data.getName()).orElseThrow();

        return this;
    }

    @Override
    public TypedEObject evaluate(final TypedEObject candidate) {
        double sum = 0.0;
        for(final EStructuralFeature feature : candidate.eSpace()) {
            final double x = candidate.eGetAsDouble(feature);
            sum += x * x;
        }

        final TypedEObject result = optimisationSpace.newEObject();
        result.eSet(objective, NumericConversions.toType(sum, objective.getEType()));

        return result;
    }

    @Override
    public boolean isThreadSafe() {
        return true;
    }
}
  • The plugin needs a META-INF/beans.xml, so CDI finds its beans, and every package with beans needs opens <package> to weld.core.impl; in the module-info.java; without it, every run fails with an InaccessibleObjectException.
  • An optimisation function is evaluated in parallel only if isThreadSafe() returns true: then evaluate may only read what init set up and has to draw random numbers from the task stream of the evaluation (RandomService.taskRandom). The README of de.evoal.optimisation.api lists the rules.
  • An optional attribute without a default that is not set has no value: test it with eIsSet before lookup.

The configuration then uses the type like every other function:

import "definitions" from 'com.example.functions';

'optimisation-function' := 'sum-of-squares' { objective := data 'y'; };

Checks in the DL

Write the checks of the attributes as annotations of the DL type instead of Java code, so the configuration is checked when it is loaded and the users see the rules in the DL file:

  • constraint annotations such as @LowerBoundary and @UpperBoundary for the values of attributes, @MinimumNumberOfElements and @MaximumNumberOfElements for arrays;
  • requirement annotations such as @Requires, @Excludes, @RequiresScale and @RequiresRepresentation for the configuration around the component, e.g. the scale of the search variables it works on.

The Definition Language page lists them.

Literature and credits

@Literature { keys := ["kennedy1995particle"]; } on a type names the publications the component implements. The BibTeX entries lie in META-INF/evoal/credits.bib of the plugin, and META-INF/evoal/credits.json lists the citations and the libraries of the plugin. A run writes the literature of its configuration into citations.bib of its output folder; see Credits and Licences.

A plugin outside the core repository

The EvoAl template is a Maven project for a plugin of your own: the module declaration, beans.xml, a DL file, a component with a unit test, the credits and a usage example. A plugin of the core repository is registered as described in Writing an Optimisation Algorithm.