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/>.
- */
- #ifdef TUNE
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "bitboards.h"
- #include "board.h"
- #include "evaluate.h"
- #include "history.h"
- #include "move.h"
- #include "piece.h"
- #include "psqt.h"
- #include "search.h"
- #include "tune.h"
- #include "thread.h"
- #include "transposition.h"
- #include "types.h"
- #include "uci.h"
- EtherTuple *TupleStack;
- int TupleStackSize = STACKSIZE
- extern EvalTrace T, EmptyTace;
- extern const int PawnValue
- extern const int KnightValue
- extern const int BishopValue
- extern const int RookValue
- extern const int QueenValue
- extern const int KingValue
- extern const int PawnPSQT[32]
- extern const int KnightPSQT[32]
- extern const int BishopPSQT[32]
- extern const int RookPSQT[32]
- extern const int QueenPSQT[32]
- extern const int KingPSQT[32]
- extern const int PawnIsolated
- extern const int PawnStacked
- extern const int PawnBackwards[2]
- extern const int PawnConnected[32]
- extern const int KnightRammedPawns
- extern const int KnightOutpost[2]
- extern const int KnightMobility[9]
- extern const int BishopPair
- extern const int BishopRammedPawns
- extern const int BishopOutpost[2]
- extern const int BishopMobility[14]
- extern const int RookFile[2]
- extern const int RookOnSeventh
- extern const int RookMobility[15]
- extern const int QueenMobility[28]
- extern const int KingDefenders[12]
- extern const int KingShelter[2][8][8]
- extern const int PassedPawn[2][2][8]
- extern const int ThreatPawnAttackedByOne
- extern const int ThreatMinorAttackedByPawn
- extern const int ThreatMinorAttackedByMajor
- extern const int ThreatQueenAttackedByOne
- extern const int KingSafetyWeights
- extern const int KingSafetyBase
- extern const int KingSafetyWeakSquares
- extern const int KingSafetyEnemyPawns
- extern const int KingSafetyNoEnemyQueens
- void initParameters(double *cparams[NE][PHASE_NB]);
- int i = 0;
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(PawnValue[a]),
- cparams[i][EG] = ScoreEG(PawnValue[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(KnightValue[a]),
- cparams[i][EG] = ScoreEG(KnightValue[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(BishopValue[a]),
- cparams[i][EG] = ScoreEG(BishopValue[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(RookValue[a]),
- cparams[i][EG] = ScoreEG(RookValue[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(QueenValue[a]),
- cparams[i][EG] = ScoreEG(QueenValue[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(KingValue[a]),
- cparams[i][EG] = ScoreEG(KingValue[a]);
- for(int a = 0; a < 32; a++, i++)
- cparams[i][MG] = ScoreMG(PawnPSQT[a]),
- cparams[i][EG] = ScoreEG(PawnPSQT[a]);
- for(int a = 0; a < 32; a++, i++)
- cparams[i][MG] = ScoreMG(KnightPSQT[a]),
- cparams[i][EG] = ScoreEG(KnightPSQT[a]);
- for(int a = 0; a < 32; a++, i++)
- cparams[i][MG] = ScoreMG(BishopPSQT[a]),
- cparams[i][EG] = ScoreEG(BishopPSQT[a]);
- for(int a = 0; a < 32; a++, i++)
- cparams[i][MG] = ScoreMG(RookPSQT[a]),
- cparams[i][EG] = ScoreEG(RookPSQT[a]);
- for(int a = 0; a < 32; a++, i++)
- cparams[i][MG] = ScoreMG(QueenPSQT[a]),
- cparams[i][EG] = ScoreEG(QueenPSQT[a]);
- for(int a = 0; a < 32; a++, i++)
- cparams[i][MG] = ScoreMG(KingPSQT[a]),
- cparams[i][EG] = ScoreEG(KingPSQT[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(PawnIsolated[a]),
- cparams[i][EG] = ScoreEG(PawnIsolated[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(PawnStacked[a]),
- cparams[i][EG] = ScoreEG(PawnStacked[a]);
- for(int a = 0; a < 2; a++, i++)
- cparams[i][MG] = ScoreMG(PawnBackwards[a]),
- cparams[i][EG] = ScoreEG(PawnBackwards[a]);
- for(int a = 0; a < 32; a++, i++)
- cparams[i][MG] = ScoreMG(PawnConnected[a]),
- cparams[i][EG] = ScoreEG(PawnConnected[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(KnightRammedPawns[a]),
- cparams[i][EG] = ScoreEG(KnightRammedPawns[a]);
- for(int a = 0; a < 2; a++, i++)
- cparams[i][MG] = ScoreMG(KnightOutpost[a]),
- cparams[i][EG] = ScoreEG(KnightOutpost[a]);
- for(int a = 0; a < 9; a++, i++)
- cparams[i][MG] = ScoreMG(KnightMobility[a]),
- cparams[i][EG] = ScoreEG(KnightMobility[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(BishopPair[a]),
- cparams[i][EG] = ScoreEG(BishopPair[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(BishopRammedPawns[a]),
- cparams[i][EG] = ScoreEG(BishopRammedPawns[a]);
- for(int a = 0; a < 2; a++, i++)
- cparams[i][MG] = ScoreMG(BishopOutpost[a]),
- cparams[i][EG] = ScoreEG(BishopOutpost[a]);
- for(int a = 0; a < 14; a++, i++)
- cparams[i][MG] = ScoreMG(BishopMobility[a]),
- cparams[i][EG] = ScoreEG(BishopMobility[a]);
- for(int a = 0; a < 2; a++, i++)
- cparams[i][MG] = ScoreMG(RookFile[a]),
- cparams[i][EG] = ScoreEG(RookFile[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(RookOnSeventh[a]),
- cparams[i][EG] = ScoreEG(RookOnSeventh[a]);
- for(int a = 0; a < 15; a++, i++)
- cparams[i][MG] = ScoreMG(RookMobility[a]),
- cparams[i][EG] = ScoreEG(RookMobility[a]);
- for(int a = 0; a < 28; a++, i++)
- cparams[i][MG] = ScoreMG(QueenMobility[a]),
- cparams[i][EG] = ScoreEG(QueenMobility[a]);
- for(int a = 0; a < 12; a++, i++)
- cparams[i][MG] = ScoreMG(KingDefenders[a]),
- cparams[i][EG] = ScoreEG(KingDefenders[a]);
- for(int a = 0; a < 2; a++)
- for(int b = 0; b < 8; b++)
- for(int c = 0; c < 8; c++, i++)
- cparams[i][MG] = ScoreMG(KingShelter[a][b][c]),
- cparams[i][EG] = ScoreEG(KingShelter[a][b][c]);
- for(int a = 0; a < 2; a++)
- for(int b = 0; b < 2; b++)
- for(int c = 0; c < 8; c++, i++)
- cparams[i][MG] = ScoreMG(PassedPawn[a][b][c]),
- cparams[i][EG] = ScoreEG(PassedPawn[a][b][c]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(ThreatPawnAttackedByOne[a]),
- cparams[i][EG] = ScoreEG(ThreatPawnAttackedByOne[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(ThreatMinorAttackedByPawn[a]),
- cparams[i][EG] = ScoreEG(ThreatMinorAttackedByPawn[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(ThreatMinorAttackedByMajor[a]),
- cparams[i][EG] = ScoreEG(ThreatMinorAttackedByMajor[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(ThreatQueenAttackedByOne[a]),
- cparams[i][EG] = ScoreEG(ThreatQueenAttackedByOne[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(KingSafetyWeights[a]),
- cparams[i][EG] = ScoreEG(KingSafetyWeights[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(KingSafetyBase[a]),
- cparams[i][EG] = ScoreEG(KingSafetyBase[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(KingSafetyWeakSquares[a]),
- cparams[i][EG] = ScoreEG(KingSafetyWeakSquares[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(KingSafetyEnemyPawns[a]),
- cparams[i][EG] = ScoreEG(KingSafetyEnemyPawns[a]);
- for(int a = 0; a < 1; a++, i++)
- cparams[i][MG] = ScoreMG(KingSafetyNoEnemyQueens[a]),
- cparams[i][EG] = ScoreEG(KingSafetyNoEnemyQueens[a]);
- };
- double computeOptimalK(EtherEntry *entries, double *params[NE][PHASE_NB]){
- double start = -10.0, end = 10.0, delta = 1.0;
- double curr = start, thisError, bestError = completeLinearError(entries, params, start);
- for (int i = 0; i < 10; i++){
- printf("Computing K Iteration [%d] ", i);
- curr = start - delta;
- while (curr < end){
- curr = curr + delta;
- thisError = completeLinearError(entries, params, start);
- if (thisError <= bestError)
- bestError = thisError, start = curr;
- }
- printf("K = %f E = %f\n", start, bestError);
- end = start + delta;
- start = start - delta;
- delta = delta / 10.0;
- }
- return start;
- }
- double completeLinearError(EtherEntry *entries, double *params[NE][PHASE_NB], double K){
- double total = 0.0
- #pragma omp parallel shared(total)
- {
- #pragma omp for schedule(static, NP / NC) reduction(+:total)
- for (int i = 0; i < NP; i++)
- total += pow(entries[i].result - sigmoid(K, linearEvaluation(entries[i], params)), 2);
- }
- return total / (double)NP;
- };
- double singleLinearError(EtherEntry entry, double *params[NE][PHASE_NB], double K){
- return entry.result - sigmoid(K, linearEvaluation(entry, params));
- };
- double linearEvaluation(EtherEntry entry, double *params[NE][PHASE_NB]){
- double eval, evals[NE], mg, eg;
- for (int i = 0, mg = eg = 0; i < entry.ntuples[0]; i++){
- mg += entry.tuples[0][i].coeff * params[entry.tuples[0][i].index][MG];
- eg += entry.tuples[0][i].coeff * params[entry.tuples[0][i].index][EG];
- }
- mg += entry.counts[0][MG];
- eg += entry.counts[0][EG];
- mg = mg;
- eg = eg;
- evals[0] = ((mg * (256 - entry.phase) + eg * entry.phase) / 256.0);
- for (int i = 0, mg = eg = 0; i < entry.ntuples[1]; i++){
- mg += entry.tuples[1][i].coeff * params[entry.tuples[1][i].index][MG];
- eg += entry.tuples[1][i].coeff * params[entry.tuples[1][i].index][EG];
- }
- mg += entry.counts[1][MG];
- eg += entry.counts[1][EG];
- mg = mg * mg / 64;
- eg = 0;
- evals[1] = ((mg * (256 - entry.phase) + eg * entry.phase) / 256.0);
- for (int i = 0, eval = 0; i < NE; i++)
- eval += evals[i];
- return eval;
- double sigmoid(double K, double score){
- return 1.0 / (1.0 + pow(10.0, -K * S / 400.0));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement