Skip to content

Commit

Permalink
Make each feature set provide initial PSQT values for each feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sopel97 committed May 18, 2021
1 parent 4bf282f commit ac2d94a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
8 changes: 7 additions & 1 deletion feature_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,10 @@ def get_virtual_to_real_features_gather_indices(self):
indices.append([offset + i for i in i_fact])
real_offset += feature.num_real_features
offset += feature.num_features
return indices
return indices

def get_initial_psqt_features(self):
init = []
for feature in self.features:
init += feature.get_initial_psqt_features()
return init
28 changes: 28 additions & 0 deletions halfka.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ def halfka_idx(is_white_pov: bool, king_sq: int, sq: int, p: chess.Piece):
p_idx = (p.piece_type - 1) * 2 + (p.color != is_white_pov)
return 1 + orient(is_white_pov, sq) + p_idx * NUM_SQ + king_sq * NUM_PLANES

def halfka_psqts():
# values copied from stockfish, in stockfish internal units
piece_values = {
chess.PAWN : 126,
chess.KNIGHT : 781,
chess.BISHOP : 825,
chess.ROOK : 1276,
chess.QUEEN : 2538
}

values = [0] * (NUM_PLANES * NUM_SQ)

for ksq in range(64):
for s in range(64):
for pt, val in piece_values.items():
idxw = halfka_idx(True, ksq, s, chess.Piece(pt, chess.WHITE))
idxb = halfka_idx(True, ksq, s, chess.Piece(pt, chess.BLACK))
values[idxw] = val
values[idxb] = -val

return values

class Features(FeatureBlock):
def __init__(self):
super(Features, self).__init__('HalfKA', 0x5f134cb8, OrderedDict([('HalfKA', NUM_PLANES * NUM_SQ)]))
Expand All @@ -27,6 +49,9 @@ def piece_features(turn):
return indices
return (piece_features(chess.WHITE), piece_features(chess.BLACK))

def get_initial_psqt_features(self):
return halfka_psqts()

class FactorizedFeatures(FeatureBlock):
def __init__(self):
super(FactorizedFeatures, self).__init__('HalfKA^', 0x5f134cb8, OrderedDict([('HalfKA', NUM_PLANES * NUM_SQ), ('A', NUM_SQ * NUM_PT)]))
Expand All @@ -42,6 +67,9 @@ def get_feature_factors(self, idx):

return [idx, self.get_factor_base_feature('A') + a_idx]

def get_initial_psqt_features(self):
return halfka_psqts() + [0] * (NUM_SQ * NUM_PT)

'''
This is used by the features module for discovery of feature blocks.
'''
Expand Down
6 changes: 6 additions & 0 deletions halfkp.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def piece_features(turn):
return indices
return (piece_features(chess.WHITE), piece_features(chess.BLACK))

def get_initial_psqt_features(self):
raise Exception('Not supported yet. See HalfKA')

class FactorizedFeatures(FeatureBlock):
def __init__(self):
super(FactorizedFeatures, self).__init__('HalfKP^', 0x5d69d5b8, OrderedDict([('HalfKP', NUM_PLANES * NUM_SQ), ('HalfK', NUM_SQ), ('P', NUM_SQ * 10 )]))
Expand Down Expand Up @@ -60,6 +63,9 @@ def get_feature_factors(self, idx):

return [idx, self.get_factor_base_feature('HalfK') + k_idx, self.get_factor_base_feature('P') + p_idx]

def get_initial_psqt_features(self):
raise Exception('Not supported yet. See HalfKA^')

'''
This is used by the features module for discovery of feature blocks.
'''
Expand Down

0 comments on commit ac2d94a

Please sign in to comment.