def move(self, model, board, temp=1.0, dirichlet=True): #Given a python-chess board, give a normal move turn = board.turn k_castle = board.has_kingside_castling_rights(turn) q_castle = board.has_queenside_castling_rights(turn) kb_castle = board.has_kingside_castling_rights(not turn) qb_castle = board.has_queenside_castling_rights(not turn) check = board.is_check() position = helper.conversion(str(board), turn, k_castle, q_castle, check, kb_castle, qb_castle) #We will be exploring the statespace using only the policy-head policy, _ = model.predict(position) policy = policy.reshape(1858, 1) move = helper.policy_to_move(policy, board, dirichlet=dirichlet, temp=temp) #Push move now try: board.push_uci(move) except: print("Error with move") for legal_move in board.legal_moves: board.push(legal_move) break
def test_codeblock_attributes(): doc = conversion( ''' ::: {latex-admonition-color=xyz latex-admonition-linewidth=xyz' latex-admonition-margin=xyz latex-admonition-innermargin=xyz latex-admonition-localfootnotes=true} ::: ::::::::: ''', 'latex') text = convert_text(doc, input_format='panflute', output_format='latex', extra_args=['--wrap=none']) assert '\\begin{env-' in text assert '\\end{env-' in text assert pandoc_latex_admonition.environment_option( 'left', 2, 5, -4, 'black') in doc.get_metadata()['header-includes'][-1]
def main(): # read in data data = pd.read_csv("cldf/data.csv") data = data.sort_values( by=['var_id']) # ensure data is ordered by line for future filtering coding_conversions = pd.read_csv("etc/codes.csv") # create new dataframe to fill in new_data = pd.DataFrame( columns=["song_id", "society_id", "var_id", "code", "ID"]) # for logging line_lag = "line_0" for index, row in data.iterrows(): split_df = pd.DataFrame( columns=["song_id", "society_id", "var_id", "code", "ID"]) line = row['var_id'] if (pd.isnull(row["code"])): print(line + " - " + str(row["song_id"]) + " - " + str(row["society_id"]) + " is missing a value") continue # Data was ordered in line order, so we only need the line set once. # Print the line being analysed for logging purposes if (line_lag != line): print("Recoding " + line + "...") line_lag = line value_set = coding_conversions.loc[coding_conversions["var_id"] == line, "code"].to_numpy() value = row['code'] new_codes = conversion(value, value_set) ## If new codes are -99 then they were invalid codes and we need to correct them if (new_codes[0] == -99): with open("logs/invalid_codes.txt", "a") as invalid_file: invalid_file.write(line + " - " + str(row["song_id"]) + " - " + str(row["society_id"]) + " is an invalid code\n") split_df = split_df.append([row] * len(new_codes), ignore_index=True) split_df['code'] = new_codes new_data = new_data.append(split_df) # save file new_data.to_csv("cldf/data.csv", index=False)
def test_beamer_notes(): doc = conversion( ''' ::: {latex-admonition-color=black} ::: This a text[^note] [^note]: This is a *note* ::: ''', 'beamer') text = convert_text(doc, input_format='panflute', output_format='beamer', extra_args=['--wrap=none']) assert 'This a text\\footnote<.->[frame]{This is a \\emph{note}}' in text
def play_from( board, positions, moves, values, lock ): #Possible improvement could be placing our data into tuples?? post-processing required tho import chess import chess.uci import numpy as np from helper import conversion, one_hot stockfish = chess.uci.popen_engine("stockfish") info_handler = chess.uci.InfoHandler() stockfish.info_handlers.append(info_handler) stockfish.uci() while not board.is_game_over(): stockfish.position(board) stock_move = stockfish.go( movetime=250) #Set stockfish to 5 moves per second #Generate position turn = board.turn k_castle = board.has_kingside_castling_rights(turn) q_castle = board.has_queenside_castling_rights(turn) kb_castle = board.has_kingside_castling_rights(not turn) qb_castle = board.has_queenside_castling_rights(not turn) check = board.is_check() position = conversion(str(board), turn, k_castle, q_castle, check, kb_castle, qb_castle) #Append position to training data mate = info_handler.info["score"][1].mate #Acquire lock so that all data is appended to our lists in the correct order! lock.acquire() positions.append(position) moves.append(one_hot( stock_move[0])) #Add stockfish's choice to training data if mate is None: value = np.tanh(info_handler.info["score"][1].cp / 300) values.append(value) else: values.append(mate / np.abs(mate)) lock.release() board.push(stock_move[0])
def test_latex_images(): doc = conversion( ''' ::: {latex-admonition-color=black} ::: ![Title]() Hello ::::::::: ''', 'latex') text = convert_text(doc, input_format='panflute', output_format='latex', extra_args=['--wrap=none']) assert '\\begin{env-' in text assert '\\end{env-' in text assert pandoc_latex_admonition.environment_option( 'left', 2, 5, -4, 'black') in doc.get_metadata()['header-includes'][-1] assert isinstance(doc.content[-1], Para) assert isinstance(doc.content[-1].content[0], Image)
def test_conversion(): # single codings print("singles") single_1 = (conversion(2**1, [1, 4, 7, 10, 13])) == (1, ) single_2 = (conversion(2, [1, 4, 7, 10, 13])) == (1, ) single_3 = (conversion(2**10, [1, 4, 7, 10, 13])) == (10, ) print("twos") # two codings double_1 = (conversion(20, [4, 2, 1])) == (4, 2) # 4 , 2 double_2 = (conversion(1028, [4, 2, 10])) == (2, 10) # 10, 2 print('threes') # # # three codings triple_1 = (conversion(8336, [13, 7, 4, 2, 6])) == (13, 7, 4) # 13, 7 , 4 triple_2 = (conversion(8464, [13, 8, 4, 2, 6])) == (13, 8, 4) # 13, 8, 4 assert all( [single_1, single_2, single_3, double_1, double_2, triple_1, triple_2])
def test_codeblock_classes(): doc = conversion( ''' --- pandoc-latex-admonition: - classes: ['class1', 'class2'] color: red position: right linewidth: 5 innermargin: -8 margin: 10 --- ~~~{.class1 .class2} ~~~ ''', 'latex') text = convert_text(doc, input_format='panflute', output_format='latex', extra_args=['--wrap=none']) assert '\\begin{env-' in text assert '\\end{env-' in text assert pandoc_latex_admonition.environment_option( 'right', 5, -8, 10, 'red') in doc.get_metadata()['header-includes'][-1]