Class: Annealing::Configuration

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_hash = {}) ⇒ Configuration

Returns a new instance of Configuration.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/annealing/configuration.rb', line 18

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)
  @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_downObject

Returns the value of attribute cool_down.



11
12
13
# File 'lib/annealing/configuration.rb', line 11

def cool_down
  @cool_down
end

#cooling_rateObject

Returns the value of attribute cooling_rate.



11
12
13
# File 'lib/annealing/configuration.rb', line 11

def cooling_rate
  @cooling_rate
end

#energy_calculatorObject

Returns the value of attribute energy_calculator.



11
12
13
# File 'lib/annealing/configuration.rb', line 11

def energy_calculator
  @energy_calculator
end

#state_changeObject

Returns the value of attribute state_change.



11
12
13
# File 'lib/annealing/configuration.rb', line 11

def state_change
  @state_change
end

#temperatureObject

Returns the value of attribute temperature.



11
12
13
# File 'lib/annealing/configuration.rb', line 11

def temperature
  @temperature
end

#termination_conditionObject

Returns the value of attribute termination_condition.



11
12
13
# File 'lib/annealing/configuration.rb', line 11

def termination_condition
  @termination_condition
end

Instance Method Details

#merge(config_hash) ⇒ Object

Return new configuration that merges new attributes with current



31
32
33
# File 'lib/annealing/configuration.rb', line 31

def merge(config_hash)
  self.class.new(attributes.merge(config_hash))
end

#validate!Object

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/annealing/configuration.rb', line 35

def validate!
  message = 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 !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, message) if message
end