Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.82 KB | None | 0 0
  1. /*
  2.   Ethereal is a UCI chess playing engine authored by Andrew Grant.
  3.   <https://github.com/AndyGrant/Ethereal>     <andrew@grantnet.us>
  4.  
  5.   Ethereal is free software: you can redistribute it and/or modify
  6.   it under the terms of the GNU General Public License as published by
  7.   the Free Software Foundation, either version 3 of the License, or
  8.   (at your option) any later version.
  9.  
  10.   Ethereal is distributed in the hope that it will be useful,
  11.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.   GNU General Public License for more details.
  14.  
  15.   You should have received a copy of the GNU General Public License
  16.   along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18.  
  19. #ifdef TUNE
  20.  
  21. #include <math.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25.  
  26. #include "bitboards.h"
  27. #include "board.h"
  28. #include "evaluate.h"
  29. #include "history.h"
  30. #include "move.h"
  31. #include "piece.h"
  32. #include "psqt.h"
  33. #include "search.h"
  34. #include "tune.h"
  35. #include "thread.h"
  36. #include "transposition.h"
  37. #include "types.h"
  38. #include "uci.h"
  39.  
  40. EtherTuple *TupleStack;
  41. int TupleStackSize = STACKSIZE
  42.  
  43. extern EvalTrace T, EmptyTace;
  44.  
  45. extern const int PawnValue
  46. extern const int KnightValue
  47. extern const int BishopValue
  48. extern const int RookValue
  49. extern const int QueenValue
  50. extern const int KingValue
  51. extern const int PawnPSQT[32]
  52. extern const int KnightPSQT[32]
  53. extern const int BishopPSQT[32]
  54. extern const int RookPSQT[32]
  55. extern const int QueenPSQT[32]
  56. extern const int KingPSQT[32]
  57. extern const int PawnIsolated
  58. extern const int PawnStacked
  59. extern const int PawnBackwards[2]
  60. extern const int PawnConnected[32]
  61. extern const int KnightRammedPawns
  62. extern const int KnightOutpost[2]
  63. extern const int KnightMobility[9]
  64. extern const int BishopPair
  65. extern const int BishopRammedPawns
  66. extern const int BishopOutpost[2]
  67. extern const int BishopMobility[14]
  68. extern const int RookFile[2]
  69. extern const int RookOnSeventh
  70. extern const int RookMobility[15]
  71. extern const int QueenMobility[28]
  72. extern const int KingDefenders[12]
  73. extern const int KingShelter[2][8][8]
  74. extern const int PassedPawn[2][2][8]
  75. extern const int ThreatPawnAttackedByOne
  76. extern const int ThreatMinorAttackedByPawn
  77. extern const int ThreatMinorAttackedByMajor
  78. extern const int ThreatQueenAttackedByOne
  79. extern const int KingSafetyWeights
  80. extern const int KingSafetyBase
  81. extern const int KingSafetyWeakSquares
  82. extern const int KingSafetyEnemyPawns
  83. extern const int KingSafetyNoEnemyQueens
  84.  
  85. void initParameters(double *cparams[NE][PHASE_NB]);
  86.  
  87.     int i = 0;
  88.  
  89.     for(int a = 0; a < 1; a++, i++)
  90.         cparams[i][MG] = ScoreMG(PawnValue[a]),
  91.         cparams[i][EG] = ScoreEG(PawnValue[a]);
  92.  
  93.     for(int a = 0; a < 1; a++, i++)
  94.         cparams[i][MG] = ScoreMG(KnightValue[a]),
  95.         cparams[i][EG] = ScoreEG(KnightValue[a]);
  96.  
  97.     for(int a = 0; a < 1; a++, i++)
  98.         cparams[i][MG] = ScoreMG(BishopValue[a]),
  99.         cparams[i][EG] = ScoreEG(BishopValue[a]);
  100.  
  101.     for(int a = 0; a < 1; a++, i++)
  102.         cparams[i][MG] = ScoreMG(RookValue[a]),
  103.         cparams[i][EG] = ScoreEG(RookValue[a]);
  104.  
  105.     for(int a = 0; a < 1; a++, i++)
  106.         cparams[i][MG] = ScoreMG(QueenValue[a]),
  107.         cparams[i][EG] = ScoreEG(QueenValue[a]);
  108.  
  109.     for(int a = 0; a < 1; a++, i++)
  110.         cparams[i][MG] = ScoreMG(KingValue[a]),
  111.         cparams[i][EG] = ScoreEG(KingValue[a]);
  112.  
  113.     for(int a = 0; a < 32; a++, i++)
  114.         cparams[i][MG] = ScoreMG(PawnPSQT[a]),
  115.         cparams[i][EG] = ScoreEG(PawnPSQT[a]);
  116.  
  117.     for(int a = 0; a < 32; a++, i++)
  118.         cparams[i][MG] = ScoreMG(KnightPSQT[a]),
  119.         cparams[i][EG] = ScoreEG(KnightPSQT[a]);
  120.  
  121.     for(int a = 0; a < 32; a++, i++)
  122.         cparams[i][MG] = ScoreMG(BishopPSQT[a]),
  123.         cparams[i][EG] = ScoreEG(BishopPSQT[a]);
  124.  
  125.     for(int a = 0; a < 32; a++, i++)
  126.         cparams[i][MG] = ScoreMG(RookPSQT[a]),
  127.         cparams[i][EG] = ScoreEG(RookPSQT[a]);
  128.  
  129.     for(int a = 0; a < 32; a++, i++)
  130.         cparams[i][MG] = ScoreMG(QueenPSQT[a]),
  131.         cparams[i][EG] = ScoreEG(QueenPSQT[a]);
  132.  
  133.     for(int a = 0; a < 32; a++, i++)
  134.         cparams[i][MG] = ScoreMG(KingPSQT[a]),
  135.         cparams[i][EG] = ScoreEG(KingPSQT[a]);
  136.  
  137.     for(int a = 0; a < 1; a++, i++)
  138.         cparams[i][MG] = ScoreMG(PawnIsolated[a]),
  139.         cparams[i][EG] = ScoreEG(PawnIsolated[a]);
  140.  
  141.     for(int a = 0; a < 1; a++, i++)
  142.         cparams[i][MG] = ScoreMG(PawnStacked[a]),
  143.         cparams[i][EG] = ScoreEG(PawnStacked[a]);
  144.  
  145.     for(int a = 0; a < 2; a++, i++)
  146.         cparams[i][MG] = ScoreMG(PawnBackwards[a]),
  147.         cparams[i][EG] = ScoreEG(PawnBackwards[a]);
  148.  
  149.     for(int a = 0; a < 32; a++, i++)
  150.         cparams[i][MG] = ScoreMG(PawnConnected[a]),
  151.         cparams[i][EG] = ScoreEG(PawnConnected[a]);
  152.  
  153.     for(int a = 0; a < 1; a++, i++)
  154.         cparams[i][MG] = ScoreMG(KnightRammedPawns[a]),
  155.         cparams[i][EG] = ScoreEG(KnightRammedPawns[a]);
  156.  
  157.     for(int a = 0; a < 2; a++, i++)
  158.         cparams[i][MG] = ScoreMG(KnightOutpost[a]),
  159.         cparams[i][EG] = ScoreEG(KnightOutpost[a]);
  160.  
  161.     for(int a = 0; a < 9; a++, i++)
  162.         cparams[i][MG] = ScoreMG(KnightMobility[a]),
  163.         cparams[i][EG] = ScoreEG(KnightMobility[a]);
  164.  
  165.     for(int a = 0; a < 1; a++, i++)
  166.         cparams[i][MG] = ScoreMG(BishopPair[a]),
  167.         cparams[i][EG] = ScoreEG(BishopPair[a]);
  168.  
  169.     for(int a = 0; a < 1; a++, i++)
  170.         cparams[i][MG] = ScoreMG(BishopRammedPawns[a]),
  171.         cparams[i][EG] = ScoreEG(BishopRammedPawns[a]);
  172.  
  173.     for(int a = 0; a < 2; a++, i++)
  174.         cparams[i][MG] = ScoreMG(BishopOutpost[a]),
  175.         cparams[i][EG] = ScoreEG(BishopOutpost[a]);
  176.  
  177.     for(int a = 0; a < 14; a++, i++)
  178.         cparams[i][MG] = ScoreMG(BishopMobility[a]),
  179.         cparams[i][EG] = ScoreEG(BishopMobility[a]);
  180.  
  181.     for(int a = 0; a < 2; a++, i++)
  182.         cparams[i][MG] = ScoreMG(RookFile[a]),
  183.         cparams[i][EG] = ScoreEG(RookFile[a]);
  184.  
  185.     for(int a = 0; a < 1; a++, i++)
  186.         cparams[i][MG] = ScoreMG(RookOnSeventh[a]),
  187.         cparams[i][EG] = ScoreEG(RookOnSeventh[a]);
  188.  
  189.     for(int a = 0; a < 15; a++, i++)
  190.         cparams[i][MG] = ScoreMG(RookMobility[a]),
  191.         cparams[i][EG] = ScoreEG(RookMobility[a]);
  192.  
  193.     for(int a = 0; a < 28; a++, i++)
  194.         cparams[i][MG] = ScoreMG(QueenMobility[a]),
  195.         cparams[i][EG] = ScoreEG(QueenMobility[a]);
  196.  
  197.     for(int a = 0; a < 12; a++, i++)
  198.         cparams[i][MG] = ScoreMG(KingDefenders[a]),
  199.         cparams[i][EG] = ScoreEG(KingDefenders[a]);
  200.  
  201.     for(int a = 0; a < 2; a++)
  202.         for(int b = 0; b < 8; b++)
  203.             for(int c = 0; c < 8; c++, i++)
  204.                 cparams[i][MG] = ScoreMG(KingShelter[a][b][c]),
  205.                 cparams[i][EG] = ScoreEG(KingShelter[a][b][c]);
  206.  
  207.     for(int a = 0; a < 2; a++)
  208.         for(int b = 0; b < 2; b++)
  209.             for(int c = 0; c < 8; c++, i++)
  210.                 cparams[i][MG] = ScoreMG(PassedPawn[a][b][c]),
  211.                 cparams[i][EG] = ScoreEG(PassedPawn[a][b][c]);
  212.  
  213.     for(int a = 0; a < 1; a++, i++)
  214.         cparams[i][MG] = ScoreMG(ThreatPawnAttackedByOne[a]),
  215.         cparams[i][EG] = ScoreEG(ThreatPawnAttackedByOne[a]);
  216.  
  217.     for(int a = 0; a < 1; a++, i++)
  218.         cparams[i][MG] = ScoreMG(ThreatMinorAttackedByPawn[a]),
  219.         cparams[i][EG] = ScoreEG(ThreatMinorAttackedByPawn[a]);
  220.  
  221.     for(int a = 0; a < 1; a++, i++)
  222.         cparams[i][MG] = ScoreMG(ThreatMinorAttackedByMajor[a]),
  223.         cparams[i][EG] = ScoreEG(ThreatMinorAttackedByMajor[a]);
  224.  
  225.     for(int a = 0; a < 1; a++, i++)
  226.         cparams[i][MG] = ScoreMG(ThreatQueenAttackedByOne[a]),
  227.         cparams[i][EG] = ScoreEG(ThreatQueenAttackedByOne[a]);
  228.  
  229.     for(int a = 0; a < 1; a++, i++)
  230.         cparams[i][MG] = ScoreMG(KingSafetyWeights[a]),
  231.         cparams[i][EG] = ScoreEG(KingSafetyWeights[a]);
  232.  
  233.     for(int a = 0; a < 1; a++, i++)
  234.         cparams[i][MG] = ScoreMG(KingSafetyBase[a]),
  235.         cparams[i][EG] = ScoreEG(KingSafetyBase[a]);
  236.  
  237.     for(int a = 0; a < 1; a++, i++)
  238.         cparams[i][MG] = ScoreMG(KingSafetyWeakSquares[a]),
  239.         cparams[i][EG] = ScoreEG(KingSafetyWeakSquares[a]);
  240.  
  241.     for(int a = 0; a < 1; a++, i++)
  242.         cparams[i][MG] = ScoreMG(KingSafetyEnemyPawns[a]),
  243.         cparams[i][EG] = ScoreEG(KingSafetyEnemyPawns[a]);
  244.  
  245.     for(int a = 0; a < 1; a++, i++)
  246.         cparams[i][MG] = ScoreMG(KingSafetyNoEnemyQueens[a]),
  247.         cparams[i][EG] = ScoreEG(KingSafetyNoEnemyQueens[a]);
  248.  
  249. };
  250.  
  251. double computeOptimalK(EtherEntry *entries, double *params[NE][PHASE_NB]){
  252.  
  253.     double start = -10.0, end = 10.0, delta = 1.0;
  254.     double curr = start, thisError, bestError = completeLinearError(entries, params, start);
  255.  
  256.     for (int i = 0; i < 10; i++){
  257.         printf("Computing K Iteration [%d] ", i);
  258.  
  259.         curr = start - delta;
  260.         while (curr < end){
  261.             curr = curr + delta;
  262.             thisError = completeLinearError(entries, params, start);
  263.             if (thisError <= bestError)
  264.                 bestError = thisError, start = curr;
  265.         }
  266.  
  267.         printf("K = %f E = %f\n", start, bestError);
  268.  
  269.         end = start + delta;
  270.         start = start - delta;
  271.         delta = delta / 10.0;
  272.     }
  273.  
  274.     return start;
  275. }
  276.  
  277. double completeLinearError(EtherEntry *entries, double *params[NE][PHASE_NB], double K){
  278.  
  279.     double total = 0.0
  280.  
  281.     #pragma omp parallel shared(total)
  282.     {
  283.         #pragma omp for schedule(static, NP / NC) reduction(+:total)
  284.         for (int i = 0; i < NP; i++)
  285.             total += pow(entries[i].result - sigmoid(K, linearEvaluation(entries[i], params)), 2);
  286.     }
  287.  
  288.     return total / (double)NP;
  289. };
  290.  
  291. double singleLinearError(EtherEntry entry, double *params[NE][PHASE_NB], double K){
  292.     return entry.result - sigmoid(K, linearEvaluation(entry, params));
  293. };
  294.  
  295. double linearEvaluation(EtherEntry entry, double *params[NE][PHASE_NB]){
  296.  
  297.     double eval, evals[NE], mg, eg;
  298.  
  299.     for (int i = 0, mg = eg = 0; i < entry.ntuples[0]; i++){
  300.         mg += entry.tuples[0][i].coeff * params[entry.tuples[0][i].index][MG];
  301.         eg += entry.tuples[0][i].coeff * params[entry.tuples[0][i].index][EG];
  302.     }
  303.  
  304.     mg += entry.counts[0][MG];
  305.     eg += entry.counts[0][EG];
  306.  
  307.     mg = mg;
  308.     eg = eg;
  309.  
  310.     evals[0] = ((mg * (256 - entry.phase) + eg * entry.phase) / 256.0);
  311.  
  312.     for (int i = 0, mg = eg = 0; i < entry.ntuples[1]; i++){
  313.         mg += entry.tuples[1][i].coeff * params[entry.tuples[1][i].index][MG];
  314.         eg += entry.tuples[1][i].coeff * params[entry.tuples[1][i].index][EG];
  315.     }
  316.  
  317.     mg += entry.counts[1][MG];
  318.     eg += entry.counts[1][EG];
  319.  
  320.     mg = mg * mg / 64;
  321.     eg = 0;
  322.  
  323.     evals[1] = ((mg * (256 - entry.phase) + eg * entry.phase) / 256.0);
  324.  
  325.     for (int i = 0, eval = 0; i < NE; i++)
  326.         eval += evals[i];
  327.     return eval;
  328.  
  329. double sigmoid(double K, double score){
  330.     return 1.0 / (1.0 + pow(10.0, -K * S / 400.0));
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement