-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenetic_algorithm.sf
51 lines (40 loc) · 1022 Bytes
/
genetic_algorithm.sf
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
#!/usr/bin/ruby
#
## A simple genetic algorithm in Sidef
## Inspired by Daniel Shiffman
## https://www.youtube.com/watch?v=DIXtg5VVz2E
#
var target = 'sidef lang'.chars
var parts = [('a'..'z')..., ' '].shuffle
var population_num = 150
var mutation_rate = 0.01
var population = population_num.of {
target.len.of { parts.rand }
}
func fitness(item) {
item ~Z== target -> count_by { _ }
}
func crossover(a, b) {
a ~Z b -> map { |g|
1.rand < mutation_rate ? parts.rand : g.rand
}
}
loop {
var matingpool = population.map { |item|
([item] * fitness(item))...
}
var fitness_score = 0
var target_found = false
population.map! {
var child = crossover(matingpool.pick(2)...)
fitness_score += fitness(child)
target_found = true if child==target
child
}
printf("Average fitness score: %.2f%%\n",
fitness_score/population_num / target.len * 100)
if (target_found) {
say "** Target found!"
break
}
}