-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneticOptimizer.pm
186 lines (141 loc) · 4.82 KB
/
GeneticOptimizer.pm
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package GeneticOptimizer;
#use strict;
#use warnings;
use GeneticOptimizer::FamilyForrest;
#########################################
## Constructor #
#########################################
sub new {
my ($class, %args) = @_;
my $self = {
fitness => $args{fitness},
population => $args{population} || 20,
sample_size => $args{sample_size} || 5,
this_generation => 0,
max_generations => $args{max_generations},
solutions => [],
ancestry_tree =>
GeneticOptimizer::FamilyForrest->new(),
};
#################
# Error checking
#
die "GeneticOptimizer::new failure: missing pattern"
unless (defined $args{pattern});
die "GeneticOptimizer::new failure: zero population is useless"
unless (defined $args{population});
die "GeneticOptimizer::new failure: missing fitness function"
unless (defined $args{fitness});
# Create actual class
bless ($self, $class);
##########################
# Seed initial population
#
$args{pattern}->current_generation (0);
$args{pattern}->max_generation ($self->{max_generations});
for (1 .. $args{population}) {
my $solution = $args{pattern}->derive();
my ($fitness, $metadata) = $self->fitness ($solution);
my $id = $self->{ancestry_tree}->add (
parents => [undef, undef],
genome => $solution,
score => $fitness,
metadata => $metadata,
generation => 0,
);
push (@{$self->{solutions}}, {
id => $id,
fitness => $fitness,
genome => $solution,
metadata => $metadata,
parents => [undef, undef],
});
}
return $self->sort_solutions;
}
#########################################
## Convenience functions #
#########################################
# Accessors
sub solutions { return @{$_[0]->{solutions}} }
sub best_id { return $_[0]->{solutions}[0]{id} }
sub best_score { return $_[0]->{solutions}[0]{fitness} }
sub best_genome { return $_[0]->{solutions}[0]{genome} }
sub this_generation { return $_[0]->{this_generation} }
sub max_generations { return $_[0]->{max_generations} }
sub ancestral_tree { return $_[0]->{ancestry_tree} }
sub maxed_out {
my ($self) = @_;
return not (
$self->{this_generation} < $self->{max_generations}
);
}
sub set_fitness { $_[0]->{fitness} = $_[1] }
# Fitness functions
sub fitness {
my ($self, $genome) = @_;
return $self->{fitness}->($genome)
}
# Thank you http://geneticprogramming.us/Fitness.html
sub adjust_fitness { return 1.0 / ($_[0] + 1.0) }
# Sorts solutions by fitness rating
sub sort_solutions {
my ($self) = @_;
$self->{solutions} = [
sort { $a->{fitness} <=> $b->{fitness} } @{$self->{solutions}}
];
return $self;
}
#########################################
## Step functions #
#########################################
# Executes a single step in the genetic optimization.
# Returns 0 if the algorithm has completed
# Returns 1 otherwise
sub step {
my ($self) = @_;
# Check for passing max iterations
return 0 if ($self->maxed_out);
# Check for best possible solution
return 0 if ($self->best_score == 0);
# Increment generation number
$self->{this_generation} += 1;
# Generate the children
my @children = ();
while (scalar @children < $self->{population}) {
my $parent_1 = tournament_select ($self->{sample_size}, $self->{solutions});
my $parent_2 = tournament_select ($self->{sample_size}, $self->{solutions});
my $child = $parent_1->{genome}->mix($parent_2->{genome});
my ($fitness, $metadata) = $self->fitness($child);
my $id = $self->{ancestry_tree}->add (
parents => [$parent_1->{id}, $parent_2->{id}],
genome => {$child->hash},
score => $fitness,
metadata => $metadata,
generation => $self->this_generation,
);
push (@children, {
id => $id,
fitness => $fitness,
genome => $child,
metadata => $metadata,
parents => [$parent_1->{id}, $parent_2->{id}],
});
}
$self->{solutions} = \@children;
$self->sort_solutions;
return 1;
}
# Selects a tournament of size N randomly, and picks the best solution out of
# that set
sub tournament_select {
my ($size, $pool) = @_;
my @candidates = ();
while (scalar @candidates < $size) {
my $index = utils::rint (0, scalar @{$pool});
push (@candidates, $pool->[$index]);
}
@candidates = sort { $a->{fitness} <=> $b->{fitness} } @candidates;
return $candidates[0];
}
1;