Menu

Custom Crossover and Mutate Operator

Anonymous
2011-04-19
2013-02-08
  • Anonymous

    Anonymous - 2011-04-19

    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?

    Thanks

     
  • Felix Reimann

    Felix Reimann - 2011-04-19

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

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

    org.opt4j.operator
    

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

    BooleanGenotype
    

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

    CrossoverBoolean
    

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

    BooleanGenotype
    

    .

    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 http://opt4j.sourceforge.net/documentation/2.3/book.xhtml.

    public class MyGenotypeModule extends CrossoverModule {
        protected void config() {
            bind(CrossoverBoolean.class).to(MyOperator.class);
        }
    }
    

    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

    Anonymous - 2011-04-19

    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.

     

Log in to post a comment.