for文が一回多く行われる

python

1from dual_network import DN_INPUT_SHAPE 2from math import sqrt 3from tensorflow.keras.models import load_model 4from pathlib import Path 5import numpy as np 6import battle 7from battle import Battle 8import pokedex as p 9import moves as m 10 11# パラメータの準備12PV_EVALUATE_COUNT = 50 # 1推論あたりのシミュレーション回数(本家は1600)13 14# 推論15def predict(model, state):16 # 推論のための入力データのシェイプの変換17 x=np.array(state)18 x=x.reshape(1,4,2)19 20 # 推論21 y=model.predict(x,batch_size=1)22 23 # 方策の取得24 policies=y[0][0:4]25 26 # 価値の取得27 value=y[1][0]28 29 return policies, value 30 31# ノードのリストを試行回数のリストに変換32def nodes_to_scores(nodes):33 scores = []34 for c in nodes:35 scores.append(c.n)36 return scores 37 38# モンテカルロ木探索のスコアの取得39#def pv_mcts_scores(model, p1_is,p1_mae_action,p1_took_damage,p1_nokorihp,p1_is,p2_mae_action,p2_took_damage,p2_nokorihp, temperature): #stateに8つの状態40def pv_mcts_scores(model, state, temperature,winner=None): #stateに8つの状態41# モンテカルロ木探索のノードの定義42 class Node:43 player1=[44 p.Jolteon([m.BodySlam(),m.DoubleKick(),m.PinMissle(),m.Thunderbolt()])45 ]46 47 player2=[48 p.Rhydon([m.Earthquake(), m.RockSlide(), m.Surf(), m.BodySlam()])49 ]50 51 # ノードの初期化52 def __init__(self, state, p,winner):53 self.state = state # 状態54 self.p = p # 方策55 self.w = 0 # 累計価値56 self.n = 0 # 試行回数57 self.winner=winner 58 self.child_nodes = None # 子ノード群59 (self.p1_is,self.p1_mae_action,self.p1_took_damage,self.p1_nokorihp),(self.p1_is,self.p2_mae_action,self.p2_took_damage,self.p2_nokorihp)=state 60 self.turn=061 62 # 局面の価値の計算63 def evaluate(self): #Battle が入る64 # ゲーム終了時65 if self.winner is not None:66 # 勝敗結果で価値を取得67 #print("hplen",len(self.p1_nokorihp))68 battle=Battle(player1,player2)69 value = 0 if self.winner == player1 else -170 71 # 累計価値と試行回数の更新72 self.w += value 73 self.n += 174 return value 75 76 # 子ノードが存在しない時77 if not self.child_nodes:78 # ニューラルネットワークの推論で方策と価値を取得79 policies, value = predict(model, state)80 81 # 累計価値と試行回数の更新82 self.w += value 83 self.n += 184 85 86 # 子ノードの展開87 self.child_nodes = []88 a=[6,7,8,9]89 for action, policy in zip(a, policies):90 battle=Battle(player1,player2)91 zyoutai=battle.forward_step(self.p1_nokorihp,self.p2_nokorihp,action)92 winner = battle.get_winner()93 self.child_nodes.append(Node(zyoutai, policy,winner))94 95 96 return value 97 98 # 子ノードが存在する時99 else:100 # アーク評価値が最大の子ノードの評価で価値を取得101 value = self.next_child_node().evaluate()102 103 # 累計価値と試行回数の更新104 self.w += value 105 self.n += 1106 return value 107 108 # アーク評価値が最大の子ノードを取得109 def next_child_node(self):110 # アーク評価値の計算111 C_PUCT = 1.0112 t = sum(nodes_to_scores(self.child_nodes))113 pucb_values = []114 #print("前 child_nodes",len(self.child_nodes))115 for child_node in self.child_nodes:116 pucb_values.append((-child_node.w / child_node.n if child_node.n else 0.0) +117 C_PUCT * child_node.p * sqrt(t) / (1 + child_node.n))118 self.turn+=1119 120 # アーク評価値が最大の子ノードを返す121 print("argmax",np.argmax(pucb_values))122 print("turn",self.turn)123 print("len(pucb_values)",len(pucb_values))124 print("pucb_values",pucb_values)125 index=np.argmax(pucb_values)126 a = index.item()127 print("index",type(index))128 print("index",index)129 print("len(self.child_nodes)",len(self.child_nodes))130 print("self.child_nodes",self.child_nodes)131 return self.child_nodes[a]132 133 # 現在の局面のノードの作成134 root_node = Node(state, 0,winner)135 136 # 複数回の評価の実行137 for _ in range(PV_EVALUATE_COUNT):138 root_node.evaluate()139 140 # 合法手の確率分布141 scores = nodes_to_scores(root_node.child_nodes)142 if temperature == 0: # 最大値のみ1143 action = np.argmax(scores)144 scores = np.zeros(len(scores))145 scores[action] = 1146 else: # ボルツマン分布でバラつき付加147 scores = boltzman(scores, temperature)148 return scores 149 150# モンテカルロ木探索で行動選択151def pv_mcts_action(model, temperature=0):152 def pv_mcts_action(state):153 scores = pv_mcts_scores(model, state, temperature,winner)154 rng=np.random.default_rng()155 return rng.choice([0,1,2,3], p=scores)156 return pv_mcts_action 157 158# ボルツマン分布159def boltzman(xs, temperature):160 xs = [x ** (1 / temperature) for x in xs]161 return [x / sum(xs) for x in xs]

コメントを投稿

0 コメント