/
00-functions.t
82 lines (54 loc) · 2.56 KB
/
00-functions.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env perl6
use v6;
use Test;
use Algorithm::Evolutionary::Simple;
my $length = 32;
my @χ= random-chromosome( $length );
cmp-ok( @χ, "ne", random-chromosome($length), "Random chromosomes");
my $number-ones = reduce { $^b + $^a }, 0, |@χ;
cmp-ok( max-ones( @χ ), "==", $number-ones, "Max ones correct");
my $population-size = 32;
my @initial-population;
for 1..$population-size -> $p {
@initial-population.push: random-chromosome( $length );
cmp-ok( @initial-population[$p-1], "==", mutation( @initial-population[$p-1] ), "Mutation works" );
}
my @another-population = initialize( size => $population-size,
genome-length => $length );
cmp-ok( @another-population.elems, "==", $population-size );
# Crossover
my @χs = crossover( @initial-population[0], @initial-population[1]);
cmp-ok( @initial-population[$_], &[!eqv], @χs[$_], "$_ chromosome xovered" ) for 0..1;
# Evaluation
my %fitness-of;
my $population = evaluate( population => @initial-population,
fitness-of => %fitness-of,
evaluator => &max-ones );
my $one-of-them = $population.pick();
ok( %fitness-of{$one-of-them}, "Evaluated to " ~ %fitness-of{$one-of-them});
my $best = $population.sort(*.value).reverse.[0..1].Bag;
my $initial-fitness = $best.values.[0];
my @pool = get-pool-roulette-wheel( $population, $population-size-2);
cmp-ok( @pool.elems, "==", $population-size-2, "Correct number of elements" );
# Reproduce
my @new-population= produce-offspring( @pool );
cmp-ok( @new-population.elems, "==", $population-size-2, "Correct number of elements in reproduction" );
$population = Bag(evaluate( population => @new-population,
fitness-of => %fitness-of,
evaluator => &max-ones ) ∪ $best );
cmp-ok( $population.elems, "<=", $population-size, "Correct number of elements in new generation" );
my $now-fitness = $population.sort(*.value).reverse.[0].value;
cmp-ok( $now-fitness, ">=", $initial-fitness, "Improving fitness " );
$population = generation( population => $population,
fitness-of => %fitness-of,
evaluator => &max-ones,
population-size => $population-size);
my $evolved-fitness = $population.sort(*.value).reverse.[0].value;
cmp-ok( $evolved-fitness, ">=", $now-fitness, "Improving fitness by evolving " );
# Merge populations
my $another-population = evaluate( population => @another-population,
fitness-of => %fitness-of,
evaluator => &max-ones );
my $merged = mix( $population, $another-population, $population-size);
cmp-ok( best-fitness($merged), ">=", $evolved-fitness, "Improving fitness by mixing " );
done-testing;