-
Notifications
You must be signed in to change notification settings - Fork 0
/
colony.rb
55 lines (41 loc) · 1.26 KB
/
colony.rb
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
require 'ant'
class Colony
attr_reader :best_path
def initialize( number_of_ants, pheromonal_strength, evaporation_rate )
@pheromonal_strength = pheromonal_strength
@evaporation_rate = evaporation_rate / 100.0 # turn into fractional amount
@nodes = []
0.upto(47) { |i| @nodes << Node.new( i ) }
@number_of_ants = number_of_ants
new_ants
end
def iteration
new_ants
(0).upto(47) { @ants.each { |ant| ant.step } }
iteration_best_path = @ants.min_by { |ant| ant.path.distance }.path
if @best_path.nil?
@best_path = iteration_best_path
elsif iteration_best_path.distance < @best_path.distance
@best_path = iteration_best_path
end
@nodes.each { |node| node.evaporate( @evaporation_rate ) }
lay_pheromones
end
def node( number )
@nodes.select { |n| n.number == number }[0]
end
def shortest_distance
@best_path.distance
end
def average_distance
@ants.inject(0){ |sum,ant| sum += ant.path.distance } / @ants.size
end
private
def new_ants
@ants = []
1.upto( @number_of_ants ) { @ants << Ant.new( @nodes.choice, self ) }
end
def lay_pheromones
@ants.each { |ant| ant.path.lay_pheromones( @pheromonal_strength ) }
end
end