From 390a7d1cbf46eb57ada3c1418c1b82e05efda90b Mon Sep 17 00:00:00 2001 From: aohongchang Date: Wed, 3 Feb 2021 15:47:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?ChessGame=5Ftf2.py=20=20=20=E4=B8=ADtime.cl?= =?UTF-8?q?ock()=20=20=20=E6=8D=A2=E6=88=90=20time.perf=5Fcounter()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/codeStyles/Project.xml | 9 +++++++++ .idea/codeStyles/codeStyleConfig.xml | 5 +++++ .idea/vcs.xml | 6 ++++++ ChessGame_tf2.py | 4 ++-- cchess-zero.ipynb | 2 +- 5 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..acb4196 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ChessGame_tf2.py b/ChessGame_tf2.py index 279a08f..4c3a7dd 100755 --- a/ChessGame_tf2.py +++ b/ChessGame_tf2.py @@ -182,9 +182,9 @@ def change_player(self): def perform_AI(self): print ('...AI is calculating...') - START_TIME = time.clock() + START_TIME = time.perf_counter() move, win_rate = self.cchess_engine.select_move(self.ai_function) - time_used = time.clock() - START_TIME + time_used = time.perf_counter() - START_TIME print ('...Use %fs...' % time_used) if self.current_player == "w": self.time_red.append(time_used) diff --git a/cchess-zero.ipynb b/cchess-zero.ipynb index 229469e..8ea1a9c 100644 --- a/cchess-zero.ipynb +++ b/cchess-zero.ipynb @@ -82,7 +82,7 @@ "我们是要抛弃人类棋谱的,学会如何下棋完全是通过自对弈来完成。\n", "\n", "过程是这样,首先生成棋谱,然后将棋谱作为输入训练神经网络,训练好的神经网络用来预测落子和胜率。如下图:\n", - "![a1](assets\\a1.png\")" + "" ] }, { From 4e6c939aa84d6d64b0b3a9c3f2feb94d7e5724d1 Mon Sep 17 00:00:00 2001 From: aohongchang <1535533540@qq.com> Date: Thu, 8 Jul 2021 10:56:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=8D=A2tensorflow1.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ral_Reinforcement_Learning_Algorithm.ipynb | 2 +- ...e_Game_of_Go_without_Human_Knowledge.ipynb | 2 +- chessman/__pycache__/Bing.cpython-35.pyc | Bin 1705 -> 1654 bytes chessman/__pycache__/Che.cpython-35.pyc | Bin 1511 -> 1460 bytes chessman/__pycache__/Ma.cpython-35.pyc | Bin 1523 -> 1472 bytes chessman/__pycache__/Pao.cpython-35.pyc | Bin 1480 -> 1429 bytes chessman/__pycache__/Shi.cpython-35.pyc | Bin 1868 -> 1817 bytes chessman/__pycache__/Shuai.cpython-35.pyc | Bin 1706 -> 1655 bytes chessman/__pycache__/Xiang.cpython-35.pyc | Bin 1580 -> 1529 bytes chessman/__pycache__/__init__.cpython-35.pyc | Bin 183 -> 160 bytes main.py | 28 ++++++++++++------ policy_value_network.py | 4 +-- 12 files changed, 23 insertions(+), 13 deletions(-) mode change 100755 => 100644 chessman/__pycache__/__init__.cpython-35.pyc diff --git a/Mastering_Chess_and_Shogi_by_Self-Play_with_a_General_Reinforcement_Learning_Algorithm.ipynb b/Mastering_Chess_and_Shogi_by_Self-Play_with_a_General_Reinforcement_Learning_Algorithm.ipynb index 8d7c777..00ccd44 100644 --- a/Mastering_Chess_and_Shogi_by_Self-Play_with_a_General_Reinforcement_Learning_Algorithm.ipynb +++ b/Mastering_Chess_and_Shogi_by_Self-Play_with_a_General_Reinforcement_Learning_Algorithm.ipynb @@ -579,7 +579,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.7" + "version": "3.8.5" } }, "nbformat": 4, diff --git a/Mastering_the_Game_of_Go_without_Human_Knowledge.ipynb b/Mastering_the_Game_of_Go_without_Human_Knowledge.ipynb index 6626403..5df5189 100644 --- a/Mastering_the_Game_of_Go_without_Human_Knowledge.ipynb +++ b/Mastering_the_Game_of_Go_without_Human_Knowledge.ipynb @@ -837,7 +837,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.7" + "version": "3.8.5" } }, "nbformat": 4, diff --git a/chessman/__pycache__/Bing.cpython-35.pyc b/chessman/__pycache__/Bing.cpython-35.pyc index 2d8028c3665b43c7b1af4743262d9f1180927d48..8747aa4df7f4e973ee5d94a0ca043402d50e2857 100644 GIT binary patch delta 54 zcmZ3<`;CV~oR^oY_LM}zMvhKKd42uR;?$yI{lxr?{Jiw!jKsWj{p93~)Z${@s??(V K&5s$6u>b(#oD(kq delta 105 zcmeyyvyzuXoR^o&=}2|dMvhL#pd|g!;?$yI{p5_)y!7Id{Jd0sm;Ca)oczQTpkSVE wN@{9>Zcb`qQC?-<)&Kwi diff --git a/chessman/__pycache__/Che.cpython-35.pyc b/chessman/__pycache__/Che.cpython-35.pyc index 441c38d17a4c53509c88bb6f3f3108c15191a06c..988e2950d83664ffe422ed64a858635ae7e8fecb 100644 GIT binary patch delta 54 zcmaFPy@i`YoR^oY_LM}zMvi<&c|HBm;?$yI{lxr?{Jiw!jKsWj{p93~)Z${@s??(V J%_kYOphW%9;?$yI{p5_)y!7Id{Jd0sm;Ca)oczQTpkSVE wN@{9>Zcb`qQC?Zcb`qQC?OphW%9;?$yI{p5_)y!7Id{Jd0sm;Ca)oczQTpkSVE wN@{9>Zcb`qQC?Zcb`qQC?gQ70G~o8IRF3v diff --git a/chessman/__pycache__/Shuai.cpython-35.pyc b/chessman/__pycache__/Shuai.cpython-35.pyc index e9e3ead898e12b01e971a94985ffcb0490cb2a00..b7b9587c1fdbc158dc2f4d81195286a16417eacc 100644 GIT binary patch delta 54 zcmZ3*`<;hFoR^oY_LM}zMve+bc?12>;?$yI{lxr?{Jiw!jKsWj{p93~)Z${@s??(V K&6gN&umAwya}y!} delta 105 zcmey)vx=8PoR^o&=}2|dMve-`pk)2f;?$yI{p5_)y!7Id{Jd0sm;Ca)oczQTpkSVE wN@{9>Zcb`qQC?;?$yI{lxr?{Jiw!jKsWj{p93~)Z${@s??(V J&8Ha?SpeR|5}yD7 delta 105 zcmey#y@rQFoR^o&=}2|dMvg+ppk)2f;?$yI{p5_)y!7Id{Jd0sm;Ca)oczQTpkSVE wN@{9>Zcb`qQC?MgRZ+ diff --git a/chessman/__pycache__/__init__.cpython-35.pyc b/chessman/__pycache__/__init__.cpython-35.pyc old mode 100755 new mode 100644 index d2aac0b14205ba517d730785ca8e6e507cee848b..f37b022bf738d0e3e608b5eb8888c0d78d48a63c GIT binary patch delta 61 zcmdnaxPXyEoR^oY_LM}zM2y!7OZ#JqI<cz~`5?u`#kq-jG4b)4d6^~g@p=W7w>WHa^HWN5QtcQS K7#NCym;nI2+a{m@ diff --git a/main.py b/main.py index 5f09bbb..a10da15 100755 --- a/main.py +++ b/main.py @@ -6,6 +6,11 @@ asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) import tensorflow as tf + +# from tensorflow.python.framework import ops +# ops.reset_default_graph() + + import numpy as np import os import sys @@ -20,13 +25,13 @@ from threading import Lock from concurrent.futures import ThreadPoolExecutor -def flipped_uci_labels(param): +def flipped_uci_labels(param):#快速翻动标签 def repl(x): return "".join([(str(9 - int(a)) if a.isdigit() else a) for a in x]) - +# Python isdigit() 方法检测字符串是否只由数字组成。 return [repl(x) for x in param] -# 创建所有合法走子UCI,size 2086 +# 创建所有合法走子UCI,size 2086 UCCI中国象棋通用引擎协议 def create_uci_labels(): labels_array = [] letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] @@ -51,6 +56,7 @@ def create_uci_labels(): [(l1, t) for t in range(10)] + \ [(l1 + a, n1 + b) for (a, b) in [(-2, -1), (-1, -2), (-2, 1), (1, -2), (2, -1), (-1, 2), (2, 1), (1, 2)]] # 马走日 + #z画一个坐标系可以看出来,这些点是代表马🐎在当前位置可以走的8个位置 for (l2, n2) in destinations: if (l1, n1) != (l2, n2) and l2 in range(9) and n2 in range(10): move = letters[l1] + numbers[n1] + letters[l2] + numbers[n2] @@ -64,11 +70,11 @@ def create_uci_labels(): return labels_array -def create_position_labels(): +def create_position_labels():#创建一个位置/安置标签 labels_array = [] - letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] + letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']#横坐标 letters.reverse() - numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] + numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']#纵坐标 for l1 in range(9): for n1 in range(10): @@ -77,7 +83,7 @@ def create_position_labels(): # labels_array.reverse() return labels_array -def create_position_labels_reverse(): +def create_position_labels_reverse(): #创建一个新的位置标签 labels_array = [] letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] letters.reverse() @@ -90,7 +96,7 @@ def create_position_labels_reverse(): labels_array.reverse() return labels_array -class leaf_node(object): +class leaf_node(object): #目标叶子节点 def __init__(self, in_parent, in_prior_p, in_state): self.P = in_prior_p self.Q = 0 @@ -120,6 +126,8 @@ def get_Q_plus_U(self, c_puct): this node's prior adjusted for its visit count, u c_puct -- a number in (0, inf) controlling the relative impact of values, Q, and prior probability, P, on this node's score. + 计算并返回该节点的值:叶计算、Q和 该节点已根据其访问计数 + (u c_puck——(0,inf)中的一个数字,控制值Q和的相对影响 在这个节点的scor上,先验概率P """ # self._u = c_puct * self._P * np.sqrt(self._parent._n_visits) / (1 + self._n_visits) self.U = c_puct * self.P * np.sqrt(self.parent.N) / ( 1 + self.N) @@ -1333,7 +1341,9 @@ def get_action(self, state, temperature = 1e-3): # for i in range(self.playout_counts): # state_sim = copy.deepcopy(state) # self.mcts.do_simulation(state_sim, self.game_borad.current_player, self.game_borad.restrict_round) - + # 取得当前局面下所有子节点的合法走子和相应的访问量。 + # 这个所有子节点可能并不会覆盖所有合法的走子,这个是由树搜索的质量决定的,加大模拟次数会搜索更多不同的走法, + # 就是加大思考的深度,考虑更多的局面,避免出现有些特别重要的棋步却没有考虑到的情况。 self.mcts.main(state, self.game_borad.current_player, self.game_borad.restrict_round, self.playout_counts) actions_visits = [(act, nod.N) for act, nod in self.mcts.root.child.items()] diff --git a/policy_value_network.py b/policy_value_network.py index bb6c5f9..719823f 100755 --- a/policy_value_network.py +++ b/policy_value_network.py @@ -1,7 +1,7 @@ #coding:utf-8 import tensorflow as tf +from tensorflow.python.framework import ops import numpy as np - import os @@ -12,7 +12,7 @@ def __init__(self, res_block_nums = 7): self.is_logging = True """reset TF Graph""" - tf.reset_default_graph() + ops.reset_default_graph() """Creat a new graph for the network""" # g = tf.Graph()