围棋博弈程序的实现与思考(3)——UCT算法

我们已经知道UCB算法能够更快地找到靠谱的着手点,续上一篇的问,能不能再优化?

首先要知道的是,为什么UCB算法比盲目的蒙特卡罗局面评估收敛得更快?我的理解,是因为在算法执行的过程中,UCB算法能不断根据之前的结果调整策略,选择优先评估哪一个可下点。其实这是一种在线的机器学习策略。对于上一篇提到过的多臂匪徒问题,可以用UCB算法很好地解决。对于围棋博弈问题而言,UCB算法相比于朴素的蒙特卡罗局面评估方法,收敛速度有很大的提高,但确实存在可进一步优化的地方。

上一篇中,把围棋棋盘上的可下点比作了角子机,但他们之间有什么不同呢?

答案是——多臂匪徒问题只有一层角子机,围棋博弈问题则是多层角子机构成的博弈树!

终于提到博弈树了,需知在绝大多数棋类博弈问题中,博弈树是必不可少的工具。这里简单介绍一下博弈树搜索中用到的最基本搜索方法——最大最小搜索(如果对博弈树毫无概念,请自行google)。在一个二人零和博弈游戏中,参与博弈的双方所作出的每一个决策都是为了己方利益的最大化(废话~)。假设我们把黑方获胜时的棋局形势设为一个正数值v,白方获胜则是-v(其他情况下局势介于v和-v之间),则黑棋的每一手棋都是为了使局势尽可能的大,白棋反之。表现在博弈树中,则黑棋层总是选择局势值最大的结点作为结果返回上一层,白棋层反之——这就是最大最小搜索。

有了以上的科普,这里就给出上一篇的答案——更优化的算法,UCT算法(UCB for tree)。以下是算法描述:

给定一棵博弈树。

1) 从博弈树的根点开始向下搜索,执行2)。

2)遇到节点a后,若a存在从未评估过的子节点,执行3),否则执行4)。

3) 通过MonteCarlo方法评估该子节点,得到收益值后更新该子节点至根节点路径上所有节点的平均收益值,执行1)。

4) 计算每个子节点的UCB值,将UCB值最高的子节点作为节点a,执行2)。

5) 算法可随时终止,通常达到给定时间或尝试次数后终止。

根节点下平均收益值最高的子节点作为算法的输出。


对于这个算法,有几点需要解释:

1)博弈树的根节点指的是当前的局面。

2)评估过的节点及其平均收益值将在程序运行过程中保存及更新。

3)收益值可自行设定合适的值。我知道MOGO的做法是将其设为1(胜)或0(负),我的程序Foolish Go的做法是,所得地域 / 总地域。

4)这个算法是现代围棋博弈程序的基石。


个人对这个算法的理解是,本质上是一种迭代加深的DFS。

为了更好地理解UCT算法的收敛性,不妨思考,这个算法会怎样“意识”到下一手棋应该征子?

理论的东西差不多介绍完了,下一篇将进入实战。

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Python实现UCT算法的示例代码: ```python import math import random class Node: def __init__(self, state, parent=None): self.state = state self.parent = parent self.children = [] self.wins = 0 self.visits = 0 def is_leaf(self): return len(self.children) == 0 def is_fully_expanded(self): return all(child.visits > 0 for child in self.children) def add_child(self, child_state): child_node = Node(child_state, self) self.children.append(child_node) return child_node class UCT: def __init__(self, state, exploration_constant=1.4): self.root = Node(state) self.exploration_constant = exploration_constant def select(self): node = self.root while not node.is_leaf(): node = self._uct_select(node) return node def expand(self, node): untried_actions = [action for action in self._get_actions(node.state) if not any(child.state == action for child in node.children)] if untried_actions: action = random.choice(untried_actions) child_node = node.add_child(action) return child_node else: return None def simulate(self, state): while not self._is_terminal(state): action = random.choice(self._get_actions(state)) state = self._get_next_state(state, action) return self._get_reward(state) def backpropagate(self, node, reward): while node is not None: node.visits += 1 node.wins += reward node = node.parent def run(self, num_iterations): for i in range(num_iterations): node = self.select() child = self.expand(node) if child: reward = self.simulate(child.state) self.backpropagate(child, reward) else: reward = self.simulate(node.state) self.backpropagate(node, reward) best_child = None best_score = float('-inf') for child in self.root.children: score = child.wins / child.visits + self.exploration_constant * math.sqrt(2 * math.log(self.root.visits) / child.visits) if score > best_score: best_child = child best_score = score return best_child.state def _uct_select(self, node): best_child = None best_score = float('-inf') for child in node.children: score = child.wins / child.visits + self.exploration_constant * math.sqrt(2 * math.log(node.visits) / child.visits) if score > best_score: best_child = child best_score = score return best_child def _get_actions(self, state): # Return a list of possible actions from the given state pass def _get_next_state(self, state, action): # Return the next state given the current state and action pass def _get_reward(self, state): # Return the reward for the given state pass def _is_terminal(self, state): # Return True if the given state is a terminal state, False otherwise pass ``` 要使用这个算法,需要在 `UCT` 类中实现 `_get_actions`、`_get_next_state`、`_get_reward` 和 `_is_terminal` 方法。这些方法需要根据具体的问题实现。 例如,如果我们想使用 UCT 算法解决一个棋盘游戏,可以实现这些方法如下: ```python class Board: def __init__(self): self.board = [[0] * 3 for _ in range(3)] def is_valid_move(self, row, col): return self.board[row][col] == 0 def make_move(self, row, col, player): self.board[row][col] = player def is_win(self, player): for i in range(3): if self.board[i][0] == player and self.board[i][1] == player and self.board[i][2] == player: return True if self.board[0][i] == player and self.board[1][i] == player and self.board[2][i] == player: return True if self.board[0][0] == player and self.board[1][1] == player and self.board[2][2] == player: return True if self.board[0][2] == player and self.board[1][1] == player and self.board[2][0] == player: return True return False def is_full(self): return all(self.board[i][j] != 0 for i in range(3) for j in range(3)) class TicTacToeUCT(UCT): def __init__(self): super().__init__(Board()) def _get_actions(self, state): actions = [] for i in range(3): for j in range(3): if state.is_valid_move(i, j): actions.append((i, j)) return actions def _get_next_state(self, state, action): row, col = action player = 1 if state.is_full() or state.is_win(2) else 2 next_state = Board() next_state.board = [row[:] for row in state.board] next_state.make_move(row, col, player) return next_state def _get_reward(self, state): if state.is_win(1): return 1 elif state.is_win(2): return 0 else: return 0.5 def _is_terminal(self, state): return state.is_full() or state.is_win(1) or state.is_win(2) ``` 这个例子中,我们使用 UCT 算法解决井字棋游戏。对于 `_get_actions` 方法,我们返回一个包含所有空位置的列表。对于 `_get_next_state` 方法,我们先判断当前玩家是谁,然后创建一个新的棋盘状态,并在新状态上执行该动作。对于 `_get_reward` 方法,我们返回 1(玩家1赢)、0(玩家2赢)或0.5(平局)中的一个。对于 `_is_terminal` 方法,我们检查棋盘是否已满或某个玩家已经赢了。 使用这个算法的示例代码如下: ```python game = TicTacToeUCT() for i in range(10000): game.run(1) best_move = game.run(100) print(best_move) ``` 这个例子中,我们在 UCT 算法中运行 10000 次迭代,然后再运行 100 次迭代来选择下一步最佳动作。在这个例子中,UCT 算法将选择最有可能导致胜利的行动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值