Skip navigation links

Package org.opt4j.operators

Provides the classes for general (generic) operators.

See: Description

Package org.opt4j.operators Description

Provides the classes for general (generic) operators.

The Genotype objects are changed within the optimization process in order to find better solutions. The variation is performed by the Operator classes. The framework already contains several operators:

Each Operator is parameterized with the corresponding target Genotype. Adding custom operators, e.g., a new Crossover operator for the DoubleGenotype, is quite simple. The new operator has to be extended from Crossover with the parameter DoubleGenotype or alternatively directly from the CrossoverDouble. The corresponding crossover method has to be implemented. Finally, the operator has to be added with the OperatorModule.addOperator(Class) method. Therefore, you can extend a custom module from CrossoverModule.

The appropriate operator is determined at runtime by the framework by checking the parameter of the operator. Alternatively, the Apply annotation can be used to specify a different target Genotype class.

Creating completely new operators is done by extending the Operator interface to the new operator with the specific method. The new operator implementation can be bound directly within a module in the AbstractModule.configure(com.google.inject.Binder) method by bind(CustomOperator.class).to(CustomOperatorImplmenentation.class) or using the addOperator methods in the respective OperatorModule. Note that this operator will only handle exactly its target Genotype classes. To indicate a specific target, use the Apply annotation at the respective operator class.

A new generic operator should be instantiated from the AbstractGenericOperator. See the existing generic operators.

Integration of a Custom Operator

To add new operators, two tasks have to be performed: In the following, a custom Crossover operator for the BooleanGenotype shall be implemented that performs a bitwise or for one offspring and a bitwise and for the other offspring.
 public class MyOperator implements Crossover<BooleanGenotype> {
        public Pair<BooleanGenotype> crossover(BooleanGenotype parent1, BooleanGenotype parent2) {
                BooleanGenotype g1 = parent1.newInstance();
                BooleanGenotype g2 = parent1.newInstance();
 
                for (int i = 0; i < parent1.size(); i++) {
                        g1.add(i, parent1.get(i) || parent2.get(i));
                        g2.add(i, parent1.get(i) && parent2.get(i));
                }
 
                return new Pair<BooleanGenotype>(g1, g2);
        }
 }
 
To tell the framework to use this operator, implement a CrossoverModule and add your custom operator.
 public class MyOperatorModule extends CrossoverModule {
        protected void config() {
                addOperator(MyOperator.class);
        }
 }
 
Skip navigation links