jinni 0.3.1 → 0.3.2
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/.gitignore +1 -0
- data/Gemfile +0 -2
- data/jinni.gemspec +1 -0
- data/lib/jinni/creature.rb +11 -6
- data/lib/jinni/eigenclass.rb +23 -8
- data/lib/jinni/version.rb +1 -1
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a0f79f6c7ca99a3e844ec4c1d9d333d2cd1180af
|
4
|
+
data.tar.gz: 1a50257f304821f54c245f953d16954a4edbdda3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 813486f90cc727e5d87997593a799828158261c03694c4224c836998158a5eddc3e7a723e2d0eb8bcbb18c1a1191599009e6a38f7c4e05fbab76adacd753d9b1
|
7
|
+
data.tar.gz: 4f708c1c65d01878c8021e1157885d1a9c9e75056928147673d95d65172ddb184b85f5a7111258d1ea23f1c05f47ba14babd32384e121336c116b5ac8228c122
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/jinni.gemspec
CHANGED
data/lib/jinni/creature.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
module Jinni
|
2
|
+
# creature instacce methods
|
3
|
+
|
2
4
|
class Creature
|
3
5
|
attr_reader :genes
|
4
6
|
|
5
|
-
|
7
|
+
# move this into the specific creature class, not the generic Creature
|
8
|
+
# @@genes = Hash.new
|
6
9
|
|
7
10
|
# redefine this method in your class
|
8
11
|
def fitness
|
@@ -14,13 +17,14 @@ module Jinni
|
|
14
17
|
@@genes
|
15
18
|
end
|
16
19
|
|
20
|
+
# method to return a possibly mutated version of a given object
|
17
21
|
def mutate(rate = 0.01)
|
18
22
|
binary = self.to_binary
|
19
23
|
newBinary = binary.chars.map { |bit| bit == "0" ? "1" : "0" if rand < rate }
|
20
24
|
return self.class.new_from_binary newBinary
|
21
25
|
end
|
22
26
|
|
23
|
-
#
|
27
|
+
# method to cross two creatures. returns a child
|
24
28
|
# usage:
|
25
29
|
# child = bill << ted
|
26
30
|
def cross(object)
|
@@ -42,7 +46,7 @@ module Jinni
|
|
42
46
|
|
43
47
|
# serialize object into binary, according to schema laid out in eigenclass
|
44
48
|
def to_binary
|
45
|
-
|
49
|
+
self.class.genes.collect {|gene, range|
|
46
50
|
output = String.new
|
47
51
|
value = self.send(gene) - self.class.send("#{gene}_min")
|
48
52
|
difference = range.bits - value.bits
|
@@ -53,7 +57,7 @@ module Jinni
|
|
53
57
|
|
54
58
|
private
|
55
59
|
|
56
|
-
# generic initialize from hash, called by the
|
60
|
+
# generic initialize from hash, called by the other initializers
|
57
61
|
def initialize(hash)
|
58
62
|
hash.each_pair do |gene, value|
|
59
63
|
instance_variable_set( "@#{gene}", value )
|
@@ -63,7 +67,7 @@ module Jinni
|
|
63
67
|
# used internally by ::new_random, a la #initialize
|
64
68
|
def initialize_randomly(*args, &block)
|
65
69
|
params = Hash.new
|
66
|
-
|
70
|
+
self.class.genes.each_pair do |gene, range|
|
67
71
|
value = self.class.send("#{gene}_min") + rand(range)
|
68
72
|
params[gene] = value
|
69
73
|
end
|
@@ -76,10 +80,11 @@ module Jinni
|
|
76
80
|
initialize(hash)
|
77
81
|
end
|
78
82
|
|
83
|
+
# method to return a creature attributes hash from a given binary string
|
79
84
|
def hash_from_binary(binary)
|
80
85
|
params = Hash.new
|
81
86
|
start = 0
|
82
|
-
|
87
|
+
self.class.genes.each_pair do |gene, range|
|
83
88
|
binary_chunk = binary[start..(start = start + range.bits - 1)]
|
84
89
|
break if binary_chunk.class != String
|
85
90
|
offset = binary_chunk.to_i(2)
|
data/lib/jinni/eigenclass.rb
CHANGED
@@ -1,13 +1,19 @@
|
|
1
1
|
module Jinni
|
2
2
|
class Creature
|
3
|
-
# genetic eigenclass methods
|
4
|
-
class << self
|
5
3
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
def self.inherited(subclass)
|
5
|
+
subclass.class_variable_set("@@genes", Hash.new)
|
6
|
+
# create a class variable called @@genes in subclass
|
7
|
+
# create getter and setter methods for @@genes
|
8
|
+
end
|
9
|
+
|
10
|
+
# creature class methods
|
11
|
+
class << self
|
12
|
+
# # use after calling all attr_genetics
|
13
|
+
# def set_schema
|
14
|
+
# total = @@genes.values.reduce( :+ )
|
15
|
+
# @@schema_total = total
|
16
|
+
# end
|
11
17
|
|
12
18
|
# to be used like `Klass.new()`
|
13
19
|
def random_new(*args, &block)
|
@@ -23,12 +29,21 @@ module Jinni
|
|
23
29
|
obj
|
24
30
|
end
|
25
31
|
|
32
|
+
def genes
|
33
|
+
class_variable_get "@@genes"
|
34
|
+
end
|
35
|
+
|
36
|
+
def genes= genes
|
37
|
+
class_variable_set "@@genes", genes
|
38
|
+
end
|
39
|
+
|
26
40
|
|
27
41
|
# use like attr_accessor
|
28
42
|
def attr_genetic( gene, min, max )
|
29
43
|
range = max - min + 1
|
30
44
|
|
31
|
-
@@genes
|
45
|
+
# update @@genes within (Fish)
|
46
|
+
self.genes[gene] = range
|
32
47
|
|
33
48
|
# getter
|
34
49
|
define_method(gene) do
|
data/lib/jinni/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jinni
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Monks
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
description:
|
42
56
|
email:
|
43
57
|
- a@monks.co
|
@@ -54,6 +68,8 @@ files:
|
|
54
68
|
- bin/setup
|
55
69
|
- jinni.gemspec
|
56
70
|
- lib/jinni.rb
|
71
|
+
- lib/jinni/.eigenclass.rb.swp
|
72
|
+
- lib/jinni/.version.rb.swp
|
57
73
|
- lib/jinni/creature.rb
|
58
74
|
- lib/jinni/eigenclass.rb
|
59
75
|
- lib/jinni/genepool.rb
|