import chess.uci import chess.pgn import sys import string import NeuralNet import os import ast # Neural net topology topology = [64, 44, 18, 1] # Assigns the existing trained weights in the text file to the neural net. with open("./TrainedWeightsText.txt", "r") as myfile: weightsF = myfile.read().replace('\n', '') weightsL = ast.literal_eval(weightsF) evalNet = NeuralNet.Net(topology) for layer in range(len(evalNet.layers) - 1): for neuron in range(len(evalNet.layers[layer])): evalNet.layers[layer][neuron].outputWeights = weightsL[layer][neuron] path = "./PGNFiles/McDonnell.pgn" # Trains neural network. with open(path) as f: count = 0 for n in range(100): try: print("GAMECOUNT", n) game = chess.pgn.read_game(f) while not game.is_end(): node = game.variations[0]
import copy from BackEndChess import * from ChessBoard import * import random import NeuralNet import ast # Converts string of weights to actual list of weights. with open("./RealWeights/RealWeights9.txt", "r") as myfile: weightsF = myfile.read().replace('\n', '') weightsL = ast.literal_eval(weightsF) # Sets neural network weights to the weights stored in the text file. myNet = NeuralNet.Net([64, 44, 18, 1]) for layer in range(len(myNet.layers)-1): for neuron in range(len(myNet.layers[layer])): myNet.layers[layer][neuron].outputWeights = weightsL[layer][neuron] # Translates the chess board that is in the game to a form which is readable by the neural network. def translateBoard(board): tBoard = [] for row in range(7, -1, -1): for col in range(8): if board[row][col] != None: if isinstance(board[row][col], Pawn): if board[row][col].color == "White": tBoard.append(.01) else: tBoard.append(-.01)