static int eval_black_rook(int sq) { int score = 0; int file = calc_file(sq); int rank = calc_rank(sq); int k_sq = King_Square(B); bool wf,bf; wf = (bool)(true && (file_mask[file] & board->bb_pawns[W])); bf = (bool)(true && (file_mask[file] & board->bb_pawns[B])); if(rank == RANK_2) score += ROOK_ON_7TH_REWARD; if(!bf && !wf) { score += ROOK_ON_OPEN_FILE_REWARD; if(rank < RANK_8) { if(PieceType(sq + 16) == BLACK_ROOK) score+= ROOKS_DOUBLED; } } if(!bf && wf) score += ROOK_ON_SEMI_OPEN_FILE_REWARD; //black trapped rook: if(sq == H8 || sq == G8) { if(k_sq > E8 && k_sq < H8) score -= TRAPPED_ROOK_PENALTY; } if(sq >= A8 && sq <= C8) { if(k_sq <= D8) score -= TRAPPED_ROOK_PENALTY; } if(file == FILE_E) score += ROOK_ON_E_FILE_REWARD; if(file == FILE_D) score += ROOK_ON_D_FILE_REWARD; return score; }
Datum ts_rank_tt(PG_FUNCTION_ARGS) { TSVector txt = PG_GETARG_TSVECTOR(0); TSQuery query = PG_GETARG_TSQUERY(1); float res; res = calc_rank(getWeights(NULL), txt, query, DEF_NORM_METHOD); PG_FREE_IF_COPY(txt, 0); PG_FREE_IF_COPY(query, 1); PG_RETURN_FLOAT4(res); }
Datum ts_rank_ttf(PG_FUNCTION_ARGS) { TSVector txt = PG_GETARG_TSVECTOR(0); TSQuery query = PG_GETARG_TSQUERY(1); int method = PG_GETARG_INT32(2); float res; res = calc_rank(getWeights(NULL), txt, query, method); PG_FREE_IF_COPY(txt, 0); PG_FREE_IF_COPY(query, 1); PG_RETURN_FLOAT4(res); }
Datum ts_rank_wtt(PG_FUNCTION_ARGS) { ArrayType *win = (ArrayType *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); TSVector txt = PG_GETARG_TSVECTOR(1); TSQuery query = PG_GETARG_TSQUERY(2); float res; res = calc_rank(getWeights(win), txt, query, DEF_NORM_METHOD); PG_FREE_IF_COPY(win, 0); PG_FREE_IF_COPY(txt, 1); PG_FREE_IF_COPY(query, 2); PG_RETURN_FLOAT4(res); }
Datum rank(PG_FUNCTION_ARGS) { ArrayType *win = (ArrayType *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_DATUM(2)); int method = DEF_NORM_METHOD; float res = 0.0; float ws[lengthof(weights)]; float4 *arrdata; int i; if (ARR_NDIM(win) != 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array of weight must be one-dimensional"))); if (ARRNELEMS(win) < lengthof(weights)) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("array of weight is too short"))); if (ARR_HASNULL(win)) ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), errmsg("array of weight must not contain nulls"))); arrdata = (float4 *) ARR_DATA_PTR(win); for (i = 0; i < lengthof(weights); i++) { ws[i] = (arrdata[i] >= 0) ? arrdata[i] : weights[i]; if (ws[i] > 1.0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("weight out of range"))); } if (PG_NARGS() == 4) method = PG_GETARG_INT32(3); res = calc_rank(ws, txt, query, method); PG_FREE_IF_COPY(win, 0); PG_FREE_IF_COPY(txt, 1); PG_FREE_IF_COPY(query, 2); PG_RETURN_FLOAT4(res); }
Datum rank_def(PG_FUNCTION_ARGS) { tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); float res = 0.0; int method = DEF_NORM_METHOD; if (PG_NARGS() == 3) method = PG_GETARG_INT32(2); res = calc_rank(weights, txt, query, method); PG_FREE_IF_COPY(txt, 0); PG_FREE_IF_COPY(query, 1); PG_RETURN_FLOAT4(res); }
static int eval_white_rook(int sq) { int score = 0; int file = calc_file(sq); int rank = calc_rank(sq); int k_sq = King_Square(W); bool wf,bf; wf = (bool)(true && (file_mask[file] & board->bb_pawns[W])); bf = (bool)(true && (file_mask[file] & board->bb_pawns[B])); if(rank == RANK_7) score += ROOK_ON_7TH_REWARD; if(!wf && !bf) { score += ROOK_ON_OPEN_FILE_REWARD; //opened file with doubled rooks //(backward rook examined if exists): if(rank > RANK_1) { if(PieceType(sq-16) == WHITE_ROOK) score+= ROOKS_DOUBLED; } } if(!wf && bf) score += ROOK_ON_SEMI_OPEN_FILE_REWARD; //white trapped rook: if(sq == H1 || sq == G1) { if(k_sq > E1 && k_sq < H1) score -= TRAPPED_ROOK_PENALTY; } if(sq >= A1 && sq <= C1) { if(k_sq <= D1) score -= TRAPPED_ROOK_PENALTY; } if(file == FILE_E) score += ROOK_ON_E_FILE_REWARD; if(file == FILE_D) score += ROOK_ON_D_FILE_REWARD; return score; }
/** * @detail * All variables are named as in the fore mentioned paper * 1. Use a row of blocks of Matrix M to create a matrix X * 2. Use a column of blocks of Matrix M to create a matrix Y * 3. Get invertible matrices P and Q such that P(X.Minv.Y)Q = [I 0] * (I is of r dimension; 0 is of 2-r dimension; 0 < r < 3) * 4. Define matrix A as follows: * a) if r = 0 then A = I * b) if r = 1 then A = [[1 1] [1 0]] * c) if r = 2 then A = [[0 1] [1 1]] * 5. Then the following is a (t+2, 2) block invertible matrix: * | M Y | * | X X.Minv.Y + Pinv.A.Qinv | */ static gf2matrix *extend_block_invertible_by2(const gf2matrix *m) { gf2matrix *result = NULL; gf2matrix *x = NULL, *y = NULL; int multiples = get_rows(m) / 2; int x_start = get_random(0, multiples - 1) * 2; int y_start = get_random(0, multiples - 1) * 2; if (!m) { gf2matrix *_2x2; get_2x2invertible_pair(get_random(0, MAX_2X2_INVERTIBLES - 1), &_2x2, NULL); result = dup_matrix(_2x2); return result; } /* print_matrix(m, "Extending m by 2:"); */ /* step 1 */ x = extract_block_row(NULL, x_start, m); assert(x); /* print_matrix(x, "X:"); */ /* step 2 */ y = extract_block_col(NULL, y_start, m); assert(y); /* print_matrix(y, "Y:"); */ { /* steps 3, 4, 5 */ int r; /* r is the rank of X.Minv.Y */ gf2matrix *i0mat = new_matrix(2, 2); gf2matrix *prod = new_matrix(2, 2); gf2matrix *temp = NULL; gf2matrix *mInv = invert_matrix(NULL, m); gf2matrix *xminvy = NULL; gf2matrix *a2 = NULL; { assert(mInv); /* calculate X.Minv.Y */ gf2matrix *minvy = mul_matrices(NULL, mInv, y); xminvy = mul_matrices(NULL, x, minvy); assert(xminvy); free_matrix(minvy); } /* print_matrix(xminvy, "xM-1y: "); */ r = calc_rank(xminvy); /* printf("rank of X.Minv.Y = %d\n", r); */ init_IO_matrix(i0mat, r); /* print_matrix(i0mat, "I0:"); */ a2 = make_A2_matrix(r); temp = new_matrix(2, 2); while (!result) { int i, j, i_tries, j_tries; for (i = get_random(0, MAX_2X2_INVERTIBLES - 1), i_tries = 0; i_tries < MAX_2X2_INVERTIBLES && !result; i = (i + 1) % MAX_2X2_INVERTIBLES, ++i_tries) { gf2matrix *q, *qinv; get_2x2invertible_pair(i, &q, &qinv); /* print_matrix(q, "Q:"); */ mul_matrices(temp, xminvy, q); assert(temp); for (j = get_random(0, MAX_2X2_INVERTIBLES - 1), j_tries = 0; j_tries < MAX_2X2_INVERTIBLES && !result; j = (j + 1) % MAX_2X2_INVERTIBLES, ++j_tries) { gf2matrix *p, *pinv; get_2x2invertible_pair(j, &p, &pinv); mul_matrices(prod, p, temp); assert(prod); /* print_matrix(p, "P:"); */ /* print_matrix(prod, "P.X.Minv.Y.Q:"); */ if (comp_matrices(prod, i0mat) == 0) { /* step 5 */ result = extend_by_blocks(m, x, y, xminvy, pinv, a2, qinv); assert(result); break; } } } } free_matrix(a2); free_matrix(xminvy); free_matrix(mInv); free_matrix(prod); free_matrix(temp); free_matrix(i0mat); if (!result) printf("incorrect matrix expansion algorithm"); } cleanup: free_matrix(y); free_matrix(x); return result; }
skew_node* skew_node::set_right_child(skew_node* t){ right_child = t; calc_rank(); }
skew_node* skew_node::set_left_child(skew_node* t){ left_child = t; calc_rank(); }
skew_node::skew_node(int x){ right_child = nullptr; left_child = nullptr; data = x; calc_rank(); }
int pv_to_string(char *s,int depth) {//takes tha addr of buffer as argument //and returns the pv string length in moves count. int i,file,rank; move_data_t m; int len,move_count = 0; len = pv_collect(); //check for redundancy repetitions: if(len > MAX_PLY-1) len = depth; for(i = 0; i < len; i++) { m.p = pv_backup[i].p; file = calc_file(m.from); rank = calc_rank(m.from); switch(file) { case FILE_A: *s = 'a'; break; case FILE_B: *s = 'b'; break; case FILE_C: *s = 'c'; break; case FILE_D: *s = 'd'; break; case FILE_E: *s = 'e'; break; case FILE_F: *s = 'f'; break; case FILE_G: *s = 'g'; break; case FILE_H: *s = 'h'; break; default: break; } s++; switch(rank) { case RANK_1: *s = '1'; break; case RANK_2: *s = '2'; break; case RANK_3: *s = '3'; break; case RANK_4: *s = '4'; break; case RANK_5: *s = '5'; break; case RANK_6: *s = '6'; break; case RANK_7: *s = '7'; break; case RANK_8: *s = '8'; break; } s++; file = calc_file(m.to); rank = calc_rank(m.to); switch(file) { case FILE_A: *s = 'a'; break; case FILE_B: *s = 'b'; break; case FILE_C: *s = 'c'; break; case FILE_D: *s = 'd'; break; case FILE_E: *s = 'e'; break; case FILE_F: *s = 'f'; break; case FILE_G: *s = 'g'; break; case FILE_H: *s = 'h'; break; default: break; } s++; switch(rank) { case RANK_1: *s = '1'; break; case RANK_2: *s = '2'; break; case RANK_3: *s = '3'; break; case RANK_4: *s = '4'; break; case RANK_5: *s = '5'; break; case RANK_6: *s = '6'; break; case RANK_7: *s = '7'; break; case RANK_8: *s = '8'; break; } s++; switch(GetType(m.promoted)) { case QUEEN: *s = 'Q'; s++; break; case ROOK: *s = 'R'; s++; break; case BISHOP: *s = 'B'; s++; break; case KNIGHT: *s = 'N'; s++; break; default: break; } *s = ' '; s++; move_count++; } *s = '\0'; //null terminating return move_count; }
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]); }
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]); }