Skip to content

Commit 874ea1a

Browse files
committed
go: add missing field types
1 parent 29917b4 commit 874ea1a

File tree

3 files changed

+63
-50
lines changed

3 files changed

+63
-50
lines changed

Benchmark/go/advanced/main.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
import math
44
from square import Square
55
from constants import SIZE, GAMES, KOMI, EMPTY, WHITE, BLACK, SHOW, PASS, MAXMOVES, TIMESTAMP, MOVES
6-
from typing import List
6+
from typing import List, final, Set
77
import time
88

99
#@fields({'empties':List(int)
1010
# ,'board':{'useful':Function(NamedParameters([('pos',int)])
1111
# ,int)}
1212
# ,'empty_pos':List(int)})
13+
@final
1314
class EmptySet:
1415
#def __init__(self:EmptySet, board:{'useful':Function(NamedParameters([('pos',int)]), int)})->Void:
1516
def __init__(self, board: Board) -> None:
16-
self.board = board
17-
self.empties = list(range(SIZE*SIZE))
18-
self.empty_pos = list(range(SIZE*SIZE))
17+
self.board: Board = board
18+
self.empties: List[int] = list(range(SIZE*SIZE))
19+
self.empty_pos: List[int] = list(range(SIZE*SIZE))
1920

2021
#def random_choice(self:EmptySet)->int:
2122
def random_choice(self) -> int:
@@ -46,11 +47,12 @@ def set(self, i: int, pos: int) -> None:
4647
self.empty_pos[pos] = i
4748

4849
#@fields({'hash':int})
50+
@final
4951
class ZobristHash:
5052
#def __init__(self:ZobristHash, board:{'squares':List(Square)})->Void:
5153
def __init__(self, board: Board) -> None:
52-
self.hash_set = set()
53-
self.hash = 0
54+
self.hash_set: Set[int] = set()
55+
self.hash: int = 0
5456
for square in board.squares:
5557
self.hash ^= square.zobrist_strings[EMPTY]
5658
self.hash_set.clear()
@@ -77,18 +79,19 @@ def dupe(self) -> bool:
7779
# ,'white_dead':int
7880
# ,'black_dead':int
7981
# ,'lastmove':int})
82+
@final
8083
class Board:
8184
#def __init__(self:Board)->Void:
8285
def __init__(self) -> None:
83-
self.squares = []
84-
self.emptyset = EmptySet(self)
85-
self.zobrist = ZobristHash(self)
86-
self.color = BLACK
87-
self.finished = False
88-
self.lastmove = -2
89-
self.history = []
90-
self.white_dead = 0
91-
self.black_dead = 0
86+
self.squares: List[Square] = []
87+
self.emptyset: EmptySet = EmptySet(self)
88+
self.zobrist: ZobristHash = ZobristHash(self)
89+
self.color: int = BLACK
90+
self.finished: bool = False
91+
self.lastmove: int = -2
92+
self.history: List[int] = []
93+
self.white_dead: int = 0
94+
self.black_dead: int = 0
9295
self.squares = [Square(self, pos) for pos in range(SIZE*SIZE)]
9396
for square in self.squares:
9497
square.set_neighbours()
@@ -241,16 +244,17 @@ def check(self) -> None:
241244

242245

243246
#@fields({'pos':int, 'wins':int, 'losses':int})
247+
@final
244248
class UCTNode:
245249
#def __init__(self:UCTNode)->Void:
246250
def __init__(self) -> None:
247-
self.bestchild = None
248-
self.pos = -1
249-
self.wins = 0
250-
self.losses = 0
251-
self.pos_child = [None for x in range(SIZE*SIZE)]
252-
self.parent = None
253-
self.unexplored = True
251+
self.bestchild: None | UCTNode = None
252+
self.pos: int = -1
253+
self.wins: int = 0
254+
self.losses: int = 0
255+
self.pos_child: List[None | UCTNode] = [None for x in range(SIZE*SIZE)]
256+
self.parent: None | UCTNode = None
257+
self.unexplored: List[int] = []
254258

255259
#def play(self:UCTNode, board:Board)->Void:
256260
def play(self, board: Board) -> None:
@@ -285,7 +289,7 @@ def select(self, board: Board) -> int:
285289
self.unexplored[i] = self.unexplored[len(self.unexplored)-1]
286290
self.unexplored.pop()
287291
return pos
288-
elif self.bestchild:
292+
elif self.bestchild is not None:
289293
return self.bestchild.pos
290294
else:
291295
return PASS
@@ -315,7 +319,11 @@ def update_path(self, board: Board, color: int, path: List[UCTNode]) -> None:
315319
#def score(self:UCTNode)->float:
316320
def score(self) -> float:
317321
winrate = self.wins/float(self.wins+self.losses)
318-
parentvisits = self.parent.wins+self.parent.losses
322+
parentvisits = 0
323+
if self.parent is not None:
324+
parentvisits += self.parent.wins
325+
if self.parent is not None:
326+
parentvisits += self.parent.losses
319327
if not parentvisits:
320328
return winrate
321329
nodevisits = self.wins+self.losses

Benchmark/go/advanced/square.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22
import random
3-
from typing import List
3+
from typing import List, final
44
import __static__
55
from constants import SIZE, GAMES, KOMI, EMPTY, WHITE, BLACK, SHOW, PASS, MAXMOVES, TIMESTAMP, MOVES
66

@@ -32,6 +32,7 @@
3232
def to_pos(x: int, y: int) -> int:
3333
return y * SIZE + x
3434

35+
@final
3536
class Square:
3637
def __init__(self: Square, board: Board, pos: int) -> None:
3738
self.board: Board = board

Benchmark/go/shallow/main.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import math
44
from square import Square
55
from constants import SIZE, GAMES, KOMI, EMPTY, WHITE, BLACK, SHOW, PASS, MAXMOVES, TIMESTAMP, MOVES
6-
from typing import List
6+
from typing import List, Set
77
import time
88

99

@@ -14,9 +14,9 @@
1414
class EmptySet:
1515
# def __init__(self:EmptySet, board:{'useful':Function(NamedParameters([('pos',int)]), int)})->Void:
1616
def __init__(self, board: Board) -> None:
17-
self.board = board
18-
self.empties = list(range(SIZE * SIZE))
19-
self.empty_pos = list(range(SIZE * SIZE))
17+
self.board: Board = board
18+
self.empties: List[int] = list(range(SIZE*SIZE))
19+
self.empty_pos: List[int] = list(range(SIZE*SIZE))
2020

2121
# def random_choice(self:EmptySet)->int:
2222
def random_choice(self) -> int:
@@ -51,8 +51,8 @@ def set(self, i: int, pos: int) -> None:
5151
class ZobristHash:
5252
# def __init__(self:ZobristHash, board:{'squares':List(Square)})->Void:
5353
def __init__(self, board: Board) -> None:
54-
self.hash_set = set()
55-
self.hash = 0
54+
self.hash_set: Set[int] = set()
55+
self.hash: int = 0
5656
for square in board.squares:
5757
self.hash ^= square.zobrist_strings[EMPTY]
5858
self.hash_set.clear()
@@ -83,16 +83,16 @@ def dupe(self) -> bool:
8383
class Board:
8484
# def __init__(self:Board)->Void:
8585
def __init__(self) -> None:
86-
self.squares = []
87-
self.emptyset = EmptySet(self)
88-
self.zobrist = ZobristHash(self)
89-
self.color = BLACK
90-
self.finished = False
91-
self.lastmove = -2
92-
self.history = []
93-
self.white_dead = 0
94-
self.black_dead = 0
95-
self.squares = [Square(self, pos) for pos in range(SIZE * SIZE)]
86+
self.squares: List[Square] = []
87+
self.emptyset: EmptySet = EmptySet(self)
88+
self.zobrist: ZobristHash = ZobristHash(self)
89+
self.color: int = BLACK
90+
self.finished: bool = False
91+
self.lastmove: int = -2
92+
self.history: List[int] = []
93+
self.white_dead: int = 0
94+
self.black_dead: int = 0
95+
self.squares = [Square(self, pos) for pos in range(SIZE*SIZE)]
9696
for square in self.squares:
9797
square.set_neighbours()
9898
square.color = EMPTY
@@ -249,13 +249,13 @@ def check(self) -> None:
249249
class UCTNode:
250250
# def __init__(self:UCTNode)->Void:
251251
def __init__(self) -> None:
252-
self.bestchild = None
253-
self.pos = -1
254-
self.wins = 0
255-
self.losses = 0
256-
self.pos_child = [None for x in range(SIZE * SIZE)]
257-
self.parent = None
258-
self.unexplored = True
252+
self.bestchild: None | UCTNode = None
253+
self.pos: int = -1
254+
self.wins: int = 0
255+
self.losses: int = 0
256+
self.pos_child: List[None | UCTNode] = [None for x in range(SIZE*SIZE)]
257+
self.parent: None | UCTNode = None
258+
self.unexplored: List[int] = []
259259

260260
# def play(self:UCTNode, board:Board)->Void:
261261
def play(self, board: Board) -> None:
@@ -290,7 +290,7 @@ def select(self, board: Board) -> int:
290290
self.unexplored[i] = self.unexplored[len(self.unexplored) - 1]
291291
self.unexplored.pop()
292292
return pos
293-
elif self.bestchild:
293+
elif self.bestchild is not None:
294294
return self.bestchild.pos
295295
else:
296296
return PASS
@@ -322,7 +322,11 @@ def update_path(self, board: Board, color: int, path: List[UCTNode]) -> None:
322322
# def score(self:UCTNode)->float:
323323
def score(self) -> float:
324324
winrate = self.wins / float(self.wins + self.losses)
325-
parentvisits = self.parent.wins + self.parent.losses
325+
parentvisits = 0
326+
if self.parent is not None:
327+
parentvisits += self.parent.wins
328+
if self.parent is not None:
329+
parentvisits += self.parent.losses
326330
if not parentvisits:
327331
return winrate
328332
nodevisits = self.wins + self.losses

0 commit comments

Comments
 (0)