gimuby 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/Gemfile +1 -0
  2. data/LICENSE.md +25 -0
  3. data/README.md +0 -0
  4. data/lib/gimuby.rb +10 -0
  5. data/lib/gimuby/config.rb +39 -0
  6. data/lib/gimuby/dependencies.rb +37 -0
  7. data/lib/gimuby/event/event.rb +29 -0
  8. data/lib/gimuby/event/event_manager.rb +34 -0
  9. data/lib/gimuby/factory.rb +275 -0
  10. data/lib/gimuby/genetic/archipelago/archipelago.rb +305 -0
  11. data/lib/gimuby/genetic/archipelago/connect_strategy/barabasi_albert_connect_strategy.rb +77 -0
  12. data/lib/gimuby/genetic/archipelago/connect_strategy/circle_connect_strategy.rb +11 -0
  13. data/lib/gimuby/genetic/archipelago/connect_strategy/connect_strategy.rb +34 -0
  14. data/lib/gimuby/genetic/archipelago/connect_strategy/constant_degree_connect_strategy.rb +22 -0
  15. data/lib/gimuby/genetic/archipelago/connect_strategy/fully_connected_connect_strategy.rb +11 -0
  16. data/lib/gimuby/genetic/archipelago/connect_strategy/random_connect_strategy.rb +29 -0
  17. data/lib/gimuby/genetic/archipelago/connect_strategy/watts_strogatz_connect_strategy.rb +63 -0
  18. data/lib/gimuby/genetic/archipelago/measure/clustering_coefficient_measure.rb +92 -0
  19. data/lib/gimuby/genetic/archipelago/measure/connected_measure.rb +64 -0
  20. data/lib/gimuby/genetic/archipelago/measure/diameter_measure.rb +38 -0
  21. data/lib/gimuby/genetic/archipelago/measure/measure.rb +7 -0
  22. data/lib/gimuby/genetic/archipelago/measure/shortest_paths_measure.rb +46 -0
  23. data/lib/gimuby/genetic/population/pick_strategy/bests_pick_strategy.rb +17 -0
  24. data/lib/gimuby/genetic/population/pick_strategy/pick_strategy.rb +21 -0
  25. data/lib/gimuby/genetic/population/pick_strategy/random_wheel_pick_strategy.rb +40 -0
  26. data/lib/gimuby/genetic/population/pick_strategy/tournament_pick_strategy.rb +26 -0
  27. data/lib/gimuby/genetic/population/population.rb +97 -0
  28. data/lib/gimuby/genetic/population/replace_strategy/replace_strategy.rb +9 -0
  29. data/lib/gimuby/genetic/population/replace_strategy/replace_worst_replace_strategy.rb +52 -0
  30. data/lib/gimuby/genetic/population/replace_strategy/uniform_replace_strategy.rb +48 -0
  31. data/lib/gimuby/genetic/solution/check_strategy/check_strategy.rb +8 -0
  32. data/lib/gimuby/genetic/solution/check_strategy/permutation_check_strategy.rb +37 -0
  33. data/lib/gimuby/genetic/solution/check_strategy/solution_space_check_strategy.rb +74 -0
  34. data/lib/gimuby/genetic/solution/function_based_solution.rb +64 -0
  35. data/lib/gimuby/genetic/solution/mutation_strategy/mutation_strategy.rb +22 -0
  36. data/lib/gimuby/genetic/solution/mutation_strategy/permutation_mutation_strategy.rb +17 -0
  37. data/lib/gimuby/genetic/solution/mutation_strategy/solution_space_mutation_strategy.rb +69 -0
  38. data/lib/gimuby/genetic/solution/new_generation_strategy/average_new_generation_strategy.rb +41 -0
  39. data/lib/gimuby/genetic/solution/new_generation_strategy/combined_new_generation_strategy.rb +27 -0
  40. data/lib/gimuby/genetic/solution/new_generation_strategy/cross_over_new_generation_strategy.rb +40 -0
  41. data/lib/gimuby/genetic/solution/new_generation_strategy/new_generation_strategy.rb +9 -0
  42. data/lib/gimuby/genetic/solution/new_generation_strategy/parent_range_new_generation_strategy.rb +42 -0
  43. data/lib/gimuby/genetic/solution/solution.rb +86 -0
  44. data/lib/gimuby/problem/foxholes/foxholes.rb +76 -0
  45. data/lib/gimuby/problem/foxholes/foxholes_solution.rb +29 -0
  46. data/lib/gimuby/problem/lennard_jones/lennard_jones.rb +38 -0
  47. data/lib/gimuby/problem/lennard_jones/lennard_jones_solution.rb +62 -0
  48. data/lib/gimuby/problem/rastrigin/rastrigin.rb +26 -0
  49. data/lib/gimuby/problem/rastrigin/rastrigin_solution.rb +35 -0
  50. data/lib/gimuby/problem/rosenbrock/rosenbrock.rb +14 -0
  51. data/lib/gimuby/problem/rosenbrock/rosenbrock_solution.rb +39 -0
  52. data/lib/gimuby/problem/schaffer/schaffer.rb +18 -0
  53. data/lib/gimuby/problem/schaffer/schaffer_solution.rb +29 -0
  54. data/lib/gimuby/problem/sphere/sphere.rb +9 -0
  55. data/lib/gimuby/problem/sphere/sphere_solution.rb +27 -0
  56. data/lib/gimuby/problem/step/step.rb +9 -0
  57. data/lib/gimuby/problem/step/step_solution.rb +29 -0
  58. data/lib/gimuby/problem/tsp/tsp.rb +76 -0
  59. data/lib/gimuby/problem/tsp/tsp_solution.rb +46 -0
  60. metadata +128 -0
@@ -0,0 +1,26 @@
1
+ # http://en.wikipedia.org/wiki/Rastrigin_function
2
+ class Rastrigin
3
+
4
+ # @param x_values [Array<Float>]
5
+ # @return Float
6
+ def evaluate(x_values)
7
+ a = get_a
8
+ value = a * x_values.length
9
+ x_values.each do |x_i|
10
+ cos_arg = 2.0 * Math::PI * x_i
11
+ sum_term_1 = (x_i ** 2.0)
12
+ sum_term_2 = a * Math::cos(cos_arg)
13
+ value += sum_term_1 - sum_term_2
14
+ end
15
+ value
16
+ end
17
+
18
+ protected
19
+
20
+ # A Rastrigin parameter
21
+ # @return Float
22
+ def get_a
23
+ 10.0
24
+ end
25
+
26
+ end
@@ -0,0 +1,35 @@
1
+ require 'gimuby/config'
2
+ require 'gimuby/problem/rastrigin/rastrigin'
3
+ require 'gimuby/genetic/solution/function_based_solution'
4
+ require 'gimuby/genetic/solution/check_strategy/solution_space_check_strategy'
5
+ require 'gimuby/genetic/solution/new_generation_strategy/combined_new_generation_strategy'
6
+ require 'gimuby/genetic/solution/new_generation_strategy/parent_range_new_generation_strategy'
7
+ require 'gimuby/genetic/solution/new_generation_strategy/cross_over_new_generation_strategy'
8
+ require 'gimuby/genetic/solution/new_generation_strategy/average_new_generation_strategy'
9
+ require 'gimuby/genetic/solution/mutation_strategy/solution_space_mutation_strategy'
10
+
11
+ class RastriginSolution < FunctionBasedSolution
12
+
13
+ def evaluate
14
+ get_rastrigin.evaluate(@x_values)
15
+ end
16
+
17
+ protected
18
+
19
+ def get_x_value_min
20
+ -5.12
21
+ end
22
+
23
+ def get_x_value_max
24
+ 5.12
25
+ end
26
+
27
+ def get_dimension_number
28
+ $config.rastrigin_dimension
29
+ end
30
+
31
+ def get_rastrigin
32
+ Rastrigin.new
33
+ end
34
+
35
+ end
@@ -0,0 +1,14 @@
1
+ # Rosenbrock function (banana / valley function)
2
+ # Optimal is at (1, 1)
3
+ # http://en.wikipedia.org/wiki/Rosenbrock_function
4
+ class Rosenbrock
5
+
6
+ def evaluate(values)
7
+ x = values.shift
8
+ y = values.shift
9
+ term_1 = (1 - x) ** 2
10
+ term_2 = 100 * (y - x ** 2) ** 2
11
+ term_1 + term_2
12
+ end
13
+
14
+ end
@@ -0,0 +1,39 @@
1
+ require 'gimuby/config'
2
+ require 'gimuby/problem/rosenbrock/rosenbrock'
3
+ require 'gimuby/genetic/solution/function_based_solution'
4
+ require 'gimuby/genetic/solution/check_strategy/solution_space_check_strategy'
5
+ require 'gimuby/genetic/solution/new_generation_strategy/combined_new_generation_strategy'
6
+ require 'gimuby/genetic/solution/new_generation_strategy/parent_range_new_generation_strategy'
7
+ require 'gimuby/genetic/solution/new_generation_strategy/cross_over_new_generation_strategy'
8
+ require 'gimuby/genetic/solution/new_generation_strategy/average_new_generation_strategy'
9
+ require 'gimuby/genetic/solution/mutation_strategy/solution_space_mutation_strategy'
10
+
11
+ class RosenbrockSolution < FunctionBasedSolution
12
+
13
+ def initialize(x_values = nil)
14
+ super(x_values)
15
+ end
16
+
17
+ def evaluate
18
+ get_rosenbrock.evaluate(@x_values.clone)
19
+ end
20
+
21
+ protected
22
+
23
+ def get_x_value_min
24
+ -2.0
25
+ end
26
+
27
+ def get_x_value_max
28
+ 2.0
29
+ end
30
+
31
+ def get_dimension_number
32
+ 2
33
+ end
34
+
35
+ def get_rosenbrock
36
+ Rosenbrock.new
37
+ end
38
+
39
+ end
@@ -0,0 +1,18 @@
1
+ # Schaffer F6 function
2
+ #
3
+ # http://zhanggw.wordpress.com/2010/09/25/optimization-schaffer-f6-function-using-basic-genetic-algorithm-2/
4
+ class Schaffer
5
+
6
+ def evaluate(values)
7
+ x_values = values.clone
8
+ x = x_values.shift
9
+ y = x_values.shift
10
+ sqrt_arg = x**2 + y**2
11
+ sin_arg = Math.sqrt(sqrt_arg)
12
+ numerator = Math.sin(sin_arg) ** 2 - 0.5
13
+ denominator_part = 0.001 * ( x ** 2 + y ** 2 )
14
+ denominator = (1 + denominator_part)**2
15
+ 0.5 + numerator/denominator
16
+ end
17
+
18
+ end
@@ -0,0 +1,29 @@
1
+ require 'gimuby/config'
2
+ require 'gimuby/problem/schaffer/schaffer'
3
+ require 'gimuby/genetic/solution/function_based_solution'
4
+
5
+ class SchafferSolution < FunctionBasedSolution
6
+
7
+ def evaluate
8
+ get_schaffer.evaluate(@x_values.clone)
9
+ end
10
+
11
+ protected
12
+
13
+ def get_x_value_min
14
+ -100
15
+ end
16
+
17
+ def get_x_value_max
18
+ 100
19
+ end
20
+
21
+ def get_dimension_number
22
+ 2
23
+ end
24
+
25
+ def get_schaffer
26
+ Schaffer.new
27
+ end
28
+
29
+ end
@@ -0,0 +1,9 @@
1
+ class Sphere
2
+ def evaluate(x_values)
3
+ sum = 0
4
+ x_values.each do |x_value|
5
+ sum += x_value ** 2
6
+ end
7
+ sum
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ require 'gimuby/problem/sphere/sphere'
2
+ require 'gimuby/genetic/solution/function_based_solution'
3
+
4
+ class SphereSolution < FunctionBasedSolution
5
+
6
+ def evaluate
7
+ get_sphere.evaluate(@x_values.clone)
8
+ end
9
+
10
+ protected
11
+
12
+ def get_x_value_min
13
+ -5.12
14
+ end
15
+
16
+ def get_x_value_max
17
+ 5.12
18
+ end
19
+
20
+ def get_dimension_number
21
+ 2
22
+ end
23
+
24
+ def get_sphere
25
+ Sphere.new
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ class Step
2
+ def evaluate(x_values)
3
+ sum = 0
4
+ x_values.each do |x_value|
5
+ sum += x_value.to_i
6
+ end
7
+ sum
8
+ end
9
+ end
@@ -0,0 +1,29 @@
1
+ require 'gimuby/config'
2
+ require 'gimuby/problem/step/step'
3
+ require 'gimuby/genetic/solution/function_based_solution'
4
+
5
+ class StepSolution < FunctionBasedSolution
6
+
7
+ def evaluate
8
+ get_step.evaluate(@x_values.clone)
9
+ end
10
+
11
+ protected
12
+
13
+ def get_x_value_min
14
+ -5.12
15
+ end
16
+
17
+ def get_x_value_max
18
+ 5.12
19
+ end
20
+
21
+ def get_dimension_number
22
+ 5
23
+ end
24
+
25
+ def get_step
26
+ Step.new
27
+ end
28
+
29
+ end
@@ -0,0 +1,76 @@
1
+ require 'gimuby/config'
2
+
3
+ # Implement a TSP problem
4
+ class Tsp
5
+
6
+ def initialize
7
+ ensure_distance_matrix
8
+ end
9
+
10
+ def get_number_of_points
11
+ @distance_matrix.length
12
+ end
13
+
14
+ def get_permutation_distance(permutation)
15
+ previous = permutation[-1]
16
+ distance = 0
17
+ permutation.each do |current|
18
+ marginal_distance = get_distance(previous, current)
19
+ distance += marginal_distance
20
+ previous = current
21
+ end
22
+ distance
23
+ end
24
+
25
+ def get_distance(from, to)
26
+ @distance_matrix[from][to]
27
+ end
28
+
29
+ protected
30
+
31
+ def ensure_distance_matrix
32
+ path = $config.persistence_dir_path + '/TSP_distances_' +
33
+ $config.tsp_number_points.to_s + '.data'
34
+ load_distance_matrix(path)
35
+ if @distance_matrix.nil?
36
+ init_distance_matrix()
37
+ persist_distance_matrix(path)
38
+ end
39
+ end
40
+
41
+ def load_distance_matrix(path)
42
+ if File::exists? path
43
+ f = File.new(path, 'r')
44
+ @distance_matrix = Marshal.load(f.read())
45
+ f.close()
46
+ end
47
+ end
48
+
49
+ def persist_distance_matrix(path)
50
+ f = File.new(path, 'w')
51
+ f.write(Marshal.dump(@distance_matrix))
52
+ f.close()
53
+ end
54
+
55
+ def init_distance_matrix
56
+ @distance_matrix = []
57
+ max_index = $config.tsp_number_points - 1
58
+ (0..max_index).each do |city_index1|
59
+ @distance_matrix[city_index1] = []
60
+ (0..max_index).each do |city_index2|
61
+ if city_index1 != city_index2
62
+ distance = get_random_distance
63
+ else
64
+ distance = 0
65
+ end
66
+ @distance_matrix[city_index1][city_index2] = distance
67
+ end
68
+ end
69
+ end
70
+
71
+ def get_random_distance
72
+ min_value = -10000
73
+ max_value = 10000
74
+ rand(max_value - min_value) + min_value
75
+ end
76
+ end
@@ -0,0 +1,46 @@
1
+ require 'gimuby/dependencies'
2
+ require 'gimuby/genetic/solution/solution'
3
+ require 'gimuby/genetic/solution/check_strategy/permutation_check_strategy'
4
+ require 'gimuby/genetic/solution/new_generation_strategy/cross_over_new_generation_strategy'
5
+ require 'gimuby/genetic/solution/mutation_strategy/permutation_mutation_strategy'
6
+
7
+ class TspSolution < Solution
8
+
9
+ def initialize(permutation = nil)
10
+ @check_strategy = PermutationCheckStrategy.new()
11
+ @new_generation_strategy = CrossOverNewGenerationStrategy.new()
12
+ @mutation_strategy = PermutationMutationStrategy.new()
13
+ super(permutation)
14
+ check
15
+ end
16
+
17
+ attr_accessor :permutation
18
+
19
+ def evaluate
20
+ get_tsp.get_permutation_distance(@permutation)
21
+ end
22
+
23
+ def get_solution_representation
24
+ @permutation.clone
25
+ end
26
+
27
+ def set_solution_representation(representation)
28
+ @permutation = representation.clone
29
+ end
30
+
31
+ protected
32
+
33
+ def init_representation
34
+ @permutation = get_not_randomized_permutation
35
+ @permutation = @permutation.shuffle
36
+ end
37
+
38
+ def get_not_randomized_permutation
39
+ _ = *(0..get_tsp.get_number_of_points - 1)
40
+ end
41
+
42
+ def get_tsp
43
+ $dependencies.tsp
44
+ end
45
+
46
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gimuby
3
+ version: !ruby/object:Gem::Version
4
+ hash: 7
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 7
9
+ - 2
10
+ version: 0.7.2
11
+ platform: ruby
12
+ authors:
13
+ - "Fr\xC3\xA4ntz Miccoli"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2014-03-30 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: |-
22
+ Implemented for academic purpose, Gimuby is also suitable for teaching purpose. As far as we know this implementation of genetic algorithms is the most advanced available in Ruby for itintegrates an implementation of the island model, reusable patterns for user problem composition and optimal configuration genetic algorithm
23
+ Gimuby contains the implementation of standard genetic algorithm (named population) and distributed genetic algorithm or island model (named archipelago).
24
+ The presented archipelago are NOT distributed (nor with threads, processes, or physical machines). However they let the user benefits ofthe leverage they represent to obtain a better solution with the same amount of resources spent.
25
+ Similar to: AI4R, gga4r and darwinning
26
+ email:
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - lib/gimuby/config.rb
35
+ - lib/gimuby/dependencies.rb
36
+ - lib/gimuby/event/event.rb
37
+ - lib/gimuby/event/event_manager.rb
38
+ - lib/gimuby/factory.rb
39
+ - lib/gimuby/genetic/archipelago/archipelago.rb
40
+ - lib/gimuby/genetic/archipelago/connect_strategy/barabasi_albert_connect_strategy.rb
41
+ - lib/gimuby/genetic/archipelago/connect_strategy/circle_connect_strategy.rb
42
+ - lib/gimuby/genetic/archipelago/connect_strategy/connect_strategy.rb
43
+ - lib/gimuby/genetic/archipelago/connect_strategy/constant_degree_connect_strategy.rb
44
+ - lib/gimuby/genetic/archipelago/connect_strategy/fully_connected_connect_strategy.rb
45
+ - lib/gimuby/genetic/archipelago/connect_strategy/random_connect_strategy.rb
46
+ - lib/gimuby/genetic/archipelago/connect_strategy/watts_strogatz_connect_strategy.rb
47
+ - lib/gimuby/genetic/archipelago/measure/clustering_coefficient_measure.rb
48
+ - lib/gimuby/genetic/archipelago/measure/connected_measure.rb
49
+ - lib/gimuby/genetic/archipelago/measure/diameter_measure.rb
50
+ - lib/gimuby/genetic/archipelago/measure/measure.rb
51
+ - lib/gimuby/genetic/archipelago/measure/shortest_paths_measure.rb
52
+ - lib/gimuby/genetic/population/pick_strategy/bests_pick_strategy.rb
53
+ - lib/gimuby/genetic/population/pick_strategy/pick_strategy.rb
54
+ - lib/gimuby/genetic/population/pick_strategy/random_wheel_pick_strategy.rb
55
+ - lib/gimuby/genetic/population/pick_strategy/tournament_pick_strategy.rb
56
+ - lib/gimuby/genetic/population/population.rb
57
+ - lib/gimuby/genetic/population/replace_strategy/replace_strategy.rb
58
+ - lib/gimuby/genetic/population/replace_strategy/replace_worst_replace_strategy.rb
59
+ - lib/gimuby/genetic/population/replace_strategy/uniform_replace_strategy.rb
60
+ - lib/gimuby/genetic/solution/check_strategy/check_strategy.rb
61
+ - lib/gimuby/genetic/solution/check_strategy/permutation_check_strategy.rb
62
+ - lib/gimuby/genetic/solution/check_strategy/solution_space_check_strategy.rb
63
+ - lib/gimuby/genetic/solution/function_based_solution.rb
64
+ - lib/gimuby/genetic/solution/mutation_strategy/mutation_strategy.rb
65
+ - lib/gimuby/genetic/solution/mutation_strategy/permutation_mutation_strategy.rb
66
+ - lib/gimuby/genetic/solution/mutation_strategy/solution_space_mutation_strategy.rb
67
+ - lib/gimuby/genetic/solution/new_generation_strategy/average_new_generation_strategy.rb
68
+ - lib/gimuby/genetic/solution/new_generation_strategy/combined_new_generation_strategy.rb
69
+ - lib/gimuby/genetic/solution/new_generation_strategy/cross_over_new_generation_strategy.rb
70
+ - lib/gimuby/genetic/solution/new_generation_strategy/new_generation_strategy.rb
71
+ - lib/gimuby/genetic/solution/new_generation_strategy/parent_range_new_generation_strategy.rb
72
+ - lib/gimuby/genetic/solution/solution.rb
73
+ - lib/gimuby/problem/foxholes/foxholes.rb
74
+ - lib/gimuby/problem/foxholes/foxholes_solution.rb
75
+ - lib/gimuby/problem/lennard_jones/lennard_jones.rb
76
+ - lib/gimuby/problem/lennard_jones/lennard_jones_solution.rb
77
+ - lib/gimuby/problem/rastrigin/rastrigin.rb
78
+ - lib/gimuby/problem/rastrigin/rastrigin_solution.rb
79
+ - lib/gimuby/problem/rosenbrock/rosenbrock.rb
80
+ - lib/gimuby/problem/rosenbrock/rosenbrock_solution.rb
81
+ - lib/gimuby/problem/schaffer/schaffer.rb
82
+ - lib/gimuby/problem/schaffer/schaffer_solution.rb
83
+ - lib/gimuby/problem/sphere/sphere.rb
84
+ - lib/gimuby/problem/sphere/sphere_solution.rb
85
+ - lib/gimuby/problem/step/step.rb
86
+ - lib/gimuby/problem/step/step_solution.rb
87
+ - lib/gimuby/problem/tsp/tsp.rb
88
+ - lib/gimuby/problem/tsp/tsp_solution.rb
89
+ - lib/gimuby.rb
90
+ - Gemfile
91
+ - LICENSE.md
92
+ - README.md
93
+ homepage: https://frantzmiccoli.github.io/Gimuby
94
+ licenses:
95
+ - MIT
96
+ post_install_message:
97
+ rdoc_options: []
98
+
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ~>
105
+ - !ruby/object:Gem::Version
106
+ hash: 31
107
+ segments:
108
+ - 1
109
+ - 8
110
+ version: "1.8"
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ hash: 3
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ requirements: []
121
+
122
+ rubyforge_project:
123
+ rubygems_version: 1.8.24
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: "Gimuby: genetic algorithm and island model for Ruby"
127
+ test_files: []
128
+