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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
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
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
.
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.
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.
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.