genetic_algorithms 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +4 -4
- data/LICENSE +21 -0
- data/README.md +6 -1
- data/lib/genetic_algorithms/engine.rb +19 -10
- data/lib/genetic_algorithms/version.rb +1 -1
- data/spec/engine_spec.rb +56 -23
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60f0050bbf3c28bd27cef5ac06a2c0eb98483b1b
|
4
|
+
data.tar.gz: ab85e8c0d5f9067b8c4f16e5e51a7c85981a27fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 963b2e939d68c5f04492cc386e7eddb89d5de6b70316a4f115ee9a265dfbd05ef9f83789bd596df9e593849bbf891bd819b8da1836ca46c582b2f43424ea8208
|
7
|
+
data.tar.gz: 60ca4238fbbe2c9b139faeda9f20a259fa7386fedf062a35bff2f2d0ed416ca5cf63162bae3715b5681f48ec2b7a9a4d53682272507f179a1c665d6f85783975
|
data/Gemfile.lock
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
genetic_algorithms (0.0.
|
5
|
-
logging
|
4
|
+
genetic_algorithms (0.0.5)
|
5
|
+
logging (~> 1.8)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
@@ -33,5 +33,5 @@ PLATFORMS
|
|
33
33
|
|
34
34
|
DEPENDENCIES
|
35
35
|
genetic_algorithms!
|
36
|
-
rspec
|
37
|
-
simplecov
|
36
|
+
rspec (~> 2.14)
|
37
|
+
simplecov (~> 0.8)
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2012-2014 Alexander Vanadio
|
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
CHANGED
@@ -83,7 +83,12 @@ end
|
|
83
83
|
require 'genetic_algorithms'
|
84
84
|
include GeneticAlgorithms
|
85
85
|
|
86
|
-
Engine.
|
86
|
+
Engine.configure do |config|
|
87
|
+
config.population_size = 10
|
88
|
+
config.chromosome_length = 4
|
89
|
+
end
|
90
|
+
|
91
|
+
Engine.new.start(Knapsack::BEST_SCORE) do |chromosome|
|
87
92
|
score = Knapsack.new(chromosome).utilization
|
88
93
|
|
89
94
|
if score > Knapsack::BEST_SCORE
|
@@ -1,19 +1,28 @@
|
|
1
1
|
module GeneticAlgorithms
|
2
|
-
class Engine
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
2
|
+
class Engine
|
3
|
+
|
4
|
+
class << self
|
5
|
+
attr_accessor :population_size, :chromosome_length, :num_generations
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.configure(&block)
|
9
|
+
block.call(self) if block_given?
|
10
|
+
|
11
|
+
Engine.population_size = 10 unless Engine.population_size
|
12
|
+
Engine.chromosome_length = 10 unless Engine.chromosome_length
|
13
|
+
Engine.num_generations = 5 unless Engine.num_generations
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
Engine.configure
|
18
|
+
chromosomes = Population.random_chromosomes Engine.population_size, Engine.chromosome_length
|
19
|
+
@population = Population.new chromosomes
|
11
20
|
end
|
12
21
|
|
13
22
|
def start(best_possible_score, &fitness_function)
|
14
23
|
highest_score, best_gen = 0, nil
|
15
24
|
|
16
|
-
(0
|
25
|
+
(0...Engine.num_generations).inject(@population) do |newest_population, i|
|
17
26
|
next_gen = newest_population.evolve(&fitness_function)
|
18
27
|
|
19
28
|
if newest_population.highest_score > highest_score
|
data/spec/engine_spec.rb
CHANGED
@@ -2,20 +2,50 @@ require 'spec_helper'
|
|
2
2
|
include GeneticAlgorithms
|
3
3
|
|
4
4
|
describe Engine do
|
5
|
-
|
6
|
-
# TODO: DRY out this code
|
7
|
-
describe "#start" do
|
8
|
-
context "AllOffSample fitness function" do
|
9
5
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
6
|
+
BEST_SCORE = 10
|
7
|
+
|
8
|
+
describe '.configure' do
|
9
|
+
context 'when invoked to override default values' do
|
10
|
+
before(:all) do
|
11
|
+
Engine.configure do |config|
|
12
|
+
config.population_size = 23
|
13
|
+
config.chromosome_length = 15
|
14
|
+
config.num_generations = 7
|
16
15
|
end
|
17
|
-
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'sets the population size from a config block' do
|
19
|
+
Engine.population_size.should be 23
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'sets the chromosome length from a config block' do
|
23
|
+
Engine.chromosome_length.should be 15
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'sets the num generations from a config block' do
|
27
|
+
Engine.num_generations.should be 7
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'when invoked to set defaults for you' do
|
32
|
+
it 'it gives you a non-nil population size' do
|
33
|
+
Engine.population_size.should_not be nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'it gives you a non-nil chromosome length' do
|
37
|
+
Engine.chromosome_length.should_not be nil
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'it gives you a non-nil num generations' do
|
41
|
+
Engine.num_generations.should_not be nil
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#start" do
|
18
47
|
|
48
|
+
shared_examples "a fitness function" do
|
19
49
|
it "returns a hash" do
|
20
50
|
subject.is_a?(Hash).should == true
|
21
51
|
end
|
@@ -29,29 +59,32 @@ describe Engine do
|
|
29
59
|
end
|
30
60
|
end
|
31
61
|
|
62
|
+
context "AllOffSample fitness function" do
|
63
|
+
subject do
|
64
|
+
Engine.new.start(BEST_SCORE) do |chromosome|
|
65
|
+
chromosome.each_char.inject(0) do |accum, char|
|
66
|
+
accum += 1 if char == Chromosome::OFF
|
67
|
+
accum
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
it_should_behave_like "a fitness function"
|
73
|
+
end
|
74
|
+
|
32
75
|
context "AlternatingOnOffSample fitness function" do
|
33
76
|
|
34
77
|
subject do
|
35
|
-
Engine.new
|
78
|
+
Engine.new.start(BEST_SCORE) do |chromosome|
|
36
79
|
(0...chromosome.length).inject(0) do |accum, index|
|
37
80
|
accum += 1 if index % 2 == 0 and chromosome[index] == Chromosome::ON
|
38
81
|
accum += 1 if index % 2 == 1 and chromosome[index] == Chromosome::OFF
|
39
82
|
accum
|
40
83
|
end
|
41
84
|
end
|
42
|
-
end
|
43
|
-
|
44
|
-
it "returns a hash" do
|
45
|
-
subject.is_a?(Hash).should == true
|
46
85
|
end
|
47
86
|
|
48
|
-
|
49
|
-
subject.keys.first.is_a?(Chromosome).should == true
|
50
|
-
end
|
51
|
-
|
52
|
-
it "returns the best score as a Fixnum" do
|
53
|
-
subject.values.first.is_a?(Fixnum).should == true
|
54
|
-
end
|
87
|
+
it_should_behave_like "a fitness function"
|
55
88
|
end
|
56
89
|
end
|
57
90
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: genetic_algorithms
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Vanadio
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: logging
|
@@ -64,6 +64,7 @@ files:
|
|
64
64
|
- ".travis.yml"
|
65
65
|
- Gemfile
|
66
66
|
- Gemfile.lock
|
67
|
+
- LICENSE
|
67
68
|
- README.md
|
68
69
|
- genetic_algorithms.gemspec
|
69
70
|
- lib/genetic_algorithms.rb
|