Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.80 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. #include <float.h>
  20. #include <math.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24.  
  25. #include "board.h"
  26. #include "evaluate.h"
  27. #include "search.h"
  28. #include "types.h"
  29. #include "texel.h"
  30. #include "psqt.h"
  31.  
  32. const double NumberOfPositions = 725000.0;
  33.  
  34. const double K = 0.0532;
  35.  
  36. extern int KnightMobility[2][9];
  37. extern int BishopMobility[2][14];
  38. extern int RookMobility[2][15];
  39. extern int QueenMobility[2][28];
  40. extern int PawnMidgameMap32[32];
  41. extern int PawnEndgameMap32[32];
  42. extern int KnightMidgameMap32[32];
  43. extern int KnightEndgameMap32[32];
  44. extern int BishopMidgameMap32[32];
  45. extern int BishopEndgameMap32[32];
  46. extern int RookMidgameMap32[32];
  47. extern int RookEndgameMap32[32];
  48. extern int QueenMidgameMap32[32];
  49. extern int QueenEndgameMap32[32];
  50. extern int KingMidgameMap32[32];
  51. extern int KingEndgameMap32[32];
  52.  
  53. void texelTuning(){
  54.     #ifdef TEXEL
  55.         //omp_set_num_threads(1);
  56.         TexelEntry * te = malloc(sizeof(TexelEntry) * NumberOfPositions);
  57.         initalizeTexelTuning(te);
  58.         runTexelTuning(te);
  59.     #endif
  60. }
  61.  
  62. void initalizeTexelTuning(TexelEntry * te){
  63.     int i;
  64.     FILE * fin = fopen("fens", "r");
  65.     for (i = 0; i < NumberOfPositions; i++){
  66.         fgets(te[i].fen, 128, fin);
  67.         if (strstr(te[i].fen, "1\"") != NULL) te[i].result = 0.0;
  68.         if (strstr(te[i].fen, "2\"") != NULL) te[i].result = 0.5;
  69.         if (strstr(te[i].fen, "0\"") != NULL) te[i].result = 1.0;
  70.     }
  71.    
  72.     printf("Finished Reading FENS\n");
  73. }
  74.  
  75. void runTexelTuning(TexelEntry * te){
  76.    
  77.     int i, a, iteration = 0;
  78.     int delta;
  79.     int improved = 1;
  80.     double this, best = error(te);
  81.    
  82.     int nparams = (2 * (9 + 14 + 15 + 28)) + (32 * 12);
  83.     int ** terms = malloc(sizeof(int*) * nparams);
  84.     for (i = 0, a = 0; a < 18; i++, a++) terms[i] = &KnightMobility[a/ 9][a% 9];
  85.     for (       a = 0; a < 28; i++, a++) terms[i] = &BishopMobility[a/14][a%14];
  86.     for (       a = 0; a < 30; i++, a++) terms[i] = &  RookMobility[a/15][a%15];
  87.     for (       a = 0; a < 56; i++, a++) terms[i] = & QueenMobility[a/28][a%28];
  88.     for (       a = 0; a < 32; i++, a++) terms[i] = &       PawnMidgameMap32[a];
  89.     for (       a = 0; a < 32; i++, a++) terms[i] = &       PawnEndgameMap32[a];
  90.     for (       a = 0; a < 32; i++, a++) terms[i] = &     KnightMidgameMap32[a];
  91.     for (       a = 0; a < 32; i++, a++) terms[i] = &     KnightEndgameMap32[a];
  92.     for (       a = 0; a < 32; i++, a++) terms[i] = &     BishopMidgameMap32[a];
  93.     for (       a = 0; a < 32; i++, a++) terms[i] = &     BishopEndgameMap32[a];
  94.     for (       a = 0; a < 32; i++, a++) terms[i] = &       RookMidgameMap32[a];
  95.     for (       a = 0; a < 32; i++, a++) terms[i] = &       RookEndgameMap32[a];
  96.     for (       a = 0; a < 32; i++, a++) terms[i] = &      QueenMidgameMap32[a];
  97.     for (       a = 0; a < 32; i++, a++) terms[i] = &      QueenEndgameMap32[a];
  98.     for (       a = 0; a < 32; i++, a++) terms[i] = &       KingMidgameMap32[a];
  99.     for (       a = 0; a < 32; i++, a++) terms[i] = &       KingEndgameMap32[a];
  100.     printf("Terms [%3d | %3d]\n", i, nparams);
  101.    
  102.     while (improved){
  103.        
  104.         printf("ITERATION : %d\n", iteration);
  105.         iteration++;
  106.        
  107.         improved = 0;
  108.        
  109.         for (i = 0; i < nparams; i++)
  110.             printf("%d ", *terms[i]);
  111.         printf("\n===============================\n");
  112.        
  113.         for (i = 0; i < nparams; i++){
  114.             for (delta = 1 + abs(*terms[i] / 12); delta > 0; delta /= 2){
  115.                
  116.                 // Try increasing the Parameter
  117.                 *terms[i] += delta;
  118.                 initalizePSQT();
  119.                 this = error(te);
  120.                
  121.                 // Increasing Parameter was a success
  122.                 if (this < best - DBL_EPSILON){
  123.                     best = this;
  124.                     improved = 1;
  125.                     continue;
  126.                 }
  127.                
  128.                 // Try decreasing the Parameter
  129.                 *terms[i] -= 2 * delta;
  130.                 initalizePSQT();
  131.                 this = error(te);
  132.                
  133.                 // Decreasting Parameter was a success
  134.                 if (this < best - DBL_EPSILON){
  135.                     best = this;
  136.                     improved = 1;
  137.                     continue;
  138.                 }
  139.                
  140.                 // Parameter should keep its current value
  141.                 *terms[i] += delta;
  142.             }
  143.         }
  144.     }
  145.    
  146.     printf("DONE");
  147. }
  148.  
  149. double error(TexelEntry * te){
  150.    
  151.     int i;
  152.     double total = 0;
  153.    
  154.     #pragma omp parallel shared(total)
  155.     {
  156.         #pragma omp for schedule(static, 10000) reduction(+:total)
  157.         for (i = 0; i < (int)NumberOfPositions; i++)
  158.             total += singleError(te[i]);
  159.     }
  160.    
  161.     return total;
  162. }
  163.  
  164. double singleError(TexelEntry te){
  165.     Board board;
  166.     initalizeBoard(&board, te.fen);
  167.     return pow(te.result - sigmoid(evaluateBoard(&board)), 2);
  168. }
  169.  
  170. double sigmoid(int s){
  171.     return 1.0 / (1 + pow(10, -K * (double)s / 400.0));
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement