UCT算法初探

又是一年高考出分时,感慨颇多。
两年前的我高考失利,比平时成绩差了许多,所谓的“一定能上一本A”也成了一个笑话,固然是抹不去的伤痛,高考是我前二十年受到的最大的一次打击,可以说是唯一的一次打击,只能说 ,太顺了不好。
幸好学的是喜欢的专业,至少不会厌烦,程序的创新、调试总是充满惊喜。生活总要继续,高考画上了前二十年的残缺的句号,总不能影响未来十年的黄金拼搏期。以前老师总是说,高考是一个选择,选择和更优秀的人在一起,无论在哪一个圈子,总有优秀的人,也许在别人眼中,我也是其一,,只有努力,表里如一,比他们更好。

感慨结束、今天正事:继续学习UCT

总结一下知网一篇关于UCT论文:
UCT算法分成四个部分:
a 选择节点
b 展开节点
c 棋局模拟
d 回馈更新

a 若有未被访问过的子节点,则优先以随机方式选择其中一个子节点然后执行模拟棋局 -> c, 否则继续使用节点选择公式UCB1选择子节点。当被选中的子节点为叶节点并且这个子节点被访问的次数未达到指定的次数时,则选择该子节点落子然后 ->c .当被选中的子节点为叶节点并且这个节点被访问的次数达到指定的次数,则需先展开该子节点,否则重复此步骤直到找到被访问的次数未达到指定的次数或未被访问过的叶节点为止。

b 展开
当节点为叶节点并且该节点被访问的次数达到指定的次数时,进行展开子节点。展开时对候选步做筛选,去除不合适的候选步,再将筛选后的候选步展开成子节点并随机选择其中一个节点。

c棋局模拟
当被选择的叶节点落子后执行模拟棋局,在模拟中检查是否有棋串少于四气,若有则尝试逃跑,如果有符合简单的模式库的棋型,执行库模式匹配,攻击少气的棋串,都没有就随机。

d回馈更新
将模拟棋局的结果回溯到更新游戏树节点的信息

好的,下面是一个简单的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、付费专栏及课程。

余额充值