diff --git a/src/eval_constants.hpp b/src/eval_constants.hpp index 0c8f11a9..0601ec55 100644 --- a/src/eval_constants.hpp +++ b/src/eval_constants.hpp @@ -5,169 +5,171 @@ namespace Clockwork { // clang-format off -inline const PParam PAWN_MAT = S(201, 504); -inline const PParam KNIGHT_MAT = S(812, 1582); -inline const PParam BISHOP_MAT = S(863, 1668); -inline const PParam ROOK_MAT = S(1034, 2653); -inline const PParam QUEEN_MAT = S(2402, 4096); +inline const PParam PAWN_MAT = S(202, 507); +inline const PParam KNIGHT_MAT = S(821, 1596); +inline const PParam BISHOP_MAT = S(872, 1682); +inline const PParam ROOK_MAT = S(1041, 2670); +inline const PParam QUEEN_MAT = S(2419, 4123); -inline const PParam TEMPO_VAL = S(69, 53); +inline const PParam TEMPO_VAL = S(69, 58); -inline const PParam BISHOP_XRAY_PAWNS = S(-13, -3); -inline const PParam BISHOP_PAIR_VAL = S(61, 239); +inline const PParam BISHOP_XRAY_PAWNS = S(-13, -2); +inline const PParam BISHOP_PAIR_VAL = S(62, 240); inline const PParam ROOK_OPEN_VAL = S(109, -4); -inline const PParam ROOK_SEMIOPEN_VAL = S(43, 11); -inline const PParam MINOR_BEHIND_PAWN = S(15, 41); +inline const PParam ROOK_SEMIOPEN_VAL = S(44, 12); +inline const PParam MINOR_BEHIND_PAWN = S(15, 42); inline const PParam RESTRICTED_SQUARES = S(19, 7); -inline const PParam DOUBLED_PAWN_VAL = S(-21, -81); +inline const PParam DOUBLED_PAWN_VAL = S(-22, -81); inline const PParam ISOLATED_PAWN_VAL = S(-13, -39); inline const PParam POTENTIAL_CHECKER_VAL = S(-45, -42); -inline const PParam OUTPOST_KNIGHT_VAL = S(49, 44); +inline const PParam OUTPOST_KNIGHT_VAL = S(49, 43); inline const PParam OUTPOST_BISHOP_VAL = S(42, 39); -inline const PParam PAWN_PUSH_THREAT_KNIGHT = S(39, 50); +inline const PParam PAWN_PUSH_THREAT_KNIGHT = S(39, 51); inline const PParam PAWN_PUSH_THREAT_BISHOP = S(42, 3); -inline const PParam PAWN_PUSH_THREAT_ROOK = S(17, 70); +inline const PParam PAWN_PUSH_THREAT_ROOK = S(18, 70); inline const PParam PAWN_PUSH_THREAT_QUEEN = S(62, -51); inline const std::array PAWN_PHALANX = { - S(15, -1), S(33, 35), S(59, 65), S(121, 212), S(386, 402), S(672, 816), + S(15, -2), S(33, 36), S(60, 66), S(122, 214), S(390, 405), S(680, 823), }; inline const std::array DEFENDED_PAWN = { - S(48, 32), S(39, 25), S(53, 56), S(90, 180), S(384, 121), + S(48, 33), S(39, 26), S(54, 57), S(91, 181), S(387, 122), }; inline const std::array PASSED_PAWN = { - S(-93, -259), S(-94, -217), S(-67, -86), S(-22, 39), S(87, 199), S(227, 404), + S(-94, -263), S(-95, -221), S(-68, -88), S(-23, 38), S(87, 198), S(230, 407), }; inline const std::array PASSED_CLEAR_STOPPERS = { - S(-80, 53), S(-92, 35), S(-58, 104), S(-88, 228), S(-55, 339), S(51, 221), + S(-81, 54), S(-92, 36), S(-59, 105), S(-89, 230), S(-54, 341), S(51, 224), }; inline const std::array PASSED_CLEAR_FORWARD = { - S(-16, 68), S(-28, 59), S(-60, 107), S(-32, 134), S(73, 158), S(34, 192), + S(-18, 69), S(-29, 60), S(-60, 108), S(-33, 135), S(73, 160), S(34, 195), }; inline const std::array DEFENDED_PASSED_PUSH = { - S(24, -39), S(24, -2), S(16, 34), S(21, 103), S(70, 270), S(332, 215), + S(24, -40), S(24, -2), S(16, 35), S(22, 103), S(71, 272), S(335, 219), }; inline const std::array BLOCKED_PASSED_PAWN = { - S(14, -36), S(2, 8), S(-3, -27), S(-0, -68), S(-14, -175), S(-166, -450), + S(14, -36), S(2, 8), S(-3, -26), S(-0, -68), S(-14, -176), S(-170, -457), }; inline const std::array FRIENDLY_KING_PASSED_PAWN_DISTANCE = { - S(0, 0), S(20, 233), S(9, 181), S(4, 106), S(12, 67), S(20, 72), S(58, 67), S(61, 38), + S(0, 0), S(20, 233), S(9, 181), S(3, 105), S(11, 66), S(19, 71), S(57, 66), S(61, 36), }; inline const std::array ENEMY_KING_PASSED_PAWN_DISTANCE = { - S(0, 0), S(-132, -47), S(14, -4), S(1, 99), S(27, 149), S(43, 183), S(54, 192), S(40, 172), + S(0, 0), S(-131, -46), S(15, 0), S(3, 103), S(29, 153), S(45, 188), S(56, 196), S(42, 177), }; inline const std::array KNIGHT_MOBILITY = { - S(-91, -392), S(-23, -140), S(7, -26), S(30, 23), S(57, 55), S(73, 92), S(90, 99), S(109, 108), S(129, 61), + S(-95, -396), S(-27, -142), S(4, -27), S(27, 22), S(54, 55), S(70, 92), S(88, 100), S(107, 108), S(127, 61), }; inline const std::array BISHOP_MOBILITY = { - S(-71, -349), S(-18, -129), S(35, -40), S(59, 14), S(78, 56), S(90, 87), S(95, 110), S(98, 131), S(102, 142), S(109, 148), S(117, 142), S(131, 125), S(131, 130), S(116, 95), + S(-74, -352), S(-20, -130), S(32, -40), S(57, 14), S(77, 57), S(89, 88), S(94, 111), S(96, 132), S(101, 143), S(107, 150), S(116, 144), S(130, 127), S(130, 132), S(115, 96), }; inline const std::array ROOK_MOBILITY = { - S(99, -215), S(23, -13), S(44, 21), S(60, 39), S(71, 58), S(75, 72), S(79, 88), S(85, 94), S(90, 106), S(95, 114), S(99, 120), S(99, 130), S(102, 134), S(107, 121), S(127, 82), + S(99, -211), S(24, -11), S(45, 23), S(60, 42), S(71, 60), S(75, 74), S(80, 90), S(86, 96), S(90, 108), S(96, 116), S(100, 123), S(100, 132), S(103, 136), S(108, 123), S(128, 85), }; inline const std::array QUEEN_MOBILITY = { - S(-192, -206), S(-38, -93), S(-14, 49), S(5, 189), S(22, 231), S(29, 284), S(36, 322), S(43, 339), S(47, 362), S(51, 379), S(57, 387), S(61, 394), S(64, 401), S(67, 406), S(67, 409), S(67, 411), S(63, 414), S(66, 406), S(67, 402), S(71, 394), S(61, 385), S(72, 359), S(38, 387), S(-8, 379), S(-42, 378), S(-76, 400), S(-135, 428), S(-101, 364), + S(-193, -204), S(-38, -90), S(-14, 51), S(5, 193), S(22, 236), S(30, 289), S(36, 327), S(43, 345), S(48, 368), S(51, 385), S(57, 393), S(62, 400), S(65, 407), S(67, 413), S(68, 415), S(67, 418), S(64, 420), S(66, 412), S(67, 408), S(71, 400), S(61, 391), S(73, 364), S(37, 393), S(-7, 383), S(-40, 382), S(-75, 405), S(-135, 433), S(-100, 368), }; inline const PParam PAWN_THREAT_KNIGHT = S(196, 154); inline const PParam PAWN_THREAT_BISHOP = S(171, 221); -inline const PParam PAWN_THREAT_ROOK = S(187, 155); -inline const PParam PAWN_THREAT_QUEEN = S(159, 13); +inline const PParam PAWN_THREAT_ROOK = S(190, 153); +inline const PParam PAWN_THREAT_QUEEN = S(160, 21); -inline const std::array MINOR_THREAT = { - S(7, 57), S(93, 94), S(103, 125), S(205, 123), S(162, 3), S(0, 0), -}; -inline const std::array ROOK_THREAT = { - S(7, 57), S(53, 96), S(74, 95), S(17, 11), S(150, -48), S(0, 0), -}; -inline const PParam KING_THREAT = S(-11, 156); -inline const PParam HANGING_PAWN = S(31, 93); -inline const PParam HANGING_NON_PAWN = S(69, 28); +inline const std::array, 2> MINOR_THREAT = {{ + {{ S(8, 57), S(98, 96), S(104, 125), S(178, 94), S(164, 10), }}, + {{ S(6, 58), S(87, 94), S(103, 124), S(219, 614), S(149, 49), }}, +}}; +inline const std::array, 2> ROOK_THREAT = {{ + {{ S(10, 64), S(44, 107), S(59, 112), S(12, 12), S(151, -76), }}, + {{ S(4, 49), S(62, 82), S(95, 72), S(23, 12), S(365, 898), }}, +}}; +inline const PParam KING_THREAT = S(-10, 157); +inline const PParam HANGING_PAWN = S(32, 92); +inline const PParam HANGING_NON_PAWN = S(72, 31); inline const std::array KNIGHT_ON_QUEEN = { - S(21, -17), S(22, 9), + S(21, -17), S(22, 8), }; inline const std::array BISHOP_ON_QUEEN = { - S(42, 45), S(108, -105), + S(42, 45), S(112, -112), }; inline const std::array ROOK_ON_QUEEN = { - S(37, 11), S(47, -6), + S(37, 13), S(49, -7), }; inline const std::array BISHOP_PAWNS = { - S(4, -17), S(4, -15), S(-1, -21), S(-6, -29), S(-12, -37), S(-17, -43), S(-19, -55), S(-25, -56), S(-30, -73), + S(4, -17), S(4, -15), S(-1, -21), S(-6, -29), S(-12, -37), S(-17, -43), S(-19, -55), S(-25, -57), S(-31, -74), }; -inline const PParam ROOK_LINEUP = S(17, 80); +inline const PParam ROOK_LINEUP = S(17, 81); inline const std::array PAWN_PSQT = { - S(182, 274), S(62, 395), S(68, 383), S(155, 269), S(189, 168), S(142, 209), S(121, 239), S(221, 178), // - S(41, 63), S(55, 108), S(33, 61), S(47, -6), S(39, -42), S(12, -14), S(14, 26), S(-20, 53), // - S(38, -13), S(15, -5), S(44, -45), S(25, -69), S(17, -87), S(3, -75), S(-36, -44), S(-38, -5), // - S(-6, -73), S(-31, -43), S(1, -62), S(-10, -78), S(-31, -87), S(-36, -75), S(-82, -57), S(-76, -52), // - S(-14, -110), S(18, -106), S(3, -45), S(-13, -50), S(-40, -60), S(-53, -71), S(-79, -72), S(-83, -74), // - S(9, -107), S(82, -95), S(71, -40), S(25, -11), S(-5, -32), S(-24, -60), S(-50, -59), S(-62, -58), // + S(184, 277), S(63, 399), S(69, 387), S(158, 271), S(193, 168), S(144, 211), S(123, 240), S(222, 181), // + S(42, 64), S(55, 110), S(35, 62), S(48, -4), S(40, -41), S(13, -12), S(15, 27), S(-20, 54), // + S(39, -13), S(15, -4), S(44, -45), S(26, -69), S(18, -87), S(4, -75), S(-36, -44), S(-37, -5), // + S(-6, -73), S(-30, -43), S(1, -61), S(-10, -77), S(-31, -87), S(-35, -75), S(-82, -57), S(-76, -52), // + S(-13, -111), S(18, -107), S(3, -45), S(-13, -50), S(-40, -60), S(-53, -71), S(-79, -72), S(-83, -74), // + S(9, -107), S(84, -95), S(72, -40), S(25, -11), S(-4, -32), S(-24, -60), S(-49, -59), S(-62, -57), // }; inline const std::array KNIGHT_PSQT = { - S(-259, -499), S(-256, 50), S(-263, -30), S(0, 41), S(-91, 42), S(-265, 82), S(-344, 98), S(-346, -403), // - S(-2, -12), S(2, 27), S(86, 25), S(84, 60), S(89, 48), S(51, 28), S(-16, 24), S(-44, 22), // - S(40, -25), S(42, 28), S(57, 60), S(73, 70), S(57, 71), S(21, 56), S(8, 35), S(-34, 9), // - S(89, 22), S(91, 47), S(101, 68), S(104, 108), S(108, 107), S(72, 71), S(52, 37), S(33, 31), // - S(78, 15), S(101, 2), S(99, 49), S(94, 79), S(81, 82), S(73, 63), S(58, 18), S(24, 24), // - S(19, -42), S(44, -25), S(51, 20), S(63, 52), S(58, 50), S(31, 14), S(15, -18), S(-23, -41), // - S(37, -25), S(39, -11), S(27, -19), S(37, 8), S(34, 6), S(5, -44), S(-30, -1), S(-49, -82), // - S(-44, -92), S(10, -29), S(28, -43), S(44, -33), S(28, -19), S(-9, -51), S(-20, -27), S(-71, -140), // + S(-261, -501), S(-258, 51), S(-264, -30), S(1, 42), S(-89, 41), S(-265, 81), S(-346, 100), S(-348, -406), // + S(-0, -11), S(4, 27), S(86, 24), S(84, 60), S(90, 48), S(52, 27), S(-15, 26), S(-41, 20), // + S(41, -25), S(43, 28), S(56, 63), S(75, 71), S(58, 72), S(21, 59), S(10, 35), S(-34, 11), // + S(90, 23), S(92, 47), S(102, 69), S(105, 110), S(109, 108), S(73, 73), S(52, 38), S(33, 32), // + S(79, 15), S(102, 3), S(100, 50), S(95, 81), S(81, 83), S(73, 64), S(58, 19), S(24, 25), // + S(18, -41), S(44, -25), S(51, 20), S(63, 52), S(59, 51), S(31, 14), S(15, -18), S(-24, -41), // + S(36, -23), S(39, -10), S(27, -19), S(37, 9), S(34, 7), S(5, -44), S(-31, 0), S(-49, -84), // + S(-44, -93), S(10, -29), S(28, -43), S(44, -32), S(28, -18), S(-9, -51), S(-21, -27), S(-71, -141), // }; inline const std::array BISHOP_PSQT = { - S(-128, 20), S(-189, 101), S(-398, 165), S(-270, 97), S(-300, 139), S(-301, 151), S(-223, 125), S(-126, 70), // - S(-26, -21), S(-64, 86), S(-46, 62), S(-67, 60), S(-65, 74), S(-38, 46), S(-10, 41), S(-51, 24), // - S(35, 18), S(17, 57), S(31, 68), S(28, 53), S(23, 43), S(21, 47), S(10, 42), S(11, 12), // - S(17, -7), S(50, 20), S(61, 35), S(80, 55), S(103, 40), S(42, 19), S(45, -4), S(-1, -3), // - S(37, -45), S(45, 2), S(77, 13), S(92, 26), S(76, 37), S(66, 24), S(21, 4), S(10, -38), // - S(57, -30), S(81, -25), S(96, 5), S(64, 28), S(61, 13), S(57, 13), S(59, -4), S(17, -23), // - S(27, -73), S(113, -57), S(66, -19), S(48, -2), S(31, -2), S(45, -44), S(45, -58), S(38, -50), // - S(43, -72), S(23, -25), S(30, -10), S(44, -32), S(28, -19), S(38, 14), S(44, -17), S(39, -61), // + S(-131, 21), S(-191, 102), S(-400, 166), S(-272, 97), S(-303, 138), S(-304, 152), S(-226, 127), S(-129, 72), // + S(-27, -20), S(-64, 87), S(-46, 61), S(-68, 59), S(-66, 74), S(-40, 48), S(-9, 41), S(-51, 24), // + S(34, 17), S(15, 59), S(29, 68), S(26, 54), S(22, 42), S(20, 46), S(9, 42), S(11, 12), // + S(15, -7), S(48, 20), S(60, 35), S(79, 54), S(103, 39), S(41, 20), S(44, -4), S(-2, -3), // + S(36, -44), S(44, 2), S(76, 13), S(91, 26), S(74, 37), S(64, 24), S(19, 6), S(9, -39), // + S(56, -30), S(79, -26), S(95, 4), S(63, 28), S(59, 12), S(56, 13), S(58, -4), S(15, -23), // + S(25, -74), S(112, -58), S(65, -20), S(46, -2), S(29, -2), S(44, -44), S(44, -58), S(37, -51), // + S(42, -73), S(22, -25), S(29, -11), S(43, -33), S(27, -20), S(36, 14), S(42, -17), S(38, -62), // }; inline const std::array ROOK_PSQT = { - S(174, 107), S(201, 128), S(155, 157), S(140, 130), S(201, 84), S(158, 112), S(167, 127), S(151, 125), // - S(87, 149), S(144, 153), S(185, 126), S(140, 112), S(177, 98), S(145, 123), S(98, 155), S(88, 156), // - S(57, 143), S(177, 86), S(202, 71), S(173, 57), S(178, 73), S(120, 114), S(115, 129), S(62, 169), // - S(30, 111), S(92, 118), S(111, 93), S(95, 94), S(122, 76), S(83, 118), S(75, 131), S(20, 157), // - S(5, 44), S(63, 54), S(56, 76), S(25, 85), S(35, 89), S(24, 114), S(7, 111), S(-14, 115), // - S(17, -36), S(84, -10), S(72, 15), S(50, 27), S(59, 35), S(37, 57), S(33, 42), S(-5, 48), // - S(-63, -41), S(61, -77), S(67, -40), S(56, -6), S(59, -6), S(45, 8), S(35, -8), S(4, -5), // - S(0, -63), S(24, -32), S(86, -43), S(95, -43), S(97, -38), S(74, -16), S(68, -22), S(47, -17), // + S(175, 109), S(204, 129), S(157, 159), S(143, 131), S(204, 86), S(162, 113), S(168, 131), S(152, 128), // + S(87, 151), S(145, 155), S(188, 127), S(142, 113), S(179, 99), S(146, 125), S(99, 157), S(89, 159), // + S(57, 145), S(178, 88), S(205, 71), S(176, 57), S(180, 74), S(121, 115), S(115, 132), S(63, 170), // + S(30, 111), S(93, 120), S(113, 94), S(97, 94), S(125, 76), S(85, 118), S(75, 133), S(21, 158), // + S(5, 45), S(63, 55), S(57, 77), S(26, 86), S(35, 91), S(25, 115), S(8, 112), S(-14, 117), // + S(17, -34), S(85, -10), S(73, 16), S(51, 29), S(61, 35), S(38, 58), S(34, 43), S(-4, 48), // + S(-62, -41), S(63, -75), S(68, -38), S(56, -5), S(60, -4), S(46, 9), S(37, -8), S(5, -4), // + S(1, -62), S(24, -30), S(87, -42), S(96, -42), S(98, -37), S(75, -15), S(69, -21), S(48, -16), // }; inline const std::array QUEEN_PSQT = { - S(89, 154), S(145, 154), S(57, 295), S(-3, 394), S(34, 348), S(72, 255), S(71, 174), S(26, 205), // - S(58, 182), S(77, 237), S(37, 319), S(-81, 434), S(-41, 407), S(19, 286), S(58, 173), S(40, 144), // - S(20, 232), S(59, 260), S(28, 336), S(-9, 370), S(11, 358), S(50, 257), S(81, 154), S(62, 118), // - S(16, 170), S(28, 236), S(-12, 303), S(-12, 356), S(5, 350), S(22, 235), S(60, 153), S(35, 116), // - S(13, 143), S(11, 165), S(3, 215), S(-22, 284), S(-11, 300), S(11, 234), S(16, 173), S(36, 80), // - S(4, 50), S(34, 59), S(31, 134), S(10, 155), S(19, 162), S(22, 185), S(41, 114), S(21, 94), // - S(-24, -94), S(5, -94), S(13, -26), S(40, 13), S(36, 52), S(39, 24), S(12, 63), S(25, 36), // - S(-41, -80), S(-10, -240), S(21, -243), S(33, -134), S(40, -55), S(38, -74), S(36, -61), S(5, -7), // + S(89, 154), S(146, 154), S(56, 299), S(-2, 396), S(35, 349), S(71, 258), S(72, 175), S(25, 208), // + S(58, 184), S(78, 239), S(37, 322), S(-82, 438), S(-39, 406), S(20, 288), S(58, 175), S(40, 146), // + S(20, 235), S(60, 261), S(29, 338), S(-8, 373), S(11, 362), S(51, 259), S(82, 155), S(63, 119), // + S(16, 172), S(28, 238), S(-13, 307), S(-12, 360), S(5, 353), S(21, 238), S(60, 155), S(35, 117), // + S(13, 145), S(10, 169), S(3, 218), S(-23, 287), S(-11, 303), S(11, 238), S(16, 175), S(36, 81), // + S(4, 51), S(35, 60), S(31, 135), S(10, 157), S(20, 164), S(22, 187), S(41, 116), S(22, 95), // + S(-24, -95), S(4, -93), S(13, -25), S(40, 14), S(37, 54), S(39, 26), S(12, 65), S(25, 37), // + S(-42, -80), S(-10, -240), S(21, -245), S(33, -133), S(40, -55), S(38, -74), S(36, -60), S(5, -5), // }; inline const std::array KING_PSQT = { - S(167, -550), S(473, 83), S(313, 194), S(20, 190), S(0, 0), S(0, 0), S(0, 0), S(0, 0), // - S(366, -181), S(363, 189), S(209, 203), S(26, 156), S(0, 0), S(0, 0), S(0, 0), S(0, 0), // - S(197, 46), S(230, 175), S(114, 198), S(-49, 179), S(0, 0), S(0, 0), S(0, 0), S(0, 0), // - S(39, 8), S(113, 114), S(-21, 171), S(-117, 201), S(0, 0), S(0, 0), S(0, 0), S(0, 0), // - S(-112, -0), S(-22, 78), S(-85, 147), S(-180, 198), S(0, 0), S(0, 0), S(0, 0), S(0, 0), // - S(-54, -32), S(12, 34), S(-68, 115), S(-123, 160), S(0, 0), S(0, 0), S(0, 0), S(0, 0), // - S(23, -91), S(29, -11), S(-33, 51), S(-108, 107), S(0, 0), S(0, 0), S(0, 0), S(0, 0), // - S(-23, -208), S(3, -90), S(-73, -26), S(-82, -39), S(0, 0), S(0, 0), S(0, 0), S(0, 0), // + S(164, -555), S(473, 84), S(313, 195), S(18, 191), S(0, 0), S(0, 0), S(0, 0), S(0, 0), // + S(367, -183), S(363, 189), S(210, 204), S(25, 156), S(0, 0), S(0, 0), S(0, 0), S(0, 0), // + S(197, 45), S(230, 175), S(114, 199), S(-51, 179), S(0, 0), S(0, 0), S(0, 0), S(0, 0), // + S(37, 9), S(112, 114), S(-22, 172), S(-118, 201), S(0, 0), S(0, 0), S(0, 0), S(0, 0), // + S(-114, -1), S(-22, 77), S(-86, 146), S(-182, 198), S(0, 0), S(0, 0), S(0, 0), S(0, 0), // + S(-55, -35), S(11, 34), S(-69, 115), S(-125, 160), S(0, 0), S(0, 0), S(0, 0), S(0, 0), // + S(22, -93), S(28, -12), S(-34, 50), S(-110, 107), S(0, 0), S(0, 0), S(0, 0), S(0, 0), // + S(-24, -211), S(2, -91), S(-75, -27), S(-84, -40), S(0, 0), S(0, 0), S(0, 0), S(0, 0), // }; -inline const PParam KS_NO_QUEEN = S(-80, -447); +inline const PParam KS_NO_QUEEN = S(-80, -449); inline const std::array PT_INNER_RING_ATTACKS = { - S(7, -4), S(10, 12), S(10, 5), S(4, 2), S(3, -9), + S(7, -4), S(10, 12), S(10, 5), S(4, 1), S(3, -9), }; inline const std::array PT_OUTER_RING_ATTACKS = { S(4, -7), S(5, 5), S(2, 2), S(3, -0), S(4, -2), @@ -179,30 +181,31 @@ inline const PParam KS_FLANK_DOUBLE_ATTACK = S(3, -1); inline const PParam KS_FLANK_DOUBLE_DEFENSE = S(-2, 2); inline const std::array, 4> KING_SHELTER = {{ - {{ S(16, -19), S(-6, 5), S(-3, -9), S(7, -16), S(12, -29), S(7, -53), S(-4, -45), }}, - {{ S(2, 4), S(-21, 2), S(-17, 10), S(-10, 18), S(-6, 8), S(-9, -12), S(-19, -21), }}, - {{ S(-5, -1), S(-13, -8), S(-14, 18), S(-10, 19), S(-8, 10), S(-8, -16), S(-24, -37), }}, - {{ S(6, 9), S(-10, 18), S(-7, 38), S(-2, 43), S(-2, 38), S(3, 19), S(9, -14), }}, + {{ S(16, -19), S(-7, 5), S(-3, -10), S(6, -16), S(12, -29), S(6, -54), S(-4, -46), }}, + {{ S(2, 3), S(-21, 2), S(-17, 10), S(-10, 17), S(-7, 8), S(-10, -13), S(-19, -21), }}, + {{ S(-3, -1), S(-12, -9), S(-12, 17), S(-8, 18), S(-7, 9), S(-6, -17), S(-22, -38), }}, + {{ S(6, 8), S(-11, 18), S(-7, 38), S(-2, 43), S(-2, 38), S(3, 19), S(9, -14), }}, }}; inline const std::array BLOCKED_SHELTER_STORM = { - S(0, 0), S(0, 0), S(9, 23), S(-6, 3), S(-7, 10), S(-6, 28), S(1, 48), + S(0, 0), S(0, 0), S(9, 24), S(-6, 3), S(-8, 10), S(-6, 29), S(1, 49), }; inline const std::array, 4> SHELTER_STORM = {{ - {{ S(5, 11), S(-41, -123), S(-9, -39), S(-1, 5), S(-3, 4), S(-6, 11), S(-5, 10), }}, - {{ S(8, -1), S(-25, -125), S(-6, -29), S(-4, 1), S(-2, -3), S(-9, 4), S(1, 3), }}, - {{ S(-0, 11), S(-8, -69), S(7, -15), S(0, 9), S(-4, 11), S(-9, 19), S(-6, 21), }}, - {{ S(0, 0), S(3, -63), S(-1, 25), S(-3, 25), S(-6, 8), S(-12, 11), S(-9, 19), }}, + {{ S(5, 12), S(-42, -121), S(-9, -39), S(-1, 5), S(-3, 4), S(-6, 11), S(-6, 10), }}, + {{ S(8, -1), S(-25, -124), S(-7, -29), S(-5, 1), S(-2, -2), S(-9, 5), S(0, 4), }}, + {{ S(-0, 12), S(-8, -69), S(6, -15), S(-0, 9), S(-4, 12), S(-10, 19), S(-6, 22), }}, + {{ S(0, 1), S(2, -62), S(-1, 25), S(-3, 26), S(-6, 9), S(-12, 11), S(-9, 19), }}, }}; inline TunableSigmoid<32> KING_SAFETY_ACTIVATION( - 1422, 904, -21, -4 + 1427, 910, -21, -4 ); inline VParam WINNABLE_PAWNS = V(-20); -inline VParam WINNABLE_SYM = V(101); +inline VParam WINNABLE_SYM = V(102); inline VParam WINNABLE_ASYM = V(87); inline VParam WINNABLE_PAWN_ENDGAME = V(75); -inline VParam WINNABLE_BIAS = V(-386); +inline VParam WINNABLE_BIAS = V(-389); +// Epoch duration: 9.56388s // Epoch duration: 7.65336s // clang-format on } // namespace Clockwork diff --git a/src/evaltune_main.cpp b/src/evaltune_main.cpp index 263cb8f5..bec41de8 100644 --- a/src/evaltune_main.cpp +++ b/src/evaltune_main.cpp @@ -30,63 +30,156 @@ using namespace Clockwork::Autograd; void print_params(); f64 find_optimal_k(const std::vector& positions, const std::vector& targets) { - constexpr f64 left0 = 0.001; - constexpr f64 right0 = 0.010; - constexpr int zooms = 22; - constexpr f64 phi = 0.6180339887498948482; + constexpr f64 K_MIN = 0.0015; + constexpr f64 K_MAX = 0.0035; + + constexpr int COARSE_SAMPLES = 256; + constexpr int KEEP_BEST = 3; + constexpr int LOCAL_SAMPLES = 64; + constexpr int REFINEMENTS = 3; + + // Adjust this to your fixed point representation + constexpr f64 VALUE_SCALE = 1.0; + + + // ------------------------------------------------------------ + // Fast sigmoid + // ------------------------------------------------------------ + + auto sigmoid = [](f64 x) -> f64 { + return 1.0 / (1.0 + std::exp(-x)); + }; + + + // ------------------------------------------------------------ + // Build cache + // ------------------------------------------------------------ + + std::vector white_pov_cache; + white_pov_cache.reserve(positions.size()); + + for (const auto& pos : positions) { + + Score output = evaluate_white_pov(pos); + + // Force evaluation before graph destruction + i32 raw = output.get_value(); + + white_pov_cache.push_back(static_cast(raw) * VALUE_SCALE); + + Graph::get().cleanup(); + } + + + // ------------------------------------------------------------ + // Cheap evaluator + // ------------------------------------------------------------ auto evaluate_loss = [&](f64 K) -> f64 { f64 loss = 0.0; - for (size_t i = 0; i < positions.size(); ++i) { - // TODO: this can absolutely be optimized by just caching the eval results and just multiplying by K and then doing the sigmoid after. - // Definitely implement this if we try the K tuning every time, not just when we modify the dataset. - ValueHandle output = (evaluate_white_pov(positions[i]) * K).sigmoid(); + for (size_t i = 0; i < white_pov_cache.size(); ++i) { - const f64 p = output.get_value(); - const f64 e = p - targets[i]; - loss += e * e; + f64 p = sigmoid(white_pov_cache[i] * K); - Graph::get().cleanup(); + f64 e = p - targets[i]; + + loss += e * e; } - return loss / positions.size(); + return loss / white_pov_cache.size(); + }; + + + struct Sample { + f64 k; + f64 loss; }; - f64 left = left0; - f64 right = right0; - f64 c = right - phi * (right - left); - f64 d = left + phi * (right - left); + auto sample_grid = [&](f64 left, f64 right, int count) { + std::vector samples; + samples.reserve(count); - f64 fc = evaluate_loss(c); - f64 fd = evaluate_loss(d); + for (int i = 0; i < count; i++) { - for (int i = 0; i < zooms; ++i) { - std::cout << "Zoom " << i + 1 << "/" << zooms << ": left=" << left << ", right=" << right - << ", c=" << c << ", d=" << d << ", fc=" << fc << ", fd=" << fd << "\n"; - if (fc < fd) { - right = d; - d = c; - fd = fc; + f64 t = static_cast(i) / (count - 1); - c = right - phi * (right - left); - fc = evaluate_loss(c); - } else { - left = c; - c = d; - fc = fd; + f64 k = left + t * (right - left); - d = left + phi * (right - left); - fd = evaluate_loss(d); + samples.push_back({k, evaluate_loss(k)}); } + + std::sort(samples.begin(), samples.end(), [](const Sample& a, const Sample& b) { + return a.loss < b.loss; + }); + + return samples; + }; + + + struct Region { + f64 center; + f64 radius; + }; + + + // ------------------------------------------------------------ + // Initial search + // ------------------------------------------------------------ + + auto initial = sample_grid(K_MIN, K_MAX, COARSE_SAMPLES); + + + std::vector regions; + + f64 initial_radius = (K_MAX - K_MIN) / (COARSE_SAMPLES - 1); + + + for (int i = 0; i < KEEP_BEST; i++) { + regions.push_back({initial[i].k, initial_radius}); } - const f64 best_k = 0.5 * (left + right); - std::cout << "Best K = " << best_k << " (1/K = " << (1.0 / best_k) << ")\n"; + // ------------------------------------------------------------ + // Refinement + // ------------------------------------------------------------ + + for (int r = 0; r < REFINEMENTS; r++) { + + std::vector candidates; + + for (const auto& region : regions) { + + auto local = sample_grid(region.center - region.radius, region.center + region.radius, + LOCAL_SAMPLES); + + candidates.insert(candidates.end(), local.begin(), local.end()); + } + + + std::sort(candidates.begin(), candidates.end(), [](const Sample& a, const Sample& b) { + return a.loss < b.loss; + }); + + + f64 spacing = std::abs(candidates[1].k - candidates[0].k); + + + regions.clear(); + + for (int i = 0; i < KEEP_BEST; i++) { + + regions.push_back({candidates[i].k, spacing * 2.0}); + } + + + std::cout << "refinement " << r + 1 << ": K=" << candidates[0].k + << " loss=" << candidates[0].loss << "\n"; + } + - return best_k; + return regions[0].center; } int main() { @@ -568,8 +661,8 @@ void print_params() { << std::endl; std::cout << std::endl; - print_table("MINOR_THREAT", MINOR_THREAT); - print_table("ROOK_THREAT", ROOK_THREAT); + print_2d_array("MINOR_THREAT", MINOR_THREAT); + print_2d_array("ROOK_THREAT", ROOK_THREAT); std::cout << "inline const PParam KING_THREAT = " << KING_THREAT << ";" << std::endl; std::cout << "inline const PParam HANGING_PAWN = " << HANGING_PAWN << ";" << std::endl; std::cout << "inline const PParam HANGING_NON_PAWN = " << HANGING_NON_PAWN << ";" << std::endl diff --git a/src/evaluation.cpp b/src/evaluation.cpp index 5f07eaab..2885ad18 100644 --- a/src/evaluation.cpp +++ b/src/evaluation.cpp @@ -524,14 +524,16 @@ PScore evaluate_threats(const Position& pos, const EvalData& data) { | data.attacked_by(color, PieceType::Bishop)); for (Square sq : b) { PieceType pt = pos.piece_at(sq); - eval += MINOR_THREAT[static_cast(pt) - static_cast(PieceType::Pawn)]; + eval += MINOR_THREAT[pos.active_color() == color] + [static_cast(pt) - static_cast(PieceType::Pawn)]; } // Rook threats b = weak & data.attacked_by(color, PieceType::Rook); for (Square sq : b) { PieceType pt = pos.piece_at(sq); - eval += ROOK_THREAT[static_cast(pt) - static_cast(PieceType::Pawn)]; + eval += ROOK_THREAT[pos.active_color() == color] + [static_cast(pt) - static_cast(PieceType::Pawn)]; } // King threats