Class: EvoSynth::Problems::TSP
- Defined in:
- lib/evosynth/problems/tsp.rb
Instance Attribute Summary collapse
-
#matrix ⇒ Object
readonly
Returns the value of attribute matrix.
Attributes inherited from Evaluator
Instance Method Summary collapse
- #calculate_fitness(individual) ⇒ Object
- #distance(from, to) ⇒ Object
-
#initialize(filename) ⇒ TSP
constructor
TODO: refactor and generalize this class, externalize parsing of problem definition file.
- #size ⇒ Object
- #to_s ⇒ Object
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
#matrix ⇒ Object (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 |
#size ⇒ Object
59 60 61 |
# File 'lib/evosynth/problems/tsp.rb', line 59 def size @matrix.column_size end |
#to_s ⇒ Object
63 64 65 |
# File 'lib/evosynth/problems/tsp.rb', line 63 def to_s @matrix.to_s end |