TwoObjectivesInstance::TwoObjectivesInstance(string name, string filename1, string filename2) :m_Name(name), m_File1Path(filename1), m_File2Path(filename2) { // On initialise srand std::srand(std::time(0)); // On établit la précision //cout.precision(dbl::max_digits10); cout.precision(8); // On parse les fichiers this->m_File1Matrix = this->parsingTSPFile(filename1, &this->m_File1Dimension); this->m_File2Matrix = this->parsingTSPFile(filename2, &this->m_File2Dimension); // On teste si les dimensions des deux fichiers sont les mêmes // Si ce n'est pas le cas, il y'a un problème if(this->m_File1Dimension != this->m_File2Dimension) { cout << "Erreur: les deux fichiers n'ont pas le même nombre de villes" << endl; cout << filename1 << " possède " << this->m_File1Dimension << " villes"<< endl; cout << filename2 << " possède " << this->m_File2Dimension << " villes"<< endl; exit(EXIT_FAILURE); } // On génère 500 solutions for(int i = 0; i < SOLUTIONS; i++) { #if SHOW_DEBUGS cout << endl << "Solution N°" << i+1 << endl; #endif // SHOW_DEBUGS this->generateSolution(i); } // Filtrage Online this->onlineFiltering(); // Filtrage Offline this->offlineFiltering(); // On affiche les 500 solutions #if SHOW_DEBUGS for(int i = 0; i < SOLUTIONS; i++) { cout << endl << "Solution N°" << i+1 << endl; cout << "Distance: " << this->m_solutions[i].Getdistance() << " - Coût: " << this->m_solutions[i].Getcost() << endl; cout << "Dominée online: " << this->m_solutions[i].GetOnlineDominated() << endl; cout << "Dominée offline: " << this->m_solutions[i].GetOfflineDominated() << endl; } #endif // SHOW_DEBUGS // On crée un graphique sur gnuplot avec toutes les solutions this->makePlot(this->m_Name, OFFLINE, false); this->makePlot(this->m_Name, ONLINE, false); // On crée un graphique sur gnuplot avec le Front de Pareto this->makePlot(this->m_Name, OFFLINE, true); this->makePlot(this->m_Name, ONLINE, true); // 5) Fichier 10 approx front pareto this->createApproxFile(); // On exécute au moins 10 fois cout << "Approximations du front de Pareto pour l'instance " << m_Name << endl; for(int i = 0; i < 10; i++) { double cpu0 = get_cpu_time(); // On remplit un front de Pareto dans le fichier vector<Solution> best_sols = PLS(); double cpu1 = get_cpu_time(); cout << "Temps d'exécution itération " << i+1 << " : " << (int)( (cpu1 - cpu0) * 1000) << " milliseconds (CPU time)" << endl; this->fillApproxFile(best_sols, i); } makePlotForPLS(); }
static int evaluate() { int sq; int score[2]; sq_t king_square[2]; state_t *state = &board->state; //clearing data: score[W] = 0; score[B] = 0; ///pawn evaluation: eval_pawn_struct(&score[0]); ///piece evaluation //kings king_square[W] = King_Square(W); king_square[B] = King_Square(B); score[W] += psq_table[WK][king_square[W]]; score[W] += eval_white_king(king_square[W]); score[B] += psq_table[BK][king_square[B]]; score[B] += eval_black_king(king_square[B]); //queens: for(sq = PLS(WQ); sq <= H8; sq = PLN(sq)) { score[W] += psq_table[WQ][sq]; if(calc_rank(sq) == RANK_7 && calc_rank(king_square[BLACK]) == RANK_8) score[W] += QUEEN_ON_7TH_REWARD; score[W] += eval_q_mobility(sq,W); } for(sq = PLS(BQ); sq <= H8; sq = PLN(sq)) { score[B] += psq_table[BQ][sq]; if(calc_rank(sq) == RANK_2 && calc_rank(king_square[WHITE]) == RANK_1) score[B] += QUEEN_ON_7TH_REWARD; score[B] += eval_q_mobility(sq,B); } //rooks: for(sq = PLS(WR); sq <= H8; sq = PLN(sq)) { score[W] += psq_table[WR][sq]; score[W] += eval_white_rook(sq); score[W] += eval_r_mobility(sq,W); } for(sq = PLS(BR); sq <= H8; sq = PLN(sq)) { score[B] += psq_table[BR][sq]; score[B] += eval_black_rook(sq); score[B] += eval_r_mobility(sq,B); } //bishops: for(sq = PLS(WB); sq <= H8; sq = PLN(sq)) { score[W] += psq_table[WB][sq]; score[W] += eval_white_bishop(sq); score[W] += eval_b_mobility(sq,W); //outposts - twice as low than a knight outpost: if(!((1ULL << rsz[sq]) & pawn_attacks[B])) { if(psq_outposts[W][sq]) { //if not attacked by an opponent's pawn, //this could be a weak square that matters, //in accordance with the outposts table: score[W] += (psq_outposts[W][sq]) / 2; //additional bonus if it's reinforced by own pawns: if((1ULL << rsz[sq]) & pawn_attacks[W]) score[W] += (psq_outposts[W][sq]) / 2; } } } for(sq = PLS(BB); sq <= H8; sq = PLN(sq)) { score[B] += psq_table[BB][sq]; score[B] += eval_black_bishop(sq); score[B] += eval_b_mobility(sq,B); //outposts: if(!((1ULL << rsz[sq]) & pawn_attacks[W])) { if(psq_outposts[B][sq]) { score[B] += (psq_outposts[B][sq]) / 2; if((1ULL << rsz[sq]) & pawn_attacks[B]) score[B] += (psq_outposts[B][sq]) / 2; } } } //knights: for(sq = PLS(WN); sq <= H8; sq = PLN(sq)) { score[W] += psq_table[WN][sq]; score[W] += eval_n_mobility(sq,W); //outposts: if(!((1ULL << rsz[sq]) & pawn_attacks[B])) { if(psq_outposts[W][sq]) { score[W] += psq_outposts[W][sq]; if((1ULL << rsz[sq]) & pawn_attacks[W]) score[W] += psq_outposts[W][sq]; } } } for(sq = PLS(BN); sq <= H8; sq = PLN(sq)) { score[B] += psq_table[BN][sq]; score[B] += eval_n_mobility(sq,B); //outposts: if(!((1ULL << rsz[sq]) & pawn_attacks[W])) { if(psq_outposts[B][sq]) { score[B] += psq_outposts[B][sq]; if((1ULL << rsz[sq]) & pawn_attacks[B]) score[B] += psq_outposts[B][sq]; } } } //bishop pair bonus depending on pawns on board: if(Bishops(W) >= 2) score[W] += BISHOP_PAIR_REWARD - state->pawns; if(Bishops(B) >= 2) score[B] += BISHOP_PAIR_REWARD - state->pawns; //calculate basic knight and rook material imbalances: /********************************************************** quote from GM L.Kaufman: "A further refinement would be to raise the knight's value by 1/16 and lower the rook's value by 1/8 for each pawn above five of the side being valued, with the opposite adjustment for each pawn short of five". ***********************************************************/ score[W] += Rooks(W) * rook_imbalance[Pawns(W)]; score[W] += Knights(W) * knight_imbalance[Pawns(W)]; score[B] += Rooks(B) * rook_imbalance[Pawns(B)]; score[B] += Knights(B) * knight_imbalance[Pawns(B)]; if(opening) { score[W] += eval_white_opening(); score[B] += eval_black_opening(); } ///final results: score[W] += state->material_value[WHITE] + \ state->pawn_value[WHITE]; score[B] += state->material_value[BLACK] + \ state->pawn_value[BLACK]; if(board->side == W) return (score[W]-score[B]); else return -(score[W]-score[B]); }
static int evaluate_endgame() { int sq; int score[2]; sq_t king_square[2]; state_t *state = &board->state; //clearing data: score[W] = 0; score[B] = 0; ///pawn evaluation: eval_pawn_struct_endgame(&score[0]); ///piece evaluation //kings king_square[W] = King_Square(W); king_square[B] = King_Square(B); score[W] += endgame_king_psq[W][king_square[W]]; score[B] += endgame_king_psq[B][king_square[B]]; if(state->material) { //queens: for(sq = PLS(WQ); sq <= H8; sq = PLN(sq)) { score[W] += psq_table[WQ][sq]; if(calc_rank(sq) == RANK_7 && calc_rank(king_square[BLACK]) == RANK_8) score[W] += QUEEN_ON_7TH_REWARD; } for(sq = PLS(BQ); sq <= H8; sq = PLN(sq)) { score[B] += psq_table[BQ][sq]; if(calc_rank(sq) == RANK_2 && calc_rank(king_square[WHITE]) == RANK_1) score[B] += QUEEN_ON_7TH_REWARD; } //rooks: for(sq = PLS(WR); sq <= H8; sq = PLN(sq)) { score[W] += psq_table[WR][sq]; score[W] += eval_white_rook(sq); } for(sq = PLS(BR); sq <= H8; sq = PLN(sq)) { score[B] += psq_table[BR][sq]; score[B] += eval_black_rook(sq); } //bishops: for(sq = PLS(WB); sq <= H8; sq = PLN(sq)) { score[W] += psq_table[WB][sq]; score[W] += eval_white_bishop(sq); //outposts if(!((1ULL << rsz[sq]) & pawn_attacks[B])) { if(psq_outposts[W][sq]) { score[W] += (psq_outposts[W][sq]) / 2; if((1ULL << rsz[sq]) & pawn_attacks[W]) score[W] += (psq_outposts[W][sq]) / 2; } } } for(sq = PLS(BB); sq <= H8; sq = PLN(sq)) { score[B] += psq_table[BB][sq]; score[B] += eval_black_bishop(sq); //outposts: if(!((1ULL << rsz[sq]) & pawn_attacks[W])) { if(psq_outposts[B][sq]) { score[B] += (psq_outposts[B][sq]) / 2; if((1ULL << rsz[sq]) & pawn_attacks[B]) score[B] += (psq_outposts[B][sq]) / 2; } } } //knights: for(sq = PLS(WN); sq <= H8; sq = PLN(sq)) { score[W] += psq_table[WN][sq]; //outposts: if(!((1ULL << rsz[sq]) & pawn_attacks[B])) { if(psq_outposts[W][sq]) { score[W] += psq_outposts[W][sq]; if((1ULL << rsz[sq]) & pawn_attacks[W]) score[W] += psq_outposts[W][sq]; } } } for(sq = PLS(BN); sq <= H8; sq = PLN(sq)) { score[B] += psq_table[BN][sq]; //outposts: if(!((1ULL << rsz[sq]) & pawn_attacks[W])) { if(psq_outposts[B][sq]) { score[B] += psq_outposts[B][sq]; if((1ULL << rsz[sq]) & pawn_attacks[B]) score[B] += psq_outposts[B][sq]; } } } //bishop pair bonus depending on pawns on board: if(Bishops(W) >= 2) score[W] += BISHOP_PAIR_REWARD - state->pawns; if(Bishops(B) >= 2) score[B] += BISHOP_PAIR_REWARD - state->pawns; //calculate basic knight and rook material imbalances: score[W] += Rooks(W) * rook_imbalance[Pawns(W)]; score[W] += Knights(W) * knight_imbalance[Pawns(W)]; score[B] += Rooks(B) * rook_imbalance[Pawns(B)]; score[B] += Knights(B) * knight_imbalance[Pawns(B)]; //a piece of code to encourage exchanges //in case of material advantage: if(state->material_value[W] > state->material_value[B] || state->pawn_value[W] > state->pawn_value[B]) score[W] -= (state->piece_count[W] + state->piece_count[B]) * 2; if(state->material_value[B] > state->material_value[W] || state->pawn_value[B] > state->pawn_value[W]) score[B] -= (state->piece_count[B] + state->piece_count[W]) * 2; } return endeval(&score[0]); }
uint64 perft(int depth) { int i,move_count; move_t ms[MOVE_STACK]; uint64 nodes; uint64 val; #ifdef EVASIONS char strbuff[256]; move_t ms_test[MOVE_STACK]; #endif #ifdef BITS bitboard_t bb; #endif if(depth == 0) return 1; if((val = probe_hash(depth)) != 0) return val; nodes = 0; val = 0; #ifdef BITS bb = 0; for(i = PLS(WP); i <= H8; i = PLN(i)) bitset(&bb, rsz[i]); if(bb != board->bb_pawns[W]) printf("pawn_error\n"); bb = 0; for(i = PLS(BP); i <= H8; i = PLN(i)) bitset(&bb, rsz[i]); if(bb != board->bb_pawns[B]) printf("pawn_error\n"); #endif #ifdef EVASIONS if(is_in_check(board->side)) { move_count = move_gen_evasions(&ms[0]); if(move_count != move_gen_legal(&ms_test[0])) { board_to_fen(&strbuff[0]); printf("error: \n %s \n",strbuff); } } else { move_count = move_gen(&ms[0]); } #else move_count = move_gen(&ms[0]); #endif for(i = 0; i < move_count; i++) { if(!move_make(ms[i])) continue; if(depth == 1) nodes++; else nodes += perft(depth - 1); move_undo(); } record_hash(depth,nodes); return (nodes); }