1
- // @Grab('org.apache.commons:commons-math3:3.6.1 ')
1
+ // @Grab('org.apache.commons:commons-math4:4.0-beta1 ')
2
2
// inspired by https://github.com/apache/commons-math/blob/MATH_3_X/src/userguide/java/org/apache/commons/math3/userguide/genetics/HelloWorldExample.java
3
3
4
4
import groovy.transform.*
5
- import org.apache.commons.math3 .genetics.*
6
- import static org.apache.commons.math3 .genetics.GeneticAlgorithm.randomGenerator as r
5
+ import org.apache.commons.math4.legacy .genetics.*
6
+ import static org.apache.commons.math4.legacy .genetics.GeneticAlgorithm.randomGenerator as r
7
7
8
8
class Scenario {
9
9
public static SENTENCE = " To be or not to be?"
@@ -19,20 +19,20 @@ double MUTATION_RATE = 0.03
19
19
double ELITISM_RATE = 0.1
20
20
int TOURNAMENT_ARITY = 2
21
21
22
- GeneticAlgorithm ga = new GeneticAlgorithm (
22
+ var ga = new GeneticAlgorithm (
23
23
new OnePointCrossover<Character > (), CROSSOVER_RATE ,
24
24
new RandomCharacterMutation (), MUTATION_RATE ,
25
25
new TournamentSelection (TOURNAMENT_ARITY ))
26
26
27
- def randomSentence = (0 .. < Scenario . DIMENSION ). collect{ Scenario . randomChar }
28
- def popList = (0 .. < POPULATION_SIZE ). collect{ new StringChromosome (randomSentence) }
29
- Population initial = new ElitisticListPopulation (popList, 2 * popList. size(), ELITISM_RATE )
27
+ var randomSentence = (0 .. < Scenario . DIMENSION ). collect{ Scenario . randomChar }
28
+ var popList = (0 .. < POPULATION_SIZE ). collect{ new StringChromosome (randomSentence) }
29
+ var initial = new ElitisticListPopulation (popList, 2 * popList. size(), ELITISM_RATE )
30
30
31
- def stoppingCondition = new StoppingCondition () {
31
+ var stoppingCondition = new StoppingCondition () {
32
32
int generation = 0
33
33
34
34
boolean isSatisfied (Population population ) {
35
- Chromosome fittestChromosome = population. fittestChromosome
35
+ var fittestChromosome = population. fittestChromosome
36
36
if (generation == 1 || generation % 10 == 0 )
37
37
println " Generation $generation : $fittestChromosome "
38
38
generation++
@@ -41,15 +41,15 @@ def stoppingCondition = new StoppingCondition() {
41
41
}
42
42
43
43
println " Starting evolution ..."
44
- Population finalPopulation = ga. evolve(initial, stoppingCondition)
44
+ var finalPopulation = ga. evolve(initial, stoppingCondition)
45
45
println " Generation $ga . generationsEvolved : $finalPopulation . fittestChromosome "
46
46
47
47
@InheritConstructors @AutoImplement
48
48
class StringChromosome extends AbstractListChromosome<Character > {
49
49
double fitness () {
50
50
(0 .. < Scenario . DIMENSION ). inject(0 ) { sum , i ->
51
- def target = Scenario . SENTENCE . charAt(i)
52
- def allele = representation. get(i). charValue()
51
+ var target = Scenario . SENTENCE . charAt(i)
52
+ var allele = representation. get(i). charValue()
53
53
// fitter if closer ascii "distance"
54
54
sum - (target - allele). abs()
55
55
}
@@ -63,7 +63,7 @@ class StringChromosome extends AbstractListChromosome<Character> {
63
63
class RandomCharacterMutation implements MutationPolicy {
64
64
Chromosome mutate (Chromosome original ) {
65
65
int mutationIndex = r. nextInt(Scenario . DIMENSION )
66
- def mutation = original. representation. toList()
66
+ var mutation = original. representation. toList()
67
67
mutation. set(mutationIndex, Scenario . randomChar)
68
68
original. newFixedLengthChromosome(mutation)
69
69
}
0 commit comments