Class: PetriDish::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/petri_dish/configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/petri_dish/configuration.rb', line 25

def initialize
  @logger = default_logger
  @max_generations = default_max_generations
  @population_size = default_population_size
  @mutation_rate = default_mutation_rate
  @elitism_rate = default_elitism_rate
  @genetic_material = default_genetic_material
  @fitness_function = default_fitness_function
  @parents_selection_function = default_parents_selection_function
  @crossover_function = default_crossover_function
  @mutation_function = default_mutation_function
  @highest_fitness_callback = default_highest_fitness_callback
  @end_condition_function = default_end_condition_function
  @max_generation_reached_callback = default_max_generation_reached_callback
  @generation_start_callback = default_generation_start_callback
  @end_condition_reached_callback = default_end_condition_reached_callback
end

Instance Attribute Details

#crossover_functionObject

Returns the value of attribute crossover_function.



3
4
5
# File 'lib/petri_dish/configuration.rb', line 3

def crossover_function
  @crossover_function
end

#elitism_rateObject

Returns the value of attribute elitism_rate.



3
4
5
# File 'lib/petri_dish/configuration.rb', line 3

def elitism_rate
  @elitism_rate
end

#end_condition_functionObject

Returns the value of attribute end_condition_function.



3
4
5
# File 'lib/petri_dish/configuration.rb', line 3

def end_condition_function
  @end_condition_function
end

#end_condition_reached_callbackObject

Returns the value of attribute end_condition_reached_callback.



3
4
5
# File 'lib/petri_dish/configuration.rb', line 3

def end_condition_reached_callback
  @end_condition_reached_callback
end

#fitness_functionObject

Returns the value of attribute fitness_function.



3
4
5
# File 'lib/petri_dish/configuration.rb', line 3

def fitness_function
  @fitness_function
end

#generation_start_callbackObject

Returns the value of attribute generation_start_callback.



3
4
5
# File 'lib/petri_dish/configuration.rb', line 3

def generation_start_callback
  @generation_start_callback
end

#genetic_materialObject

Returns the value of attribute genetic_material.



3
4
5
# File 'lib/petri_dish/configuration.rb', line 3

def genetic_material
  @genetic_material
end

#highest_fitness_callbackObject

Returns the value of attribute highest_fitness_callback.



3
4
5
# File 'lib/petri_dish/configuration.rb', line 3

def highest_fitness_callback
  @highest_fitness_callback
end

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'lib/petri_dish/configuration.rb', line 3

def logger
  @logger
end

#max_generation_reached_callbackObject

Returns the value of attribute max_generation_reached_callback.



3
4
5
# File 'lib/petri_dish/configuration.rb', line 3

def max_generation_reached_callback
  @max_generation_reached_callback
end

#max_generationsObject

Returns the value of attribute max_generations.



3
4
5
# File 'lib/petri_dish/configuration.rb', line 3

def max_generations
  @max_generations
end

#mutation_functionObject

Returns the value of attribute mutation_function.



3
4
5
# File 'lib/petri_dish/configuration.rb', line 3

def mutation_function
  @mutation_function
end

#mutation_rateObject

Returns the value of attribute mutation_rate.



3
4
5
# File 'lib/petri_dish/configuration.rb', line 3

def mutation_rate
  @mutation_rate
end

#parents_selection_functionObject

Returns the value of attribute parents_selection_function.



3
4
5
# File 'lib/petri_dish/configuration.rb', line 3

def parents_selection_function
  @parents_selection_function
end

#population_sizeObject

Returns the value of attribute population_size.



3
4
5
# File 'lib/petri_dish/configuration.rb', line 3

def population_size
  @population_size
end

Class Method Details

.configure {|configuration = new| ... } ⇒ Object

Yields:

  • (configuration = new)


19
20
21
22
23
# File 'lib/petri_dish/configuration.rb', line 19

def self.configure
  yield(configuration = new)
  configuration.validate!
  configuration
end

Instance Method Details

#reset!Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/petri_dish/configuration.rb', line 61

def reset!
  @logger = default_logger
  @max_generations = default_max_generations
  @population_size = default_population_size
  @mutation_rate = default_mutation_rate
  @elitism_rate = default_elitism_rate
  @genetic_material = default_genetic_material
  @fitness_function = default_fitness_function
  @parents_selection_function = default_parents_selection_function
  @crossover_function = default_crossover_function
  @mutation_function = default_mutation_function
  @end_condition_function = default_end_condition_function
  @highest_fitness_callback = default_highest_fitness_callback
  @max_generation_reached_callback = default_max_generation_reached_callback
  @generation_start_callback = default_generation_start_callback
  @end_condition_reached_callback = default_end_condition_reached_callback
end

#validate!Object

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/petri_dish/configuration.rb', line 43

def validate!
  raise ArgumentError, "logger must respond to :info" unless logger.respond_to?(:info)
  raise ArgumentError, "max_generations must be greater than 0" unless max_generations > 0
  raise ArgumentError, "population_size must be greater than 0" unless population_size > 0
  raise ArgumentError, "mutation_rate must be between 0 and 1" unless mutation_rate >= 0 && mutation_rate <= 1
  raise ArgumentError, "elitism_rate must be between 0 and 1" unless elitism_rate >= 0 && elitism_rate <= 1
  raise ArgumentError, "genetic_material must be an Array" unless genetic_material.is_a?(Array)
  raise ArgumentError, "fitness_function must respond to :call" unless fitness_function.respond_to?(:call)
  raise ArgumentError, "parents_selection_function must respond to :call" unless parents_selection_function.respond_to?(:call)
  raise ArgumentError, "crossover_function must respond to :call" unless crossover_function.respond_to?(:call)
  raise ArgumentError, "mutation_function must respond to :call" unless mutation_function.respond_to?(:call)
  raise ArgumentError, "end_condition_function must respond to :call" unless end_condition_function.respond_to?(:call)
  raise ArgumentError, "highest_fitness_callback must respond to :call" unless highest_fitness_callback.nil? || highest_fitness_callback.respond_to?(:call)
  raise ArgumentError, "max_generation_reached_callback must respond to :call" unless max_generation_reached_callback.nil? || max_generation_reached_callback.respond_to?(:call)
  raise ArgumentError, "generation_start_callback must respond to :call" unless generation_start_callback.nil? || generation_start_callback.respond_to?(:call)
  raise ArgumentError, "end_condition_reached_callback must respond to :call" unless end_condition_reached_callback.nil? || end_condition_reached_callback.respond_to?(:call)
end