gemetics 0.0.1
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 +7 -0
- data/lib/gemetics.rb +111 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 86fcaddbfc978710844aa2d936c5bd029d540d3a
|
4
|
+
data.tar.gz: 725b5e5a24345f6a9bcaa71614592afdb121b17b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6b64516f052a9aed2126fc3ddd9e2179e1c03f3ea717d1d30a1fe5fe2fdfbffefb27c8cf3134a4de821a69f0ed521e14e42bd7c11b4f746bfb5d679a8b0726e8
|
7
|
+
data.tar.gz: 41295cc8f858d002b00e2bf09bf5f0b2f4038374d0bdf2d2d2958b3994d6507824a81bf4a47582a31825dcaffadf85a8978b2d6c8af6f7a01c8b2977af02d889
|
data/lib/gemetics.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
def default_GA_options()
|
2
|
+
return {
|
3
|
+
greaterBetter: true,
|
4
|
+
totalPopReplace: true,
|
5
|
+
genMax: 1000,
|
6
|
+
selectionStyle: 'tournament',
|
7
|
+
mutation_percent: 0.05,
|
8
|
+
debug: false,
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
def runAlgorithm(initialPopulation, eval, threshold, options)
|
13
|
+
# make sure options is assigned
|
14
|
+
if(options == nil)
|
15
|
+
options = default_GA_options
|
16
|
+
end
|
17
|
+
currentGen = 0
|
18
|
+
bestCanidate = initialPopulation[0]
|
19
|
+
population = initialPopulation
|
20
|
+
while(!exceedThreshold(options[:greaterBetter], bestCanidate.fitness, threshold) && currentGen < options[:genMax]) do
|
21
|
+
if(options[:debug])
|
22
|
+
puts bestCanidate.inspect
|
23
|
+
puts currentGen
|
24
|
+
end
|
25
|
+
# evaluate the population
|
26
|
+
population = eval.call(population)
|
27
|
+
if(options[:greaterBetter])
|
28
|
+
sortedPopulation = population.sort{ |x , y| y.fitness <=> x.fitness }
|
29
|
+
else
|
30
|
+
sortedPopulation = population.sort{ |x , y| x.fitness <=> y.fitness }
|
31
|
+
end
|
32
|
+
bestCanidate = population[0].clone
|
33
|
+
|
34
|
+
|
35
|
+
if(options[:totalPopReplace] == false)
|
36
|
+
# Do not replace every organism
|
37
|
+
mates = selection(sortedPopulation.clone(), options[:selectionStyle])
|
38
|
+
|
39
|
+
# mate and replace
|
40
|
+
results = mates[0].mate(mates[1])
|
41
|
+
replace = Array.new(2, Random.new.rand(population.size()))
|
42
|
+
# don't replace same org
|
43
|
+
replace[1] = Random.new.rand(population.size())while(replace[1] == replace[0])
|
44
|
+
population[replace[0]] = results[0]
|
45
|
+
population[replace[1]] = results[1]
|
46
|
+
else
|
47
|
+
# Repalce every single organism
|
48
|
+
needed = population.size()
|
49
|
+
have = 0
|
50
|
+
newPopulation = Array.new(population.size(), GeneticObject.new)
|
51
|
+
while have < needed do
|
52
|
+
mates = selection(sortedPopulation.clone(), options[:selectionStyle])
|
53
|
+
|
54
|
+
# mate and put them into new pop
|
55
|
+
results = mates[0].mate(mates[1])
|
56
|
+
results[0].mutate() if Random.new.rand() < options[:mutation_percent]
|
57
|
+
results[1].mutate() if Random.new.rand() < options[:mutation_percent]
|
58
|
+
newPopulation[have] = results[0]
|
59
|
+
newPopulation[have+1] = results[1] if (have+1) < needed
|
60
|
+
have += 2
|
61
|
+
end
|
62
|
+
population = newPopulation
|
63
|
+
end
|
64
|
+
currentGen += 1
|
65
|
+
end
|
66
|
+
return bestCanidate
|
67
|
+
end
|
68
|
+
|
69
|
+
def exceedThreshold(greaterBetter, val, threshold)
|
70
|
+
if(val == nil)
|
71
|
+
return false
|
72
|
+
end
|
73
|
+
if(greaterBetter)
|
74
|
+
return val>=threshold
|
75
|
+
else
|
76
|
+
return val<=threshold
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def selection(population, type)
|
81
|
+
# select mates
|
82
|
+
if(type == 'tournament')
|
83
|
+
return tournamentSelection(population)
|
84
|
+
else
|
85
|
+
return bestSelection(population)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def tournamentSelection(population)
|
90
|
+
population = population.shuffle
|
91
|
+
return population[0..10].sort{ |x , y| x.fitness <=> y.fitness }[0..1]
|
92
|
+
end
|
93
|
+
|
94
|
+
def bestSelection(population)
|
95
|
+
return population[0..1]
|
96
|
+
end
|
97
|
+
|
98
|
+
class GeneticObject
|
99
|
+
attr_accessor :fitness
|
100
|
+
|
101
|
+
def initialize()
|
102
|
+
end
|
103
|
+
|
104
|
+
def mutate()
|
105
|
+
raise 'Method Not Implemented'
|
106
|
+
end
|
107
|
+
|
108
|
+
def mate(other)
|
109
|
+
raise 'Meothd Not Implemented'
|
110
|
+
end
|
111
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gemetics
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Huelsman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple gem to help in the creation of genetic algorithms
|
14
|
+
email: michael.huelsman@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/gemetics.rb
|
20
|
+
homepage: https://github.com/xLeachimx/Gemetics
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.2.2
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: A genetic algorithm base
|
44
|
+
test_files: []
|