uct博弈算法

Upper Confidence Bound Apply to Tree,即ucb公式应用于蒙特卡洛搜索树

前段时间用这个算法参加了计算机博弈比赛,写的很糙,以为要爆炸,没想到效果还不错。
先简单介绍一下标题的几个名词,不了解的同学想闹明白的话自行查阅相关资料。

蒙特卡洛算法

理论基础是大数定律,多次数重复模拟的各种结果的概率接近于真实概率,模拟次数越多越接近。就像那个用多个随机点求圆的面积的方法。

ucb公式

ucb值可以理解为这条路的预期价值,价值由两个部分来组成。
该节点的ucb值 = 对该节点模拟得来的平均收益 + 该节点渴望被模拟的欲望
很明显,节点被拜访的相对次数越少,欲望就越强,为的就是不放过有潜在价值的节点。
(凭什么对别人那么好,拜访那么多次?就因为第一次拜访我就让你失望了吗?就这?就再也不相信我了?)
在这里插入图片描述
xj就是平局收益,(k是常数,一般就是1),nj是当前节点被访问次数,n是当前节点的父节点的访问次数。为什么要写成这样俺也不知道,但已经被证明了这样效果最佳。

uct算法

uct算法主要有四个步骤:选择、模拟、扩展、更新

先举个不用树结构的uct算法吧(呃,这样应该就不叫uct了,应该叫ucb算法?)。总之就是只用一层孩子节点,先理解一下原理。

干说说不出个一二三来,网上清一色拿多臂赌博机举的例子,我拿井字棋举个例子吧(默认大家都知道规则),好比我们是’X’,局面如下图,该我们走子了。
_ X O
X _ O
_ O _
首先我们把当前局面状态拿出来作为根节点,现在我们有四条路可以走,那就从根节点衍生出四个子节点,每个节点对应一条走法。

刚开始并不知道这几条路哪个好,那就先认为全部走法的价值都很高(给所有孩子都赋一个无限大的ucb值),选择孩子中ucb值最大的一个(刚开始都是无限大,但后边会变),对其进行模拟(由电脑随机走子至终局),同时本身拜访次数也加一,它的父节点,这里也是根节点的拜访次数也加一,这里代表总拜访次数,用来求节点被模拟的欲望。模拟是随机模拟,可以模拟多局,得到一个平均收益。然后重新计算该节点的ucb值(肯定比无限大小了)。

我们每次都从根节点开始选ucb值最大的孩子,对它进行模拟,更新拜访次数,并得到一个新的ucb值。电脑的运行速度是很快的,虽然是随机模拟,但大数定律是跑不了的,最终我们选择ucb值最大的那条路。

现在加上树结构说一下uct算法。

大家可能有个疑问,对于上边的井字棋,其实可以直接用树结构直接把所有情况全写出来,这样不是更简单明了?为什么不这样呢?因为一般的博弈问题,像五子棋、围棋等,用树把所有情况写到底是不现实的,单一个围棋都算到世界末日了(可以搜一下)。因此一个比较好的办法就是,挑价值比较高的走法,对它进行更深层次的搜索。

深层次树结构有什么好处?
对一般博弈问题来说,如果只生成一层孩子,也许离终局还有很远,还要走n多步,而且我们的时间有限,随机模拟次数也有限,这种局面下大数定律就有点招呼不住了。uct的思想就是挖掘深层的路线,对其中表现好的(ucb值高的)进行投资(花时间对其进行模拟),而不是把时间和资源浪费在低层次的无脑模拟上。

选择、扩展、模拟、反向传播

选择:从根节点开始,计算所有孩子的ucb值,并选择其中ucb值最大的,直到到达叶子节点。

模拟:以此局面为基础随机走子模拟至终局。

扩展:若上一步选择得到的叶子节点已经被模拟过了,则往下扩展,即生孩子,反之对其进行模拟。

更新:更新模拟收益和拜访次数,不仅自己,所有祖宗都要更新,为了正确计算各个节点的ucb值。完事儿之后再从第一步选择开始,直到时间或预设次数结束。

最后我们就走第一层孩子中ucb值最大的那条路。

个人粗浅理解,可能有纰漏,欢迎补充-v-

  • 11
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,下面是一个简单的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 算法将选择最有可能导致胜利的行动。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值