Custom Crossover and Mutate Operator


  • Anonymous

    I'm trying to implement my own Crossover and mutate operator but don't know which interface i should implement. Do you have an example how to create a custom operator for EA Algorithm?


  • Felix Reimann
    Felix Reimann

    You have to do two tasks:
    -implement the operator
    -tell the framework to use this implementation

    Select the Operator interface which fits your needs best. You find them in the


    package. The EA uses crossover and mutate operators, say your problem uses the


    (like, e.g., the Queens Example).
    By implementing the interface


    an operator is created which performs a crossover operation (as used by, e.g., EAs) on the



    public class MyOperator implements CrossoverBoolean {
            // method defined by CrossoverBoolean
        public Pair<BooleanGenotype> crossover(BooleanGenotype parent1, BooleanGenotype parent2) {
            // Instantiate genotypes of the same class like the parents. Each
            // Genotype has the newInstance() method.
            BooleanGenotype g1 = parent1.newInstance();
            BooleanGenotype g2 = parent1.newInstance();
            assert parent1.size() == parent2.size() : "problem size differs";
            for (int i = 0; i < parent1.size(); i++) {
                // your operator logic
                g1.add(i, parent1.get(i) || parent2.get(i));
                g2.add(i, parent1.get(i) && parent2.get(i));
            // The crossover operator returns two new genotype based on two parents.
            return new Pair<BooleanGenotype>(g1, g2);

    Task 2:
    To tell the framework to use your operator you implement a CrossoverModule and bind BooleanOperator to your implementation. You can find more details how this binding works in our tutorial

    public class MyGenotypeModule extends CrossoverModule {
        protected void config() {

    Now if you start Opt4J, you'll find a list item "MyGenotype" in "Modules->Optimizer->Operator" of the configurator GUI. Select it together with the other module defining your problem (again, see the tutorial) and press run. Done.


  • Anonymous

    Thank you very for the answer.
    I'm quite new to the Google Guice concept, but i have understood it a little bit and my programm runs well now.