-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevolution.rb
More file actions
52 lines (40 loc) · 1.5 KB
/
evolution.rb
File metadata and controls
52 lines (40 loc) · 1.5 KB
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
#!/usr/bin/env ruby
# encoding: utf-8
require_relative "natural_selection"
module Evolution
class Evolution
attr_reader :selection
attr_reader :rounds_to_win
attr_reader :base_strain
attr_reader :strains
def initialize(challenge, challenger, winners, randoms, rounds_to_win)
@selection = NaturalSelection::NaturalSelection.new(challenge, winners, randoms)
@rounds_to_win = rounds_to_win
@base_strain = NaturalSelection::Strain.new(challenger, nil, 0, 0)
@strains = [base_strain]
end
def run(cooldown = 0.0)
round = 1
complete = false
begin
mutants = @strains.map {|s| s.spawn_mutant(round)}
@strains = (@strains + mutants + [@base_strain]).uniq {|s| s.challenger.solution.to_s}
@strains = @selection.select(@strains)
@strains.each {|s| s.next_round }
#=== debug output ===
puts "round №#{round}"
@strains.first(10).each do |s|
puts "#{s.last_score}; Generation: #{s.generation}; Lifetime: #{s.lifetime}"
end
#====================
complete = @strains[0].lifetime > @rounds_to_win
round = round.succ
sleep cooldown
end while !complete
@strains[0].puts_evolution_chain
puts "Round №#{round}, Winner: #{@strains[0].challenger.solution.to_s}"
puts "#{@strains[0].last_score}; Generation: #{@strains[0].generation}; Lifetime: #{@strains[0].lifetime}"
@strains[0]
end
end
end