jinni 0.3.4 → 1.0.0
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/README.md +1 -45
- data/lib/jinni/creature.rb +10 -5
- data/lib/jinni/eigenclass.rb +9 -5
- data/lib/jinni/genepool.rb +10 -0
- data/lib/jinni/numeric.rb +3 -0
- data/lib/jinni/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 265cce6a9225304fcb2cf8aaff35746e70b21a9c
|
4
|
+
data.tar.gz: 1aafa6ab1f8c8a24532acc308dfaea9f9dcfbdc6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fac38fa29e93ea40c5f3ad75b7307e938dfbf6ecac0fdd5053acedbbdcc096e49075abd078d912b82073d0a377cdcb53cd3d454964bee437e7704546e2785679
|
7
|
+
data.tar.gz: e520b7da0664015a5a815d3ec49b5779cf556dc054398d2294690de2f9e12bc8ccb63d6479f14f777ac5aae94390f774a33820817dc7c55d998c73ce86474ce5
|
data/README.md
CHANGED
@@ -59,51 +59,7 @@ Or install it yourself as:
|
|
59
59
|
|
60
60
|
## API
|
61
61
|
|
62
|
-
|
63
|
-
|
64
|
-
Jinni adds the following public instance methods to your creatures:
|
65
|
-
|
66
|
-
#### fitness()
|
67
|
-
you should override this function with something sensible for your class. It returns `0.0` by default, but any float will do.
|
68
|
-
#### genes()
|
69
|
-
genes is a getter method that returns the genetic attributes available to your instance.
|
70
|
-
#### mutate(rate = 0.01)
|
71
|
-
mutate returns a slightly mutated object. Each bit in the original dna has a `rate` chance of flipping.
|
72
|
-
#### <<(object) / cross(object)
|
73
|
-
<< is the basic method used to cross two objects. It splits the dna strands of the input objects into random chunks, and then they randomly swap.
|
74
|
-
#### to_binary()
|
75
|
-
to_binary returns the binary dna strand that represents the object.
|
76
|
-
|
77
|
-
### creatures : class
|
78
|
-
|
79
|
-
Jinni adds the following public class methods to your creature class:
|
80
|
-
|
81
|
-
#### attr_genetic(:name, min, max)
|
82
|
-
use this method in your class to declare your genetic attributes.
|
83
|
-
#### random_new()
|
84
|
-
random_new initializes an object with attributes randomly assigned from within your declared range.
|
85
|
-
#### new_from_binary()
|
86
|
-
use this method to initialize an object from an arbitrary binary string.
|
87
|
-
|
88
|
-
### numeric
|
89
|
-
|
90
|
-
Jinni also monkeypatches the following methods into Numeric:
|
91
|
-
|
92
|
-
#### to_binary()
|
93
|
-
utility method to convert a numeric into a binary string.
|
94
|
-
#### bits()
|
95
|
-
utility method to return the number of bits that the binary representation of the number requires
|
96
|
-
|
97
|
-
### genepool
|
98
|
-
|
99
|
-
Jinni creates a class, Genepool which inherits from Array. Genepool has the following instance methods:
|
100
|
-
|
101
|
-
#### generate(n, mutationRate = 0.01, quality = :fitness)
|
102
|
-
use this method to create a new generation of `n` creatures based on a genepool. it uses weighted roulette wheel selection to simulate the effects of genetic fitness, then crosses the selected objects together.
|
103
|
-
#### roulette(n, quality = :fitness)
|
104
|
-
this utility method uses weighted roulette wheel selection to choose `n` objects from your gene pool influenced by fitness. It does not cross them.
|
105
|
-
#### average(quality = :fitness)
|
106
|
-
this method returns the mean of one quality through a collection of objects. It's very useful for watching your generations increase in fitness.
|
62
|
+
See the API documentation over on [rubydoc](http://www.rubydoc.info/gems/jinni/)
|
107
63
|
|
108
64
|
## Contributing
|
109
65
|
|
data/lib/jinni/creature.rb
CHANGED
@@ -7,17 +7,21 @@ module Jinni
|
|
7
7
|
# move this into the specific creature class, not the generic Creature
|
8
8
|
# @@genes = Hash.new
|
9
9
|
|
10
|
-
#
|
10
|
+
# you should override this function with something sensible for your class.
|
11
|
+
# It returns `0.0` by default, but any float will do.
|
11
12
|
def fitness
|
12
13
|
0.0
|
13
14
|
end
|
14
15
|
|
15
|
-
# getter
|
16
|
+
# genes is a getter method that returns a hash with the genetic attributes
|
17
|
+
# available to your instance as keys, and the size of their range of values
|
18
|
+
# as values
|
16
19
|
def genes
|
17
20
|
@@genes
|
18
21
|
end
|
19
22
|
|
20
|
-
#
|
23
|
+
# mutate returns a slightly mutated object. Each bit in the original dna has
|
24
|
+
# a `rate` chance of flipping.
|
21
25
|
def mutate(rate = 0.01)
|
22
26
|
binary = self.to_binary
|
23
27
|
newBinaryArray = binary.chars.map do |bit|
|
@@ -30,7 +34,8 @@ module Jinni
|
|
30
34
|
return self.class.new_from_binary newBinary
|
31
35
|
end
|
32
36
|
|
33
|
-
# method to cross two
|
37
|
+
# << is the basic method used to cross two objects. It splits the dna strands
|
38
|
+
# of the input objects into random chunks, and then they randomly swap.
|
34
39
|
# usage:
|
35
40
|
# child = bill << ted
|
36
41
|
def cross(object)
|
@@ -50,7 +55,7 @@ module Jinni
|
|
50
55
|
end
|
51
56
|
alias :<< :cross
|
52
57
|
|
53
|
-
#
|
58
|
+
# to_binary returns the binary dna strand that represents the given object.
|
54
59
|
def to_binary
|
55
60
|
self.class.genes.collect {|gene, range|
|
56
61
|
output = String.new
|
data/lib/jinni/eigenclass.rb
CHANGED
@@ -15,14 +15,17 @@ module Jinni
|
|
15
15
|
# @@schema_total = total
|
16
16
|
# end
|
17
17
|
|
18
|
-
#
|
19
|
-
|
18
|
+
# random_new initializes an object with attributes randomly
|
19
|
+
# assigned from within your declared range.
|
20
|
+
def new_randomly(*args, &block)
|
20
21
|
obj = allocate
|
21
22
|
obj.send(:initialize_randomly, *args, &block)
|
22
23
|
obj
|
23
24
|
end
|
25
|
+
alias :random_new :new_randomly
|
24
26
|
|
25
|
-
# to
|
27
|
+
# use this method to initialize an object from an arbitrary binary string.
|
28
|
+
# it's used internally by the cross method.
|
26
29
|
def new_from_binary(*args, &block)
|
27
30
|
obj = allocate
|
28
31
|
obj.send(:initialize_from_binary, *args, &block)
|
@@ -39,13 +42,14 @@ module Jinni
|
|
39
42
|
class_variable_set "@@genes", genes
|
40
43
|
end
|
41
44
|
|
42
|
-
#
|
45
|
+
# returns required length of a binary string to generate a creature of this class
|
43
46
|
def genetic_bits
|
44
47
|
genes.values.map {|range| range.bits}.reduce(:+)
|
45
48
|
end
|
46
49
|
|
47
50
|
|
48
|
-
# use
|
51
|
+
# use this method in your class to declare your genetic attributes.
|
52
|
+
# use it how you would attr_accessor
|
49
53
|
def attr_genetic( gene, min, max )
|
50
54
|
range = max - min + 1
|
51
55
|
|
data/lib/jinni/genepool.rb
CHANGED
@@ -1,4 +1,8 @@
|
|
1
1
|
class Jinni::Genepool < Array
|
2
|
+
|
3
|
+
# this utility method uses weighted roulette wheel selection to
|
4
|
+
# choose `n` objects from your gene pool influenced by fitness.
|
5
|
+
# It does not cross them.
|
2
6
|
def roulette(n, quality = :fitness)
|
3
7
|
scratch = self.clone
|
4
8
|
|
@@ -21,6 +25,10 @@ class Jinni::Genepool < Array
|
|
21
25
|
return selected
|
22
26
|
end
|
23
27
|
|
28
|
+
# use this method to create a new generation of `n` creatures based
|
29
|
+
# on a genepool. it uses weighted roulette wheel selection to simulate
|
30
|
+
# the effects of genetic fitness, then crosses the selected objects
|
31
|
+
# together.
|
24
32
|
def generate(n, mutationRate = 0.01, quality = :fitness)
|
25
33
|
scratch = self.clone
|
26
34
|
|
@@ -37,6 +45,8 @@ class Jinni::Genepool < Array
|
|
37
45
|
return generation
|
38
46
|
end
|
39
47
|
|
48
|
+
# this method returns the mean of one quality through a collection of objects.
|
49
|
+
# It's very useful for watching your generations increase in fitness.
|
40
50
|
def average(quality = :fitness)
|
41
51
|
self.map {|f| f.send(quality)}.inject{ |sum, n| sum + n }.to_f / self.length
|
42
52
|
end
|
data/lib/jinni/numeric.rb
CHANGED
data/lib/jinni/version.rb
CHANGED