Class: EvoSynth::Problems::TSP

Inherits:
Evaluator show all
Defined in:
lib/evosynth/problems/tsp.rb

Instance Attribute Summary collapse

Attributes inherited from Evaluator

#calculated, #called

Instance Method Summary collapse

Methods inherited from Evaluator

#calculate_and_set_fitness, #calculate_and_set_initial_fitness, #calculate_initial_fitness, #reset_counters

Constructor Details

#initialize(filename) ⇒ TSP

TODO: refactor and generalize this class, externalize parsing of problem definition file

needs a .tsp file



39
40
41
42
# File 'lib/evosynth/problems/tsp.rb', line 39

def initialize(filename)
	super()
	@matrix = read_matrix_from_file(filename)
end

Instance Attribute Details

#matrixObject (readonly)

Returns the value of attribute matrix.



32
33
34
# File 'lib/evosynth/problems/tsp.rb', line 32

def matrix
  @matrix
end

Instance Method Details

#calculate_fitness(individual) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/evosynth/problems/tsp.rb', line 44

def calculate_fitness(individual)
	fitness = 0.0

	individual.genome.each_with_index do |node, index|
		index_two = (index + 1) % individual.genome.size
		fitness += distance(node, individual.genome[index_two])
	end

	fitness
end

#distance(from, to) ⇒ Object



55
56
57
# File 'lib/evosynth/problems/tsp.rb', line 55

def distance(from, to)
	@matrix[from, to]
end

#sizeObject



59
60
61
# File 'lib/evosynth/problems/tsp.rb', line 59

def size
	@matrix.column_size
end

#to_sObject



63
64
65
# File 'lib/evosynth/problems/tsp.rb', line 63

def to_s
	@matrix.to_s
end