metaheuristic_algorithms 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/metaheuristic_algorithms/ant_colony_optimization.rb +20 -0
- data/lib/metaheuristic_algorithms/base_algorithm_module.rb +12 -0
- data/lib/metaheuristic_algorithms/calculation_helper.rb +47 -0
- data/lib/metaheuristic_algorithms/firefly_algorithm.rb +142 -0
- data/lib/metaheuristic_algorithms/function_wrappers/abstract_wrapper.rb +32 -0
- data/lib/metaheuristic_algorithms/function_wrappers/easom_function_wrapper.rb +30 -0
- data/lib/metaheuristic_algorithms/function_wrappers/michaelwicz_function_wrapper.rb +31 -0
- data/lib/metaheuristic_algorithms/function_wrappers/nonsmooth_multipeak_function_wrapper.rb +29 -0
- data/lib/metaheuristic_algorithms/function_wrappers/rosenbrook_function_wrapper.rb +27 -0
- data/lib/metaheuristic_algorithms/function_wrappers.rb +13 -0
- data/lib/metaheuristic_algorithms/genetic_algorithm.rb +200 -0
- data/lib/metaheuristic_algorithms/harmony_search.rb +103 -0
- data/lib/metaheuristic_algorithms/helper.rb +11 -0
- data/lib/metaheuristic_algorithms/simplified_particle_swarm_optimization.rb +87 -0
- data/lib/metaheuristic_algorithms/simulated_annealing.rb +117 -0
- data/lib/metaheuristic_algorithms/version.rb +3 -0
- data/lib/metaheuristic_algorithms/virtual_bee_algorithm.rb +27 -0
- data/lib/metaheuristic_algorithms.rb +17 -0
- data/metaheuristic_algorithms.gemspec +36 -0
- metadata +146 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1d0f703767effd3bf49ad6f0fdb937e81f4cca0b
|
4
|
+
data.tar.gz: 2b006e39389d9ab0cf870de9178150f956bd948e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3128ccca278336b88d59a1dff1e91f6e087b038693ffc8a7da83a24c64e5afe42aa5a98a809586ccdb0460a35ddebea172254df017757eb7756cbf0ee6dda579
|
7
|
+
data.tar.gz: 3cc1be0fa3cb7093f7615fc4abfd0b182c0f906f9979d4025bf1370fbf9b290d70973fc6d13d70e1be4505d671782e2526e583d95dba5d8db488e20718727afe
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
metaheuristic_algorithms
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.2
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Tadatoshi Takahashi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# MetaheuristicAlgorithms
|
2
|
+
|
3
|
+
Various metaheuristic algorithms implemented in Ruby.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'metaheuristic_algorithms'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install metaheuristic_algorithms
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
See examples in RSpec codes under spec directory.
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
28
|
+
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/metaheuristic_algorithms. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
34
|
+
|
35
|
+
|
36
|
+
## License
|
37
|
+
|
38
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
39
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "metaheuristic_algorithms"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module MetaheuristicAlgorithms
|
2
|
+
|
3
|
+
class AntColonyOptimization
|
4
|
+
include MetaheuristicAlgorithms::Helper
|
5
|
+
|
6
|
+
def initialize(function_wrapper, number_of_variables: 1, objective: :maximization)
|
7
|
+
@function_wrapper = function_wrapper
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
def search
|
14
|
+
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module MetaheuristicAlgorithms
|
2
|
+
|
3
|
+
module BaseAlgorithmModule
|
4
|
+
|
5
|
+
def get_decision_variable_value_by_randomization(decision_variable_index)
|
6
|
+
@function_wrapper.miminum_decision_variable_values[decision_variable_index]
|
7
|
+
+ (@function_wrapper.maximum_decision_variable_values[decision_variable_index] - @function_wrapper.miminum_decision_variable_values[decision_variable_index]) * bigdecimal_rand
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module MetaheuristicAlgorithms
|
2
|
+
|
3
|
+
module CalculationHelper
|
4
|
+
|
5
|
+
def degree_to_radian(degree)
|
6
|
+
degree * BigDecimal(Math::PI.to_s) / BigDecimal('180')
|
7
|
+
end
|
8
|
+
|
9
|
+
def radian_to_degree(radian)
|
10
|
+
radian * BigDecimal('180') / BigDecimal(Math::PI.to_s)
|
11
|
+
end
|
12
|
+
|
13
|
+
def bigdecimal_exp(bigdecimal_value)
|
14
|
+
BigDecimal(Math.exp(bigdecimal_value.to_f).to_s)
|
15
|
+
end
|
16
|
+
|
17
|
+
def bigdecimal_cos(angle_in_radian)
|
18
|
+
BigDecimal(Math.cos(angle_in_radian.to_f).to_s)
|
19
|
+
end
|
20
|
+
|
21
|
+
def bigdecimal_sin(angle_in_radian)
|
22
|
+
BigDecimal(Math.sin(angle_in_radian.to_f).to_s)
|
23
|
+
end
|
24
|
+
|
25
|
+
def bigdecimal_tan(angle_in_radian)
|
26
|
+
BigDecimal(Math.tan(angle_in_radian.to_f).to_s)
|
27
|
+
end
|
28
|
+
|
29
|
+
def bigdecimal_asin(sin_value)
|
30
|
+
BigDecimal(Math.asin(sin_value.to_f).to_s)
|
31
|
+
end
|
32
|
+
|
33
|
+
def bigdecimal_acos(cos_value)
|
34
|
+
BigDecimal(Math.acos(cos_value.to_f).to_s)
|
35
|
+
end
|
36
|
+
|
37
|
+
def bigdecimal_atan2(y, x)
|
38
|
+
BigDecimal(Math.atan2(y.to_f, x.to_f).to_s)
|
39
|
+
end
|
40
|
+
|
41
|
+
def bigdecimal_sqrt(bigdecimal_value)
|
42
|
+
BigDecimal(Math.sqrt(bigdecimal_value.to_f).to_s)
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
require 'bigdecimal/math'
|
3
|
+
|
4
|
+
module MetaheuristicAlgorithms
|
5
|
+
|
6
|
+
class FireflyAlgorithm
|
7
|
+
include MetaheuristicAlgorithms::BaseAlgorithmModule
|
8
|
+
include MetaheuristicAlgorithms::Helper
|
9
|
+
|
10
|
+
# Note: May be able to use Struct for this purpose, moving update_light_intensity method to FireflyAlgorithm class.
|
11
|
+
class Firefly
|
12
|
+
|
13
|
+
attr_accessor :location_coordinates
|
14
|
+
attr_accessor :light_intensity
|
15
|
+
|
16
|
+
def initialize(function_wrapper, location_coordinates, light_intensity)
|
17
|
+
@function_wrapper = function_wrapper
|
18
|
+
@location_coordinates = location_coordinates
|
19
|
+
@light_intensity = light_intensity
|
20
|
+
end
|
21
|
+
|
22
|
+
def update_light_intensity
|
23
|
+
@light_intensity = @function_wrapper.objective_function_value(@location_coordinates)
|
24
|
+
end
|
25
|
+
|
26
|
+
def deep_clone
|
27
|
+
clone_object = self.clone
|
28
|
+
clone_object.location_coordinates = self.location_coordinates.clone
|
29
|
+
clone_object
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
def initialize(function_wrapper, number_of_variables: 1, objective: :maximization)
|
35
|
+
@function_wrapper = function_wrapper
|
36
|
+
@number_of_variables = number_of_variables
|
37
|
+
@objective_method_name = case objective
|
38
|
+
when :maximization
|
39
|
+
:max_by
|
40
|
+
when :minimization
|
41
|
+
:min_by
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def search(number_of_fireflies: 10, maximun_generation: 10, randomization_parameter_alpha: BigDecimal('0.2'), absorption_coefficient_gamma: BigDecimal('1.0'))
|
46
|
+
|
47
|
+
number_of_fireflies = number_of_fireflies.to_i unless number_of_fireflies.kind_of?(Integer)
|
48
|
+
maximun_generation = maximun_generation.to_i unless maximun_generation.kind_of?(Integer)
|
49
|
+
randomization_parameter_alpha = BigDecimal(randomization_parameter_alpha.to_s) unless randomization_parameter_alpha.kind_of?(BigDecimal)
|
50
|
+
absorption_coefficient_gamma = BigDecimal(absorption_coefficient_gamma.to_s) unless absorption_coefficient_gamma.kind_of?(BigDecimal)
|
51
|
+
|
52
|
+
initialize_fireflies(number_of_fireflies)
|
53
|
+
|
54
|
+
# 0 to maximun_generation-1
|
55
|
+
(0...maximun_generation).each do |generation|
|
56
|
+
|
57
|
+
@fireflies.each { |firefly| firefly.update_light_intensity }
|
58
|
+
|
59
|
+
move_fireflies(randomization_parameter_alpha, absorption_coefficient_gamma)
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
solution_firefly = @fireflies.send(@objective_method_name) { |firefly| firefly.light_intensity }
|
64
|
+
|
65
|
+
{ best_decision_variable_values: solution_firefly.location_coordinates, best_objective_function_value: solution_firefly.light_intensity }
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
def initialize_fireflies(number_of_fireflies)
|
71
|
+
|
72
|
+
@fireflies = []
|
73
|
+
|
74
|
+
for i in (0...number_of_fireflies)
|
75
|
+
decision_variable_values = (0...@number_of_variables).map do |variable_index|
|
76
|
+
get_decision_variable_value_by_randomization(variable_index)
|
77
|
+
end
|
78
|
+
|
79
|
+
firefly = Firefly.new(@function_wrapper, decision_variable_values, BigDecimal('0'))
|
80
|
+
@fireflies << firefly
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
def move_fireflies(randomization_parameter_alpha, absorption_coefficient_gamma)
|
86
|
+
|
87
|
+
attractiveness_beta_at_distance_0 = BigDecimal('1')
|
88
|
+
|
89
|
+
fireflies_copy = @fireflies.map(&:deep_clone)
|
90
|
+
|
91
|
+
@fireflies.each do |firefly_i|
|
92
|
+
|
93
|
+
fireflies_copy.delete(firefly_i)
|
94
|
+
|
95
|
+
fireflies_copy.each do |firefly_j|
|
96
|
+
|
97
|
+
if firefly_i.light_intensity < firefly_j.light_intensity
|
98
|
+
|
99
|
+
distance_of_two_fireflies = distance_of_two_fireflies(firefly_i, firefly_j)
|
100
|
+
|
101
|
+
attractiveness_beta = attractiveness_beta_at_distance_0 * BigMath.exp(-absorption_coefficient_gamma * distance_of_two_fireflies.power(2), 10)
|
102
|
+
|
103
|
+
(0...@number_of_variables).each do |variable_index|
|
104
|
+
new_location_coordinate = firefly_i.location_coordinates[variable_index] * (BigDecimal('1') - attractiveness_beta)
|
105
|
+
+ firefly_j.location_coordinates[variable_index] * attractiveness_beta
|
106
|
+
+ randomization_parameter_alpha * (bigdecimal_rand - BigDecimal('0.5'))
|
107
|
+
new_location_coordinate = constrain_within_range(new_location_coordinate, variable_index)
|
108
|
+
|
109
|
+
firefly_i.location_coordinates[variable_index] = new_location_coordinate
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
def constrain_within_range(location_coordinate, variable_index)
|
121
|
+
if location_coordinate < @function_wrapper.miminum_decision_variable_values[variable_index]
|
122
|
+
@function_wrapper.miminum_decision_variable_values[variable_index]
|
123
|
+
elsif location_coordinate > @function_wrapper.maximum_decision_variable_values[variable_index]
|
124
|
+
@function_wrapper.maximum_decision_variable_values[variable_index]
|
125
|
+
else
|
126
|
+
location_coordinate
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def distance_of_two_fireflies(firefly_1, firefly_2)
|
131
|
+
|
132
|
+
sum_of_squares = (0...@number_of_variables).inject(BigDecimal('0')) do |sum, variable_index|
|
133
|
+
sum + (firefly_1.location_coordinates[variable_index] - firefly_2.location_coordinates[variable_index]).power(2)
|
134
|
+
end
|
135
|
+
|
136
|
+
sum_of_squares.sqrt(10)
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module MetaheuristicAlgorithms
|
2
|
+
|
3
|
+
module FunctionWrappers
|
4
|
+
|
5
|
+
class AbstractWrapper
|
6
|
+
|
7
|
+
# Return value: Array
|
8
|
+
def maximum_decision_variable_values
|
9
|
+
raise "#{__method__} method must be implemented in the subclass"
|
10
|
+
end
|
11
|
+
|
12
|
+
# Return value: Array
|
13
|
+
def miminum_decision_variable_values
|
14
|
+
raise "#{__method__} method must be implemented in the subclass"
|
15
|
+
end
|
16
|
+
|
17
|
+
# Input value: Array
|
18
|
+
def objective_function_value(decision_variable_values)
|
19
|
+
raise "#{__method__} method must be implemented in the subclass"
|
20
|
+
end
|
21
|
+
|
22
|
+
# For the algorithm that requires initial estimate that is depending on the particular objective function:
|
23
|
+
# Return value: Array
|
24
|
+
def initial_decision_variable_value_estimates
|
25
|
+
raise "#{__method__} method must be implemented in the subclass"
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'bigdecimal/math'
|
2
|
+
|
3
|
+
module MetaheuristicAlgorithms
|
4
|
+
|
5
|
+
module FunctionWrappers
|
6
|
+
|
7
|
+
class EasomFunctionWrapper < AbstractWrapper
|
8
|
+
include MetaheuristicAlgorithms::CalculationHelper
|
9
|
+
|
10
|
+
def maximum_decision_variable_values
|
11
|
+
[BigDecimal('10')]
|
12
|
+
end
|
13
|
+
|
14
|
+
def miminum_decision_variable_values
|
15
|
+
[BigDecimal('-10')]
|
16
|
+
end
|
17
|
+
|
18
|
+
def objective_function_value(decision_variable_values)
|
19
|
+
-bigdecimal_cos(decision_variable_values[0]) * BigMath.exp((-(decision_variable_values[0] - BigMath.PI(10)).power(2)), 10)
|
20
|
+
end
|
21
|
+
|
22
|
+
def initial_decision_variable_value_estimates
|
23
|
+
raise "#{__method__} method must be implemented in the subclass"
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'bigdecimal/math'
|
2
|
+
|
3
|
+
module MetaheuristicAlgorithms
|
4
|
+
|
5
|
+
module FunctionWrappers
|
6
|
+
|
7
|
+
class MichaelwiczFunctionWrapper < AbstractWrapper
|
8
|
+
include MetaheuristicAlgorithms::CalculationHelper
|
9
|
+
|
10
|
+
def maximum_decision_variable_values
|
11
|
+
[BigDecimal('4'), BigDecimal('4')]
|
12
|
+
end
|
13
|
+
|
14
|
+
def miminum_decision_variable_values
|
15
|
+
[BigDecimal('0'), BigDecimal('0')]
|
16
|
+
end
|
17
|
+
|
18
|
+
def objective_function_value(decision_variable_values)
|
19
|
+
-bigdecimal_sin(decision_variable_values[0]) * bigdecimal_sin(decision_variable_values[0].power(2) / BigMath.PI(10)).power(20) -
|
20
|
+
bigdecimal_sin(decision_variable_values[1]) * bigdecimal_sin(decision_variable_values[1].power(2) / BigMath.PI(10)).power(20)
|
21
|
+
end
|
22
|
+
|
23
|
+
def initial_decision_variable_value_estimates
|
24
|
+
raise "#{__method__} method must be implemented in the subclass"
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'bigdecimal/math'
|
2
|
+
|
3
|
+
module MetaheuristicAlgorithms
|
4
|
+
|
5
|
+
module FunctionWrappers
|
6
|
+
|
7
|
+
class NonsmoothMultipeakFunctionWrapper < AbstractWrapper
|
8
|
+
|
9
|
+
def maximum_decision_variable_values
|
10
|
+
[BigDecimal('5'), BigDecimal('5')]
|
11
|
+
end
|
12
|
+
|
13
|
+
def miminum_decision_variable_values
|
14
|
+
[BigDecimal('-5'), BigDecimal('-5')]
|
15
|
+
end
|
16
|
+
|
17
|
+
def objective_function_value(decision_variable_values)
|
18
|
+
(decision_variable_values[0].abs + decision_variable_values[1].abs) * BigMath.exp((BigDecimal('-0.0625') * (decision_variable_values[0].power(2) + decision_variable_values[1].power(2))), 10)
|
19
|
+
end
|
20
|
+
|
21
|
+
def initial_decision_variable_value_estimates
|
22
|
+
raise "#{__method__} method must be implemented in the subclass"
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module MetaheuristicAlgorithms
|
2
|
+
|
3
|
+
module FunctionWrappers
|
4
|
+
|
5
|
+
class RosenbrookFunctionWrapper < AbstractWrapper
|
6
|
+
|
7
|
+
def maximum_decision_variable_values
|
8
|
+
[BigDecimal('5'), BigDecimal('5')]
|
9
|
+
end
|
10
|
+
|
11
|
+
def miminum_decision_variable_values
|
12
|
+
[BigDecimal('-5'), BigDecimal('-5')]
|
13
|
+
end
|
14
|
+
|
15
|
+
def objective_function_value(decision_variable_values)
|
16
|
+
(BigDecimal('1') - decision_variable_values[0]).power(2) + BigDecimal('100') * (decision_variable_values[1] - decision_variable_values[0].power(2)).power(2)
|
17
|
+
end
|
18
|
+
|
19
|
+
def initial_decision_variable_value_estimates
|
20
|
+
[BigDecimal('2'), BigDecimal('2')]
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require "metaheuristic_algorithms/function_wrappers/abstract_wrapper"
|
2
|
+
require "metaheuristic_algorithms/function_wrappers/rosenbrook_function_wrapper"
|
3
|
+
require "metaheuristic_algorithms/function_wrappers/nonsmooth_multipeak_function_wrapper"
|
4
|
+
require "metaheuristic_algorithms/function_wrappers/easom_function_wrapper"
|
5
|
+
require "metaheuristic_algorithms/function_wrappers/michaelwicz_function_wrapper"
|
6
|
+
|
7
|
+
module MetaheuristicAlgorithms
|
8
|
+
|
9
|
+
module FunctionWrappers
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,200 @@
|
|
1
|
+
module MetaheuristicAlgorithms
|
2
|
+
|
3
|
+
class GeneticAlgorithm
|
4
|
+
include MetaheuristicAlgorithms::BaseAlgorithmModule
|
5
|
+
include MetaheuristicAlgorithms::Helper
|
6
|
+
|
7
|
+
def initialize(function_wrapper, number_of_variables: 1, objective: :maximization)
|
8
|
+
@function_wrapper = function_wrapper
|
9
|
+
@number_of_variables = number_of_variables
|
10
|
+
@objective_method_name = case objective
|
11
|
+
when :maximization
|
12
|
+
:max
|
13
|
+
when :minimization
|
14
|
+
:min
|
15
|
+
end
|
16
|
+
|
17
|
+
## Decided to use decimal number representation and convert it to binary number by unpack method
|
18
|
+
## because it is difficult to initialize variable within the given range
|
19
|
+
# @string_length_in_bits = 16
|
20
|
+
end
|
21
|
+
|
22
|
+
def search(population_size: 20, maximum_number_of_generations: 100, number_of_mutation_sites: BigDecimal('2'), crossover_probability: BigDecimal('0.95'), mutation_probability: BigDecimal('0.05'))
|
23
|
+
|
24
|
+
population_size = population_size.to_i unless population_size.kind_of?(Integer)
|
25
|
+
maximum_number_of_generations = maximum_number_of_generations.to_i unless maximum_number_of_generations.kind_of?(Integer)
|
26
|
+
number_of_mutation_sites = BigDecimal(number_of_mutation_sites.to_s) unless number_of_mutation_sites.kind_of?(BigDecimal)
|
27
|
+
crossover_probability = BigDecimal(crossover_probability.to_s) unless crossover_probability.kind_of?(BigDecimal)
|
28
|
+
mutation_probability = BigDecimal(mutation_probability.to_s) unless mutation_probability.kind_of?(BigDecimal)
|
29
|
+
|
30
|
+
initialize_population(population_size)
|
31
|
+
|
32
|
+
(0...maximum_number_of_generations).each do |generation_index|
|
33
|
+
|
34
|
+
@population_copy = deep_clone_population
|
35
|
+
|
36
|
+
(0...population_size).each do |individual_index|
|
37
|
+
|
38
|
+
if bigdecimal_rand < crossover_probability
|
39
|
+
|
40
|
+
# Crossover pair:
|
41
|
+
crossover_pair_1_index = generate_random_index(population_size)
|
42
|
+
crossover_pair_2_index = generate_random_index(population_size)
|
43
|
+
|
44
|
+
crossover(crossover_pair_1_index, crossover_pair_2_index)
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
if bigdecimal_rand < mutation_probability
|
49
|
+
|
50
|
+
mutation_individual_index = generate_random_index(population_size)
|
51
|
+
|
52
|
+
mutate(mutation_individual_index, number_of_mutation_sites)
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
objective_function_value = @population_fitness.send(@objective_method_name)
|
61
|
+
decision_variable_values = @population[@population_fitness.index(objective_function_value)]
|
62
|
+
|
63
|
+
{ best_decision_variable_values: decision_variable_values, best_objective_function_value: objective_function_value }
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def initialize_population(population_size)
|
70
|
+
|
71
|
+
@population = []
|
72
|
+
@population_fitness = []
|
73
|
+
|
74
|
+
# 0 to population_size-1
|
75
|
+
(0...population_size).each do |individual_index|
|
76
|
+
decision_variable_values = (0...@number_of_variables).map do |variable_index|
|
77
|
+
get_decision_variable_value_by_randomization(variable_index)
|
78
|
+
end
|
79
|
+
@population << decision_variable_values
|
80
|
+
@population_fitness << @function_wrapper.objective_function_value(decision_variable_values)
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
def generate_random_index(population_size)
|
86
|
+
(population_size * rand).floor
|
87
|
+
end
|
88
|
+
|
89
|
+
def crossover(crossover_pair_1_index, crossover_pair_2_index)
|
90
|
+
|
91
|
+
crossover_pair_1_decimal_values = []
|
92
|
+
crossover_pair_2_decimal_values = []
|
93
|
+
|
94
|
+
(0...@number_of_variables).each do |variable_index|
|
95
|
+
|
96
|
+
crossover_pair_1_decimal_value = @population_copy[crossover_pair_1_index][variable_index]
|
97
|
+
crossover_pair_2_decimal_value = @population_copy[crossover_pair_2_index][variable_index]
|
98
|
+
|
99
|
+
crossover_pair_1_binary_32_string = decimal_to_binary_32_string(crossover_pair_1_decimal_value)
|
100
|
+
crossover_pair_2_binary_32_string = decimal_to_binary_32_string(crossover_pair_2_decimal_value)
|
101
|
+
|
102
|
+
# 32 bits string with the first bit as sign bit. Hence, the length is 32 - 1:
|
103
|
+
crossover_point = 31 * rand
|
104
|
+
|
105
|
+
crossover_pair_1_binary_32_string_after_crossover = crossover_pair_1_binary_32_string[0, crossover_point] +
|
106
|
+
crossover_pair_2_binary_32_string[crossover_point+1, 32]
|
107
|
+
crossover_pair_2_binary_32_string_after_crossover = crossover_pair_2_binary_32_string[0, crossover_point] +
|
108
|
+
crossover_pair_1_binary_32_string[crossover_point+1, 32]
|
109
|
+
|
110
|
+
crossover_pair_1_decimal_value_after_crossover = binary_32_string_to_decimal(crossover_pair_1_binary_32_string_after_crossover)
|
111
|
+
crossover_pair_2_decimal_value_after_crossover = binary_32_string_to_decimal(crossover_pair_2_binary_32_string_after_crossover)
|
112
|
+
|
113
|
+
if in_the_range?(crossover_pair_1_decimal_value_after_crossover, variable_index) &&
|
114
|
+
in_the_range?(crossover_pair_2_decimal_value_after_crossover, variable_index)
|
115
|
+
crossover_pair_1_decimal_values << crossover_pair_1_decimal_value_after_crossover
|
116
|
+
crossover_pair_2_decimal_values << crossover_pair_2_decimal_value_after_crossover
|
117
|
+
else
|
118
|
+
crossover_pair_1_decimal_values << crossover_pair_1_decimal_value
|
119
|
+
crossover_pair_2_decimal_values << crossover_pair_2_decimal_value
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
new_crossover_pair_1_fitness = @function_wrapper.objective_function_value(crossover_pair_1_decimal_values)
|
125
|
+
new_crossover_pair_2_fitness = @function_wrapper.objective_function_value(crossover_pair_2_decimal_values)
|
126
|
+
|
127
|
+
if new_crossover_pair_1_fitness > @population_fitness[crossover_pair_1_index] &&
|
128
|
+
new_crossover_pair_2_fitness > @population_fitness[crossover_pair_2_index]
|
129
|
+
|
130
|
+
@population[crossover_pair_1_index] = @population_copy[crossover_pair_1_index] = crossover_pair_1_decimal_values
|
131
|
+
@population[crossover_pair_2_index] = @population_copy[crossover_pair_2_index] = crossover_pair_2_decimal_values
|
132
|
+
|
133
|
+
@population_fitness[crossover_pair_1_index] = new_crossover_pair_1_fitness
|
134
|
+
@population_fitness[crossover_pair_2_index] = new_crossover_pair_2_fitness
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
def decimal_to_binary_32_string(decimal_number)
|
141
|
+
[decimal_number].pack('g').bytes.map{|n| "%08b" % n}.join
|
142
|
+
end
|
143
|
+
|
144
|
+
def binary_32_string_to_decimal(binary_32_string)
|
145
|
+
decimal_float_value = [binary_32_string].pack("B*").unpack('g')[0].round(4)
|
146
|
+
BigDecimal(decimal_float_value.to_s)
|
147
|
+
end
|
148
|
+
|
149
|
+
def mutate(individual_index, number_of_mutation_sites)
|
150
|
+
|
151
|
+
decimal_values = (0...@number_of_variables).map do |variable_index|
|
152
|
+
|
153
|
+
decimal_value = @population_copy[individual_index][variable_index]
|
154
|
+
binary_32_string = decimal_to_binary_32_string(decimal_value)
|
155
|
+
|
156
|
+
mutated_binary_32_string = binary_32_string.clone
|
157
|
+
|
158
|
+
(0...number_of_mutation_sites).each do |i|
|
159
|
+
mutation_site_index = generate_mutation_site_index
|
160
|
+
# Flips 1 to 0 or 0 to 1:
|
161
|
+
mutated_binary_32_string[mutation_site_index] = ((binary_32_string[mutation_site_index].to_i + 1).modulo(2)).to_s
|
162
|
+
end
|
163
|
+
|
164
|
+
decimal_value_after_mutation = binary_32_string_to_decimal(mutated_binary_32_string)
|
165
|
+
|
166
|
+
if in_the_range?(decimal_value_after_mutation, variable_index)
|
167
|
+
decimal_value_after_mutation
|
168
|
+
else
|
169
|
+
decimal_value
|
170
|
+
end
|
171
|
+
|
172
|
+
end
|
173
|
+
|
174
|
+
new_individual_fitness = @function_wrapper.objective_function_value(decimal_values)
|
175
|
+
|
176
|
+
if new_individual_fitness > @population_fitness[individual_index]
|
177
|
+
|
178
|
+
@population[individual_index] = @population_copy[individual_index] = decimal_values
|
179
|
+
|
180
|
+
@population_fitness[individual_index] = new_individual_fitness
|
181
|
+
|
182
|
+
end
|
183
|
+
|
184
|
+
end
|
185
|
+
|
186
|
+
def generate_mutation_site_index
|
187
|
+
(31 * rand).floor
|
188
|
+
end
|
189
|
+
|
190
|
+
def in_the_range?(decimal_value, variable_index)
|
191
|
+
decimal_value >= @function_wrapper.miminum_decision_variable_values[variable_index] && decimal_value <= @function_wrapper.maximum_decision_variable_values[variable_index]
|
192
|
+
end
|
193
|
+
|
194
|
+
def deep_clone_population
|
195
|
+
@population.map { |individual| individual.clone }
|
196
|
+
end
|
197
|
+
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
module MetaheuristicAlgorithms
|
2
|
+
|
3
|
+
class HarmonySearch
|
4
|
+
include MetaheuristicAlgorithms::BaseAlgorithmModule
|
5
|
+
include MetaheuristicAlgorithms::Helper
|
6
|
+
|
7
|
+
# objective: :maximization or :minimization
|
8
|
+
def initialize(function_wrapper, number_of_variables: 1, objective: :maximization)
|
9
|
+
@function_wrapper = function_wrapper
|
10
|
+
@number_of_variables = number_of_variables
|
11
|
+
@objective_method_name = case objective
|
12
|
+
when :maximization
|
13
|
+
:max
|
14
|
+
when :minimization
|
15
|
+
:min
|
16
|
+
end
|
17
|
+
@objective_comparison_operator = case objective
|
18
|
+
when :maximization
|
19
|
+
:>
|
20
|
+
when :minimization
|
21
|
+
:<
|
22
|
+
end
|
23
|
+
|
24
|
+
@decision_variable_range = [@function_wrapper.miminum_decision_variable_values, @function_wrapper.maximum_decision_variable_values]
|
25
|
+
end
|
26
|
+
|
27
|
+
def search(maximum_attempt: 2500, pitch_adjusting_range: BigDecimal('100'), harmony_search_size: 20, harmony_memory_acceping_rate: BigDecimal('0.95'), pitch_adjusting_rate: BigDecimal('0.7'))
|
28
|
+
|
29
|
+
maximum_attempt = maximum_attempt.to_i unless maximum_attempt.kind_of?(Integer)
|
30
|
+
pitch_adjusting_range = BigDecimal(pitch_adjusting_range.to_s) unless pitch_adjusting_range.kind_of?(BigDecimal)
|
31
|
+
harmony_search_size = harmony_search_size.to_i unless harmony_search_size.kind_of?(Integer)
|
32
|
+
harmony_memory_acceping_rate = BigDecimal(harmony_memory_acceping_rate.to_s) unless harmony_memory_acceping_rate.kind_of?(BigDecimal)
|
33
|
+
pitch_adjusting_rate = BigDecimal(pitch_adjusting_rate.to_s) unless pitch_adjusting_rate.kind_of?(BigDecimal)
|
34
|
+
|
35
|
+
initialize_harmony_memory(harmony_search_size)
|
36
|
+
|
37
|
+
# 0 to maximum_attempt-1
|
38
|
+
(0...maximum_attempt).each do |count|
|
39
|
+
|
40
|
+
decision_variable_values = (0...@number_of_variables).map do |variable_index|
|
41
|
+
|
42
|
+
if rand > harmony_memory_acceping_rate
|
43
|
+
decision_variable_value = get_decision_variable_value_by_randomization(variable_index)
|
44
|
+
else
|
45
|
+
# Since the array index starts with 0 unlike MatLab, 1 is not added as in the code example in MatLab:
|
46
|
+
harmony_memory_random_index = (harmony_search_size * bigdecimal_rand).fix
|
47
|
+
decision_variable_value = @harmony_memory[harmony_memory_random_index][variable_index]
|
48
|
+
|
49
|
+
if bigdecimal_rand < pitch_adjusting_rate
|
50
|
+
pitch_adjusting = (@function_wrapper.maximum_decision_variable_values[variable_index] - @function_wrapper.miminum_decision_variable_values[variable_index]) / pitch_adjusting_range
|
51
|
+
decision_variable_value = decision_variable_value + pitch_adjusting * (bigdecimal_rand - BigDecimal('0.5'))
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
decision_variable_value
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
best_function_value = @function_wrapper.objective_function_value(decision_variable_values)
|
60
|
+
|
61
|
+
# TODO: Evaluate this logic. It appears that best_function_value_harmony_memory only needs to store the max best_function_value and the corresponding harmony_memory_random_index of harmony_memory.
|
62
|
+
# The simpler implementation for MatLab doesn't seem to be a good implementation in Ruby here.
|
63
|
+
# Or MatLab code can use Object-Oriented class as a value object (There is Object-Oriented construct in MatLab).
|
64
|
+
# When objective = :maximization, "if best_function_value > @best_function_value_harmony_memory.max"
|
65
|
+
# When objective = :minimization, "if best_function_value < @best_function_value_harmony_memory.min"
|
66
|
+
if best_function_value.send(@objective_comparison_operator, @best_function_value_harmony_memory.send(@objective_method_name))
|
67
|
+
# If harmony_memory_random_index is not set in the if statement above, it means a new search and use the index for the max best function value:
|
68
|
+
harmony_memory_random_index ||= @best_function_value_harmony_memory.index(@best_function_value_harmony_memory.send(@objective_method_name))
|
69
|
+
@harmony_memory[harmony_memory_random_index] = decision_variable_values
|
70
|
+
@best_function_value_harmony_memory[harmony_memory_random_index] = best_function_value
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
objective_function_value = @best_function_value_harmony_memory.send(@objective_method_name)
|
76
|
+
decision_variable_values = @harmony_memory[@best_function_value_harmony_memory.index(objective_function_value)]
|
77
|
+
|
78
|
+
{ best_decision_variable_values: decision_variable_values, best_objective_function_value: objective_function_value }
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
def initialize_harmony_memory(harmony_search_size)
|
84
|
+
|
85
|
+
@harmony_memory = []
|
86
|
+
@best_function_value_harmony_memory = []
|
87
|
+
|
88
|
+
for i in (0...harmony_search_size)
|
89
|
+
|
90
|
+
decision_variable_values = (0...@number_of_variables).map do |variable_index|
|
91
|
+
get_decision_variable_value_by_randomization(variable_index)
|
92
|
+
end
|
93
|
+
|
94
|
+
@harmony_memory << decision_variable_values
|
95
|
+
@best_function_value_harmony_memory << @function_wrapper.objective_function_value(decision_variable_values)
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module MetaheuristicAlgorithms
|
2
|
+
|
3
|
+
class SimplifiedParticleSwarmOptimization
|
4
|
+
include MetaheuristicAlgorithms::BaseAlgorithmModule
|
5
|
+
include MetaheuristicAlgorithms::Helper
|
6
|
+
|
7
|
+
def initialize(function_wrapper, number_of_variables: 1, objective: :maximization)
|
8
|
+
@function_wrapper = function_wrapper
|
9
|
+
@number_of_variables = number_of_variables
|
10
|
+
@objective_method_name = case objective
|
11
|
+
when :maximization
|
12
|
+
:max
|
13
|
+
when :minimization
|
14
|
+
:min
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def search(number_of_particiles: 20, number_of_iterations: 15, social_coefficient: BigDecimal('0.5'), random_variable_coefficient: BigDecimal('0.2'))
|
19
|
+
|
20
|
+
number_of_particiles = number_of_particiles.to_i unless number_of_particiles.kind_of?(Integer)
|
21
|
+
number_of_iterations = number_of_iterations.to_i unless number_of_iterations.kind_of?(Integer)
|
22
|
+
social_coefficient = BigDecimal(social_coefficient.to_s) unless social_coefficient.kind_of?(BigDecimal)
|
23
|
+
random_variable_coefficient = BigDecimal(random_variable_coefficient.to_s) unless random_variable_coefficient.kind_of?(BigDecimal)
|
24
|
+
|
25
|
+
initialize_particles(number_of_particiles)
|
26
|
+
|
27
|
+
global_best_position = nil
|
28
|
+
best_function_value = nil
|
29
|
+
|
30
|
+
# 0 to number_of_iterations-1
|
31
|
+
(0...number_of_iterations).each do |iteration|
|
32
|
+
|
33
|
+
function_values = @particle_locations.map do |particle_location|
|
34
|
+
@function_wrapper.objective_function_value(particle_location)
|
35
|
+
end
|
36
|
+
|
37
|
+
best_function_value = function_values.send(@objective_method_name)
|
38
|
+
global_best_position = @particle_locations[function_values.index(best_function_value)]
|
39
|
+
|
40
|
+
move_particles(global_best_position, social_coefficient, random_variable_coefficient)
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
{ best_decision_variable_values: global_best_position, best_objective_function_value: best_function_value }
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
def initialize_particles(number_of_particiles)
|
50
|
+
|
51
|
+
@particle_locations = []
|
52
|
+
|
53
|
+
# 0 to number_of_particiles-1
|
54
|
+
(0...number_of_particiles).each do |individual_index|
|
55
|
+
decision_variable_values = (0...@number_of_variables).map do |variable_index|
|
56
|
+
get_decision_variable_value_by_randomization(variable_index)
|
57
|
+
end
|
58
|
+
@particle_locations << decision_variable_values
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
def move_particles(global_best_position, social_coefficient, random_variable_coefficient)
|
64
|
+
|
65
|
+
# 0 to @particle_locations-1
|
66
|
+
@particle_locations = @particle_locations.map do |particle_location|
|
67
|
+
|
68
|
+
(0...@number_of_variables).map do |variable_index|
|
69
|
+
|
70
|
+
# The value out-of-range in order to enter while loop
|
71
|
+
new_particle_location_coordinate = @function_wrapper.miminum_decision_variable_values[variable_index] - BigDecimal('1')
|
72
|
+
|
73
|
+
while new_particle_location_coordinate < @function_wrapper.miminum_decision_variable_values[variable_index] || new_particle_location_coordinate > @function_wrapper.maximum_decision_variable_values[variable_index]
|
74
|
+
new_particle_location_coordinate = (BigDecimal('1') - social_coefficient) * particle_location[variable_index] + social_coefficient * global_best_position[variable_index] + random_variable_coefficient * (bigdecimal_rand - BigDecimal('0.5'))
|
75
|
+
end
|
76
|
+
|
77
|
+
new_particle_location_coordinate
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'bigdecimal/math'
|
2
|
+
require 'distribution'
|
3
|
+
|
4
|
+
module MetaheuristicAlgorithms
|
5
|
+
|
6
|
+
class SimulatedAnnealing
|
7
|
+
include MetaheuristicAlgorithms::Helper
|
8
|
+
|
9
|
+
def initialize(function_wrapper, number_of_variables: 1, objective: :maximization)
|
10
|
+
@function_wrapper = function_wrapper
|
11
|
+
@number_of_variables = number_of_variables
|
12
|
+
@objective_comparison_operator = case objective
|
13
|
+
when :maximization
|
14
|
+
:>
|
15
|
+
when :minimization
|
16
|
+
:<
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def search(temperature: BigDecimal('100.0'), minimal_temperature: BigDecimal('1'), maximum_number_of_rejections: 2500,
|
21
|
+
maximum_number_of_runs: 500, maximum_number_of_acceptances: 15,
|
22
|
+
bolzmann_constant: BigDecimal('1'), cooling_factor: BigDecimal('0.95'), energy_norm: BigDecimal('10'),
|
23
|
+
standard_diviation_for_estimation: BigDecimal('6'), ratio_of_energy_delta_over_evaluation_delta: BigDecimal('10'))
|
24
|
+
|
25
|
+
temperature = BigDecimal(temperature.to_s) unless temperature.kind_of?(BigDecimal)
|
26
|
+
minimal_temperature = BigDecimal(minimal_temperature.to_s) unless minimal_temperature.kind_of?(BigDecimal)
|
27
|
+
maximum_number_of_rejections = maximum_number_of_rejections.to_i unless maximum_number_of_rejections.kind_of?(Integer)
|
28
|
+
maximum_number_of_runs = maximum_number_of_runs.to_i unless maximum_number_of_runs.kind_of?(Integer)
|
29
|
+
maximum_number_of_acceptances = maximum_number_of_acceptances.to_i unless maximum_number_of_acceptances.kind_of?(Integer)
|
30
|
+
bolzmann_constant = BigDecimal(bolzmann_constant.to_s) unless bolzmann_constant.kind_of?(BigDecimal)
|
31
|
+
cooling_factor = BigDecimal(cooling_factor.to_s) unless cooling_factor.kind_of?(BigDecimal)
|
32
|
+
energy_norm = BigDecimal(energy_norm.to_s) unless energy_norm.kind_of?(BigDecimal)
|
33
|
+
standard_diviation_for_estimation = BigDecimal(standard_diviation_for_estimation.to_s) unless standard_diviation_for_estimation.kind_of?(BigDecimal)
|
34
|
+
ratio_of_energy_delta_over_evaluation_delta = BigDecimal(ratio_of_energy_delta_over_evaluation_delta.to_s) unless ratio_of_energy_delta_over_evaluation_delta.kind_of?(BigDecimal)
|
35
|
+
|
36
|
+
number_of_runs = 0
|
37
|
+
number_of_rejections = 0
|
38
|
+
number_of_acceptances = 0
|
39
|
+
total_evaluations = 0
|
40
|
+
|
41
|
+
initial_estimates = @function_wrapper.initial_decision_variable_value_estimates
|
42
|
+
|
43
|
+
best_evaluation = @function_wrapper.objective_function_value(initial_estimates)
|
44
|
+
|
45
|
+
best_solution = initial_estimates
|
46
|
+
|
47
|
+
# TODO: Add the code to check with minimal_function_value if looking for the minimal value:
|
48
|
+
while temperature > minimal_temperature && number_of_rejections <= maximum_number_of_rejections
|
49
|
+
|
50
|
+
number_of_runs = number_of_runs + 1
|
51
|
+
|
52
|
+
if number_of_runs >= maximum_number_of_runs || number_of_acceptances > maximum_number_of_acceptances
|
53
|
+
|
54
|
+
temperature = cooling_factor * temperature
|
55
|
+
total_evaluations = total_evaluations + 1
|
56
|
+
number_of_runs = 1
|
57
|
+
number_of_acceptances = 1
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
new_estimates = estimate_solution(initial_estimates, standard_diviation_for_estimation)
|
62
|
+
new_evaluation = @function_wrapper.objective_function_value(new_estimates)
|
63
|
+
|
64
|
+
evaluation_delta = ratio_of_energy_delta_over_evaluation_delta * (new_evaluation - best_evaluation)
|
65
|
+
|
66
|
+
# Accept if improved:
|
67
|
+
# When objective = :maximization, "if evaluation_delta > 0 && evaluation_delta > energy_norm"
|
68
|
+
# When objective = :minimization, "if evaluation_delta < 0 && -evaluation_delta > energy_norm"
|
69
|
+
if evaluation_delta.send(@objective_comparison_operator, BigDecimal('0')) && evaluation_delta.abs > energy_norm
|
70
|
+
best_solution = new_estimates
|
71
|
+
best_evaluation = new_evaluation
|
72
|
+
number_of_acceptances = number_of_acceptances + 1
|
73
|
+
number_of_rejections = 0
|
74
|
+
# Accept with a small probability if not improved
|
75
|
+
elsif acceptance_probability(evaluation_delta, temperature, bolzmann_constant) > bigdecimal_rand
|
76
|
+
best_solution = new_estimates
|
77
|
+
best_evaluation = new_evaluation
|
78
|
+
number_of_acceptances = number_of_acceptances + 1
|
79
|
+
else
|
80
|
+
number_of_rejections = number_of_rejections + 1
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
{ best_decision_variable_values: best_solution, best_objective_function_value: best_evaluation }
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
def estimate_solution(previous_estimates, standard_diviation_for_estimation)
|
91
|
+
|
92
|
+
(0...@number_of_variables).map do |variable_index|
|
93
|
+
|
94
|
+
# The value out-of-range in order to enter while loop
|
95
|
+
new_estimate = @function_wrapper.miminum_decision_variable_values[variable_index] - BigDecimal('1')
|
96
|
+
|
97
|
+
while new_estimate < @function_wrapper.miminum_decision_variable_values[variable_index] || new_estimate > @function_wrapper.maximum_decision_variable_values[variable_index]
|
98
|
+
|
99
|
+
# MatLab example code uses newGuess = initialGuess + rand(1,2) * randn;
|
100
|
+
# But in our case, the value range is different.
|
101
|
+
new_estimate = BigDecimal(Distribution::Normal.rng(previous_estimates[variable_index], standard_diviation_for_estimation).call.to_s)
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
new_estimate
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
def acceptance_probability(evaluation_delta, temperature, bolzmann_constant)
|
112
|
+
BigMath.exp((-evaluation_delta / (bolzmann_constant * temperature)), 10)
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
3
|
+
module MetaheuristicAlgorithms
|
4
|
+
|
5
|
+
class VirtualBeeAlgorithm
|
6
|
+
include MetaheuristicAlgorithms::Helper
|
7
|
+
|
8
|
+
def initialize(function_wrapper, number_of_variables: 1, objective: :maximization)
|
9
|
+
@function_wrapper = function_wrapper
|
10
|
+
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
def search(number_of_virtual_bees: 20, number_of_foraging_explorations: 600)
|
15
|
+
|
16
|
+
number_of_virtual_bees = number_of_virtual_bees.to_i unless number_of_virtual_bees.kind_of?(Integer)
|
17
|
+
number_of_foraging_explorations = number_of_foraging_explorations.to_i unless number_of_foraging_explorations.kind_of?(Integer)
|
18
|
+
|
19
|
+
solution_estimates = @function_wrapper.initial_decision_variable_value_estimates
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'metaheuristic_algorithms/version'
|
2
|
+
require 'bigdecimal'
|
3
|
+
require 'metaheuristic_algorithms/calculation_helper'
|
4
|
+
require 'metaheuristic_algorithms/function_wrappers'
|
5
|
+
require 'metaheuristic_algorithms/base_algorithm_module'
|
6
|
+
require 'metaheuristic_algorithms/helper'
|
7
|
+
require 'metaheuristic_algorithms/harmony_search'
|
8
|
+
require 'metaheuristic_algorithms/firefly_algorithm'
|
9
|
+
require 'metaheuristic_algorithms/genetic_algorithm'
|
10
|
+
require 'metaheuristic_algorithms/simulated_annealing'
|
11
|
+
require 'metaheuristic_algorithms/simplified_particle_swarm_optimization'
|
12
|
+
require 'metaheuristic_algorithms/ant_colony_optimization'
|
13
|
+
require 'metaheuristic_algorithms/virtual_bee_algorithm'
|
14
|
+
|
15
|
+
module MetaheuristicAlgorithms
|
16
|
+
# Your code goes here...
|
17
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'metaheuristic_algorithms/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "metaheuristic_algorithms"
|
8
|
+
spec.version = MetaheuristicAlgorithms::VERSION
|
9
|
+
spec.authors = ["Tadatoshi Takahashi"]
|
10
|
+
spec.email = ["tadatoshi@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Various metaheuristic algorithms implemented in Ruby.}
|
13
|
+
spec.description = %q{Various metaheuristic algorithms implemented in Ruby.}
|
14
|
+
spec.homepage = "https://github.com/tadatoshi/metaheuristic_algorithms"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "distribution"
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "rspec"
|
35
|
+
spec.add_development_dependency "pry-byebug"
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: metaheuristic_algorithms
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tadatoshi Takahashi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: distribution
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-byebug
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Various metaheuristic algorithms implemented in Ruby.
|
84
|
+
email:
|
85
|
+
- tadatoshi@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".ruby-gemset"
|
93
|
+
- ".ruby-version"
|
94
|
+
- ".travis.yml"
|
95
|
+
- CODE_OF_CONDUCT.md
|
96
|
+
- Gemfile
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- lib/metaheuristic_algorithms.rb
|
103
|
+
- lib/metaheuristic_algorithms/ant_colony_optimization.rb
|
104
|
+
- lib/metaheuristic_algorithms/base_algorithm_module.rb
|
105
|
+
- lib/metaheuristic_algorithms/calculation_helper.rb
|
106
|
+
- lib/metaheuristic_algorithms/firefly_algorithm.rb
|
107
|
+
- lib/metaheuristic_algorithms/function_wrappers.rb
|
108
|
+
- lib/metaheuristic_algorithms/function_wrappers/abstract_wrapper.rb
|
109
|
+
- lib/metaheuristic_algorithms/function_wrappers/easom_function_wrapper.rb
|
110
|
+
- lib/metaheuristic_algorithms/function_wrappers/michaelwicz_function_wrapper.rb
|
111
|
+
- lib/metaheuristic_algorithms/function_wrappers/nonsmooth_multipeak_function_wrapper.rb
|
112
|
+
- lib/metaheuristic_algorithms/function_wrappers/rosenbrook_function_wrapper.rb
|
113
|
+
- lib/metaheuristic_algorithms/genetic_algorithm.rb
|
114
|
+
- lib/metaheuristic_algorithms/harmony_search.rb
|
115
|
+
- lib/metaheuristic_algorithms/helper.rb
|
116
|
+
- lib/metaheuristic_algorithms/simplified_particle_swarm_optimization.rb
|
117
|
+
- lib/metaheuristic_algorithms/simulated_annealing.rb
|
118
|
+
- lib/metaheuristic_algorithms/version.rb
|
119
|
+
- lib/metaheuristic_algorithms/virtual_bee_algorithm.rb
|
120
|
+
- metaheuristic_algorithms.gemspec
|
121
|
+
homepage: https://github.com/tadatoshi/metaheuristic_algorithms
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
metadata:
|
125
|
+
allowed_push_host: https://rubygems.org
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 2.4.7
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: Various metaheuristic algorithms implemented in Ruby.
|
146
|
+
test_files: []
|