ai-neat 0.1 → 0.2.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/README.md +93 -16
- data/Rakefile +1 -1
- data/lib/ai/neat/creature.rb +6 -6
- data/lib/ai/neat/method.rb +3 -3
- data/lib/ai/neat/version.rb +1 -1
- data/lib/ai/neat.rb +7 -7
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a08b3d42052a14c089e05cd8c5dbf0394957b7aa8f8595c997fe009a0b39dc9
|
4
|
+
data.tar.gz: 888d3b0ee6454df857526dfcfd2cf610c567958add930bfcdfc2950cc022df19
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13e4e964245318cae710b0541d44e62188babe6ea376427270ec06f433370097d632d7e0eef7146edd016c0e7a3f23036d70d03695f9be4846e584d213f8b28f
|
7
|
+
data.tar.gz: 48133e068c285e66edc206c3ee7323e8aadee455773e236e92fcbe49358cb648ab4f7466cbf1bdb1326e1569299614b90fb92eb7fdefca57f613cafd97555239
|
data/README.md
CHANGED
@@ -20,6 +20,83 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
+
1. Get started with config
|
24
|
+
|
25
|
+
```
|
26
|
+
config = {
|
27
|
+
models: [
|
28
|
+
{ node_count: 5, node_type: :input },
|
29
|
+
{ node_count: 3, node_type: :output, activationfunc: :softmax }
|
30
|
+
],
|
31
|
+
mutation_rate: 0.1,
|
32
|
+
crossover_method: :random,
|
33
|
+
mutation_method: :random,
|
34
|
+
population_size: population_size
|
35
|
+
}
|
36
|
+
```
|
37
|
+
|
38
|
+
`models` parameter are array of layers of NEAT at least their are needed 2 layers are `node_type` = `:input` and `:output` but it's possible to add any `:middle` layer between of them.
|
39
|
+
|
40
|
+
```
|
41
|
+
models: [
|
42
|
+
{ node_count: 5, node_type: :input },
|
43
|
+
{ node_count: 5, node_type: :inpu, activationfunc: :softmax },
|
44
|
+
{ node_count: 3, node_type: :output, activationfunc: :softmax }
|
45
|
+
]
|
46
|
+
```
|
47
|
+
|
48
|
+
And `activationfunc` are needed if that layer is not output layer. Current supported `activationfunc` are `:relu`, `:tanh`, `:sigmoid`, `:leaky_relu`, `:softmax`.
|
49
|
+
|
50
|
+
Supported `crossover_method` are `:random` and `:slice`.
|
51
|
+
|
52
|
+
Supported `mutation_method` are `:random` and `:edit`.
|
53
|
+
|
54
|
+
2. Initiate `Neat` Class from previous config
|
55
|
+
|
56
|
+
```
|
57
|
+
neat = Ai::Neat::Neat.new(config)
|
58
|
+
```
|
59
|
+
|
60
|
+
3. Set generation loop
|
61
|
+
|
62
|
+
4. Set inputs array equal to number of `node_type` = `:input` to each Creature `[i]`
|
63
|
+
|
64
|
+
```
|
65
|
+
neat.set_inputs(inputs, i)
|
66
|
+
```
|
67
|
+
|
68
|
+
If your simmulation have various creature life time like a gameover you can skip to set inputs that creature.
|
69
|
+
|
70
|
+
5. When set inputs compleated
|
71
|
+
|
72
|
+
```
|
73
|
+
neat.feed_forward
|
74
|
+
```
|
75
|
+
|
76
|
+
6. Get decisions for each creature in array
|
77
|
+
|
78
|
+
```
|
79
|
+
decisions = neat.decisions
|
80
|
+
```
|
81
|
+
|
82
|
+
returned `decisions` value are array of integer that started from `0` to `node_count - 1` of `:output` layer.
|
83
|
+
|
84
|
+
7. Finish generation
|
85
|
+
|
86
|
+
```
|
87
|
+
neat.do_gen
|
88
|
+
```
|
89
|
+
|
90
|
+
And you can get best creature result by
|
91
|
+
|
92
|
+
```
|
93
|
+
neat.best_creature
|
94
|
+
```
|
95
|
+
|
96
|
+
This function will return index of best creature in lastest generation.
|
97
|
+
|
98
|
+
## Example
|
99
|
+
|
23
100
|
```
|
24
101
|
population_size = 100
|
25
102
|
|
@@ -48,30 +125,30 @@ scores = population_size.times.map { 0 }
|
|
48
125
|
inputs = 5.times.map { rand(-1.0..1.0) }
|
49
126
|
|
50
127
|
(0..(scores.count - 1)).each do |i|
|
51
|
-
|
128
|
+
neat.set_inputs(inputs, i)
|
52
129
|
end
|
53
130
|
|
54
131
|
neat.feed_forward
|
55
132
|
decisions = neat.decisions
|
56
133
|
|
57
134
|
(0..(scores.count - 1)).each do |i|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
135
|
+
if inputs.last > 0
|
136
|
+
case decisions[i]
|
137
|
+
when 0
|
138
|
+
scores[i] += 1
|
139
|
+
when 1
|
140
|
+
scores[i] -= 1
|
141
|
+
end
|
142
|
+
else
|
143
|
+
case decisions[i]
|
144
|
+
when 0
|
145
|
+
scores[i] -= 1
|
146
|
+
when 1
|
147
|
+
scores[i] += 1
|
148
|
+
end
|
71
149
|
end
|
72
|
-
end
|
73
150
|
|
74
|
-
|
151
|
+
scores[i] = 0 if scores[i] < 0
|
75
152
|
end
|
76
153
|
end
|
77
154
|
|
data/Rakefile
CHANGED
data/lib/ai/neat/creature.rb
CHANGED
@@ -49,15 +49,15 @@ module Ai
|
|
49
49
|
|
50
50
|
def flatten_genes=(genes)
|
51
51
|
(0..(@network.layers.count - 2)).each do |i|
|
52
|
-
|
53
|
-
|
54
|
-
|
52
|
+
(0..@network.layers[i].nodes.count - 1).each do |w|
|
53
|
+
(0..@network.layers[i].nodes[w].weights.count - 1).each do |e|
|
54
|
+
@network.layers[i].nodes[w].weights[e] = genes.first
|
55
55
|
genes.shift
|
56
56
|
end
|
57
57
|
end
|
58
|
-
|
59
|
-
|
60
|
-
|
58
|
+
|
59
|
+
(0..@network.layers[i].bias.weights.count - 1).each do |w|
|
60
|
+
@network.layers[i].bias.weights[w] = genes.first
|
61
61
|
genes.shift
|
62
62
|
end
|
63
63
|
end
|
data/lib/ai/neat/method.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
module Ai
|
4
4
|
module Neat
|
5
5
|
def self.activationfunc(method, value)
|
6
|
-
case method
|
6
|
+
case method.to_sym
|
7
7
|
when :relu
|
8
8
|
value.positive? ? value : 0
|
9
9
|
when :tanh
|
@@ -29,7 +29,7 @@ module Ai
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def self.crossover(method, genes_x, genes_y)
|
32
|
-
case method
|
32
|
+
case method.to_sym
|
33
33
|
when :random
|
34
34
|
genes = []
|
35
35
|
|
@@ -55,7 +55,7 @@ module Ai
|
|
55
55
|
def self.mutate(method, genes, rate)
|
56
56
|
genes = genes.clone
|
57
57
|
|
58
|
-
case method
|
58
|
+
case method.to_sym
|
59
59
|
when :random
|
60
60
|
(0..(genes.count - 1)).each do |i|
|
61
61
|
genes[i] = rand(-1.0..1.0) if rand < rate
|
data/lib/ai/neat/version.rb
CHANGED
data/lib/ai/neat.rb
CHANGED
@@ -38,13 +38,13 @@ module Ai
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def crossover
|
41
|
-
|
41
|
+
(0..@population_size - 1).each do |i|
|
42
42
|
@old_creatures = @creatures.clone
|
43
43
|
parent_x = pick_creature
|
44
44
|
parent_y = pick_creature
|
45
45
|
|
46
46
|
genes = Ai::Neat.crossover(@crossover_method, parent_x.flatten_genes, parent_y.flatten_genes)
|
47
|
-
|
47
|
+
@creatures[i].flatten_genes = genes
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
@@ -98,10 +98,10 @@ module Ai
|
|
98
98
|
end
|
99
99
|
|
100
100
|
def mutate
|
101
|
-
|
102
|
-
genes =
|
101
|
+
(0..@population_size - 1).each do |i|
|
102
|
+
genes = @creatures[i].flatten_genes
|
103
103
|
genes = Ai::Neat.mutate(@mutation_method, genes, @mutation_rate)
|
104
|
-
|
104
|
+
@creatures[i].flatten_genes = genes
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
@@ -109,11 +109,11 @@ module Ai
|
|
109
109
|
sum = 0.0
|
110
110
|
|
111
111
|
@old_creatures.each do |creature|
|
112
|
-
sum += creature.score
|
112
|
+
sum += creature.score ** 2
|
113
113
|
end
|
114
114
|
|
115
115
|
@old_creatures.each do |creature|
|
116
|
-
creature.fitness = (creature.score
|
116
|
+
creature.fitness = (creature.score ** 2) / sum
|
117
117
|
end
|
118
118
|
|
119
119
|
index = 0
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ai-neat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sukrit Sunama
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-18 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: NeuroEvolution of Augmenting Topologies (NEAT) ruby gem.
|
14
14
|
email:
|
@@ -44,7 +44,7 @@ metadata:
|
|
44
44
|
homepage_uri: https://github.com/Sunama/ai-neat
|
45
45
|
source_code_uri: https://github.com/Sunama/ai-neat
|
46
46
|
changelog_uri: https://github.com/Sunama/ai-neat
|
47
|
-
post_install_message:
|
47
|
+
post_install_message:
|
48
48
|
rdoc_options: []
|
49
49
|
require_paths:
|
50
50
|
- lib
|
@@ -59,8 +59,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: '0'
|
61
61
|
requirements: []
|
62
|
-
rubygems_version: 3.
|
63
|
-
signing_key:
|
62
|
+
rubygems_version: 3.2.3
|
63
|
+
signing_key:
|
64
64
|
specification_version: 4
|
65
65
|
summary: NeuroEvolution of Augmenting Topologies (NEAT) ruby gem.
|
66
66
|
test_files: []
|