Advertisement
alkkofficial

Untitled

Mar 30th, 2023
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. # PROTOTYPE GEN 3.0
  2. # zan1ling4 真零 | TrueZero
  3. # imports
  4. import numpy as np
  5. import chess
  6. import pandas as pd
  7. import joblib
  8. import boardnotation
  9. # set up AI (random forest)
  10.  
  11. rf = joblib.load("forest.joblib")
  12.  
  13.  
  14. # set up important functions
  15. def board_data(board):
  16. board_array = np.zeros((8, 8, 13), dtype=np.int64)
  17. move_data = {
  18. "p": 0,
  19. "k": 0,
  20. "q": 0,
  21. "r": 0,
  22. "n": 0,
  23. "b": 0,
  24. "empty": 0,
  25. }
  26. for i in range(64):
  27. piece = board.piece_at(i)
  28. if piece != None:
  29. x = str(piece).lower()
  30. move_data[x] = move_data[x] + 1
  31. else:
  32. move_data["empty"] = move_data["empty"] + 1
  33. if piece is not None:
  34. color = int(piece.color)
  35. piece_type = piece.piece_type - 1
  36. board_array[i // 8][i % 8][piece_type + 6 * color] = 1
  37. else:
  38. board_array[i // 8][i % 8][-1] = 1
  39. return board_array.flatten(), move_data
  40.  
  41.  
  42. def board_format(board):
  43. ai_moves = list(board)
  44. counter = 0
  45. final_li = []
  46. temp_li = []
  47. while counter < len(ai_moves):
  48. if counter % 13 == 0 and counter > 0:
  49. final_li.append(temp_li)
  50. temp_li = []
  51. temp_li.append(ai_moves[counter])
  52. counter += 1
  53. store = []
  54. for x in final_li:
  55. x = [str(i) for i in x]
  56. x = "".join(x)
  57. store.append(int(x, 2))
  58. final_li = store
  59. final_li = [str(i) for i in final_li]
  60. final_li = "".join(final_li)
  61. final_data = float(final_li)
  62. # Define the integer value to rescale
  63. x = final_data
  64.  
  65. # Define the range of values for the logarithmic scale
  66. start = 1
  67. stop = 1.1 # 2
  68.  
  69. # Define the base of the logarithm (10 for base-10 scaling)
  70. base = 10
  71.  
  72. # Rescale the integer logarithmically
  73. rescaled = (
  74. (np.log10(x) - np.log10(start))
  75. / (np.log10(stop) - np.log10(start))
  76. * (np.log10(base) ** 2)
  77. )
  78. # Return the rescaled value
  79. return rescaled
  80.  
  81. def append_data(node,move_count,piece_stat):
  82. df = pd.DataFrame(
  83. columns=[
  84. "board_data",
  85. "num_of_moves",
  86. "p",
  87. "k",
  88. "q",
  89. "r",
  90. "n",
  91. "b",
  92. "empty",
  93. ]
  94. )
  95. d = {
  96. "board_data": node,
  97. "num_of_moves": move_count,
  98. "p": piece_stat["p"],
  99. "k": piece_stat["k"],
  100. "q": piece_stat["q"],
  101. "r": piece_stat["r"],
  102. "n": piece_stat["n"],
  103. "b": piece_stat["b"],
  104. "empty": piece_stat["empty"],
  105. }
  106. df.loc[len(df)] = d
  107. return df
  108.  
  109.  
  110. def negamax_ab(node, piece_stat, alpha, beta, board, move_count, move, colour, depth=2,):
  111. if depth == 0:
  112. node = board_format(node) # make board readable for randomforest
  113. df = append_data(node,move_count,piece_stat) # function that makes data understandable for the random forest
  114. best_val = rf.predict(df) * colour
  115. return best_val, move
  116. child_nodes = list(board.legal_moves)
  117. # child_nodes = ordermoves(child_nodes) # make an ordermove function
  118. value = -np.inf
  119. indexer = 0
  120. best_move = ""
  121. for child in child_nodes:
  122. test_board = board.copy()
  123. test_board.push(child)
  124. str_move = str(child)
  125. child, piece_stat = board_data(test_board)
  126. x,y = negamax_ab(child, piece_stat, -beta, -alpha ,board,move_count,str_move,-colour, depth - 1,)
  127. v = float(x)
  128. if -v > value:
  129. value = -v
  130. best_move = y
  131. alpha = max(alpha, value)
  132. test_board.pop()
  133. if alpha >= beta:
  134. break
  135. indexer += 1
  136. return value, best_move # y is the best move
  137.  
  138.  
  139. # set up chess game
  140.  
  141. NUMBER_OF_GAMES = 10
  142.  
  143.  
  144. for _ in range(NUMBER_OF_GAMES):
  145. board = chess.Board()
  146. legal_moves = board.legal_moves
  147. raw_board, piece_stat = board_data(board)
  148. move_count = 0
  149. df = pd.read_csv("all_openings.csv")
  150. df.sample(frac=1)
  151. opening = df.iloc[np.random.randint(0,len(df))]
  152. opening = str(opening["pgn"])
  153. right_moves = boardnotation.convert_pgn_to_lan(str(opening))
  154. right_moves = right_moves.split()
  155. final_str = ""
  156. for x in right_moves:
  157. final_str = final_str + x + " "
  158. final_str = final_str[:-1]
  159. final_str = final_str.split()
  160. for move in final_str:
  161. board.push(chess.Move.from_uci(move))
  162. print((move))
  163. while not board.is_game_over():
  164. if move_count % 2 == 0:
  165. colour = 1 # white
  166. else:
  167. colour = -1 # black
  168. score,move = negamax_ab(raw_board, piece_stat, -np.inf, np.inf, board, move_count,"",colour, 2)
  169. print(move)
  170. board.push(chess.Move.from_uci(move))
  171. move_count += 1
  172. print(board.result())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement