ImageVerifierCode 换一换
格式:DOCX , 页数:36 ,大小:100.32KB ,
资源ID:27127192      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/27127192.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(四子棋人工智能.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

四子棋人工智能.docx

1、四子棋人工智能人工智能作业姓名:guojl学号:2620139012指导老师:刘峡壁1. 要求每一道编程题需提交四份文件:(1)可执行程序;(2)源代码;(3)程序设计说明;(4)程序使用说明。注:以上文件请打包成一个压缩文件,以“学号_姓名_大作业名称”方式命名后提交。在文件中请留下你的个人联系方式,以便在出现文件不能解压、不能打开、程序不能编译运行等各种情况时与你联系Implement a computer game program which must satisfy the following requirements:1. Use alpha-beta algorithm to se

2、arch the solutions;2. The allowed maximum depth of the game tree is8;3. Use neural network to evaluate the states of leaves in the game tree;4. Use some machine learning method to train this neural network in the process of competing againstitself or others. Tic Tac Toe on a 4x4 Board(4x4一字棋)Playing

3、 Rules: 1) X always goes first.2) The Players alternate placing Xs and Os on the board until either (a) one player has four in a row horizontally, vertically or diagonally; or (b) all 16 squares are filled.3) If a player is able to draw four Xs or four Os in a row, that player wins.4) If all 16 squa

4、res are filled and neither player has four in a row, the game is a draw.1, 给电脑安装JDK1.6以上版本,推荐JDK1.72, 请将BP_net_weight.txt文件拷贝到我的电脑的C盘根目录下3, 点击运行Tic-Toc-Too.jar或者Tic-Toc-Too-*.exe其中包结构如下:1,BpNeuralNetworks代码如下:packagencse.bpNeuralNetworks;importjava.io.BufferedReader;importjava.io.BufferedWriter;impo

5、rtjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;/* * 神经网络评估 * author guojl * version 20141124 */public class BpNeuralNetworks private static double ETA = 0.7; private static int N = 4; / 输入层节点数 private stati

6、c int SIZE = N * N; private static int MAXVAL = Short.MAX_VALUE; private static int MINVAL = Short.MIN_VALUE; private double input_layer; / 输入层 private double competition_layer; / 隐藏层 private double output_layer; / 输出层(激活函数作用之前) private double y_i; / 神经网络输出(经过激活函数作用) private double w_ji; / 隐藏层到输出层权重

7、,隐藏层 N个节点到输出层1个节点 private double w_kj; / 输入层到隐藏层权重,输入层 N个节点到隐藏层 N个节点 private double y_j; / 隐藏层经过激活函数作用之后,即出输出的输入/ privateStringfilepath=System.getProperty(user.dir)+/src/ncse/res/BP_net_weight.txt ;/ private String filepath=getClass().getResource(/)+/ncse/res/BP_net_weight.txt ; private String filep

8、ath=C:/BP_net_weight.txt ; / 神经网络初始化 publicBpNeuralNetworks() input_layer = new doubleSIZE; competition_layer = new doubleSIZE; w_ji = new doubleSIZE; w_kj = new doubleSIZESIZE; y_j = new doubleSIZE; init_net(); / 初始化神经网络的输入层隐藏层输出层 private void init_net() for (int i = 0; i SIZE; i+) input_layeri = 0

9、.0; competition_layeri = 0.0; output_layer = 0.0; / 初始化权重 public void init_weight() for (int i = 0; i SIZE; i+) w_jii = Math.random(); for (int i = 0; i SIZE; i+) for (int j = 0; j SIZE; j+) w_kjij = Math.random(); / 激活函数。采用双曲正切函数。 / 输入:整合函数值(整合函数为加权求和)。 / 返回值:激活函数值。 private double logistic_s(double

10、 theta) return (1.0 - Math.exp(-theta) / (1.0 + Math.exp(theta); / BP神经网络应用函数。输入棋盘,立即得到其倒推值。 / 输入:当前棋盘状态。 / 返回值:神经网络输出值(需要乘以MAX才等于倒推值)。 public double BP_net_value(int chess_board) init_net(); / 输入层赋值,当前棋盘状态 for (int i = 0; i SIZE; i+) input_layeri = (double) chess_boardi; / 计算隐藏层 for (int j = 0; j S

11、IZE; j+) for (int k = 0; k SIZE; k+) competition_layerj += w_kjkj * input_layerk; / 计算输出层 for (int j = 0; j SIZE; j+) y_jj = logistic_s(competition_layerj); output_layer += w_jij * y_jj; y_i = logistic_s(output_layer); returny_i; / 调整权值函数。进行不超过最大迭代次数的正向/反向传播过程,不断调整权值。如果误差小于允许值则结束。 / 输入:一组学习样本(代表棋盘的1

12、6维向量以及对应倒推值)。 / 返回值:void public void adjust_BP_net_weight(int chess_board, intbacked_up_value) intiteration_times = 1000000; inttemp_iteration_times = 0; while (true) y_i = BP_net_value(chess_board); / 如果达到允许的最小误差或最大迭代次数,退出并结束本次学习 if (Math.abs(y_i - (double) backed_up_value / (double) MAXVAL) = iter

13、ation_times) break; / 开始反向传播误差,调整权重 for (int j = 0; j SIZE; j+) w_jij = w_jij - ETA * y_jj * (1 - y_i * y_i) * (y_i - (double) backed_up_value / (double) MAXVAL); for (int k = 0; k N * N; k+) for (int j = 0; j N; j+) w_kjkj = w_kjkj - ETA * input_layerk * (1 - y_jj * y_jj) * w_jij * (1 - y_i * y_i)

14、* (y_i - (double) backed_up_value / (double) MAXVAL); temp_iteration_times+; return; / 读取神经网络的权重,即神经网络训练的模型 / 按照写入顺序读取,数字之间用空格分开 / 两组权重为两行(第一组之后又换行符) public void read_weight() String num; try String encoding = utf-8;/ File file = new File(././res/BP_net_weight.txt); File file = new File(filepath); i

15、f (file.isFile() &file.exists() / 判断文件是否存在 System.out.println(*); InputStreamReader read = new InputStreamReader( new FileInputStream(file), encoding); BufferedReaderbufferedReader = new BufferedReader(read); String lineTxt = null; lineTxt = bufferedReader.readLine(); num = lineTxt.split( ); for (in

16、t i = 0; i SIZE; i+) w_jii = Double.parseDouble(numi); lineTxt = bufferedReader.readLine(); num = lineTxt.split( ); for (int i = 0; i SIZE; i+) for (int j = 0; j SIZE; j+) w_kjij = Double.parseDouble(numi * SIZE + j); read.close(); else System.out.println(read_weight找不到指定的文件); catch (Exception e) Sy

17、stem.out.println(read_weight读取文件内容出错); e.printStackTrace(); / 保存神经网络训练的模型 / 此函数与public void read_weight()对应 public void write_mode() try File file = new File(filepath); file.createNewFile(); OutputStreamWriterow = new OutputStreamWriter(new FileOutputStream(file), utf-8); BufferedWriterbwrite = new

18、BufferedWriter(ow); for (int i = 0; i SIZE; i+) bwrite.write(w_jii + ); bwrite.write(n); for (int i = 0; i SIZE; i+) for (int j = 0; j SIZE; j+) bwrite.write(w_kjij + ); bwrite.close(); catch (Exception e) System.out.println(write_mode找不到指定的文件); 2,Checker棋盘程序如下:packagencse.checkerOP;importncse.bpNeu

19、ralNetworks.BpNeuralNetworks;/* * 棋盘程序 * authorguojl *version 20141124 */publicclass Checker privatestaticintN = 4; privatestaticintSIZE = N * N; / MAXVAL MINVAL 分别是默认的最大值、最小值点 privatestaticintMAXVAL = Short.MAX_VALUE; privatestaticintMINVAL = Short.MIN_VALUE; / 电脑一直使用 1 代替 / 人一直使用 -1 代替 / 在显示时,根据是否

20、先手,确定棋子是 “O” 还是 “X” privatestaticintcomputer = 1; / 电脑 privatestaticintplayer = -1; / 人 privateintwhofirst = 1; / 默认电脑先手 privateintm_chess_board; / 棋盘,用一维数组(N * N)表示二维数组(N,N) privateBpNeuralNetworksbpnet; / 神经网络 / 设置先手 publicvoidsetWhofirst(intwhofirst) this.whofirst = whofirst; / 初始化棋盘对象 public Che

21、cker() this.m_chess_board = newintSIZE; bpnet = newBpNeuralNetworks(); / 读取神经网络训练的模型 bpnet.read_weight(); / 清空棋盘 this.clear_board(); / 清空棋盘 publicvoidclear_board() for (int i = 0; i SIZE; i+) this.m_chess_boardi = 0; / 获取棋盘某一点值 / 0 : 表示没有棋子 / 1 :表示是电脑的棋子 / -1: 表示是人的棋子 publicintget_chess_board_val(in

22、t i, int j) returnthis.m_chess_boardi * N + j; / 打印棋盘函数,调试用。 / 输入:void / 返回值:void privatevoidprint_chess_board(int chess_board) int i = 0, j = 0; for (i = 0; i N; i+) for (j = 0; j N; j+) if (j N - 1) System.out.println(chess_boardi * N + j + ); else System.out.println(chess_boardi * N + j + n); ret

23、urn; / 在棋盘(x,y)处放置棋子。棋子类型为枚举pieceA,B。 / 输入:棋盘、x、y、棋子类型。 / 返回值:void privatevoidplace_piece_on_chess_board(int chess_board, int x, int y, int type) intpos = x * N + y; if (pos= 0) / 防止计算结果出错,导致数组越界 chess_boardx * N + y = type; else / 如果计算结果越出数组大小,打印当前 x, y值 / 调试用 System.out.println(x = + x + y = + y);

24、 / 功能同重载函数 / 不同点为,此函数默认对当前棋盘操作 / 是对外提供的在棋盘上下棋子的接口函数 publicvoidplace_piece(int x, int y, int type) place_piece_on_chess_board(this.m_chess_board, x, y, type); / 测试指定起点与步长的N个数数否相等,相等返回数值 / 否则返回0 / 用于判断当前棋盘是否有N个相同的棋子连续 privateinttest_N_num_equal(int chess_board, int start, int step) /* * 原理:一行棋子相同,则是在数

25、组中 N 倍数点起始连续N个点相同且不是0 * 一列棋子相同,则是判断前N个点中某一点起始,隔N的倍数个点的值都相同且不为0 * 对角线,间隔分别是:主对角线(N+1) * 次对角线(N-1) * 以上判断,都是相隔固定个点, * */ int i = 0; intcheck_flag = 1; for (i = 0; i N - 1; i+) if (chess_boardstart + i * step != chess_boardstart + (i + 1) * step) check_flag = 0; break; if (check_flag = 1 &chess_boardst

26、art + i * step != 0) returnchess_boardstart + i * step; return 0; / 测试当前棋局谁赢。 / 输入:棋盘。 / 返回值:为0表示A、B都没赢,为1表示A胜,为-1表示B胜。 privateinttest_who_win(int chess_board) / 分别测试一行、一列、两个对角线连成一线四种情况下的输赢 int ret = 0; for (int i = 0; i N; i+) / 行 ret = test_N_num_equal(chess_board, (i * N), 1); if (ret != 0) retur

27、n ret; / 列 ret = test_N_num_equal(chess_board, i, N); if (ret != 0) return ret; / 主对角线 ret = test_N_num_equal(chess_board, 0, (N + 1); if (ret != 0) return ret; / 次对角线 ret = test_N_num_equal(chess_board, (N - 1), (N - 1); if (ret != 0) return ret; return 0; / 同private inttest_who_win(int chess_board) / 此函数式对外提供的接口函数 publicintwho_win() returntest_who_win(this.m_chess_board); / 测试当前棋盘(博弈树结点)属于极大结点或极小结点。 / 输入:棋盘。 / 返回值:为

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1