mhl 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module MHL
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -19,10 +19,10 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_dependency 'bitstring'
22
- spec.add_dependency 'concurrent-ruby'
22
+ spec.add_dependency 'concurrent-ruby', '~> 0.5'
23
23
  spec.add_dependency 'erv'
24
+ spec.add_dependency 'facter'
24
25
 
25
26
  spec.add_development_dependency 'bundler', '~> 1.3'
26
27
  spec.add_development_dependency 'rake'
27
- spec.add_development_dependency 'rspec'
28
28
  end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ describe MHL::GeneticAlgorithmSolver do
4
+
5
+ it 'should accept bitstring representation genotypes' do
6
+ MHL::GeneticAlgorithmSolver.new(
7
+ :population_size => 128,
8
+ :genotype_space_type => :bitstring,
9
+ :mutation_threshold => 0.5,
10
+ :recombination_threshold => 0.5,
11
+ :genotype_space_conf => {
12
+ :bitstring_length => 120,
13
+ },
14
+ :logger => :stderr,
15
+ :log_level => ENV['DEBUG'] ? Logger::DEBUG : Logger::WARN,
16
+ )
17
+ end
18
+
19
+ it 'should accept integer representation genotypes' do
20
+ MHL::GeneticAlgorithmSolver.new(
21
+ :population_size => 128,
22
+ :genotype_space_type => :integer,
23
+ :mutation_probability => 0.5,
24
+ :recombination_probability => 0.5,
25
+ :genotype_space_conf => {
26
+ :dimensions => 6,
27
+ :recombination_type => :intermediate,
28
+ },
29
+ :logger => :stderr,
30
+ :log_level => ENV['DEBUG'] ? Logger::DEBUG : Logger::WARN,
31
+ )
32
+ end
33
+
34
+ it 'should solve a 2-dimension parabola in integer space' do
35
+ solver = MHL::GeneticAlgorithmSolver.new(
36
+ :population_size => 40,
37
+ :genotype_space_type => :integer,
38
+ :mutation_probability => 0.5,
39
+ :recombination_probability => 0.5,
40
+ :genotype_space_conf => {
41
+ :dimensions => 2,
42
+ :recombination_type => :intermediate,
43
+ :random_func => lambda { Array.new(2) { rand(20) } }
44
+ },
45
+ :exit_condition => lambda {|generation,best_sample| best_sample[:fitness] == 0},
46
+ :logger => :stderr,
47
+ :log_level => ENV['DEBUG'] ? Logger::DEBUG : Logger::WARN,
48
+ )
49
+ solver.solve(Proc.new{|genotype| -(genotype[0]**2 + genotype[1]**2) })
50
+ end
51
+
52
+ end
@@ -0,0 +1,77 @@
1
+ require 'test_helper'
2
+
3
+ describe MHL::IntegerVectorGenotypeSpace do
4
+ let :logger do
5
+ l = Logger.new(STDOUT)
6
+ l.level = ENV['DEBUG'] ? Logger::DEBUG : Logger::WARN
7
+ l
8
+ end
9
+
10
+ it 'should refuse to work with non-positive dimensions' do
11
+ assert_raises(ArgumentError) do
12
+ MHL::IntegerVectorGenotypeSpace.new(
13
+ :dimensions => -rand(100),
14
+ :recombination_type => :intermediate,
15
+ :logger => logger,
16
+ # :random_func => lambda { Array.new(2) { rand(20) } }
17
+ )
18
+ end
19
+ end
20
+
21
+ it 'should refuse to work with non- line or intermediate recombination' do
22
+ assert_raises(ArgumentError) do
23
+ MHL::IntegerVectorGenotypeSpace.new(
24
+ :dimensions => 2,
25
+ :recombination_type => :something,
26
+ :logger => logger,
27
+ )
28
+ end
29
+ end
30
+
31
+ describe 'with constraints' do
32
+ it 'should enforce constraints on generation' do
33
+ x1 = rand(100); x2 = x1 + rand(200)
34
+ y1 = -rand(100); y2 = y1 + rand(200)
35
+ is = MHL::IntegerVectorGenotypeSpace.new(
36
+ :dimensions => 2,
37
+ :recombination_type => :intermediate,
38
+ :constraints => [ { :from => x1, :to => x2 },
39
+ { :from => y1, :to => y2 } ],
40
+ :logger => logger,
41
+ )
42
+ genotype = is.get_random
43
+ genotype.size.must_equal 2
44
+ genotype[0].must_be :>=, x1
45
+ genotype[0].must_be :<=, x2
46
+ genotype[1].must_be :>=, y1
47
+ genotype[1].must_be :<=, y2
48
+ end
49
+
50
+ it 'should enforce constraints on reproduction' do
51
+ x1 = rand(100); x2 = x1 + rand(200)
52
+ y1 = -rand(100); y2 = y1 + rand(200)
53
+ is = MHL::IntegerVectorGenotypeSpace.new(
54
+ :dimensions => 2,
55
+ :recombination_type => :intermediate,
56
+ :constraints => [ { :from => x1, :to => x2 },
57
+ { :from => y1, :to => y2 } ],
58
+ :logger => logger,
59
+ )
60
+ g1 = { :genotype => [ x1, y1 ] }
61
+ g2 = { :genotype => [ x2, y2 ] }
62
+ a, b = is.reproduce_from(
63
+ g1, g2,
64
+ ERV::RandomVariable.new(:distribution => :geometric,
65
+ :probability_of_success => 0.05),
66
+ ERV::RandomVariable.new(:distribution => :uniform,
67
+ :min_value => -0.25,
68
+ :max_value => 1.25)
69
+ )
70
+ a[:genotype][0].must_be :>=, x1
71
+ a[:genotype][0].must_be :<=, x2
72
+ b[:genotype][1].must_be :>=, y1
73
+ b[:genotype][1].must_be :<=, y2
74
+ end
75
+ end
76
+
77
+ end
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+
3
+ describe MHL::MultiSwarmQPSOSolver do
4
+
5
+ it 'should solve a 2-dimension parabola in real space' do
6
+ solver = MHL::MultiSwarmQPSOSolver.new(
7
+ :swarm_size => 20,
8
+ :num_swarms => 4,
9
+ :random_position_func => lambda { Array.new(2) { rand(20).to_f } },
10
+ :random_velocity_func => lambda { Array.new(2) { rand(10).to_f } },
11
+ :exit_condition => lambda {|generation,best_sample| best_sample[:height].abs < 0.001 },
12
+ :logger => :stdout,
13
+ :log_level => ENV['DEBUG'] ? Logger::DEBUG : Logger::WARN,
14
+ )
15
+ solver.solve(Proc.new{|position| -(position[0]**2 + position[1]**2) })
16
+ end
17
+
18
+ end
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require 'test_helper'
2
2
 
3
3
  describe MHL::ParticleSwarmOptimizationSolver do
4
4
 
@@ -8,9 +8,10 @@ describe MHL::ParticleSwarmOptimizationSolver do
8
8
  :random_position_func => lambda { Array.new(2) { rand(20) } },
9
9
  :random_velocity_func => lambda { Array.new(2) { rand(10) } },
10
10
  :exit_condition => lambda {|generation,best_sample| best_sample[:height].abs < 0.001 },
11
+ :logger => :stderr,
12
+ :log_level => ENV['DEBUG'] ? Logger::DEBUG : Logger::WARN,
11
13
  )
12
14
  solver.solve(Proc.new{|position| -(position[0]**2 + position[1]**2) })
13
15
  end
14
16
 
15
17
  end
16
-
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ describe MHL::QuantumPSOSolver do
4
+
5
+ it 'should solve a 2-dimension parabola in real space' do
6
+ solver = MHL::QuantumPSOSolver.new(
7
+ :swarm_size => 40,
8
+ :random_position_func => lambda { Array.new(2) { rand(20) } },
9
+ :exit_condition => lambda {|generation,best_sample| best_sample[:height].abs < 0.001 },
10
+ :logger => :stderr,
11
+ :log_level => ENV['DEBUG'] ? Logger::DEBUG : Logger::WARN,
12
+ )
13
+ solver.solve(Proc.new{|position| -(position[0]**2 + position[1]**2) })
14
+ end
15
+
16
+ end
@@ -0,0 +1,6 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/spec'
3
+
4
+ require 'logger'
5
+
6
+ require 'mhl'
metadata CHANGED
@@ -1,99 +1,99 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mhl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mauro Tortonesi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-06 00:00:00.000000000 Z
11
+ date: 2014-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - '>='
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
14
19
  name: bitstring
20
+ prerelease: false
21
+ type: :runtime
15
22
  version_requirements: !ruby/object:Gem::Requirement
16
23
  requirements:
17
24
  - - '>='
18
25
  - !ruby/object:Gem::Version
19
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
20
28
  requirement: !ruby/object:Gem::Requirement
21
29
  requirements:
22
- - - '>='
30
+ - - ~>
23
31
  - !ruby/object:Gem::Version
24
- version: '0'
32
+ version: '0.5'
33
+ name: concurrent-ruby
25
34
  prerelease: false
26
35
  type: :runtime
27
- - !ruby/object:Gem::Dependency
28
- name: concurrent-ruby
29
36
  version_requirements: !ruby/object:Gem::Requirement
30
37
  requirements:
31
- - - '>='
38
+ - - ~>
32
39
  - !ruby/object:Gem::Version
33
- version: '0'
40
+ version: '0.5'
41
+ - !ruby/object:Gem::Dependency
34
42
  requirement: !ruby/object:Gem::Requirement
35
43
  requirements:
36
44
  - - '>='
37
45
  - !ruby/object:Gem::Version
38
46
  version: '0'
47
+ name: erv
39
48
  prerelease: false
40
49
  type: :runtime
41
- - !ruby/object:Gem::Dependency
42
- name: erv
43
50
  version_requirements: !ruby/object:Gem::Requirement
44
51
  requirements:
45
52
  - - '>='
46
53
  - !ruby/object:Gem::Version
47
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
48
56
  requirement: !ruby/object:Gem::Requirement
49
57
  requirements:
50
58
  - - '>='
51
59
  - !ruby/object:Gem::Version
52
60
  version: '0'
61
+ name: facter
53
62
  prerelease: false
54
63
  type: :runtime
55
- - !ruby/object:Gem::Dependency
56
- name: bundler
57
64
  version_requirements: !ruby/object:Gem::Requirement
58
65
  requirements:
59
- - - ~>
66
+ - - '>='
60
67
  - !ruby/object:Gem::Version
61
- version: '1.3'
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
62
70
  requirement: !ruby/object:Gem::Requirement
63
71
  requirements:
64
72
  - - ~>
65
73
  - !ruby/object:Gem::Version
66
74
  version: '1.3'
75
+ name: bundler
67
76
  prerelease: false
68
77
  type: :development
69
- - !ruby/object:Gem::Dependency
70
- name: rake
71
78
  version_requirements: !ruby/object:Gem::Requirement
72
79
  requirements:
73
- - - '>='
80
+ - - ~>
74
81
  - !ruby/object:Gem::Version
75
- version: '0'
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
76
84
  requirement: !ruby/object:Gem::Requirement
77
85
  requirements:
78
86
  - - '>='
79
87
  - !ruby/object:Gem::Version
80
88
  version: '0'
89
+ name: rake
81
90
  prerelease: false
82
91
  type: :development
83
- - !ruby/object:Gem::Dependency
84
- name: rspec
85
92
  version_requirements: !ruby/object:Gem::Requirement
86
93
  requirements:
87
94
  - - '>='
88
95
  - !ruby/object:Gem::Version
89
96
  version: '0'
90
- requirement: !ruby/object:Gem::Requirement
91
- requirements:
92
- - - '>='
93
- - !ruby/object:Gem::Version
94
- version: '0'
95
- prerelease: false
96
- type: :development
97
97
  description: A Ruby Metaheuristics library
98
98
  email:
99
99
  - mauro.tortonesi@unife.it
@@ -107,14 +107,26 @@ files:
107
107
  - Rakefile
108
108
  - lib/mhl.rb
109
109
  - lib/mhl/bitstring_genotype_space.rb
110
+ - lib/mhl/charged_swarm.rb
111
+ - lib/mhl/generic_particle.rb
112
+ - lib/mhl/generic_swarm.rb
110
113
  - lib/mhl/genetic_algorithm_solver.rb
111
114
  - lib/mhl/integer_genotype_space.rb
115
+ - lib/mhl/multiswarm_qpso_solver.rb
116
+ - lib/mhl/particle.rb
112
117
  - lib/mhl/particle_swarm_optimization_solver.rb
118
+ - lib/mhl/pso_swarm.rb
119
+ - lib/mhl/qpso_swarm.rb
120
+ - lib/mhl/quantum_particle.rb
121
+ - lib/mhl/quantum_particle_swarm_optimization_solver.rb
113
122
  - lib/mhl/version.rb
114
123
  - mhl.gemspec
115
- - spec/mhl/genetic_algorithm_spec.rb
116
- - spec/mhl/particle_swarm_optimization_solver_spec.rb
117
- - spec/spec_helper.rb
124
+ - test/mhl/genetic_algorithm_solver_test.rb
125
+ - test/mhl/integer_genotype_space_test.rb
126
+ - test/mhl/multiswarm_qpso_solver_test.rb
127
+ - test/mhl/particle_swarm_optimization_solver_test.rb
128
+ - test/mhl/quantum_particle_swarm_optimization_solver_test.rb
129
+ - test/test_helper.rb
118
130
  homepage: https://github.com/mtortonesi/ruby-mhl
119
131
  licenses:
120
132
  - MIT
@@ -135,11 +147,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
147
  version: '0'
136
148
  requirements: []
137
149
  rubyforge_project:
138
- rubygems_version: 2.2.1
150
+ rubygems_version: 2.1.9
139
151
  signing_key:
140
152
  specification_version: 4
141
153
  summary: A scientific library for Ruby that provides several metaheuristics
142
154
  test_files:
143
- - spec/mhl/genetic_algorithm_spec.rb
144
- - spec/mhl/particle_swarm_optimization_solver_spec.rb
145
- - spec/spec_helper.rb
155
+ - test/mhl/genetic_algorithm_solver_test.rb
156
+ - test/mhl/integer_genotype_space_test.rb
157
+ - test/mhl/multiswarm_qpso_solver_test.rb
158
+ - test/mhl/particle_swarm_optimization_solver_test.rb
159
+ - test/mhl/quantum_particle_swarm_optimization_solver_test.rb
160
+ - test/test_helper.rb
@@ -1,53 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe MHL::GeneticAlgorithmSolver do
4
-
5
- it 'should accept bitstring representation genotypes' do
6
- lambda {
7
- MHL::GeneticAlgorithmSolver.new(
8
- :population_size => 128,
9
- :genotype_space_type => :bitstring,
10
- :mutation_threshold => 0.5,
11
- :recombination_threshold => 0.5,
12
- :genotype_space_conf => {
13
- :bitstring_length => 120,
14
- }
15
- )
16
- }.should_not raise_error
17
- end
18
-
19
- it 'should accept integer representation genotypes' do
20
- lambda {
21
- MHL::GeneticAlgorithmSolver.new(
22
- :population_size => 128,
23
- :genotype_space_type => :integer,
24
- :mutation_probability => 0.5,
25
- :recombination_probability => 0.5,
26
- :genotype_space_conf => {
27
- :dimensions => 6,
28
- :recombination_type => :intermediate,
29
- }
30
- )
31
- }.should_not raise_error
32
- end
33
-
34
- it 'should solve a 2-dimension parabola in integer space' do
35
- solver = MHL::GeneticAlgorithmSolver.new(
36
- :population_size => 40,
37
- :genotype_space_type => :integer,
38
- :mutation_probability => 0.5,
39
- :recombination_probability => 0.5,
40
- :genotype_space_conf => {
41
- :dimensions => 2,
42
- :recombination_type => :intermediate,
43
- :random_func => lambda { Array.new(2) { rand(20) } }
44
- },
45
- :exit_condition => lambda {|generation,best_sample| best_sample[:fitness] == 0}
46
- )
47
- solver.solve(Proc.new{|genotype| -(genotype[0]**2 + genotype[1]**2) })
48
- end
49
-
50
- # it 'should solve a 2-dimension parabola in real space'
51
-
52
- end
53
-
@@ -1,19 +0,0 @@
1
- # This file was generated by the `rspec --init` command. Conventionally, all
2
- # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
- # Require this file using `require "spec_helper"` to ensure that it is only
4
- # loaded once.
5
-
6
- require 'mhl'
7
-
8
- # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
- RSpec.configure do |config|
10
- config.treat_symbols_as_metadata_keys_with_true_values = true
11
- config.run_all_when_everything_filtered = true
12
- config.filter_run :focus
13
-
14
- # Run specs in random order to surface order dependencies. If you find an
15
- # order dependency and want to debug it, you can fix the order by providing
16
- # the seed, which is printed after each run.
17
- # --seed 1234
18
- config.order = 'random'
19
- end