Snakenet.rb now applies modifications to the weights in both directions (e.g. positive and negative), creating two new AIs.
Some checks reported errors
continuous-integration/drone/push Build encountered an error

This commit is contained in:
Fabian Schlenz 2019-10-18 17:04:44 +02:00
parent 1418d519d5
commit 38aa654c54

View File

@ -244,31 +244,45 @@ class AI
def evolve def evolve
w = @weights.dup w = @weights.dup
action = rand(5) action = rand(4)
if action==0 #swap #if action==0 #swap
i1 = rand(network_size()) # i1 = rand(network_size())
i2 = rand(network_size()) # i2 = rand(network_size())
temp = w[i1] # temp = w[i1]
w[i1] = w[i2] # w[i1] = w[i2]
w[i2] = temp # w[i2] = temp
elsif action==1 #change single value if action==0 #change single value
i = rand(network_size()) i = rand(network_size())
w[i] = rand() * 2 - 1.0 diff = rand() * 0.2 - 0.1
elsif action==2 #invert single value w2 = w.dup
w[i] += diff
w[i] = 1.0 if w[i]>1.0
w[i] = -1.0 if w[i]<-1.0
w2[i] -= diff
w2[i] = 1.0 if w2[i]>1.0
w2[i] = -1.0 if w2[i]<-1.0
return [AI.new(w), AI.new(w2)]
elsif action==1 #invert single value
i = rand(network_size()) i = rand(network_size())
w[i] *= -1.0 w[i] *= -1.0
elsif action==3 elsif action==2
(0...network_size()).each do |i| (0...network_size()).each do |i|
w[i] = rand() * 2 - 1.0 if rand(5)==0 w[i] = rand() * 2 - 1.0 if rand(5)==0
end end
else #change multiple values else #change multiple values
w2 = w.dup
(0...network_size()).each do |i| (0...network_size()).each do |i|
if (rand(5)==0) if (rand(5)==0)
w[i] += rand() / 5.0 - 0.1 diff = rand() * 0.2 - 0.1
w[i] += diff
w[i] = 1.0 if w[i]>1.0 w[i] = 1.0 if w[i]>1.0
w[i] = -1.0 if w[i]<-1.0 w[i] = -1.0 if w[i]<-1.0
w2[i] -= diff
w2[i] = 1.0 if w2[i]>1.0
w2[i] = -1.0 if w2[i]<-1.0
end end
end end
return [AI.new(w), AI.new(w2)]
end end
return AI.new(w) return AI.new(w)
@ -389,6 +403,7 @@ loop do
10.times do 10.times do
ais << AI.new ais << AI.new
end end
ais = ais.flatten
round+=1 round+=1
end end
rescue SystemExit, Interrupt rescue SystemExit, Interrupt