/
evolib.jl
701 lines (594 loc) · 21.6 KB
/
evolib.jl
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
################################################################################
## LIBRARY FOR EVOLUTIONARY ALGORITHMS ##
################################################################################
#
# Authors: Joris Bayer
# Stefan Kroboth
#
# TODO:
# * [DONE] Create random Gene with constraints
# * [DONE] Create random Chromosome
# * [DONE] properly implement sort!() for the population
# * try to get the type constraints to work
# * more printing functions (human readable)
# * possibility to flag chromosomes as 'bad'
# * [PARTIAL?] handling of objective functions
# - new idea: get rid of the objective function within types entirely
# * [DONE] roulette wheel selection
# - needs objective function and a working sort
# + Update: why would it need the objective function? What was I thinking?
# * [DONE] crossover
# * [DONE] mutation
# * different selection/competition models
# - replace parents
# - in competition with parents
# - decision wether a child becomes part of the population or not
# - in short: all that (kappa, lambda) stuff (Papers?)
# * design similar to the one shown in prototype.jl
# - although in a more general way
# * stopping criterion
# * should Vector rather be AbstractArray{T,1} ?
# * when adding Chromosome to Population, check that the number of
# genes per chromosome are the same
# * It could be that sort doesn't need to be implemented as long as
# isless is defined for a certain type -- we should check that
# * [PRIORITY] Remove the dirty hack in roulette!
# * Implement adapting the standard deviation of genes
# * [DONE] Create type BitGene for "classical" genetic algorithms
# - [DONE] bits() could be helpful for that
# - [DONE] check if there is a way to convert to gray code
# * [PRIORITY] Make gray2binary work for bitstrings
# * discuss about how to split up this file
# * add more to the todo list
# I have no idea what I'm doing
abstract AbstractEvolutionary
abstract AbstractGene <: AbstractEvolutionary
abstract AbstractChromosome <: AbstractEvolutionary
abstract AbstractPopulation <: AbstractEvolutionary
abstract AbstractGenerations <: AbstractEvolutionary
abstract AbstractGeneticProbabilities <: AbstractEvolutionary
abstract AbstractGeneticOperation <: AbstractEvolutionary
abstract Recombination <: AbstractGeneticOperation
abstract Mutation <: AbstractGeneticOperation
abstract Reproduction <: AbstractGeneticOperation
abstract Immigration <: AbstractGeneticOperation
################################################################################
## GENE TYPE ##
################################################################################
type Gene <: AbstractGene
gene::Number
std::Float64
upper_limit::Float64
lower_limit::Float64
# Point to discuss: Should 'factor' be a part of the Gene type?
# Could be individually adapted, if necessary...
function Gene(gene::Number)
new(gene, 0.5, NaN, NaN)
end
function Gene(gene::Number, std::Float64)
new(gene, std, NaN, NaN)
end
function Gene(gene::Number, std::Float64, upper_limit::Float64,
lower_limit::Float64)
if lower_limit >= upper_limit
error("lower_limit must be less than upper_limit")
end
new(gene, std, upper_limit, lower_limit)
end
end
# Copy-constructor
function copy(gene::Gene)
Gene(copy(gene.gene),
copy(gene.std),
copy(gene.upper_limit),
copy(gene.lower_limit))
end
# Utility functions
std(gene::Gene) = gene.std
# increase standard deviation
function broaden_std(gene::Gene, factor::Float64)
gene.std *= factor
end
# decrease standard deviation
function narrow_std(gene::Gene, factor::Float64)
gene.std /= factor
end
# create random Gene
rand(T::Type{Gene}) = Gene(rand(), rand())
rand(T::Type{Gene}, std::Float64) = Gene(rand(), std)
rand(T::Type{Gene}, upper_limit::Float64, lower_limit::Float64) = Gene(rand()*(upper_limit-lower_limit)+lower_limit, rand(), upper_limit, lower_limit)
# create value in the given limit
rand(T::Type{Gene}, std::Float64, upper_limit::Float64, lower_limit::Float64) = Gene(rand()*(upper_limit-lower_limit)+lower_limit, std, upper_limit, lower_limit)
################################################################################
## BIT GENE TYPE ##
################################################################################
type BitGene <: AbstractGene
gene::ASCIIString # in gray code
gene_type::Type # for converting back
function BitGene(gene_type::Type, gene::ASCIIString)
if 8*sizeof(gene_type) != length(gene)
error("length of ASCII string does not agree with provided type")
end
new(gene, gene_type)
end
function BitGene{T<:Integer}(gene::T)
new(int2gray(gene), T)
end
end
int2gray(num::Integer) = bits(num $ (num >> 1))
function binary2int(bs::ASCIIString)
tmp = copy(bs)
s_len = length(tmp)
out = 0
for i = s_len-1:-1:0
# this feels so dirty...
out += 2^i*parse_int(ASCIIString([uint8(tmp[s_len-i])]))
end
return out
end
function gray2binary(num::Integer)
tmp = copy(num)
shift = 1
while shift < 8*sizeof(num)
tmp = tmp $ (tmp >> shift)
shift *= 2
end
return tmp
end
gray2binary(s::ASCIIString) = gray2binary(binary2int(s))
################################################################################
## CHROMOSOME TYPE ##
################################################################################
type Chromosome <: AbstractChromosome
genes::Vector
length::Int64
fitness::Float64
obj_func::Function
function Chromosome(genes::Vector{Gene}, obj_func::Function)
new(map(copy, genes), length(genes), Inf, obj_func)
# map ensures that each element of the vector is copied,
# not just the array itself. copy() would just copy the array
# and leave all references in genes as the are, which is wrong.
# the following might not work...
# maybe fitness shouldn't be calculated when created.
# but I think it's a good idea to keep the objective function around.
# Or maybe not (what if the objective function changes?).
# Comments requested ;)
fitness = obj_func(genes)
end
function Chromosome(genes::Vector{Gene})
#print("Warning: No objective function passed!\n")
new(copy(genes), length(genes), Inf, (x)->()) # hack, I don't know how to pass
# a 'None' function
end
function Chromosome(genes::Vector{Gene}, fitness::Float64)
#print("Warning: No objective function passed!\n")
new(copy(genes), length(genes), copy(fitness), (x)->())
end
function Chromosome(genes::Vector{Gene}, obj_func::Function)
new(copy(genes), length(genes), Inf, obj_func)
end
function Chromosome(genes::Vector{Gene}, fitness::Float64, obj_func::Function)
new(copy(genes), length(genes), copy(fitness), obj_func)
end
function Chromosome()
#print("Warning: No objective function passed!\n")
new(Gene[], 0, Inf, (x)->())
end
end
# Copy-constructor
function copy(chr::Chromosome)
# not sure if function has to be copied
Chromosome(copy(chr.genes),
copy(chr.fitness),
copy(chr.obj_func))
end
# referencing
ref(chromosome::Chromosome, ind...) = chromosome.genes[ind...]
# Utility functions
size(chromosome::Chromosome) = chromosome.length
length(chromosome::Chromosome) = chromosome.length
# print the stds of all genes of a chromosome indicated by a range/index
std(chromosome::Chromosome, ind...) = [std(gene) | gene = chromosome[ind...]]
# replace Gene of a Chromosome
function assign(chr::Chromosome, g::Gene, idx::Int64)
chr.genes[idx] = copy(g)
end
# replace Genes of a Chromosome
function assign(chr::Chromosome, gs::Vector{Gene}, idx::Range1{Int64})
chr.genes[idx] = map(copy, gs)
end
# replace Genes of a Chromosome
function assign(chr::Chromosome, gs::Vector{Gene}, idx::Vector{Int64})
chr.genes[idx] = map(copy, gs)
end
# push gene onto the chromosome
function push(chr::Chromosome, g::Gene)
push(chr.genes, copy(g))
chr.length += 1
end
(+)(chr::Chromosome, g::Gene) = push(chr, g)
# apply the objective function defined in the Chromosome type
function apply_obj_func(chromosome::Chromosome)
chromosome.fitness = chromosome.obj_func(chromosome) # not sure if this works, gotta test
end
# apply an objective function to a chromosome
function apply_obj_func(chromosome::Chromosome, obj_func::Function)
# untested
chromosome.fitness = obj_func(chromosome)
end
# increase standard deviation of all genes in Chromosome
function broaden_std(chromosome::Chromosome, factor::Float64)
for i = 1:length(chromosome)
# this can probably be done with map
broaden_std(chromosome[i], factor)
end
end
# decrease standard deviation of all genes in Chromosome
function narrow_std(chromosome::Chromosome, factor::Float64)
for i = 1:length(chromosome)
narrow_std(chromosome[i], factor)
end
end
rand(T::Type{Chromosome}, num::Int64, x...) = Chromosome([rand(Gene, x...) | i=1:num]) # neat
rand(T::Type{Chromosome}, num::Int64, obj_func::Function, x...) = Chromosome([rand(Gene, x...) | i=1:num], obj_func) # neat
function print(chromosome::Chromosome)
for i = 1:length(chromosome.genes)
print("|")
print(chromosome[i].gene)
end
println("|")
end
################################################################################
## POPULATION TYPE ##
################################################################################
type Population <: AbstractPopulation
chromosomes::Vector
pop_size::Int64
# several chromosomes passed
function Population(chromosomes::Vector)
new(chromosomes, length(chromosomes))
# should we copy that too?
end
# one chromosome passed
function Population(chromosome::Chromosome)
new([chromosome], 1)
# should we copy that too?
end
# empty Population
function Population()
new(Chromosome[], 0)
end
end
# Copy-constructor
function copy(pop::Population)
Population(copy(pop.chromosomes))
end
# referencing
ref(population::Population, ind...) = population.chromosomes[ind...]
# Utility functions
size(population::Population) = population.pop_size
length(population::Population) = population.pop_size
# overload (+) for easier appending
(+)(pop::Population, chr::Chromosome) = push(pop, chr)
# sum of the fitness of all chromosomes of a population
function fitness_sum(population::Population)
fitness_sum = 0
for i=1:length(population)
fitness_sum += population[i].fitness
end
return fitness_sum
end
# sum of the inverse of fitness of all chromosomes of a population
# needed for roulette
function inv_fitness_sum(population::Population)
fitness_sum = 0
for i=1:length(population)
fitness_sum += 1/population[i].fitness
end
return fitness_sum
end
function print_population(population::Population)
for i=1:length(population)
tmp = population[i]
print("$tmp \n")
end
end
# Modifiers
function push(population::Population, chromosome::Chromosome)
push(population.chromosomes, copy(chromosome))
population.pop_size += 1
end
function isless(chr1::Chromosome, chr2::Chromosome)
return chr1.fitness < chr2.fitness
end
ismore(chr1::Chromosome, chr2::Chromosome) = !isless(chr1, chr2)
# in-place sort
function sort!(population::Population)
sort!(isless, population.chromosomes)
end
# sort
function sort(population::Population)
sort(isless, population.chromosomes)
end
# in-place reverse sort
function sortr!(population::Population)
sort!(ismore, population.chromosomes)
end
# reverse sort
function sortr(population::Population)
sort(ismore, population.chromosomes)
end
# well, that one was easy.
rand(T::Type{Population}, chr_num::Int64, gene_num::Int64, obj_func::Function, x...) = Population([rand(Chromosome, gene_num, obj_func, x...) | i = 1:chr_num])
rand(T::Type{Population}, chr_num::Int64, gene_num::Int64, x...) = Population([rand(Chromosome, gene_num, x...) | i = 1:chr_num])
################################################################################
## GENERATIONS TYPE ##
################################################################################
# keeps track of generations
type Generations <: AbstractGenerations
populations::Array
generations::Int64
function Generations(population)
new([copy(population)], 1)
end
function Generations()
new(Population[], 0)
end
end
# Copy-Constructor
# does this even make sense for Generations?
function copy(gen::Generations)
Generations(copy(populations),
copy(generations))
end
# referencing
ref(generations::Generations, ind...) = generations.populations[ind...]
# Utility functions
get_generations(generations::Generations) = generations.generations
# Modifiers
function push(generations::Generations, population::Population)
push(generations.populations, copy(population))
generations.generations += 1
end
(+)(generations::Generations, population::Population) = push(generations, population)
################################################################################
## DATASTRUCTURE FOR GENETIC ALGORITHM PROBABILITIES ##
################################################################################
type GeneticProbabilities <: AbstractGeneticProbabilities
mutation::Float64
recombination::Float64
reproduction::Float64
immigration::Float64
function GeneticProbabilities(mutation::Float64,
recombination::Float64,
reproduction::Float64,
immigration::Float64)
sum = mutation + recombination + reproduction + immigration
new(mutation/sum, recombination/sum, reproduction/sum, immigration/sum)
end
end
get_vector(gp::GeneticProbabilities) = [gp.mutation, gp.recombination, gp.reproduction, gp.immigration]
################################################################################
## FUNCTIONS ##
################################################################################
# Roulette Wheel Selection on Population
function roulette(pop::Population)
#sort!(pop) # I don't think sorting is necessary and might even lead to
# problems... gotta check that
f_sum = inv_fitness_sum(pop)
idx = rand()*f_sum
x = 0
elem = 1
for i=1:length(pop)
# DIRTY DIRTY HACK!
# This is supposed to work without abs(), but this leads to problems when
# negative fitness is allowed... we have to find a fix for this.
x += abs(1/pop[i].fitness)
if idx < x
return elem
end
elem += 1
end
error("weird error that should not happen. You probably didn't define a fitness.")
end
# return several indices determined by roulette
roulette(pop::Population, num::Int64) = [ roulette(pop) | i = 1:num ]
# Roulette Wheel Selection on general Vectors (in case probabilies are passed)
function roulette(p::Vector{Float64})
# Am I thinking wrong? Is sorting even necessary?
#prop = sortr(p) # do not sort in-place!
prop = copy(p)
prop_sum = sum(prop) # In case it doesn't sum up to 1
idx = rand()*prop_sum
x = 0
elem = 1
for i=1:length(prop)
x += prop[i]
if idx < x
return elem
end
elem += 1
end
error("dafuq?")
end
# roulette wheel selection on GeneticProbabilities
function roulette(gp::GeneticProbabilities)
idx = roulette(get_vector(gp))
if idx == 1
return Mutation
elseif idx == 2
return Recombination
elseif idx == 3
return Reproduction
elseif idx == 4
return Immigration
else
error("Error: Impossible Error.")
end
end
# make sure the gene doesn't exceed it's limits
function assess_limits(g::Gene)
# works with NaNs!
if g.gene > g.upper_limit
g.gene = g.upper_limit
elseif g.gene < g.lower_limit
g.gene = g.lower_limit
end
end
# mutate a single gene
function mutate!(g::Gene)
g.gene += g.std*randn()
assess_limits(g)
end
function mutate(g::Gene)
gn = copy(g)
gn.gene += gn.std*randn()
assess_limits(gn)
return gn
end
# mutate a single gene with a predefined standard deviation
# -> ignores std settings in g.gene
function mutate!(g::Gene, std::Float64)
g.gene += std*randn()
assess_limits(g)
end
function mutate(g::Gene, std::Float64)
gn = copy(g)
gn.gene += std*randn()
assess_limits(gn)
return gn
end
# mutate a chromosome
function mutate!(chr::Chromosome)
for i=1:length(chr)
mutate!(chr[i])
end
end
function mutate(chr::Chromosome)
chrn = Chromosome()
for i=1:length(chr)
chrn + mutate(chr[i])
end
return chrn
end
# mutate a chromosome with a predefined standard deviation
# -> ignores std settings in g.gene
function mutate!(chr::Chromosome, std::Float64)
for i=1:length(chr)
mutate!(chr[i], std)
end
end
function mutate(chr::Chromosome, std::Float64)
chrn = Chromosome()
for i=1:length(chr)
chrn + mutate(chr[i], std)
end
return chrn
end
# mutate a whole population (not sure if anyone will ever need this)
function mutate!(pop::Population)
for i=1:length(pop)
mutate!(pop[i])
end
end
function mutate(pop::Population)
popn = Population()
for i=1:length(pop)
popn + mutate(pop[i])
end
return popn
end
# mutate a whole population with predefined std (not sure if anyone will ever need this)
function mutate!(pop::Population, std::Float64)
for i=1:length(pop)
mutate!(pop[i], std)
end
end
function mutate(pop::Population, std::Float64)
popn = Population()
for i=1:length(pop)
popn + mutate(pop[i], std)
end
return popn
end
# in-place crossover
function crossover!(chr1::Chromosome, chr2::Chromosome, slices::Int64)
@assert length(chr1) == length(chr2)
# weird, even works when rand produces 0
idx = sort([1, int64(round(length(chr1) * rand(slices))), length(chr1)+1])
tmp = copy(chr1)
for i=1:length(idx)-1
if i%2 == 0
range = [idx[i]:idx[i+1]-1]
chr1[range] = map(copy, chr2[range])
chr2[range] = map(copy, tmp[range])
end
end
end
function crossover!(pop::Population, slices::Int64)
idx = roulette(pop, 2)
crossover!(pop[idx[1]], pop[idx[2]], slices)
end
# crossover
function crossover(chr1::Chromosome, chr2::Chromosome, slices::Int64)
@assert length(chr1) == length(chr2)
# weird, even works when rand produces 0
idx = sort([1, int64(round((length(chr1)-1) * rand(slices)))+1, length(chr1)+1])
#tmp = copy(chr1)
chr1n = copy(chr1)
chr2n = copy(chr2)
for i=1:length(idx)-1
if i%2 == 0
range = [idx[i]:idx[i+1]-1]
chr1n[range] = map(copy, chr2[range])
chr2n[range] = map(copy, chr1[range])
end
end
return chr1n, chr2n
end
function crossover(pop::Population, slices::Int64)
idx = roulette(pop, 2)
return crossover(pop[idx[1]], pop[idx[2]], slices)
end
crossover(chr1::Chromosome, chr2::Chromosome) = crossover(chr1, chr2, 2)
crossover(pop::Population) = crossover(pop, 2)
################################################################################
## GENETIC ALGORITHM ##
################################################################################
function genetic(pop::Population, probabilities::GeneticProbabilities, iter::Int64, obj_func::Function)
# First version of a genetic algorithm - pretty basic, needs a lot more functionality and
# probably even a better design and more flexibility.
pop_o = copy(pop) # prevent in-place fiasco
obj_func(pop_o) # make sure the the fitness for every chromosome is available
gen = Generations() # Discussion: This might get pretty big... what to do?
for j = 1:iter # max generations
pop_n = Population()
for i = 1:length(pop_o)
operation = roulette(probabilities)
if operation == Mutation
chr = mutate(pop_o[roulette(pop_o)])
elseif operation == Recombination
chr = crossover(pop_o)
chr = chr[1] # for future: take both offsprings!
elseif operation == Reproduction
chr = copy(pop_o[roulette(pop_o)])
elseif operation == Immigration
chr = rand(Chromosome, length(pop_o[1]), obj_func)
end
#[obj_func(chr[k]) | k=1:length(chr)] # for recombination?
obj_func(chr)
pop_n + chr
end
sort!(pop_n)
# Print best chromosome
print("Best chromosome of generation $(dec(j,3)): ")
print(pop_n[1])
gen + pop_n
pop_o = copy(pop_n)
end
end
################################################################################
## EVOLUTIONARY ALGORITHM ##
################################################################################
# TODO