Class: Algorithms::GeneticAlgorithm::Engine
- Inherits:
-
Object
- Object
- Algorithms::GeneticAlgorithm::Engine
- Defined in:
- lib/algorithms/genetic_algorithm/genetic_algorithm.rb
Instance Attribute Summary collapse
-
#best_solution ⇒ Object
readonly
Returns the value of attribute best_solution.
Instance Method Summary collapse
-
#initialize(population) ⇒ Engine
constructor
A new instance of Engine.
- #run(iterations, min_size: 100, generate_retry: 10) ⇒ Object
Constructor Details
#initialize(population) ⇒ Engine
Returns a new instance of Engine.
24 25 26 27 |
# File 'lib/algorithms/genetic_algorithm/genetic_algorithm.rb', line 24 def initialize(population) @population = population @best_solution = population.max_by(&:score) end |
Instance Attribute Details
#best_solution ⇒ Object (readonly)
Returns the value of attribute best_solution.
22 23 24 |
# File 'lib/algorithms/genetic_algorithm/genetic_algorithm.rb', line 22 def best_solution @best_solution end |
Instance Method Details
#run(iterations, min_size: 100, generate_retry: 10) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/algorithms/genetic_algorithm/genetic_algorithm.rb', line 29 def run(iterations, min_size: 100, generate_retry: 10) @population = generate_until_minimum(@population, min_size) iterations.times do @population = @population.map do |solution| generate_new_solution(solution, @population, generate_retry) end @best_solution = (@population + [@best_solution]).max_by(&:score) end end |