Module: Ai::Neat
- Defined in:
- lib/ai/neat.rb,
lib/ai/neat/node.rb,
lib/ai/neat/layer.rb,
lib/ai/neat/method.rb,
lib/ai/neat/network.rb,
lib/ai/neat/version.rb,
lib/ai/neat/creature.rb
Defined Under Namespace
Classes: Creature, Layer, Neat, Network, Node
Constant Summary
collapse
- VERSION =
"0.2.5"
Class Method Summary
collapse
Class Method Details
.activationfunc(method, value) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/ai/neat/method.rb', line 5
def self.activationfunc(method, value)
case method.to_sym
when :relu
value.positive? ? value : 0
when :tanh
Math.tanh(value)
when :sigmoid
(1 / (1 + Math.exp(-value)))
when :leaky_relu
value.positive? ? value : (value * 0.01)
when :softmax
sum = 0
result = []
value.each do |v|
sum += Math.exp(v)
end
value.each do |v|
result.push(Math.exp(v) / sum)
end
result
end
end
|
.crossover(method, genes_x, genes_y) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/ai/neat/method.rb', line 31
def self.crossover(method, genes_x, genes_y)
case method.to_sym
when :random
genes = []
(0..(genes_x.count - 1)).each do |i|
rand < 0.5 ? genes.push(genes_x[i]) : genes.push(genes_y[i])
end
genes
when :slice
start_index = (rand * genes_x.count).to_i
end_index = (rand * (genes_x.count - start_index + 2)).to_i + start_index + 1
cut_genes = genes_x[start_index..end_index]
genes = genes_y.clone
genes.slice!(start_index..end_index)
genes.concat(cut_genes)
genes
end
end
|
.mutate(method, genes, rate) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/ai/neat/method.rb', line 55
def self.mutate(method, genes, rate)
genes = genes.clone
case method.to_sym
when :random
(0..(genes.count - 1)).each do |i|
genes[i] = rand(-1.0..1.0) if rand < rate
end
when :edit
(0..(genes.count - 1)).each do |i|
genes[i] += rand(-0.5..0.5) if rand < rate
end
end
genes
end
|