博弈树
~描述:节点是状态,边表示动作或落子。
~原则:己方利益最大化,对方利益最小化。
Minimax算法
~定义:在理想对手的假设下,MIN方总是最小化MAX方的最大化努力。
~原理:使用了简单的深度递归搜索技术来确定每一个节点的值:它从根节点开始,一直前进到叶子节点,然后在递归回溯时,将叶子节点的效用值往上回传——对于MAX方,计算最大值,对于MIN方,计算最小值。
~示例:以下是一个2层(Two-Ply)博弈树:
1.叶子节点的效用值越大,对MAX方越有利。在MIN方会选择该节点下效用值最小的节点。即 3,2,2(第三层从左向右依次)。
2.MAX方要在“3,2,2”选择一个,选择对MAX方最有利的3。

~注:上图有颜色的方框是用Alpha-Beta算法分析的,用于将两种算法进行对比,下面将会对该算法扩展分析。

Alpha-Beta算法
以下是将上面的博弈树用Alpha-Beta算法的分解分析:
(a).

(b).

©.

(d).

(e).

(f).

总结分析:
Alpha-Beta算法用了剪枝而Minimax算法没有,从代码的运行可以明显看出二者速度的差别。

Minimax算法的代码:

# -*- coding: utf-8 -*-
"""
Created on Fri Oct 26 14:41:12 2018@author: duxiaoqin
Functions:(1) Minimax Algorithm for TicTacToe
"""from graphics import *
from tictactoe import *
from tttdraw import *
from tttinput import *
import sysdef Minimax(node, depth):result = node.isGameOver()if result != None:return result, (), depthif node.getPlayer() == TicTacToe.BLACK:bestValue = -sys.maxsizebestMove = ()bestDepth = sys.maxsizemoves = node.getAllMoves()for move in moves:child = node.clone()child.play(*move)v, _, leafDepth = Minimax(child, depth+1)if bestValue == v and bestDepth > leafDepth:bestValue = vbestMove = movebestDepth = leafDepthif bestValue < v:bestValue = vbestMove = movebestDepth = leafDepthreturn bestValue, bestMove, bestDepthelse:bestValue = sys.maxsizebestMove = ()bestDepth = sys.maxsizemoves = node.getAllMoves()for move in moves:child = node.clone()child.play(*move)v, _, leafDepth = Minimax(child, depth+1)if bestValue == v and bestDepth > leafDepth:bestValue = vbestMove = movebestDepth = leafDepthif bestValue > v:bestValue = vbestMove = movebestDepth = leafDepthreturn bestValue, bestMove, bestDepthdef main():win = GraphWin('Minimax for TicTacToe', 600, 600, autoflush=False)ttt = TicTacToe()tttdraw = TTTDraw(win)tttinput = TTTInput(win)tttdraw.draw(ttt)while win.checkKey() != 'Escape':if ttt.getPlayer() == TicTacToe.WHITE:v, move, _ = Minimax(ttt, 0)if move != ():ttt.play(*move)tttinput.input(ttt)tttdraw.draw(ttt)if ttt.isGameOver() != None:time.sleep(1)ttt.reset()tttdraw.draw(ttt)#win.getMouse()win.close()if __name__ == '__main__':main()

Alpha-Beta算法的代码:

# -*- coding: utf-8 -*-
"""
Created on Fri Oct 26 20:53:08 2018@author: duxiaoqin
Functions:(1) Alpha-Beta Algorithm for TicTacToe
"""from graphics import *
from tictactoe import *
from tttdraw import *
from tttinput import *
import sys
import timedef AlphaBeta(node, depth, alpha, beta):result = node.isGameOver()if result != None:#游戏结束return result, (), depthif node.getPlayer() == TicTacToe.BLACK:bestValue = -sys.maxsizebestMove = ()bestDepth = sys.maxsizemoves = node.getAllMoves()for move in moves:child = node.clone()child.play(*move)v, _, leafDepth = AlphaBeta(child, depth+1, alpha, beta)if bestValue == v and bestDepth > leafDepth:bestValue = vbestMove = movebestDepth = leafDepthif bestValue < v:bestValue = vbestMove = movebestDepth = leafDepthalpha = max(alpha, bestValue)if beta <= alpha:#进行剪枝break #beta pruningreturn bestValue, bestMove, bestDepthelse:bestValue = sys.maxsizebestMove = ()bestDepth = sys.maxsizemoves = node.getAllMoves()for move in moves:child = node.clone()child.play(*move)v, _, leafDepth = AlphaBeta(child, depth+1, alpha, beta)#‘_’在该函数内不起作用if bestValue == v and bestDepth > leafDepth:bestValue = vbestMove = movebestDepth = leafDepthif bestValue > v:bestValue = vbestMove = movebestDepth = leafDepthbeta = min(beta, bestValue)if beta <= alpha:break #alpha pruningreturn bestValue, bestMove, bestDepthdef main():win = GraphWin('Minimax for TicTacToe', 600, 600, autoflush=False)ttt = TicTacToe()tttdraw = TTTDraw(win)tttinput = TTTInput(win)tttdraw.draw(ttt)while win.checkKey() != 'Escape':if ttt.getPlayer() == TicTacToe.WHITE:v, move, _ = AlphaBeta(ttt, 0, -sys.maxsize, sys.maxsize)if move != ():ttt.play(*move)tttinput.input(ttt)tttdraw.draw(ttt)if ttt.isGameOver() != None:time.sleep(1)ttt.reset()tttdraw.draw(ttt)#win.getMouse()win.close()if __name__ == '__main__':main()

博弈树代码:

# -*- coding: utf-8 -*-
"""
Created on Mon Sep 10 22:25:08 2018@author: duxiaoqinFunctions:(1) TicTacToe class;
"""from myarray2d import Array2Dclass TicTacToe:BLACK = TrueWHITE = FalseEMPTY = NoneBLACKWIN = 1WHITEWIN = -1DRAW = 0def __init__(self):self.board = Array2D(3, 3)self.player = TicTacToe.BLACKself.black = []self.white = []self.magic = Array2D(3, 3)self.magic[0, 0] = 2self.magic[0, 1] = 9self.magic[0, 2] = 4self.magic[1, 0] = 7self.magic[1, 1] = 5self.magic[1, 2] = 3self.magic[2, 0] = 6self.magic[2, 1] = 1self.magic[2, 2] = 8def reset(self):self.board.clear(None)self.player = TicTacToe.BLACKself.black = []self.white = []def clone(self):newttt = TicTacToe()for row in range(3):for col in range(3):newttt.board[row, col] = self.board[row, col]newttt.player = self.playernewttt.black = self.black[:]newttt.white = self.white[:]return newtttdef ToString(self):l = []for row in range(3):for col in range(3):if self.board[row, col] == TicTacToe.BLACK:l.append('X')elif self.board[row, col] == TicTacToe.WHITE:l.append('O')else:l.append('_')return ''.join(l)def print(self):for row in range(3):for col in range(3):if self.board[row, col] == TicTacToe.BLACK:print('X', end=' ')elif self.board[row, col] == TicTacToe.WHITE:print('O', end=' ')else:print('_', end=' ')print()def play(self, row, col):self.board[row, col] = self.playerif self.player == TicTacToe.BLACK:self.black.append(self.magic[row, col])else:self.white.append(self.magic[row, col])self.player = not self.playerdef getPlayer(self):return self.playerdef getAllMoves(self):return [(row, col) for row in range(3) \for col in range(3) \if self.board[row, col] == TicTacToe.EMPTY]def isWin(self, n, goal, moves):moves_clone = moves[:]if n == 0:return goal == 0elif goal <= 0:return Falseelif len(moves_clone) == 0:return Falseelse:item = moves_clone.pop(0)if self.isWin(n-1, goal-item, moves_clone[:]):return Trueelif self.isWin(n, goal, moves_clone[:]):return Truereturn Falsedef isGameOver(self):if self.isWin(3, 15, self.black):return TicTacToe.BLACKWINelif self.isWin(3, 15, self.white):return TicTacToe.WHITEWINelif len(self.black)+len(self.white) == 9:return TicTacToe.DRAWelse:return Nonedef main():ttt = TicTacToe()ttt.play(1, 1)ttt.play(0, 0)ttt.play(2, 0)ttt.play(0, 1)ttt.play(0, 2)ttt.print()print(ttt.isGameOver())print(ttt.ToString())if __name__ == '__main__':main()

Minimax算法与Alpha-Beta算法相关推荐

  1. 五子棋AI算法第三篇-Alpha Beta剪枝

    剪枝是必须的 五子棋AI教程第二版发布啦,地址:https://github.com/lihongxun945/myblog/labels/%E4%BA%94%E5%AD%90%E6%A3%8BAI% ...

  2. alpha-beta剪枝五子棋c语言,五子棋AI算法第三篇-Alpha Beta剪枝

    剪枝是必须的 上一篇讲了极大极小值搜索,其实单纯的极大极小值搜索算法并没有实际意义. 可以做一个简单的计算,平均一步考虑 50 种可能性的话,思考到第四层,那么搜索的节点数就是 50^4 = 6250 ...

  3. 五子棋AI算法-Alpha Beta剪枝

    上一篇讲了极大极小值搜索,其实单纯的极大极小值搜索算法并没有实际意义. 可以做一个简单的计算,平均一步考虑 50 种可能性的话,思考到第四层,那么搜索的节点数就是 50^4 = 6250000,在我的 ...

  4. python alpha beta 剪枝_一看就懂的 Alpha-Beta 剪枝算法详解

    Alpha-Beta剪枝用于裁剪搜索树中没有意义的不需要搜索的树枝,以提高运算速度. 假设α为下界,β为上界,对于α ≤ N ≤ β: 若 α ≤ β  则N有解. 若 α > β 则N无解. ...

  5. PCL实现Alpha Shapes算法

    说明: 本文所用方法都来自于网络查找,本文借鉴了一下其他博主的文章,在他的基础上实现了Alpha Shapes算法.然后写了一个Alpha Shapes演示程序. Alpha Shapes演示程序下载 ...

  6. Alpha matting算法发展

    一.抠图算法简介 Alpha matting算法研究的是如何将一幅图像中的前景信息和背景信息分离的问题,即抠图.这类问题是数字图像处理与数字图像编辑领域中的一类经典问题,广泛应用于视频编缉与视频分割领 ...

  7. 平面点云的轮廓线计算-alpha shapes算法原理和实现

    alpha shape算法又称为滚球法,是一种提取边界点的算法.跟凸壳提取相比,alpha shape算法能够了凹包情形,且对多个点云时 能勾勒出多个边界线,这是他的优势. 研究alpha shape ...

  8. 基于python的AI五子棋实现(极大极小值搜索和alpha beta剪枝)

    1.极大极小值搜索介绍 人机博弈是人工智能的重要分支,人们在这一领域探索的过程中产生了大量的研究成果,而极小化极大算法(minimax)是其中最基础的算法,它由Shannon在1950年正式提出. M ...

  9. Alpha-Beta剪枝(Alpha Beta Pruning)

    Alpha-Beta剪枝算法(Alpha Beta Pruning) [说明] 本文基于<<CS 161 Recitation Notes - Minimax with Alpha Bet ...

  10. python做马尔科夫模型预测法_隐马尔可夫模型的前向算法和后向算法理解与实现(Python)...

    前言 隐马尔可夫模型(HMM)是可用于标注问题的统计学习模型,描述由隐藏的马尔可夫链随机生成观测序列的过程,属于生成模型. 马尔可夫模型理论与分析 参考<统计学习方法>这本书,书上已经讲得 ...

最新文章

  1. was unable to refresh its cache! status = Cannot execute request on any known server
  2. python中matplotlib自定义设置图像标题使用的字体类型:获取默认的字体族及字体族中对应的字体、自定义设置图像标题使用的字体类型
  3. matplotlb添加中文字体
  4. 《Excel 职场手册:260招菜鸟变达人》一第 13 招 利用数据验证给单元格添加注释,不用批注...
  5. 电子商务数据挖掘python案例_精心整理!9个 Python 实用案例分享!
  6. 以太坊的4个发展阶段与难度炸弹
  7. 光电整纬机狭缝检测工作原理
  8. 无意中最大的收获:《构建高性能Web站点》
  9. 服务器托管用户支招选择IDC经验
  10. S3C2440时钟电源管理
  11. start request repeated too quickly for docker.service
  12. Science子刊:利用DTI和NODDI纵向研究揭示轻度脑外伤后的白质微结构改变
  13. PS与CSS字间距转换
  14. 怎么把电子书格式转换成word文档
  15. oneDrive登陆界面空白 的解决办法
  16. 基于CTP的期货智能程序化交易系统平台
  17. H5地理定位、百度地图使用
  18. SQL server学习日志(二)创建表!手把手教你创建表,修改表,了解数据类型!超详细!
  19. 《Total Commander:万能文件管理器》——12.6. 附录
  20. 简单记录下几家公司的面试经历(Java一年经验)

热门文章

  1. AECC2015官方破解补丁/AdobeAfterEffectsCC2015中文版免费下载(AE安装教程)
  2. 需求分层-KANO模型解读
  3. python截图并识别文字
  4. 恩智浦imx8qxp-mek的 device Tree 结构
  5. Ubuntu12.04 64位 无法运行32位程序 吉林大学校园网客户端Drcom DrClient
  6. 如何将本地项目上传到码云
  7. VP9编码:迄今的尝试
  8. php爬虫post,PHPspider爬虫10分钟快速教程
  9. opencv中cvtcolor()函数
  10. 九度搜索引擎点击优化_「九度搜索引擎点击优化软件」网站推广方案