-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·55 lines (42 loc) · 1.11 KB
/
utils.py
File metadata and controls
executable file
·55 lines (42 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
import chess
import chess.svg
import numpy as np
import math
values = {
"p": 1,
"r": 5,
"n": 3,
"b": 3,
"q": 9,
"k": 0,
}
def get_pieces (pieceMap, colour):
pieces = []
for piece in pieceMap.values():
if colour == 1 and piece.symbol().isupper():
pieces.append(piece.symbol())
elif colour == -1 and piece.symbol().islower():
pieces.append(piece.symbol())
return pieces
def total_value(pieceList):
total = 0
for piece in pieceList:
piece = piece.lower()
total += values[piece]
return total
# normalize value of board
# -1 = black winning
# 1 = white winning
def sigmoid(x):
sig = math.exp(-np.logaddexp(0, -x))
return sig / 0.5 - 1
def evaluate_board(board):
pieceMap = board.piece_map()
listPiecesW = get_pieces(pieceMap, 1) # 1 = white
listPiecesB = get_pieces(pieceMap, -1) # -1 = black
# get value of white's pieceList
whiteVal = total_value(listPiecesW)
# get value of black's pieceList
blackVal = total_value(listPiecesB)
return sigmoid(whiteVal-blackVal)