Exemplo n.º 1
0
        public static void Start(ChessBoard cb)
        {
            NegamaxUtil.IsRunning = true;
            cb.MoveCount          = 0;

            if (UciOptions.ThreadCount == 1)
            {
                new SearchThread(0).Call();
            }
            else
            {
                var tasks = new List <Task>();
                for (var i = 0; i < UciOptions.ThreadCount; i++)
                {
                    if (i > 0)
                    {
                        ChessBoardUtil.Copy(cb, ChessBoardInstances.Get(i));
                    }

                    var i1 = i;
                    var t  = new Task(() => new SearchThread(i1).Call());
                    tasks.Add(t);
                }

                try
                {
                    tasks.ForEach(task => task.Start());
                    Task.WaitAll(tasks.ToArray());
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }
Exemplo n.º 2
0
        public static void Main()
        {
            var cb = ChessBoardInstances.Get(0);

            MainEngine.MaxDepth             = MaxPly;
            UciOut.NoOutput                 = true;
            EngineConstants.Power2TtEntries = 2;
            TtUtil.Init(false);
            long totalNodesSearched = 0;

            var epdStrings = BestMoveTest.GetEpdStrings("Resources/WAC-201.epd");

            for (var index = 0; index < NumberOfPositions; index++)
            {
                Console.WriteLine(index);
                var epdString = epdStrings[index + 20];
                Statistics.Reset();
                var epd = new Epd(epdString);
                ChessBoardUtil.SetFen(epd.GetFen(), cb);
                SearchUtil.Start(cb);
                totalNodesSearched += ChessBoardUtil.CalculateTotalMoveCount();
            }

            Console.WriteLine("Total   " + totalNodesSearched);
            Console.WriteLine("Average " + totalNodesSearched / NumberOfPositions);
        }
Exemplo n.º 3
0
        public static void Main()
        {
            var cb = ChessBoardInstances.Get(0);

            ChessBoardUtil.SetFen("1r1q1rk1/2p1npb1/b3p1p1/p5N1/1ppPB2R/P1N1P1P1/1P2QPP1/2K4R w - - 0 20 ", cb);
            EvalUtil.CalculateScore(cb, ThreadData.GetInstance(0));
        }
        public static void Main()
        {
            var fens = Tuner.LoadFens("d:\\backup\\chess\\epds\\quiet-labeled.epd", true, false);

            Console.WriteLine(fens.Count + " fens found");

            var cb         = ChessBoardInstances.Get(0);
            var threadData = ThreadData.GetInstance(0);

            foreach (var(key, value) in fens)
            {
                ChessBoardUtil.SetFen(key, cb);
                var error = Math.Pow(value - ErrorCalculator.CalculateSigmoid(EvalUtil.CalculateScore(cb, threadData)),
                                     2);

                for (var i = 0; i < LargestError.Length; i++)
                {
                    if (!(error > LargestError[i]))
                    {
                        continue;
                    }
                    LargestError[i]    = error;
                    LargestErrorFen[i] = key;
                    break;
                }
            }

            for (var i = 0; i < LargestError.Length; i++)
            {
                Console.WriteLine($"{LargestErrorFen[i],60} -> {LargestError[i]}");
            }
        }
Exemplo n.º 5
0
            public void Run()
            {
                var threadData = ThreadData.GetInstance(_threadNumber);
                var cb         = ChessBoardInstances.Get(_threadNumber);

                ChessBoardUtil.SetFen(ChessConstants.FenStart, cb);
                Perft(cb, threadData, Depth);
            }
Exemplo n.º 6
0
        public static void Main()
        {
            var cb = ChessBoardInstances.Get(0);

            ChessBoardUtil.SetFen(FenStandardMiddlegame, cb);
            TimeUtil.SetSimpleTimeWindow(5000);
            TtUtil.Init(false);
            SearchUtil.Start(cb);
            Statistics.Print();
        }
Exemplo n.º 7
0
        public static void Main()
        {
            var cb = ChessBoardInstances.Get(0);

            // read all fens, including score
            var fens = Tuner.LoadFens("d:\\backup\\chess\\epds\\violent.epd", false, true);

            Console.WriteLine("Fens found : " + fens.Count);

            // NegamaxUtil.isRunning = true;
            EngineConstants.Power2TtEntries = 1;
            TtUtil.Init(false);

            double totalPositions = 0;
            double sameScore      = 0;
            long   totalError     = 0;
            var    watch          = new Stopwatch();

            watch.Start();
            foreach (var entry in fens)
            {
                ChessBoardUtil.SetFen(entry.Key, cb);
                if (cb.CheckingPieces == 0)
                {
                    continue;
                }

                totalPositions++;
                var searchScore = NegamaxUtil.CalculateBestMove(cb, ThreadData, 0, 1, Util.ShortMin, Util.ShortMax, 0);
                TtUtil.ClearValues();
                var qScore = QuiescenceUtil.CalculateBestMove(cb, ThreadData, Util.ShortMin, Util.ShortMax);

                if (searchScore == qScore)
                {
                    sameScore++;
                }
                else
                {
                    var error = searchScore - qScore;
                    // if (error > 500) {
                    // System.out.println(searchScore + " " + qScore);
                    // QuiescenceUtil.calculateBestMove(cb, threadData, Util.SHORT_MIN, Util.SHORT_MAX);
                    // }

                    totalError += error;
                }
            }

            var averageError = (int)(totalError / (totalPositions - sameScore));

            Console.WriteLine($"{sameScore / totalPositions:f4} {averageError}");
            Console.WriteLine("msec: " + watch.ElapsedMilliseconds);
        }
Exemplo n.º 8
0
        public static void Main()
        {
            var cb = ChessBoardInstances.Get(0);

            // read all fens, including score
            var fens = Tuner.LoadFens("d:\\backup\\chess\\epds\\violent.epd", false, true);

            Console.WriteLine("Fens found : " + fens.Count);

            double sameScore    = 0;
            double totalAttacks = 0;
            var    start        = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

            foreach (var entry in fens)
            {
                ChessBoardUtil.SetFen(entry.Key, cb);
                _threadData.StartPly();
                MoveGenerator.GenerateAttacks(_threadData, cb);
                while (_threadData.HasNext())
                {
                    var move = _threadData.Next();
                    if (!cb.IsLegal(move))
                    {
                        continue;
                    }

                    totalAttacks++;
                    var seeScore      = SeeUtil.GetSeeCaptureScore(cb, move);
                    var materialScore = EvalUtil.CalculateMaterialScore(cb);
                    var qScore        = ChessConstants.ColorFactor[cb.ColorToMoveInverse] * materialScore -
                                        CalculateQScore(cb, move, true);
                    if (seeScore == qScore)
                    {
                        sameScore++;
                    }

                    // else {
                    // seeScore = SEEUtil.getSeeCaptureScore(cb, move);
                    // qScore = ChessConstants.COLOR_FACTOR[cb.colorToMoveInverse] * materialScore - calculateQScore(cb,
                    // move, true);
                    // }
                }

                _threadData.EndPly();
            }

            Console.WriteLine($"{sameScore:f0} {totalAttacks:f0} = {sameScore / totalAttacks:f4}");
            Console.WriteLine("msec: " + (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - start));
        }
Exemplo n.º 9
0
            public void Run()
            {
                Console.WriteLine("Start " + _name);

                var cb         = ChessBoardInstances.Get(_threadNumber);
                var threadData = ThreadData.GetInstance(_threadNumber);

                foreach (var position in _positions)
                {
                    ChessBoardUtil.SetFen(position.Fen, cb);
                    Assert.IsTrue(position.MoveCount == perft(cb, threadData, position.Depth));
                }

                Console.WriteLine("Done " + _name);
            }
Exemplo n.º 10
0
        public double Call()
        {
            double totalError = 0;

            foreach (var(key, value) in _fens)
            {
                ChessBoardUtil.SetFen(key, _cb);
                totalError += Math.Pow(
                    value - CalculateSigmoid(ChessConstants.ColorFactor[_cb.ColorToMove] *
                                             EvalUtil.CalculateScore(_cb, _threadData)),
                    2);
            }

            totalError /= _fens.Count;
            return(totalError);
        }
Exemplo n.º 11
0
        private static void DoTest(IReadOnlyCollection <string> epdStrings)
        {
            var correctCounter = 0;

            foreach (var epdString in epdStrings)
            {
                var epd = new Epd(epdString);
                var cb  = ChessBoardInstances.Get(0);
                ChessBoardUtil.SetFen(epd.GetFen(), cb);

                TimeUtil.Reset();
                TimeUtil.SetSimpleTimeWindow(5000);
                SearchUtil.Start(cb);

                var bestMove = new MoveWrapper(ThreadData.GetBestMove());
                if (epd.IsBestMove)
                {
                    if (epd.MoveEquals(bestMove))
                    {
                        Console.WriteLine(epd.GetId() + " BM OK");
                        correctCounter++;
                        _positionTestOk++;
                    }
                    else
                    {
                        Console.WriteLine(epd.GetId() + " BM NOK " + bestMove + " - " + epd);
                        _positionTestNok++;
                    }
                }
                else
                {
                    if (epd.MoveEquals(bestMove))
                    {
                        Console.WriteLine(epd.GetId() + " AM NOK " + epd);
                        _positionTestNok++;
                    }
                    else
                    {
                        Console.WriteLine(epd.GetId() + " AM OK");
                        correctCounter++;
                        _positionTestOk++;
                    }
                }
            }

            Console.WriteLine(correctCounter + "/" + epdStrings.Count);
        }
Exemplo n.º 12
0
        public static void SendInfo()
        {
            if (NoOutput)
            {
                return;
            }

            if (Stopwatch.IsRunning && Stopwatch.ElapsedMilliseconds < 2000)
            {
                return;
            }

            var totalMoveCount = ChessBoardUtil.CalculateTotalMoveCount();

            Console.WriteLine("info nodes " + totalMoveCount + " nps " + CalculateNps(totalMoveCount) + " hashfull " +
                              TtUtil.GetUsagePercentage());
        }
Exemplo n.º 13
0
        public static void Main()
        {
            var fens = Tuner.LoadFens("d:\\backup\\chess\\epds\\quiet-labeled.epd", true, false);

            Console.WriteLine(fens.Count + " fens found");

            var cb = ChessBoardInstances.Get(0);

            foreach (var entry in fens)
            {
                ChessBoardUtil.SetFen(entry.Key, cb);
                PieceCounts[BitOperations.PopCount((ulong)cb.AllPieces)]++;
            }

            for (var i = 0; i < 33; i++)
            {
                Console.WriteLine(i + " " + PieceCounts[i]);
            }
        }
Exemplo n.º 14
0
        public static void SendPlyInfo(ThreadData threadData)
        {
            if (NoOutput)
            {
                return;
            }

            Stopwatch.Restart();

            var totalMoveCount = ChessBoardUtil.CalculateTotalMoveCount();

            // info depth 1 seldepth 2 score cp 50 pv d2d4 d7d5 e2e3 hashfull 0 nps 1000 nodes 22
            // info depth 4 seldepth 10 score cp 40 upperbound pv d2d4 d7d5 e2e3 hashfull 0 nps 30000 nodes 1422
            Console.WriteLine("info depth " + threadData.Depth + " time " + TimeUtil.GetPassedTimeMs() + " score cp " +
                              threadData.BestScore + threadData.ScoreType.ToFriendlyName()
                              + "nps " + CalculateNps(totalMoveCount) + " nodes " + totalMoveCount + " hashfull " +
                              TtUtil.GetUsagePercentage() + " pv "
                              + PvUtil.AsString(threadData.Pv));
        }
Exemplo n.º 15
0
        public static void Main()
        {
            // read all fens, including score
            var fens = Tuner.LoadFens("d:\\backup\\chess\\epds\\quiet-labeled.epd", true, true);

            Console.WriteLine("Fens found : " + fens.Count);

            var tested = 0;
            var ok     = 0;
            var nok    = 0;

            foreach (var(fen, score) in fens)
            {
                var cb = ChessBoardInstances.Get(0);
                ChessBoardUtil.SetFen(fen, cb);
                if (BitOperations.PopCount((ulong)cb.AllPieces) > 3)
                {
                    continue;
                }

                if (cb.Pieces[ChessConstants.White][ChessConstants.Pawn] == 0 &&
                    cb.Pieces[ChessConstants.Black][ChessConstants.Pawn] == 0)
                {
                    continue;
                }

                tested++;
                if (KpkBitbase.IsDraw(cb) == (score == 0.5))
                {
                    ok++;
                }
                else
                {
                    nok++;
                }
            }

            Console.WriteLine();
            Console.WriteLine("Tested " + tested);
            Console.WriteLine("OK " + ok);
            Console.WriteLine("NOK " + nok);
        }
Exemplo n.º 16
0
        private static void Position(string[] tokens)
        {
            if (tokens[1].Equals("startpos"))
            {
                ChessBoardUtil.SetStartFen(_cb);
                DoMoves(tokens.Length == 2
                        // position startpos
                    ? new string[] { }
                        // position startpos moves f2f3 g1a3 ...
                    : Arrays.CopyOfRange(tokens, 3, tokens.Length));
            }
            else
            {
                // position fen 4k3/8/8/8/8/3K4 b kq - 0 1 moves f2f3 g1a3 ...
                var fen = tokens[2] + " " + tokens[3] + " " + tokens[4] + " " + tokens[5];
                if (tokens.Length > 6)
                {
                    fen += " " + tokens[6];
                    fen += " " + tokens[7];
                }

                ChessBoardUtil.SetFen(fen, _cb);

                if (tokens.Length == 6 || tokens.Length == 7 || tokens.Length == 8)
                {
                    // position fen 4k3/8/8/8/8/3K4 b kq - 0 1
                    DoMoves(new string[] { });
                }
                else
                {
                    // position fen 4k3/8/8/8/8/3K4 b kq - 0 1 moves f2f3 g1a3 ...
                    DoMoves(Arrays.CopyOfRange(tokens, 9, tokens.Length));
                }
            }

            ErrorLogger.StartFen   = _cb.ToString();
            TtUtil.HalfMoveCounter = _cb.MoveCounter;
        }
Exemplo n.º 17
0
        public static Dictionary <string, double> LoadFens(string fileName, bool containsResult, bool includingCheck)
        {
            Console.WriteLine("Loading " + fileName);

            var fens       = new Dictionary <string, double>();
            var checkCount = 0;
            var checkmate  = 0;
            var stalemate  = 0;

            try
            {
                foreach (var line in File.ReadLines(fileName))
                {
                    double score = 0;
                    string fenString;
                    if (containsResult)
                    {
                        var scoreString = GetScoreStringFromLine(line);
                        fenString = GetFenStringFromLine(line);
                        if (scoreString.Equals("\"1/2-1/2\";"))
                        {
                            score = 0.5;
                        }
                        else if (scoreString.Equals("\"1-0\";"))
                        {
                            score = 1;
                        }
                        else if (scoreString.Equals("\"0-1\";"))
                        {
                            score = 0;
                        }
                        else
                        {
                            throw new Exception("Unknown result: " + scoreString);
                        }
                    }
                    else
                    {
                        fenString = line;
                    }

                    ChessBoardUtil.SetFen(fenString, Cb);

                    if (Cb.CheckingPieces == 0)
                    {
                        ThreadData.StartPly();
                        MoveGenerator.GenerateAttacks(ThreadData, Cb);
                        MoveGenerator.GenerateMoves(ThreadData, Cb);
                        if (ThreadData.HasNext())
                        {
                            fens.Add(fenString, score);
                        }
                        else
                        {
                            stalemate++;
                        }

                        ThreadData.EndPly();
                    }
                    else
                    {
                        checkCount++;
                        if (!includingCheck)
                        {
                            continue;
                        }
                        ThreadData.StartPly();
                        MoveGenerator.GenerateAttacks(ThreadData, Cb);
                        MoveGenerator.GenerateMoves(ThreadData, Cb);
                        if (ThreadData.HasNext())
                        {
                            fens.Add(fenString, score);
                        }
                        else
                        {
                            checkmate++;
                        }

                        ThreadData.EndPly();
                    }

                    //line = br.readLine();
                }
            }
            catch (IOException e)
            {
                throw new Exception("", e);
            }

            Console.WriteLine("In check : " + checkCount);
            Console.WriteLine("Checkmate : " + checkmate);
            Console.WriteLine("Stalemate : " + stalemate);
            return(fens);
        }
Exemplo n.º 18
0
 private static void TestPerft1()
 {
     Console.WriteLine(1);
     ChessBoardUtil.SetStartFen(Cb);
     Assert.IsTrue(20 == Qperft(Cb, 1));
 }
Exemplo n.º 19
0
 public static void TestPerft8()
 {
     Console.WriteLine(8);
     ChessBoardUtil.SetStartFen(Cb);
     Assert.IsTrue(84998978956L == Qperft(Cb, 8));
 }
Exemplo n.º 20
0
 private static void TestPerft7()
 {
     Console.WriteLine(7);
     ChessBoardUtil.SetStartFen(Cb);
     Assert.IsTrue(3195901860L == Qperft(Cb, 7));
 }
Exemplo n.º 21
0
 private static void TestPerft6()
 {
     Console.WriteLine(6);
     ChessBoardUtil.SetStartFen(Cb);
     Assert.IsTrue(119060324 == Qperft(Cb, 6));
 }
Exemplo n.º 22
0
 private static void TestPerft5()
 {
     Console.WriteLine(5);
     ChessBoardUtil.SetStartFen(Cb);
     Assert.IsTrue(4865609 == Qperft(Cb, 5));
 }
Exemplo n.º 23
0
 private static void TestPerft4()
 {
     Console.WriteLine(4);
     ChessBoardUtil.SetStartFen(Cb);
     Assert.IsTrue(197281 == Qperft(Cb, 4));
 }
Exemplo n.º 24
0
 private static void TestPerft3()
 {
     Console.WriteLine(3);
     ChessBoardUtil.SetStartFen(Cb);
     Assert.IsTrue(8902 == Qperft(Cb, 3));
 }
Exemplo n.º 25
0
        public static void Main()
        {
            var fens = Tuner.LoadFens("d:\\backup\\chess\\epds\\quiet-labeled.epd", true, false);

            Console.WriteLine(fens.Count + " fens found");

            var kpk   = new ErrorCount("KPK ");
            var kbnk  = new ErrorCount("KBNK");
            var kbpk  = new ErrorCount("KBPK");
            var krkp  = new ErrorCount("KRKP");
            var kqkp  = new ErrorCount("KQKP");
            var krkb  = new ErrorCount("KRKB");
            var krkn  = new ErrorCount("KRKN");
            var kbpkp = new ErrorCount("KBPKP");
            var krbkb = new ErrorCount("KRBKB");
            var krbkr = new ErrorCount("KRBKR");

            var cb         = ChessBoardInstances.Get(0);
            var threadData = ThreadData.GetInstance(0);

            foreach (var(key, value) in fens)
            {
                ChessBoardUtil.SetFen(key, cb);

                var error = Math.Pow(
                    value - ErrorCalculator.CalculateSigmoid(ChessConstants.ColorFactor[cb.ColorToMove] *
                                                             EvalUtil.CalculateScore(cb, threadData)),
                    2);
                if (MaterialUtil.IsKbnk(cb.MaterialKey))
                {
                    kbnk.AddError(error);
                }
                else if (MaterialUtil.IsKqkp(cb.MaterialKey))
                {
                    kqkp.AddError(error);
                }
                else if (MaterialUtil.IsKrkp(cb.MaterialKey))
                {
                    krkp.AddError(error);
                }
                else if (MaterialUtil.IsKrkb(cb.MaterialKey))
                {
                    krkb.AddError(error);
                }
                else if (MaterialUtil.IsKrkn(cb.MaterialKey))
                {
                    krkn.AddError(error);
                }
                else if (MaterialUtil.IsKpk(cb.MaterialKey))
                {
                    krkn.AddError(error);
                }
                else if (MaterialUtil.IsKbpk(cb.MaterialKey))
                {
                    kbpk.AddError(error);
                }
                else if (MaterialUtil.IsKbpkp(cb.MaterialKey))
                {
                    kbpkp.AddError(error);
                }
                else if (MaterialUtil.IsKrbkb(cb.MaterialKey))
                {
                    krbkb.AddError(error);
                }
                else if (MaterialUtil.IsKrbkr(cb.MaterialKey))
                {
                    krbkr.AddError(error);
                }
            }

            kpk.Print();
            kbnk.Print();
            krkp.Print();
            kqkp.Print();
            krkb.Print();
            krkn.Print();
            krbkb.Print();
            krbkr.Print();
            kbpk.Print();
            kbpkp.Print();
        }