下のコードは間違いなく動く。ちなみにノートPCでもスマホでも、なんでもいいから、今見ているディスプレイを少し移動させて見て。
ほら、動いたでしょ。
だが少しの改良点もしくは問題点をはらんでいる可能性がある。
その吟味は後ですることにして、とりあえず簡単な説明を。
1)赤、黄、青をRGBで構成、たとえば赤では(100,0,0)
2)止まれと進めのそれぞれの入力の重みを-10から10までの整数でアトランダムにRs、Rg、Gs、Gg、Bs,Bg
3)それぞれの重みの合計が1以上であれば、止まれ(stop)、もしくは進め(go)をPositive False で認識
4)赤と黄で
stop(P) go(P) と認識されればアトランダムにRg,Gg,Bgを‐1
stop(P) go(F) と認識されればなにもしない
stop(F) go(P) と認識されればアトランダムにRs,Gs,Bsを +1,Rg,Gg,Bgを‐1
stop(F) go(F) と認識されればアトランダムにRs,Gs,Bsを +1
青も動揺の逆の操作
5)すべてのRGBで赤、黄が止まれ、青が進めの重みが得られたところで終了
import random
import numpy as np
colors = {"red": (100, 0, 0), "yellow": (100, 100, 0), "blue": (0, 100, 50)}
stop_synapses = {}
go_synapses = {}
stop_synapses = {k: random.randint(-10, 10) for k in ["Rs", "Gs", "Bs"]}
go_synapses = {k: random.randint(-10, 10) for k in ["Rg", "Gg", "Bg"]}
total = 400
success = 0 ; success_red = 0 ; success_yellow = 0 ; success_blue = 0
count = 0
def compute_signals(color, stop_synapses , go_synapses):
R, G, B = colors[color]
stop_signal = stop_synapses["Rs"] * R + stop_synapses["Gs"] * G + stop_synapses["Bs"] * B
go_signal = go_synapses["Rg"] * R + go_synapses["Gg"] * G + go_synapses["Bg"] * B
return stop_signal >= 1, go_signal >= 1
def stop_plus():
random_key = random.choice(list(stop_synapses.keys()))
i = within(stop_synapses[random_key] + 1)
stop_synapses[random_key] = i
def stop_minus():
random_key = random.choice(list(stop_synapses.keys()))
i = within(stop_synapses[random_key] – 1)
stop_synapses[random_key] = i
def go_plus():
random_key = random.choice(list(go_synapses.keys()))
i = within(go_synapses[random_key] + 1)
go_synapses[random_key] = i
def go_minus():
random_key = random.choice(list(go_synapses.keys()))
i = within(go_synapses[random_key] – 1)
go_synapses[random_key] = i
def within(weighting):
if weighting > 11 : weighting = 10
if weighting < -11 : weighting = -10
return weighting
for _ in range(total):
color = random.choice(list(colors.keys()))
stop, go = compute_signals(color, stop_synapses, go_synapses)
count += 1
if success_red * success_yellow * success_blue == 1:
print(stop_synapses)
print(go_synapses)
break
if color =="red":
if stop == True and go == True:
go_minus()
success_red = 0
if stop == True and go == False:
success += 1
success_red = 1
if stop == False and go == True:
stop_plus()
go_minus()
success_red = 0
if stop == False and go == False:
stop_plus()
success_red = 0
if color =="yellow":
if stop == True and go == True:
go_minus()
success_yellow = 0
if stop == True and go == False:
success += 1
success_yellow = 1
if stop == False and go == True:
stop_plus()
go_minus()
success_yellow = 0
if stop == False and go == False:
stop_plus()
success_yellow = 0
if color == "blue":
if stop == True and go == True:
stop_minus()
success_blue = 0
if stop == True and go == False:
stop_minus()
go_plus()
success_blue = 0
if stop == False and go == True:
success += 1
success_blue = 1
if stop == False and go == False:
go_plus()
success_blue = 0
if success_red * success_yellow * success_blue == 0:
print("not found")
if success_red * success_yellow * success_blue == 1:
print("total",total)
print("found on the",count)