annealing 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b20378dea6d3ebcee0d0adca58d721576605d17c34c91a1828f887733c5fc5e
4
- data.tar.gz: 62935118c575c16e8e0efaaf58224845fc8e975f230ac58b81da41cf66a9c08c
3
+ metadata.gz: c5435b7e34667fdfd63782d895080435f7f2dffb685b2a918bba83bf6781f432
4
+ data.tar.gz: 82bfb71e31afed6d53e0649b35c373fc5ea27bcc19e6b72ae8522983df1d4734
5
5
  SHA512:
6
- metadata.gz: 4916ea9c5c854ce9891b9264d7dfe117f56da58fb906735f2afb015f2a0acf08757702991d988ed82096910225cca25794b78e135635d03b41dcf51496543a72
7
- data.tar.gz: f3c74cb98d4b45512fd78bf6e18dfc751550ba33f6ccfa4ab80720d01dcdf4a1a0ec7215387033523792a503876e4abd6bc7b017330539a8367bed3da3a23741
6
+ metadata.gz: 54dd58f3c4c2007aa0c6cd0ca264dc9a53ddb9680a354ea2808ff3c7a8ebcae5fc032aefb70f9cdbb6fe023cd23d0dff014254c99a577e2119172597cefd5202
7
+ data.tar.gz: 21c78d1a11bc7ddf2e9b02e916f3bb94986944ad37ceb08cddf5a70178b82477be414e8478d1d51f26220a147f8442f5594667152bce496b7b98897204ec221f
@@ -18,7 +18,7 @@ jobs:
18
18
  runs-on: ubuntu-latest
19
19
  strategy:
20
20
  matrix:
21
- ruby-version: ['2.6']
21
+ ruby-version: ['3.0']
22
22
  steps:
23
23
  - uses: actions/checkout@v2
24
24
  - name: Set up Ruby
@@ -32,7 +32,7 @@ jobs:
32
32
  runs-on: ubuntu-latest
33
33
  strategy:
34
34
  matrix:
35
- ruby-version: ['2.6', '2.7', '3.0', '3.1']
35
+ ruby-version: ['3.0', '3.1', '3.2']
36
36
 
37
37
  steps:
38
38
  - uses: actions/checkout@v2
data/.rubocop.yml CHANGED
@@ -6,7 +6,7 @@ require:
6
6
  - rubocop-rake
7
7
 
8
8
  AllCops:
9
- TargetRubyVersion: 2.6
9
+ TargetRubyVersion: 3.0
10
10
  NewCops: enable
11
11
  DefaultFormatter: progress
12
12
  DisplayCopNames: true
data/.rubocop_todo.yml CHANGED
@@ -1,42 +1,54 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config --auto-gen-only-exclude`
3
- # on 2022-02-18 23:32:32 UTC using RuboCop version 1.23.0.
3
+ # on 2022-12-12 19:04:19 UTC using RuboCop version 1.23.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 9
9
+ # Offense count: 7
10
10
  # Configuration parameters: IgnoredMethods, CountRepeatedAttributes, Max.
11
11
  Metrics/AbcSize:
12
12
  Exclude:
13
- - 'test/annealing/configuration/configurator_test.rb'
13
+ - 'lib/annealing/configuration.rb'
14
+ - 'test/annealing/configuration_test.rb'
14
15
  - 'test/annealing/metal_test.rb'
15
16
  - 'test/annealing/simulator_test.rb'
16
17
  - 'test/annealing_test.rb'
17
18
 
18
- # Offense count: 3
19
+ # Offense count: 2
19
20
  # Configuration parameters: CountComments, Max, CountAsOne.
20
21
  Metrics/ClassLength:
21
22
  Exclude:
22
- - 'test/annealing/configuration/configurator_test.rb'
23
- - 'test/annealing/metal_test.rb'
23
+ - 'test/annealing/configuration_test.rb'
24
24
  - 'test/annealing/simulator_test.rb'
25
25
 
26
- # Offense count: 13
26
+ # Offense count: 1
27
+ # Configuration parameters: IgnoredMethods, Max.
28
+ Metrics/CyclomaticComplexity:
29
+ Exclude:
30
+ - 'lib/annealing/configuration.rb'
31
+
32
+ # Offense count: 11
27
33
  # Configuration parameters: CountComments, Max, CountAsOne, ExcludedMethods, IgnoredMethods.
28
34
  Metrics/MethodLength:
29
35
  Exclude:
30
36
  - 'lib/annealing/configuration.rb'
31
- - 'lib/annealing/simulator.rb'
32
- - 'test/annealing/configuration/configurator_test.rb'
37
+ - 'test/annealing/configuration_test.rb'
33
38
  - 'test/annealing/metal_test.rb'
34
39
  - 'test/annealing/simulator_test.rb'
35
40
  - 'test/annealing_test.rb'
36
41
 
37
- # Offense count: 8
42
+ # Offense count: 1
43
+ # Configuration parameters: IgnoredMethods, Max.
44
+ Metrics/PerceivedComplexity:
45
+ Exclude:
46
+ - 'lib/annealing/configuration.rb'
47
+
48
+ # Offense count: 5
38
49
  # Configuration parameters: Max.
39
50
  Minitest/MultipleAssertions:
40
51
  Exclude:
41
- - 'test/annealing/configuration/configurator_test.rb'
52
+ - 'test/annealing/configuration/terminators_test.rb'
42
53
  - 'test/annealing/configuration_test.rb'
54
+ - 'test/annealing/simulator_test.rb'
data/Gemfile CHANGED
@@ -7,8 +7,8 @@ gemspec
7
7
 
8
8
  gem "debug", ">= 1.0.0", require: false
9
9
  gem "minitest", "~> 5.0"
10
- gem "rake", "~> 13.0", ">= 13.0.6"
11
- gem "rubocop", "~> 1.23"
12
- gem "rubocop-minitest", "~> 0.17.0"
13
- gem "rubocop-performance", "~> 1.12"
10
+ gem "rake", "~> 13.0.0", ">= 13.0.6"
11
+ gem "rubocop", "~> 1.48.0"
12
+ gem "rubocop-minitest", "~> 0.29.0"
13
+ gem "rubocop-performance", "~> 1.16.0"
14
14
  gem "rubocop-rake", "~> 0.6.0"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- annealing (0.2.0)
4
+ annealing (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -10,40 +10,42 @@ GEM
10
10
  debug (1.4.0)
11
11
  irb (>= 1.3.6)
12
12
  reline (>= 0.2.7)
13
- io-console (0.5.11)
14
- irb (1.4.1)
13
+ io-console (0.6.0)
14
+ irb (1.6.3)
15
15
  reline (>= 0.3.0)
16
- minitest (5.14.4)
17
- parallel (1.21.0)
18
- parser (3.0.3.2)
16
+ json (2.6.3)
17
+ minitest (5.18.0)
18
+ parallel (1.22.1)
19
+ parser (3.2.1.1)
19
20
  ast (~> 2.4.1)
20
- rainbow (3.0.0)
21
+ rainbow (3.1.1)
21
22
  rake (13.0.6)
22
- regexp_parser (2.2.0)
23
- reline (0.3.1)
23
+ regexp_parser (2.7.0)
24
+ reline (0.3.2)
24
25
  io-console (~> 0.5)
25
26
  rexml (3.2.5)
26
- rubocop (1.23.0)
27
+ rubocop (1.48.0)
28
+ json (~> 2.3)
27
29
  parallel (~> 1.10)
28
- parser (>= 3.0.0.0)
30
+ parser (>= 3.2.0.0)
29
31
  rainbow (>= 2.2.2, < 4.0)
30
32
  regexp_parser (>= 1.8, < 3.0)
31
- rexml
32
- rubocop-ast (>= 1.12.0, < 2.0)
33
+ rexml (>= 3.2.5, < 4.0)
34
+ rubocop-ast (>= 1.26.0, < 2.0)
33
35
  ruby-progressbar (~> 1.7)
34
- unicode-display_width (>= 1.4.0, < 3.0)
35
- rubocop-ast (1.14.0)
36
- parser (>= 3.0.1.1)
37
- rubocop-minitest (0.17.0)
38
- rubocop (>= 0.90, < 2.0)
39
- rubocop-performance (1.12.0)
36
+ unicode-display_width (>= 2.4.0, < 3.0)
37
+ rubocop-ast (1.27.0)
38
+ parser (>= 3.2.1.0)
39
+ rubocop-minitest (0.29.0)
40
+ rubocop (>= 1.39, < 2.0)
41
+ rubocop-performance (1.16.0)
40
42
  rubocop (>= 1.7.0, < 2.0)
41
43
  rubocop-ast (>= 0.4.0)
42
44
  rubocop-rake (0.6.0)
43
45
  rubocop (~> 1.0)
44
- ruby-prof (1.4.3)
45
- ruby-progressbar (1.11.0)
46
- unicode-display_width (2.1.0)
46
+ ruby-prof (1.6.1)
47
+ ruby-progressbar (1.13.0)
48
+ unicode-display_width (2.4.2)
47
49
 
48
50
  PLATFORMS
49
51
  ruby
@@ -52,12 +54,12 @@ DEPENDENCIES
52
54
  annealing!
53
55
  debug (>= 1.0.0)
54
56
  minitest (~> 5.0)
55
- rake (~> 13.0, >= 13.0.6)
56
- rubocop (~> 1.23)
57
- rubocop-minitest (~> 0.17.0)
58
- rubocop-performance (~> 1.12)
57
+ rake (~> 13.0.0, >= 13.0.6)
58
+ rubocop (~> 1.48.0)
59
+ rubocop-minitest (~> 0.29.0)
60
+ rubocop-performance (~> 1.16.0)
59
61
  rubocop-rake (~> 0.6.0)
60
- ruby-prof (~> 1.4, >= 1.4.3)
62
+ ruby-prof (~> 1.6, >= 1.6.1)
61
63
 
62
64
  BUNDLED WITH
63
- 2.1.4
65
+ 2.4.4
data/README.md CHANGED
@@ -102,12 +102,26 @@ The annealer supports a number of configuration options. See the [configuration
102
102
 
103
103
  ### `cool_down`
104
104
 
105
- By default, the simulation will decrease the `temperature` linearly by `cooling_rate` on each step of the annealing process. In some cases you may wish to override this to use a different cooling algorithm. To do so, you can specify a custom `cool_down` function. The function can be any object that responds to `#call` and accepts three arguments: the `energy` calculation of the current object, the current `temperature` of the annealer, the `cooling_rate` for the simulation, and the number of `steps` the annealer has taken so far. It should return the new temperature as a Float.
105
+ By default, the simulation will decrease the `temperature` linearly by `cooling_rate` on each step of the annealing process. In some cases you may wish to override this to use a different cooling algorithm. To do so, you can use one of the other built-in cooling functions or you can specify a custom `cool_down` function. Custom functions can be any object that responds to `#call` and accepts four arguments: the `energy` calculation of the current object, the current `temperature` of the annealer, the `cooling_rate` for the simulation, and the number of `steps` the annealer has taken so far. It should return the new temperature as a Float.
106
106
 
107
107
  ```ruby
108
- Annealing.configuration.cool_down = lambda do |_energy, temperature, cooling_rate, steps|
109
- # Reduce temperature exponentially
110
- temperature - (cooling_rate * (steps**2))
108
+ # Use the built-in linear cool-down function (the default)
109
+ Annealing.configuration.cool_down = Annealing::Configuration::Coolers.linear
110
+
111
+ # Use the built-in exponential cool-down function
112
+ Annealing.configuration.cool_down = Annealing::Configuration::Coolers.exponential
113
+
114
+ # Use the built-in geometric cool-down function with a custom ratio (default ratio is 2)
115
+ Annealing.configuration.cool_down = Annealing::Configuration::Coolers.exponential(1.5)
116
+
117
+ # Use a custom cool down function
118
+ Annealing.configuration.cool_down = lambda do |energy, temperature, cooling_rate, steps|
119
+ # Reduce temperature exponentially when the temperature is above 500, then linearly
120
+ if temperature > 500
121
+ Annealing::Configuration::Coolers.exponential.call(energy, temperature, cooling_rate, steps)
122
+ else
123
+ Annealing::Configuration::Coolers.linear.call(energy, temperature, cooling_rate, steps)
124
+ end
111
125
  end
112
126
  ```
113
127
 
@@ -161,16 +175,6 @@ calculator = PotentialSalesCalculator.new(8)
161
175
  Annealing.configuration.energy_calculator = calculator.method(:energy)
162
176
  ```
163
177
 
164
- ### `logger`
165
-
166
- The default logger is a standard Ruby Logger that writes to stdout. You can specify a different `logger` if you wish.
167
-
168
- ```ruby
169
- logger = Logger.new('logfile.log')
170
- logger.level = Logger::DEBUG
171
- Annealing.configuration.logger = logger
172
- ```
173
-
174
178
  ### `state_change`
175
179
 
176
180
  As with `energy_calculator`, you must specify a `state_change` function in order to run any simulations; no default function is provided. The function can be any object that responds to `#call` and accepts a single argument: the `state` representing the current state that should be changed. It should return the changed state.
@@ -193,12 +197,19 @@ Annealing.configuration.state_change = instance.method(:state_change)
193
197
 
194
198
  ### `termination_condition`
195
199
 
196
- By default, a simulation will run until the temperature reaches 0. In some cases, you might want to specify a termination condition that will stop the annealing process as soon as some other condition is met regardless of the current temperature. You can define a custom `termination_condition` function, which can be any object that responds to `#call` and accepts three arguments: the current `state` of the object, the `energy` calculation of the current object, and the current `temperature` of the simulation. It should return a boolean value where `true` indicates the simulation should stop.
200
+ By default, a simulation will run until the temperature reaches 0. In some cases, you might want to specify a termination condition that will stop the annealing process as soon as some other condition is met regardless of the current temperature. To do so, you can use one of the other built-in termination condition functions or you can specify a custom one. Custom `termination_condition` functions can be any object that responds to `#call` and accepts three arguments: the current `state` of the object, the `energy` calculation of the current object, and the current `temperature` of the simulation. It should return a boolean value where `true` indicates the simulation should stop.
197
201
 
198
202
  ```ruby
203
+ # Use the built-in zero-temperature termination condition function
204
+ Annealing.configuration.termination_condition = Annealing::Configuration::Terminators.temp_is_zero?
205
+
206
+ # Use the built-in zero-energy termination condition function
207
+ Annealing.configuration.termination_condition = Annealing::Configuration::Terminators.energy_or_temp_is_zero?
208
+
209
+ # Use a custom termination condition function
199
210
  Annealing.configuration.termination_condition = lambda do |_state, energy, temperature|
200
- # Stop early if we encounter any 0-energy state
201
- energy == 0 || temperature <= 0.0
211
+ # Stop if the energy is below 500 and the temperature is below 100, or the temperature is already 0
212
+ temperature <= 0 || (energy <= 500 && temperature <= 500)
202
213
  end
203
214
  ```
204
215
 
data/annealing.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.summary = "Simulated Annealing"
13
13
  spec.description = "Simulated Annealing algoritm implementation."
14
14
  spec.homepage = "https://github.com/3zcurdia/annealing"
15
- spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 3.0.0")
16
16
 
17
17
  # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
18
 
@@ -29,6 +29,6 @@ Gem::Specification.new do |spec|
29
29
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
30
  spec.require_paths = ["lib"]
31
31
 
32
- spec.add_development_dependency "ruby-prof", "~> 1.4", ">= 1.4.3"
32
+ spec.add_development_dependency "ruby-prof", "~> 1.6", ">= 1.6.1"
33
33
  spec.metadata["rubygems_mfa_required"] = "true"
34
34
  end
data/bin/run CHANGED
@@ -39,20 +39,19 @@ state_change = lambda do |state|
39
39
  swapped
40
40
  end
41
41
 
42
- Annealing.configuration.logger.level = Logger::DEBUG
43
42
  simulator = Annealing::Simulator.new(temperature: 10_000, cooling_rate: 0.01)
44
43
  solution = simulator.run(locations,
45
44
  energy_calculator: energy_calculator,
46
45
  state_change: state_change)
47
46
 
48
47
  puts "\nInitial itinerary:"
49
- locations.each_cons(2).each_with_index do |(location1, location2), index|
48
+ locations.each_cons(2).with_index do |(location1, location2), index|
50
49
  puts "Stop ##{index + 1}: #{location1.name} -> #{location2.name} (#{location1.distance(location2)})"
51
50
  end
52
51
  puts "-------\nEnergy: #{energy_calculator.call(locations)}"
53
52
 
54
53
  puts "\nAnnealed itinerary:"
55
- solution.state.each_cons(2).each_with_index do |(location1, location2), index|
54
+ solution.state.each_cons(2).with_index do |(location1, location2), index|
56
55
  puts "Stop ##{index + 1}: #{location1.name} -> #{location2.name} (#{location1.distance(location2)})"
57
56
  end
58
57
  puts "-------\nEnergy: #{solution.energy}"
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Annealing
4
+ class Configuration
5
+ # Built-in cool down functions
6
+ module Coolers
7
+ module_function
8
+
9
+ # Reduce temperature linearly by the cooling rate
10
+ def linear
11
+ lambda { |_energy, temperature, cooling_rate, _steps|
12
+ temperature - cooling_rate
13
+ }
14
+ end
15
+
16
+ # Reduce temperature exponentially on each step by the cooling rate
17
+ def exponential
18
+ lambda { |_energy, temperature, cooling_rate, steps|
19
+ temperature - (Math.exp(steps - 1) * cooling_rate)
20
+ }
21
+ end
22
+
23
+ # Reduce temperature geometrically at a given ratio by the cooling rate
24
+ def geometric(ratio = 2)
25
+ unless ratio.positive?
26
+ raise(Annealing::Configuration::ConfigurationError,
27
+ "geometric ratio must be positive")
28
+ end
29
+
30
+ lambda { |_energy, temperature, cooling_rate, steps|
31
+ temperature - (cooling_rate * (ratio**(steps - 1)))
32
+ }
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Annealing
4
+ class Configuration
5
+ # Built-in termination condition check functions
6
+ module Terminators
7
+ module_function
8
+
9
+ # Returns true when the temperature is at or below zero
10
+ def temp_is_zero?
11
+ lambda { |_state, _energy, temperature|
12
+ temperature <= 0
13
+ }
14
+ end
15
+
16
+ # Returns true if a 0-energy state is detected, or when the temperature
17
+ # is at or below zero
18
+ def energy_or_temp_is_zero?
19
+ lambda { |state, energy, temperature|
20
+ energy <= 0 || temp_is_zero?.call(state, energy, temperature)
21
+ }
22
+ end
23
+ end
24
+ end
25
+ end
@@ -3,28 +3,70 @@
3
3
  module Annealing
4
4
  # It enables the gem configuration
5
5
  class Configuration
6
+ DEFAULT_COOLING_RATE = 0.0003
7
+ DEFAULT_INITIAL_TEMPERATURE = 10_000.0
8
+
9
+ class ConfigurationError < Annealing::Error; end
10
+
6
11
  attr_accessor :cool_down,
7
12
  :cooling_rate,
8
13
  :energy_calculator,
9
- :logger,
10
14
  :state_change,
11
15
  :temperature,
12
16
  :termination_condition
13
17
 
14
- def initialize
15
- @cool_down = lambda do |_energy, temperature, cooling_rate, _steps|
16
- # Reduce the temperature linearly by default
17
- temperature - cooling_rate
18
- end
19
- @cooling_rate = 0.0003
20
- @energy_calculator = nil
21
- @logger = Logger.new($stdout, level: Logger::INFO)
22
- @state_change = nil
23
- @temperature = 10_000.0
24
- @termination_condition = lambda do |_state, _energy, temperature|
25
- # Terminate the simulation as soon as the temperature reaches 0
26
- temperature <= 0.0
27
- end
18
+ def initialize(config_hash = {})
19
+ @cool_down = config_hash.fetch(:cool_down, Coolers.linear)
20
+ @cooling_rate = config_hash.fetch(:cooling_rate,
21
+ DEFAULT_COOLING_RATE).to_f
22
+ @energy_calculator = config_hash.fetch(:energy_calculator, nil)
23
+ @state_change = config_hash.fetch(:state_change, nil)
24
+ @temperature = config_hash.fetch(:temperature,
25
+ DEFAULT_INITIAL_TEMPERATURE).to_f
26
+ @termination_condition = config_hash.fetch(:termination_condition,
27
+ Terminators.temp_is_zero?)
28
+ end
29
+
30
+ # Return new configuration that merges new attributes with current
31
+ def merge(config_hash)
32
+ self.class.new(attributes.merge(config_hash))
33
+ end
34
+
35
+ def validate!
36
+ message = if !callable?(cool_down)
37
+ "Missing cool down function"
38
+ elsif cooling_rate.negative?
39
+ "Cooling rate cannot be negative"
40
+ elsif !callable?(energy_calculator)
41
+ "Missing energy calculator function"
42
+ elsif !callable?(state_change)
43
+ "Missing state change function"
44
+ elsif temperature.negative?
45
+ "Initial temperature cannot be negative"
46
+ elsif !callable?(termination_condition)
47
+ "Missing termination condition function"
48
+ end
49
+ raise(ConfigurationError, message) if message
50
+ end
51
+
52
+ private
53
+
54
+ def attributes
55
+ {
56
+ cool_down: cool_down,
57
+ cooling_rate: cooling_rate,
58
+ energy_calculator: energy_calculator,
59
+ state_change: state_change,
60
+ temperature: temperature,
61
+ termination_condition: termination_condition
62
+ }
63
+ end
64
+
65
+ def callable?(attribute)
66
+ attribute.respond_to?(:call)
28
67
  end
29
68
  end
30
69
  end
70
+
71
+ require "annealing/configuration/coolers"
72
+ require "annealing/configuration/terminators"
@@ -3,28 +3,23 @@
3
3
  module Annealing
4
4
  # It manages the total energy of a given collection
5
5
  class Metal
6
- include Configuration::Configurator
7
- attr_reader :state, :temperature
6
+ attr_reader :configuration, :state, :temperature
8
7
 
9
- def initialize(current_state, current_temperature, **config)
10
- init_configuration(config)
8
+ def initialize(current_state, current_temperature, configuration = nil)
9
+ @configuration = configuration || Annealing.configuration.merge({})
11
10
  @state = current_state
12
11
  @temperature = current_temperature
13
-
14
- raise(ArgumentError, "Missing energy calculator function") unless energy_calculator.respond_to?(:call)
15
-
16
- raise(ArgumentError, "Missing state change function") unless state_change.respond_to?(:call)
17
12
  end
18
13
 
19
14
  def energy
20
- @energy ||= energy_calculator.call(state)
15
+ @energy ||= configuration.energy_calculator.call(state)
21
16
  end
22
17
 
23
18
  # This method is not idempotent!
24
19
  # It relies on random probability to select the next state
25
20
  def cool!(new_temperature)
26
21
  cooled_metal = cool(new_temperature)
27
- if better_than?(cooled_metal)
22
+ if prefer?(cooled_metal)
28
23
  cooled_metal
29
24
  else
30
25
  @temperature = new_temperature
@@ -32,32 +27,21 @@ module Annealing
32
27
  end
33
28
  end
34
29
 
35
- def to_s
36
- format("%<temperature>.4f:%<energy>.4f:%<value>s",
37
- temperature: temperature,
38
- energy: energy,
39
- value: state)
40
- end
41
-
42
30
  private
43
31
 
44
- def energy_calculator
45
- current_config_for(:energy_calculator)
46
- end
47
-
48
- def state_change
49
- current_config_for(:state_change)
50
- end
32
+ # True if cooled_metal.energy is lower than current energy, otherwise let
33
+ # probability determine if we should accept a higher value over a lower
34
+ # value
35
+ def prefer?(cooled_metal)
36
+ return true if cooled_metal.energy < energy
51
37
 
52
- def better_than?(cooled_metal)
53
38
  energy_delta = energy - cooled_metal.energy
54
- energy_delta.positive? ||
55
- (Math::E**(energy_delta / cooled_metal.temperature)) > rand
39
+ (Math::E**(energy_delta / cooled_metal.temperature)) > rand
56
40
  end
57
41
 
58
42
  def cool(new_temperature)
59
- next_state = state_change.call(state)
60
- Metal.new(next_state, new_temperature, **configuration_overrides)
43
+ next_state = configuration.state_change.call(state)
44
+ Metal.new(next_state, new_temperature, configuration)
61
45
  end
62
46
  end
63
47
  end
@@ -3,68 +3,50 @@
3
3
  module Annealing
4
4
  # It runs simulated annealing
5
5
  class Simulator
6
- include Configuration::Configurator
6
+ attr_reader :configuration
7
7
 
8
- def initialize(**config)
9
- init_configuration(config)
8
+ def initialize(config_hash = {})
9
+ @configuration = Annealing.configuration.merge(config_hash)
10
10
  end
11
11
 
12
12
  def run(initial_state, config_hash = {})
13
- with_configuration_overrides(config_hash) do
14
- validate_configuration!
15
- current = Metal.new(initial_state, temperature,
16
- **configuration_overrides)
17
- Annealing.logger.debug("Original: #{current}")
13
+ with_runtime_config(config_hash) do |runtime_config|
14
+ initial_temperature = runtime_config.temperature
15
+ current = Metal.new(initial_state, initial_temperature, runtime_config)
18
16
  steps = 0
19
- until termination_condition_met?(termination_condition, current)
17
+ until termination_condition_met?(current, runtime_config)
20
18
  steps += 1
21
- current = reduce_temperature(cool_down, current, steps)
19
+ current = reduce_temperature(current, steps, runtime_config)
22
20
  end
23
- Annealing.logger.debug("Optimized: #{current}")
24
21
  current
25
22
  end
26
23
  end
27
24
 
28
25
  private
29
26
 
30
- def cool_down
31
- current_config_for(:cool_down)
32
- end
33
-
34
- def cooling_rate
35
- current_config_for(:cooling_rate).to_f
36
- end
37
-
38
- def logger
39
- current_config_for(:logger)
40
- end
41
-
42
- def temperature
43
- current_config_for(:temperature).to_f
44
- end
45
-
46
- def termination_condition
47
- current_config_for(:termination_condition)
48
- end
49
-
50
- def reduce_temperature(cool_down, metal, steps)
51
- new_temperature = cool_down.call(metal.energy, metal.temperature,
52
- cooling_rate, steps)
27
+ # Wrapper for public methods that may use a custom configuration
28
+ def with_runtime_config(config_hash)
29
+ runtime_config = if config_hash.is_a?(Hash) && config_hash.any?
30
+ configuration.merge(config_hash)
31
+ else
32
+ configuration
33
+ end
34
+ runtime_config.validate!
35
+ yield(runtime_config)
36
+ end
37
+
38
+ def reduce_temperature(metal, steps, config)
39
+ new_temperature = config.cool_down.call(metal.energy,
40
+ metal.temperature,
41
+ config.cooling_rate,
42
+ steps)
53
43
  metal.cool!(new_temperature)
54
44
  end
55
45
 
56
- def termination_condition_met?(termination_condition, metal)
57
- termination_condition.call(metal.state, metal.energy, metal.temperature)
58
- end
59
-
60
- def validate_configuration!
61
- raise(ArgumentError, "Invalid initial temperature") if temperature.negative?
62
-
63
- raise(ArgumentError, "Invalid initial cooling rate") if cooling_rate.negative?
64
-
65
- raise(ArgumentError, "Missing cool down function") unless cool_down.respond_to?(:call)
66
-
67
- raise(ArgumentError, "Missing termination condition function") unless termination_condition.respond_to?(:call)
46
+ def termination_condition_met?(metal, config)
47
+ config.termination_condition.call(metal.state,
48
+ metal.energy,
49
+ metal.temperature)
68
50
  end
69
51
  end
70
52
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Annealing
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/annealing.rb CHANGED
@@ -1,12 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "logger"
4
- require "annealing/version"
5
- require "annealing/configuration"
6
- require "annealing/configuration/configurator"
7
- require "annealing/metal"
8
- require "annealing/simulator"
9
-
10
3
  # Simulated Annealing algoritm
11
4
  # https://en.wikipedia.org/wiki/Simulated_annealing
12
5
  module Annealing
@@ -23,13 +16,15 @@ module Annealing
23
16
 
24
17
  def self.configure
25
18
  yield(configuration)
19
+ configuration
26
20
  end
27
21
 
28
22
  def self.simulate(initial_state, config_hash = {})
29
23
  Simulator.new.run(initial_state, config_hash).state
30
24
  end
31
-
32
- def self.logger
33
- configuration.logger
34
- end
35
25
  end
26
+
27
+ require "annealing/configuration"
28
+ require "annealing/metal"
29
+ require "annealing/simulator"
30
+ require "annealing/version"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: annealing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luis Ezcurdia
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2022-02-22 00:00:00.000000000 Z
12
+ date: 2023-03-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ruby-prof
@@ -17,20 +17,20 @@ dependencies:
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '1.4'
20
+ version: '1.6'
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 1.4.3
23
+ version: 1.6.1
24
24
  type: :development
25
25
  prerelease: false
26
26
  version_requirements: !ruby/object:Gem::Requirement
27
27
  requirements:
28
28
  - - "~>"
29
29
  - !ruby/object:Gem::Version
30
- version: '1.4'
30
+ version: '1.6'
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 1.4.3
33
+ version: 1.6.1
34
34
  description: Simulated Annealing algoritm implementation.
35
35
  email:
36
36
  - ing.ezcurdia@gmail.com
@@ -56,7 +56,8 @@ files:
56
56
  - bin/setup
57
57
  - lib/annealing.rb
58
58
  - lib/annealing/configuration.rb
59
- - lib/annealing/configuration/configurator.rb
59
+ - lib/annealing/configuration/coolers.rb
60
+ - lib/annealing/configuration/terminators.rb
60
61
  - lib/annealing/metal.rb
61
62
  - lib/annealing/simulator.rb
62
63
  - lib/annealing/version.rb
@@ -76,14 +77,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
77
  requirements:
77
78
  - - ">="
78
79
  - !ruby/object:Gem::Version
79
- version: 2.6.0
80
+ version: 3.0.0
80
81
  required_rubygems_version: !ruby/object:Gem::Requirement
81
82
  requirements:
82
83
  - - ">="
83
84
  - !ruby/object:Gem::Version
84
85
  version: '0'
85
86
  requirements: []
86
- rubygems_version: 3.3.7
87
+ rubygems_version: 3.4.6
87
88
  signing_key:
88
89
  specification_version: 4
89
90
  summary: Simulated Annealing
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Annealing
4
- class Configuration
5
- # Configuration mixin
6
- module Configurator
7
- def self.included(base)
8
- base.send :include, InstanceMethods
9
- end
10
-
11
- # Mixin methods
12
- module InstanceMethods
13
- def init_configuration(config_hash = {})
14
- @instance_configuration = config_hash
15
- @temporary_configuration = {}
16
- end
17
-
18
- def with_configuration_overrides(local_config_hash = {})
19
- @temporary_configuration = local_config_hash
20
- yield
21
- ensure
22
- @temporary_configuration = {}
23
- end
24
-
25
- def configuration_overrides
26
- instance_configuration.merge(temporary_configuration)
27
- end
28
-
29
- private
30
-
31
- attr_accessor :instance_configuration, :temporary_configuration
32
-
33
- def current_config_for(config)
34
- temporary_configuration[config] ||
35
- instance_configuration[config] ||
36
- Annealing.configuration.public_send(config)
37
- end
38
- end
39
- end
40
- end
41
- end