jinni 0.3.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1519546cd3391ee816b4549b277f97927fe38bcf
4
- data.tar.gz: 0d87d66bde2789bb322944cd475b32455c084130
3
+ metadata.gz: 265cce6a9225304fcb2cf8aaff35746e70b21a9c
4
+ data.tar.gz: 1aafa6ab1f8c8a24532acc308dfaea9f9dcfbdc6
5
5
  SHA512:
6
- metadata.gz: fd99d43d5632c90f9d4ad08afb65e645bfb5b13fd9106d7c2fdc565e29cc9a126ef5636190b242c25de4c73038147d2ab5ac30a6f938dff53a698a113227c808
7
- data.tar.gz: d9f480dc6a9e3985b2f5ef0eb75eabc50ee338997ded126a20f6e087762bcedfad1511766d308a14554e77ff2678e908fe60054b4be31c4b07ec816ddce27d5d
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
- ### creatures : instance
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
 
@@ -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
- # redefine this method in your class
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 for list of genes
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
- # method to return a possibly mutated version of a given object
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 creatures. returns a child
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
- # serialize object into binary, according to schema laid out in eigenclass
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
@@ -15,14 +15,17 @@ module Jinni
15
15
  # @@schema_total = total
16
16
  # end
17
17
 
18
- # to be used like `Klass.new()`
19
- def random_new(*args, &block)
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 be used by `cross`, `Klass.new_from_binary(binary_string)`
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
- # the required length of a binary string to generate a creature of this class
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 like attr_accessor
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
 
@@ -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
@@ -1,8 +1,11 @@
1
1
  class Numeric
2
+ # utility method to convert a numeric into a binary string.
2
3
  def to_binary
3
4
  to_s(2)
4
5
  end
5
6
 
7
+ # utility method to return the number of bits that the
8
+ # binary representation of the number requires
6
9
  def bits
7
10
  to_binary.length()
8
11
  end
@@ -1,3 +1,3 @@
1
1
  module Jinni
2
- VERSION = "0.3.4"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jinni
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Monks