这是一个简单的AI,哪位big old可以给我解读一下用到的技术和方法吗? 有没有评估函数和搜索树的涉及
from game_def import *
class SimpleAI(object):
def __init__(self, alpha=2.5, beta=0.75):
self.player = None
self.goal = None
self.board = None
self.alpha = alpha
self.beta = beta
def set_player_ind(self, p, state):
self.player = p
self.state = state
self.pieces = bk_init_player_pieces(self.state)
def __str__(self):
return "SimpleAI {}".format(self.player)
def is_human(self):
return False
def is_winner(self):
return bk_is_winner(self.state, self.pieces)
def update_pieces(self, move_from, move_to):
for p in range(10):
if self.pieces[p] == move_from:
self.pieces[p] = move_to
break
def set_goal(self):
if self.state == 1:
for pos in [284, 267, 266, 250, 249, 248, 233, 232, 231, 230]:
if self.board.states[pos] != self.state:
self.goal = pos
return
return
elif self.state == 4:
for pos in [4, 21, 22, 38, 39, 40, 55, 56, 57, 58]:
if self.board.states[pos] != self.state:
self.goal = pos
return
return
def is_arrived(self, pos):
h_goal = self.goal // board_width
h_pos = pos // board_width
if self.state == 1: return h_pos > h_goal or pos >= self.goal
else: return h_pos < h_goal or pos <= self.goal
def get_physic_pos(self, pos):
h = pos // board_width
w = pos % board_width
w = w * 2 - h
h = h * 1.732
return [w, h]
def get_dist(self, pos):
if self.is_arrived(pos): return 0
[a, b] = self.get_physic_pos(pos)
[c, d] = self.get_physic_pos(self.goal)
return (( a - c ) ** 2 * self.alpha + ( b - d ) ** 2) ** self.beta
def get_action(self, board):
self.board = board
self.set_goal()
max_score = -10000
max_move = None
for pos in self.pieces:
move_set = bk_search_movable_set(self.board.table, self.board.states, pos)
for aim in move_set:
score = self.get_dist(pos) - self.get_dist(aim)
if score > max_score:
max_score = score
max_move = (pos, aim)
return max_move