R G B のうち0が含んでるやつも重さの更新する値に含むのはダメなんじゃない?
そう天の声が聞こえたので def plus() の箇所を以下のように変更してみた。
def stop_plus():
random_key_idx = np.random.choice(list(np.where(np.array(colors[color])!=0))[0])
random_key=list(stop_synapses.keys())[random_key_idx]
i = min(10,stop_synapses[random_key] + 1)
stop_synapses[random_key] = i
以下 def minus() も同様にしたら、30回程度でも重みを見つけているし(なんと前のコードの8分の1)なんとなくいい感じになっている。ただときどき (False,False) もしくは(Positive,Positive)が出てくるからまずい。
そもそも、あまりに冗長すぎる。ということで、こういうのはどうだろうか。
import numpy as np
# [[Rs Rg]
# [Gs Gg]
# [Bs Bg]] という形の行列
weight=np.random.randint(-10,10,size=(3,2))
# 順に赤、黄、青
color_list=np.array([[100,0,0],[100,100,0],[0,100,50]])
total = 30
for i in range(total):
color=np.random.randint(0,3)
colors=color_list[color]
score=colors@weight
stop_score,go_score=score
change_idx=np.random.choice(list(np.where(np.array(colors)!=0))[0])
if color<2:
if stop_score>=0 and go_score>=0:
weight[:,1][change_idx]-=1
elif go_score>=0:
weight[:,0][change_idx]+=1
weight[:,1][change_idx]-=1
elif go_score<0 and stop_score<0:
weight[:,0][change_idx]+=1
else:
if stop_score>=0 and go_score>=0:
weight[:,0][change_idx]-=1
elif stop_score>=0:
weight[:,0][change_idx]-=1
weight[:,1][change_idx]+=1
elif go_score<0 and stop_score<0:
weight[:,1][change_idx]+=1
print(weight)
color_name=[‘RED’,’YELLO’,’BLUE’]
for i in range(3):
color=color_list[i]
score=color@weight
print(color_name[i])
if score[0]>=0 and score[1]<0:
print(‘stop’)
elif score[0]<0 and score[1]>=0:
print(‘go’)
おーすばらしい。うまく動いているようだ。
ちなみに相談したあとの天の声、そして日を開けずに送られてきた新たなコードは、いずれも夢をかなえるために今年大学に進んだ息子のものである。