Class: Annealing::Configuration
- Inherits:
-
Object
- Object
- Annealing::Configuration
- Defined in:
- lib/annealing/configuration.rb,
lib/annealing/configuration/coolers.rb,
lib/annealing/configuration/terminators.rb
Overview
It enables the gem configuration
Defined Under Namespace
Modules: Coolers, Terminators Classes: ConfigurationError
Constant Summary collapse
- DEFAULT_COOLING_RATE =
0.0003- DEFAULT_INITIAL_TEMPERATURE =
10_000.0- DEFAULT_RETURN_BEST =
false
Instance Attribute Summary collapse
-
#cool_down ⇒ Object
Returns the value of attribute cool_down.
-
#cooling_rate ⇒ Object
Returns the value of attribute cooling_rate.
-
#energy_calculator ⇒ Object
Returns the value of attribute energy_calculator.
-
#return_best ⇒ Object
Returns the value of attribute return_best.
-
#state_change ⇒ Object
Returns the value of attribute state_change.
-
#temperature ⇒ Object
Returns the value of attribute temperature.
-
#termination_condition ⇒ Object
Returns the value of attribute termination_condition.
Instance Method Summary collapse
-
#initialize(config_hash = {}) ⇒ Configuration
constructor
A new instance of Configuration.
-
#merge(config_hash) ⇒ Object
Return new configuration that merges new attributes with current.
- #validate! ⇒ Object
Constructor Details
#initialize(config_hash = {}) ⇒ Configuration
Returns a new instance of Configuration.
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/annealing/configuration.rb', line 20 def initialize(config_hash = {}) @cool_down = config_hash.fetch(:cool_down, Coolers.linear) @cooling_rate = config_hash.fetch(:cooling_rate, DEFAULT_COOLING_RATE).to_f @energy_calculator = config_hash.fetch(:energy_calculator, nil) @return_best = config_hash.fetch(:return_best, DEFAULT_RETURN_BEST) @state_change = config_hash.fetch(:state_change, nil) @temperature = config_hash.fetch(:temperature, DEFAULT_INITIAL_TEMPERATURE).to_f @termination_condition = config_hash.fetch(:termination_condition, Terminators.temp_is_zero?) end |
Instance Attribute Details
#cool_down ⇒ Object
Returns the value of attribute cool_down.
12 13 14 |
# File 'lib/annealing/configuration.rb', line 12 def cool_down @cool_down end |
#cooling_rate ⇒ Object
Returns the value of attribute cooling_rate.
12 13 14 |
# File 'lib/annealing/configuration.rb', line 12 def cooling_rate @cooling_rate end |
#energy_calculator ⇒ Object
Returns the value of attribute energy_calculator.
12 13 14 |
# File 'lib/annealing/configuration.rb', line 12 def energy_calculator @energy_calculator end |
#return_best ⇒ Object
Returns the value of attribute return_best.
12 13 14 |
# File 'lib/annealing/configuration.rb', line 12 def return_best @return_best end |
#state_change ⇒ Object
Returns the value of attribute state_change.
12 13 14 |
# File 'lib/annealing/configuration.rb', line 12 def state_change @state_change end |
#temperature ⇒ Object
Returns the value of attribute temperature.
12 13 14 |
# File 'lib/annealing/configuration.rb', line 12 def temperature @temperature end |
#termination_condition ⇒ Object
Returns the value of attribute termination_condition.
12 13 14 |
# File 'lib/annealing/configuration.rb', line 12 def termination_condition @termination_condition end |
Instance Method Details
#merge(config_hash) ⇒ Object
Return new configuration that merges new attributes with current
34 35 36 |
# File 'lib/annealing/configuration.rb', line 34 def merge(config_hash) self.class.new(attributes.merge(config_hash)) end |
#validate! ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/annealing/configuration.rb', line 38 def validate! = if !callable?(cool_down) "Missing cool down function" elsif cooling_rate.negative? "Cooling rate cannot be negative" elsif !callable?(energy_calculator) "Missing energy calculator function" elsif ![true, false].include?(return_best) "'Return best' specification must be either true or false" elsif !callable?(state_change) "Missing state change function" elsif temperature.negative? "Initial temperature cannot be negative" elsif !callable?(termination_condition) "Missing termination condition function" end raise(ConfigurationError, ) if end |