Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Ethereal is a UCI chess playing engine authored by Andrew Grant.
- <https://github.com/AndyGrant/Ethereal> <andrew@grantnet.us>
- Ethereal is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- Ethereal is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #include <float.h>
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "board.h"
- #include "evaluate.h"
- #include "search.h"
- #include "types.h"
- #include "texel.h"
- #include "psqt.h"
- const double NumberOfPositions = 725000.0;
- const double K = 0.0532;
- extern int KnightMobility[2][9];
- extern int BishopMobility[2][14];
- extern int RookMobility[2][15];
- extern int QueenMobility[2][28];
- extern int PawnMidgameMap32[32];
- extern int PawnEndgameMap32[32];
- extern int KnightMidgameMap32[32];
- extern int KnightEndgameMap32[32];
- extern int BishopMidgameMap32[32];
- extern int BishopEndgameMap32[32];
- extern int RookMidgameMap32[32];
- extern int RookEndgameMap32[32];
- extern int QueenMidgameMap32[32];
- extern int QueenEndgameMap32[32];
- extern int KingMidgameMap32[32];
- extern int KingEndgameMap32[32];
- void texelTuning(){
- #ifdef TEXEL
- //omp_set_num_threads(1);
- TexelEntry * te = malloc(sizeof(TexelEntry) * NumberOfPositions);
- initalizeTexelTuning(te);
- runTexelTuning(te);
- #endif
- }
- void initalizeTexelTuning(TexelEntry * te){
- int i;
- FILE * fin = fopen("fens", "r");
- for (i = 0; i < NumberOfPositions; i++){
- fgets(te[i].fen, 128, fin);
- if (strstr(te[i].fen, "1\"") != NULL) te[i].result = 0.0;
- if (strstr(te[i].fen, "2\"") != NULL) te[i].result = 0.5;
- if (strstr(te[i].fen, "0\"") != NULL) te[i].result = 1.0;
- }
- printf("Finished Reading FENS\n");
- }
- void runTexelTuning(TexelEntry * te){
- int i, a, iteration = 0;
- int delta;
- int improved = 1;
- double this, best = error(te);
- int nparams = (2 * (9 + 14 + 15 + 28)) + (32 * 12);
- int ** terms = malloc(sizeof(int*) * nparams);
- for (i = 0, a = 0; a < 18; i++, a++) terms[i] = &KnightMobility[a/ 9][a% 9];
- for ( a = 0; a < 28; i++, a++) terms[i] = &BishopMobility[a/14][a%14];
- for ( a = 0; a < 30; i++, a++) terms[i] = & RookMobility[a/15][a%15];
- for ( a = 0; a < 56; i++, a++) terms[i] = & QueenMobility[a/28][a%28];
- for ( a = 0; a < 32; i++, a++) terms[i] = & PawnMidgameMap32[a];
- for ( a = 0; a < 32; i++, a++) terms[i] = & PawnEndgameMap32[a];
- for ( a = 0; a < 32; i++, a++) terms[i] = & KnightMidgameMap32[a];
- for ( a = 0; a < 32; i++, a++) terms[i] = & KnightEndgameMap32[a];
- for ( a = 0; a < 32; i++, a++) terms[i] = & BishopMidgameMap32[a];
- for ( a = 0; a < 32; i++, a++) terms[i] = & BishopEndgameMap32[a];
- for ( a = 0; a < 32; i++, a++) terms[i] = & RookMidgameMap32[a];
- for ( a = 0; a < 32; i++, a++) terms[i] = & RookEndgameMap32[a];
- for ( a = 0; a < 32; i++, a++) terms[i] = & QueenMidgameMap32[a];
- for ( a = 0; a < 32; i++, a++) terms[i] = & QueenEndgameMap32[a];
- for ( a = 0; a < 32; i++, a++) terms[i] = & KingMidgameMap32[a];
- for ( a = 0; a < 32; i++, a++) terms[i] = & KingEndgameMap32[a];
- printf("Terms [%3d | %3d]\n", i, nparams);
- while (improved){
- printf("ITERATION : %d\n", iteration);
- iteration++;
- improved = 0;
- for (i = 0; i < nparams; i++)
- printf("%d ", *terms[i]);
- printf("\n===============================\n");
- for (i = 0; i < nparams; i++){
- for (delta = 1 + abs(*terms[i] / 12); delta > 0; delta /= 2){
- // Try increasing the Parameter
- *terms[i] += delta;
- initalizePSQT();
- this = error(te);
- // Increasing Parameter was a success
- if (this < best - DBL_EPSILON){
- best = this;
- improved = 1;
- continue;
- }
- // Try decreasing the Parameter
- *terms[i] -= 2 * delta;
- initalizePSQT();
- this = error(te);
- // Decreasting Parameter was a success
- if (this < best - DBL_EPSILON){
- best = this;
- improved = 1;
- continue;
- }
- // Parameter should keep its current value
- *terms[i] += delta;
- }
- }
- }
- printf("DONE");
- }
- double error(TexelEntry * te){
- int i;
- double total = 0;
- #pragma omp parallel shared(total)
- {
- #pragma omp for schedule(static, 10000) reduction(+:total)
- for (i = 0; i < (int)NumberOfPositions; i++)
- total += singleError(te[i]);
- }
- return total;
- }
- double singleError(TexelEntry te){
- Board board;
- initalizeBoard(&board, te.fen);
- return pow(te.result - sigmoid(evaluateBoard(&board)), 2);
- }
- double sigmoid(int s){
- return 1.0 / (1 + pow(10, -K * (double)s / 400.0));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement