改めて信号機問題を考えなおしてみた。
「夢をかなえるために」には次のような図がある。ずっとと気になっていた箇所でもある。
青の評価で重みの計算をすると止まれが重すぎるので、0 は掛け算しても変わらないから除き、止まれに向かっている 50 と 100 からの重みをそれぞれ -1 をするというものだ。
図では 7 と 10 だから 引いた重みは 6 と 9 になる。
これを繰り返して学習させていると書いてあるのだが、最初あまり気にせずやみくもにコードに手を付けていた。
でもうまくいかず、この文言をじっくり読み直してみた。そして天の声も借りずに全体の stop go を3つの重みのリストとし、全面的に書き直してみた。
件の箇所は、
change_indices = [i for i, value in enumerate(colors) if value != 0]
で 0 以外が複数ある場合の対処としてリストで取得しており、全体のコードはこうなる。
import numpy as np
import random
color_list=np.array([[254,0,0],[254,254,0],[0,254,127]])
# color_list=np.array([[100,0,0],[100,100,0],[0,100,50]])
list_stop = [random.randint(-10, 10) for _ in range(3)]
list_go = [random.randint(-10, 10) for _ in range(3)]
total = 50
def judge(color, list_stop, list_go):
colors=color_list[color]
score_stop = colors@list_stop
score_go = colors@list_go
# list_ の 0 以外を取得
change_indices = [i for i, value in enumerate(colors) if value != 0]
return score_stop,score_go,change_indices
def list_stop_increase():
for i in change_indices:
list_stop[i] += 1
return
def list_stop_decrease():
for i in change_indices:
list_stop[i] -= 1
return
def list_go_increase():
for i in change_indices:
list_go[i] += 1
return
def list_go_decrease():
for i in change_indices:
list_go[i] -= 1
return
for i in range(total):
color = np.random.randint(0,3)
score_stop,score_go,change_indices = judge(color, list_stop, list_go)
if color<2:
if score_stop > 0 and score_go > 0:
list_go_decrease()
elif score_stop <= 0 and score_go > 0:
list_stop_increase()
list_go_decrease()
elif score_stop <= 0 and score_go <= 0:
list_stop_increase
else:
if score_stop > 0 and score_go > 0:
list_stop_decrease()
elif score_stop > 0 and score_go <= 0:
list_stop_decrease()
list_go_increase()
elif score_stop <= 0 and score_go <= 0:
list_go_increase()
color_name=[‘RED’,’YELLO’,’BLUE’]
for i in range(3):
colors = color_list[i]
score_stop = colors@list_stop
score_go = colors@list_go
print(color_name[i])
if score_stop > 0 and score_go <= 0:
print(‘stop’,score_stop,list_stop)
# print(list_stop)
elif score_stop <= 0 and score_go > 0:
print(‘go’,score_go,list_go)
else:
print(‘unknown’)
動かしてみると30回程度で求まる重みで 赤、黄は止まれ、青は進めを認識している。さらにRGBを100ではなく255で表したもので出てきた重みを使って、実際の色を”RGB確認ツール”で見ながら確認すると、それぞれの色合いからほどほど離れると認識できなくなる。
おお、できているじゃないか。
やったー!って喜んでみたものの、じゃあ今までアップしてきたコードへの落とし前はどうつければいいのだろう。
いろいろ考えて末に出た結論はこうだ。
はい、ご終章さま。