Silas-Blanchard commited on
Commit
a1df8df
·
verified ·
1 Parent(s): 86e378b

model-runner.py

Browse files
Files changed (1) hide show
  1. model-runner.py +135 -0
model-runner.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import chess
2
+ import random
3
+
4
+ def cleanup_output(text, prompt, extra_len):
5
+ section = text[len(prompt):]
6
+ print("Proposed Move: %s" % section)
7
+ valid_letters = ['A','a','B','b','C','c','D','d','E','e','F','f','G','g','H','h']
8
+ valid_pieces = ['p','P','k','K','q','Q','r','R','b','B', 'n', 'N']
9
+ valid_numbers = ['1','2','3','4','5','6','7','8']
10
+
11
+ #if there are any syntatically moves in this string for pieces
12
+ countr = 0
13
+ while countr < len(section) - 3:
14
+ if(section[countr] in valid_pieces and section[countr + 1] in valid_letters and section[countr + 2] in valid_numbers):
15
+ #print(section[countr:countr+3])
16
+ return ' ' + section[countr:countr+3]
17
+ countr+=1
18
+
19
+ #variant for capturing!
20
+ countr = 0
21
+ while countr < len(section) - 4:
22
+ if(section[countr] in valid_pieces and section[countr + 1] == 'x' and section[countr + 2] in valid_letters and section[countr + 3] in valid_numbers):
23
+ #print(section[countr:countr+3])
24
+ return ' ' + section[countr:countr+5]
25
+ countr+=1
26
+
27
+ #same as moves but for pawns
28
+ countr = 0
29
+ while countr < len(section) - 2:
30
+ if(section[countr] in valid_letters and section[countr+1] in valid_numbers):
31
+ #print(section[countr:countr+2])
32
+ return ' ' + section[countr:countr+2]
33
+ countr+=1
34
+
35
+ #variant for capturing!
36
+ countr = 0
37
+ while countr < len(section) -4:
38
+ if(section[countr] in valid_letters and section[countr+1] == 'x' and section[countr+2] in valid_letters and section[countr + 3] in valid_numbers):
39
+ #print(section[countr:countr+2])
40
+ return ' ' + section[countr:countr+4]
41
+ countr+=1
42
+
43
+ return ' e8'
44
+
45
+ class AInstance:
46
+ def __init__(self, type, generator):
47
+ self.type = type
48
+ self.game_end = False
49
+ self.generator = generator
50
+
51
+ #All this does it take the gamestate and add the ai-generated result to it
52
+ def move(self, game_state):
53
+ if(self.type == "gpt2-medium-chess"):
54
+ prompt = "1-0 2700 1350 " + game_state
55
+ extra_len = 7
56
+ else:
57
+ prompt = game_state
58
+ extra_len = 5
59
+ countr = 0
60
+ while True:
61
+ generated_text = self.generator(prompt, max_length=len(prompt) + extra_len, num_return_sequences=1)[0]['generated_text']
62
+ selected_move = cleanup_output(generated_text, prompt, extra_len)
63
+
64
+ #if this move is valid then return it
65
+ proposed_board = game_state + selected_move
66
+ if(verify_move(proposed_board)):
67
+ return proposed_board
68
+ countr+=1
69
+ #goes fifty times until the AInstance object flags itself as "ended" (fundamentally unable to make a valid move)
70
+ if(countr > 50):
71
+ self.game_end = True
72
+ break
73
+
74
+ def check_if_end(self):
75
+ return self.game_end
76
+
77
+ def verify_move(string):
78
+ board = chess.Board()
79
+ print("Board: %s\n" % string)
80
+ for move in string.split():
81
+ #if this move makes no sense it will return false and the game will try again to generate a good move
82
+ try:
83
+ board.push_san(move)
84
+ except:
85
+ return False
86
+ if(board.is_valid):
87
+ return True
88
+ return False
89
+
90
+ def check_mate(string):
91
+ #simulates mate idk
92
+ if(random.randrange(0,100) == 4):
93
+ print("H")
94
+ return True
95
+ return False
96
+
97
+ def print_game(string):
98
+ print("Some kind of visualization for the chess board based on this string: %s" % string)
99
+
100
+ def make_move(instance, game_state):
101
+ print("%s's move" % instance.type)
102
+ return_state = game_state
103
+ return_state = instance.move(game_state)
104
+ game_ongoing = True
105
+ if(instance.check_if_end()):
106
+ print("This player claims they can't make a valid move after 50 tries: %s" % instance.type)
107
+ game_ongoing = False
108
+ if(check_mate(return_state)):
109
+ print("This player claims mates: %s" % instance.type)
110
+ game_ongoing = False
111
+ return(return_state, game_ongoing)
112
+
113
+
114
+ def main():
115
+ if(random.randint(0,1) == 1):
116
+ white = AInstance("gpt2", generator)
117
+ black = AInstance("gpt2-medium-chess", generator2)
118
+ print("Gpt2 is White and Gpt2 Optimized is Black\n")
119
+ else:
120
+ white = AInstance("gpt2-medium-chess", generator2)
121
+ black = AInstance("gpt2", generator)
122
+ print("Gpt2 is Black and Gpt2 Optimized is White\n")
123
+
124
+ game_state = "e4 e5"
125
+ game_ongoing = True
126
+ while game_ongoing:
127
+ game_state, game_ongoing = make_move(white, game_state)
128
+ if not game_ongoing:
129
+ print_game(game_state)
130
+ break
131
+ game_state, game_ongoing = make_move(black, game_state)
132
+ if not game_ongoing:
133
+ print_game(game_state)
134
+ break
135
+ main()