Ejemplo n.º 1
0
        public static val search(cPosicion pos, cStackMov[] ss, int ssPos, val alpha, val beta, ply depth, bool cutNode, NodeType NT, bool SpNode)
        {
            bool RootNode = (NT == cTipoNodo.RAIZ);
              bool PvNode = (NT == cTipoNodo.PV || NT == cTipoNodo.RAIZ);
              mov[] quietsSearched = new mov[64];
              cPosInfo st = new cPosInfo();
              cTablaHashStruct tte;
              cSplitPoint splitPoint = null;
              hash posKey = 0;
              mov ttMove, move, excludedMove, bestMove;
              ply ext, newDepth, predictedDepth;
              val bestValue, value, ttValue, eval, nullValue, futilityValue;
              bool inCheck, givesCheck, pvMove, singularExtensionNode, improving;
              bool captureOrPromotion, dangerous, doFullDepthSearch;
              int moveCount = 0, quietCount = 0;

              cThread thisThread = pos.ThreadActive();
              inCheck = pos.Jaques() != 0;
              if (SpNode)
              {
            splitPoint = ss[ssPos].splitPoint;
            bestMove = splitPoint.bestMove;
            bestValue = splitPoint.bestValue;
            tte = null;
            ttMove = excludedMove = cMovType.MOV_NAN;
            ttValue = cValoresJuego.NAN;
            goto moves_loop;
              }
              moveCount = quietCount = 0;
              bestValue = -cValoresJuego.INFINITO;
              ss[ssPos].currentMove = ss[ssPos].ttMove = ss[ssPos + 1].excludedMove = bestMove = cMovType.MOV_NAN;
              ss[ssPos].ply = ss[ssPos - 1].ply + 1;
              ss[ssPos + 1].skipNullMove = 0;
              ss[ssPos + 1].reduction = cPly.DEPTH_ZERO;
              ss[ssPos + 2].killers0 = ss[ssPos + 2].killers1 = cMovType.MOV_NAN;

              if (PvNode && thisThread.m_nMaxPly < ss[ssPos].ply)
            thisThread.m_nMaxPly = ss[ssPos].ply;
              if (!RootNode)
              {

            if (Signals.STOP || pos.IsTablas() || ss[ssPos].ply > cSearch.MAX_PLY)
              return ss[ssPos].ply > cSearch.MAX_PLY && !inCheck ? cEval.Eval(pos) : DrawValue[pos.ColorMueve()];

            alpha = Math.Max(cTypes.MateEnVs(ss[ssPos].ply), alpha);
            beta = Math.Min(cTypes.MateEn(ss[ssPos].ply + 1), beta);
            if (alpha >= beta)
              return alpha;
              }

              excludedMove = ss[ssPos].excludedMove;
              posKey = excludedMove != 0 ? pos.GetClaveHashExclusion() : pos.ClaveHash();
              tte = cMotor.m_TablaHash.Buscar(posKey);
              ss[ssPos].ttMove = ttMove = RootNode ? RootMoves[PVIdx].m_PV[0] : tte != null ? tte.GetMove() : cMovType.MOV_NAN;
              ttValue = (tte != null) ? value_from_tt(tte.GetValue(), ss[ssPos].ply) : cValoresJuego.NAN;

              if (!RootNode
              && tte != null
              && tte.GetDepth() >= depth
              && ttValue != cValoresJuego.NAN
              && (PvNode ? tte.GetBound() == cBordes.BOUND_EXACT
              : ttValue >= beta ? (tte.GetBound() & cBordes.BOUND_LOWER) != 0
                                : (tte.GetBound() & cBordes.BOUND_UPPER) != 0))
              {
            ss[ssPos].currentMove = ttMove;

            if (ttValue >= beta && ttMove != 0 && !pos.IsCapturaOrPromocion(ttMove) && !inCheck)
              update_stats(pos, ss, ssPos, ttMove, depth, null, 0);
            return ttValue;
              }

              if (inCheck)
              {
            ss[ssPos].staticEval = eval = cValoresJuego.NAN;
            goto moves_loop;
              }
              else if (tte != null)
              {

            if ((ss[ssPos].staticEval = eval = tte.GetValueEval()) == cValoresJuego.NAN)
              eval = ss[ssPos].staticEval = cEval.Eval(pos);

            if (ttValue != cValoresJuego.NAN)
              if ((tte.GetBound() & (ttValue > eval ? cBordes.BOUND_LOWER : cBordes.BOUND_UPPER)) != 0)
            eval = ttValue;
              }
              else
              {
            eval = ss[ssPos].staticEval = cEval.Eval(pos);
            cMotor.m_TablaHash.Save(posKey, cValoresJuego.NAN, cBordes.BOUND_NONE, cPly.DEPTH_NONE, cMovType.MOV_NAN, ss[ssPos].staticEval);
              }
              if (0 == pos.GetTipoPiezaCapturada()
              && ss[ssPos].staticEval != cValoresJuego.NAN
              && ss[ssPos - 1].staticEval != cValoresJuego.NAN
              && (move = ss[ssPos - 1].currentMove) != cMovType.MOV_NULO
              && cTypes.TipoMovimiento(move) == cMovType.NORMAL)
              {
            sq to = cTypes.GetToCasilla(move);
            Gains.Fill(pos.GetPieza(to), to, -ss[ssPos - 1].staticEval - ss[ssPos].staticEval);
              }

              if (!PvNode
              && depth < 4 * cPly.ONE_PLY
              && eval + razor_margin(depth) <= beta
              && ttMove == cMovType.MOV_NAN
              && Math.Abs(beta) < cValoresJuego.MATE_MAXIMO
              && !pos.IsPeonOn7(pos.ColorMueve())
              )
              {
            if (depth <= cPly.ONE_PLY
            && eval + razor_margin(3 * cPly.ONE_PLY) <= alpha)
              return qsearch(pos, ss, ssPos, alpha, beta, cPly.DEPTH_ZERO, cTipoNodo.NO_PV, false);
            val ralpha = alpha - razor_margin(depth);
            val v = qsearch(pos, ss, ssPos, ralpha, ralpha + 1, cPly.DEPTH_ZERO, cTipoNodo.NO_PV, false);
            if (v <= ralpha)
              return v;
              }

              if (!PvNode
              && 0 == ss[ssPos].skipNullMove
              && depth < 7 * cPly.ONE_PLY
              && eval - futility_margin(depth) >= beta
              && Math.Abs(beta) < cValoresJuego.MATE_MAXIMO
              && Math.Abs(eval) < cValoresJuego.GANA
              && pos.MaterialPieza(pos.ColorMueve()) != 0)
            return eval - futility_margin(depth);

              if (!PvNode
              && 0 == ss[ssPos].skipNullMove
              && depth >= 2 * cPly.ONE_PLY
              && eval >= beta
              && Math.Abs(beta) < cValoresJuego.MATE_MAXIMO
              && pos.MaterialPieza(pos.ColorMueve()) != 0)
              {
            ss[ssPos].currentMove = cMovType.MOV_NULO;

            ply R = 3 * cPly.ONE_PLY
             + depth / 4
             + (eval - beta) / cValoresJuego.PEON_MJ * cPly.ONE_PLY;
            pos.DoNullMov(st);
            ss[ssPos + 1].skipNullMove = 1;
            nullValue = depth - R < cPly.ONE_PLY ? -qsearch(pos, ss, ssPos + 1, -beta, -beta + 1, cPly.DEPTH_ZERO, cTipoNodo.NO_PV, false)
                              : -search(pos, ss, ssPos + 1, -beta, -beta + 1, depth - R, !cutNode, cTipoNodo.NO_PV, false);
            ss[ssPos + 1].skipNullMove = 0;
            pos.undo_null_move();
            if (nullValue >= beta)
            {

              if (nullValue >= cValoresJuego.MATE_MAXIMO)
            nullValue = beta;
              if (depth < 12 * cPly.ONE_PLY)
            return nullValue;

              ss[ssPos].skipNullMove = 1;
              val v = depth - R < cPly.ONE_PLY ? qsearch(pos, ss, ssPos, beta - 1, beta, cPly.DEPTH_ZERO, cTipoNodo.NO_PV, false)
                              : search(pos, ss, ssPos, beta - 1, beta, depth - R, false, cTipoNodo.NO_PV, false);
              ss[ssPos].skipNullMove = 0;
              if (v >= beta)
            return nullValue;
            }
              }

              if (!PvNode
              && depth >= 5 * cPly.ONE_PLY
              && 0 == ss[ssPos].skipNullMove
              && Math.Abs(beta) < cValoresJuego.MATE_MAXIMO)
              {
            val rbeta = Math.Min(beta + 200, cValoresJuego.INFINITO);
            ply rdepth = depth - 4 * cPly.ONE_PLY;
            cMovOrder mp2 = new cMovOrder(pos, ttMove, History, pos.GetTipoPiezaCapturada());
            cInfoJaque ci2 = new cInfoJaque(pos);
            while ((move = mp2.SiguienteMovimientoEnFalso()) != cMovType.MOV_NAN)
              if (pos.IsLegalMov(move, ci2.m_Clavadas))
              {
            ss[ssPos].currentMove = move;
            pos.DoMov(move, st, ci2, pos.IsJaque(move, ci2));
            value = -search(pos, ss, ssPos + 1, -rbeta, -rbeta + 1, rdepth, !cutNode, cTipoNodo.NO_PV, false);
            pos.DesMov(move);
            if (value >= rbeta)
              return value;
              }
              }

              if (depth >= (PvNode ? 5 * cPly.ONE_PLY : 8 * cPly.ONE_PLY)
              && 0 == ttMove
              && (PvNode || (ss[ssPos].staticEval + 256 >= beta)))
              {
            ply d = depth - 2 * cPly.ONE_PLY - (PvNode ? cPly.DEPTH_ZERO : depth / 4);
            ss[ssPos].skipNullMove = 1;
            search(pos, ss, ssPos, alpha, beta, d, true, PvNode ? cTipoNodo.PV : cTipoNodo.NO_PV, false);
            ss[ssPos].skipNullMove = 0;
            tte = cMotor.m_TablaHash.Buscar(posKey);
            ttMove = (tte != null) ? tte.GetMove() : cMovType.MOV_NAN;
              }
            moves_loop:
              sq prevMoveSq = cTypes.GetToCasilla(ss[ssPos - 1].currentMove);
              mov[] countermoves = { Countermoves[pos.GetPieza(prevMoveSq)][prevMoveSq].m_Key1,
                                    Countermoves[pos.GetPieza(prevMoveSq)][prevMoveSq].m_Key2 };
              sq prevOwnMoveSq = cTypes.GetToCasilla(ss[ssPos - 2].currentMove);
              mov[] followupmoves = {Followupmoves[pos.GetPieza(prevOwnMoveSq)][prevOwnMoveSq].m_Key1,
                                    Followupmoves[pos.GetPieza(prevOwnMoveSq)][prevOwnMoveSq].m_Key2 };
              cMovOrder mp = new cMovOrder(pos, ttMove, depth, History, countermoves, followupmoves, ss[ssPos]);
              cInfoJaque ci = new cInfoJaque(pos);
              value = bestValue;
              improving = ss[ssPos].staticEval >= ss[ssPos - 2].staticEval
             || ss[ssPos].staticEval == cValoresJuego.NAN
             || ss[ssPos - 2].staticEval == cValoresJuego.NAN;
              singularExtensionNode = !RootNode
                             && !SpNode
                             && depth >= 8 * cPly.ONE_PLY
                             && ttMove != cMovType.MOV_NAN
                             && 0 == excludedMove
                             && (tte.GetBound() & cBordes.BOUND_LOWER) != 0
                             && tte.GetDepth() >= depth - 3 * cPly.ONE_PLY;

              while ((move = (SpNode ? mp.SiguienteMovimiento() : mp.SiguienteMovimientoEnFalso())) != cMovType.MOV_NAN)
              {
            if (move == excludedMove)
              continue;

            if (RootNode && (Buscar(RootMoves, PVIdx, RootMoves.Count, move) == -1))
              continue;
            if (SpNode)
            {

              if (!pos.IsLegalMov(move, ci.m_Clavadas))
            continue;
              moveCount = ++splitPoint.moveCount;
              splitPoint.mutex.Liberar();
            }
            else
              ++moveCount;
            if (RootNode)
            {
              Signals.FIRST_MOVE = (moveCount == 1);

            }
            ext = cPly.DEPTH_ZERO;
            captureOrPromotion = pos.IsCapturaOrPromocion(move);
            givesCheck = cTypes.TipoMovimiento(move) == cMovType.NORMAL && 0 == ci.m_Candidatas
              ? (ci.m_Jaque[cTypes.TipoPieza(pos.GetPieza(cTypes.GetFromCasilla(move)))] & cBitBoard.m_nCasillas[cTypes.GetToCasilla(move)]) != 0
              : pos.IsJaque(move, ci);
            dangerous = givesCheck
             || cTypes.TipoMovimiento(move) != cMovType.NORMAL
             || pos.IsPeonAvanzado(move);

            if (givesCheck && pos.SEEReducido(move) >= cValoresJuego.CERO)
              ext = cPly.ONE_PLY;

            if (singularExtensionNode
            && move == ttMove
            && 0 == ext
            && pos.IsLegalMov(move, ci.m_Clavadas)
            && Math.Abs(ttValue) < cValoresJuego.GANA)
            {
              val rBeta = ttValue - depth;
              ss[ssPos].excludedMove = move;
              ss[ssPos].skipNullMove = 1;
              value = search(pos, ss, ssPos, rBeta - 1, rBeta, depth / 2, cutNode, cTipoNodo.NO_PV, false);
              ss[ssPos].skipNullMove = 0;
              ss[ssPos].excludedMove = cMovType.MOV_NAN;
              if (value < rBeta)
            ext = cPly.ONE_PLY;
            }

            newDepth = depth - cPly.ONE_PLY + ext;

            if (!PvNode
            && !captureOrPromotion
            && !inCheck
            && !dangerous
            && bestValue > cValoresJuego.MATE_MAXIMO_VS)
            {

              if (depth < 16 * cPly.ONE_PLY && moveCount >= FutilityMoveCounts[improving ? 1 : 0][depth])
              {
            if (SpNode)
              splitPoint.mutex.Bloquear();
            continue;
              }
              predictedDepth = newDepth - reduction(improving ? 1 : 0, depth, moveCount, PvNode ? 1 : 0);

              if (predictedDepth < 7 * cPly.ONE_PLY)
              {
            futilityValue = ss[ssPos].staticEval + futility_margin(predictedDepth)
                        + 128 + Gains[pos.GetPiezaMovida(move)][cTypes.GetToCasilla(move)];
            if (futilityValue <= alpha)
            {
              bestValue = Math.Max(bestValue, futilityValue);
              if (SpNode)
              {
                splitPoint.mutex.Bloquear();
                if (bestValue > splitPoint.bestValue)
                  splitPoint.bestValue = bestValue;
              }
              continue;
            }
              }

              if (predictedDepth < 4 * cPly.ONE_PLY && pos.SEEReducido(move) < cValoresJuego.CERO)
              {
            if (SpNode)
              splitPoint.mutex.Bloquear();
            continue;
              }
            }

            if (!RootNode && !SpNode && !pos.IsLegalMov(move, ci.m_Clavadas))
            {
              moveCount--;
              continue;
            }
            pvMove = PvNode && moveCount == 1;
            ss[ssPos].currentMove = move;
            if (!SpNode && !captureOrPromotion && quietCount < 64)
              quietsSearched[quietCount++] = move;

            pos.DoMov(move, st, ci, givesCheck);

            if (depth >= 3 * cPly.ONE_PLY
            && !pvMove
            && !captureOrPromotion
            && move != ttMove
            && move != ss[ssPos].killers0
            && move != ss[ssPos].killers1)
            {
              ss[ssPos].reduction = reduction(improving ? 1 : 0, depth, moveCount, PvNode ? 1 : 0);
              if (!PvNode && cutNode)
            ss[ssPos].reduction += cPly.ONE_PLY;
              else if (History[pos.GetPieza(cTypes.GetToCasilla(move))][cTypes.GetToCasilla(move)] < 0)
            ss[ssPos].reduction += cPly.ONE_PLY / 2;
              if (move == countermoves[0] || move == countermoves[1])
            ss[ssPos].reduction = Math.Max(cPly.DEPTH_ZERO, ss[ssPos].reduction - cPly.ONE_PLY);
              ply d = Math.Max(newDepth - ss[ssPos].reduction, cPly.ONE_PLY);
              if (SpNode)
            alpha = splitPoint.alpha;
              value = -search(pos, ss, ssPos + 1, -(alpha + 1), -alpha, d, true, cTipoNodo.NO_PV, false);

              if (value > alpha && ss[ssPos].reduction >= 4 * cPly.ONE_PLY)
              {
            ply d2 = Math.Max(newDepth - 2 * cPly.ONE_PLY, cPly.ONE_PLY);
            value = -search(pos, ss, ssPos + 1, -(alpha + 1), -alpha, d2, true, cTipoNodo.NO_PV, false);
              }
              doFullDepthSearch = (value > alpha && ss[ssPos].reduction != cPly.DEPTH_ZERO);
              ss[ssPos].reduction = cPly.DEPTH_ZERO;
            }
            else
              doFullDepthSearch = !pvMove;

            if (doFullDepthSearch)
            {
              if (SpNode)
            alpha = splitPoint.alpha;
              value = newDepth < cPly.ONE_PLY ?
                givesCheck ? -qsearch(pos, ss, ssPos + 1, -(alpha + 1), -alpha, cPly.DEPTH_ZERO, cTipoNodo.NO_PV, true)
                           : -qsearch(pos, ss, ssPos + 1, -(alpha + 1), -alpha, cPly.DEPTH_ZERO, cTipoNodo.NO_PV, false)
                           : -search(pos, ss, ssPos + 1, -(alpha + 1), -alpha, newDepth, !cutNode, cTipoNodo.NO_PV, false);
            }

            if (PvNode && (pvMove || (value > alpha && (RootNode || value < beta))))
              value = newDepth < cPly.ONE_PLY ?
                        givesCheck ? -qsearch(pos, ss, ssPos + 1, -beta, -alpha, cPly.DEPTH_ZERO, cTipoNodo.PV, true)
                           : -qsearch(pos, ss, ssPos + 1, -beta, -alpha, cPly.DEPTH_ZERO, cTipoNodo.PV, false)
                           : -search(pos, ss, ssPos + 1, -beta, -alpha, newDepth, false, cTipoNodo.PV, false);

            pos.DesMov(move);

            if (SpNode)
            {
              splitPoint.mutex.Bloquear();
              bestValue = splitPoint.bestValue;
              alpha = splitPoint.alpha;
            }

            if (Signals.STOP || thisThread.IsCorte())
              return cValoresJuego.CERO;
            if (RootNode)
            {
              int rmPos = Buscar(RootMoves, 0, RootMoves.Count, move);
              cRaizMov rm = RootMoves[rmPos];

              if (pvMove || value > alpha)
              {
            rm.m_nVal = value;
            rm.extract_pv_from_tt(pos);

            if (!pvMove)
              ++BestMoveChanges;
              }
              else

            rm.m_nVal = -cValoresJuego.INFINITO;
            }
            if (value > bestValue)
            {
              bestValue = SpNode ? splitPoint.bestValue = value : value;
              if (value > alpha)
              {
            bestMove = SpNode ? splitPoint.bestMove = move : move;
            if (PvNode && value < beta)
              alpha = SpNode ? splitPoint.alpha = value : value;
            else
            {
              if (SpNode)
                splitPoint.m_bCorte = true;
              break;
            }
              }
            }

            if (!SpNode
            && cMotor.m_Threads.Count >= 2
            && depth >= cMotor.m_Threads.minimumSplitDepth
            && (null == thisThread.m_SplitPointActivo
            || !thisThread.m_SplitPointActivo.allSlavesSearching)
            && thisThread.m_nSplitPointSize < cThreadBase.MAX_SPLITPOINTS_PER_THREAD)
            {
              thisThread.Split(pos, ss, ssPos, alpha, beta, ref bestValue, ref bestMove,
                                       depth, moveCount, mp, NT, cutNode);
              if (Signals.STOP || thisThread.IsCorte())
            return cValoresJuego.CERO;
              if (bestValue >= beta)
            break;
            }
              }
              if (SpNode)
            return bestValue;

              if (0 == moveCount)
            bestValue = excludedMove != 0 ? alpha
               : inCheck ? cTypes.MateEnVs(ss[ssPos].ply) : DrawValue[pos.ColorMueve()];

              else if (bestValue >= beta && !pos.IsCapturaOrPromocion(bestMove) && !inCheck)
            update_stats(pos, ss, ssPos, bestMove, depth, quietsSearched, quietCount - 1);
              cMotor.m_TablaHash.Save(posKey, value_to_tt(bestValue, ss[ssPos].ply),
               bestValue >= beta ? cBordes.BOUND_LOWER :
               PvNode && bestMove != 0 ? cBordes.BOUND_EXACT : cBordes.BOUND_UPPER,
               depth, bestMove, ss[ssPos].staticEval);
              return bestValue;
        }