Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Napoleon - benchmark.cpp

benchmark.cpp

Caricato da: Crybot
Scarica il programma completo

  1. #include "benchmark.h"
  2. #include <fstream>
  3. #include <boost/algorithm/string.hpp>
  4. #include <sstream>
  5. #include <vector>
  6. #include <string>
  7. #include <iostream>
  8. #include "fenstring.h"
  9. #include "movegenerator.h"
  10. #include "constants.h"
  11. #include "console.h"
  12. #include "search.h"
  13.  
  14. namespace Napoleon
  15. {
  16.     Benchmark::Benchmark()
  17.     {
  18.         board = Board();
  19.         board.LoadGame();
  20.     }
  21.  
  22.     void Benchmark::Start()
  23.     {
  24.         std::ifstream ff("perft.epd");
  25.         std::vector<std::string> strings;
  26.         std::vector<std::string> fields;
  27.         std::string buff;
  28.         int err = 0;
  29.         unsigned long long result;
  30.         unsigned long long excpected;
  31.         int depth;
  32.  
  33.         while (getline(ff, buff))
  34.         {
  35.             strings.push_back(buff);
  36.         }
  37.  
  38.         for (unsigned i=0; i<strings.size(); i++)
  39.         {
  40.             boost::split(fields, strings[i], boost::is_any_of(";"));
  41.             board.LoadGame(fields[0]);
  42.  
  43.             std::cout << Console::Yellow << "Position: " << i+1 << std::endl << fields[0] << std::endl;
  44.             for (unsigned l=1; l<fields.size(); l++)
  45.             {
  46.                 std::istringstream buffer(fields[l].substr(1));
  47.                 buffer >> depth;
  48.                 buffer >> excpected;
  49.  
  50.                 std::cout << Console::Reset <<  "Perft " << depth << ": " << std::endl;
  51.  
  52.                 result = Perft(depth, board);
  53.  
  54.                 if(result != excpected)
  55.                 {
  56.                     std::cout << Console::Red << "Nodes: " << result << std::endl;
  57.                     std::cout << Console::Red << "Expected: " << excpected << std::endl;
  58.                     err++;
  59.                 }
  60.                 else
  61.                 {
  62.                     std::cout << Console::Green << "Nodes: " << result << std::endl;
  63.                     std::cout << Console::Green << "Expected: " << excpected << std::endl;
  64.                 }
  65.             }
  66.         }
  67.  
  68.         if (err == 0)
  69.             std::cout << Console::Green << "Test Succesfully Passed!" << std::endl;
  70.         else
  71.             std::cout << Console::Red << "Test Not Passed!" << std::endl;
  72.         std::cout << Console::Reset;
  73.     }
  74.  
  75.     void Benchmark::CutoffTest()
  76.     {
  77.         std::ifstream ff("perft.epd");
  78.         std::vector<std::string> strings;
  79.         std::vector<std::string> fields;
  80.         std::string buff;
  81.         int err = 0;
  82.         unsigned long long result;
  83.         unsigned long long excpected;
  84.         int depth;
  85.  
  86.         while (getline(ff, buff))
  87.         {
  88.             strings.push_back(buff);
  89.         }
  90.  
  91.         for (unsigned i=0; i<50; i++)
  92.         {
  93.             boost::split(fields, strings[i], boost::is_any_of(";"));
  94.             board.LoadGame(fields[0]);
  95.  
  96.             Search::StartThinking(board);
  97.             std::cout << i << std::endl;
  98.  
  99.         }
  100.  
  101.         std::cout << "Cutoff on first move: " << board.FirstMoveCutoff << std::endl;
  102.         std::cout << "Total Cutoffs: " << board.TotalCutoffs << std::endl;
  103.         std::cout <<  Console::Green << "First Move Cutoff percentage: " << ((float)board.FirstMoveCutoff / (float)board.TotalCutoffs) * 100 << "%" << std::endl;
  104.         std::cout <<  Console::Reset << std::endl;
  105.  
  106.  
  107.  
  108.     }
  109.  
  110.  
  111.     unsigned long long Benchmark::Perft(int depth, Board& board)
  112.     {
  113.         int pos = 0;
  114.         int count;
  115.         Move move;
  116.         Move moves[Constants::MaxMoves + 2];
  117.  
  118.         unsigned long long nodes = 0;
  119.  
  120.         if ((count = board.Table.Probe(board.zobrist, depth, -32767, &move, 32767)) != TranspositionTable::Unknown)
  121.             return count;
  122.  
  123.         MoveGenerator::GetLegalMoves(moves, pos, board);
  124.  
  125.         if (depth == 1)
  126.         {
  127.             board.Table.Save(board.zobrist, depth, pos, Constants::NullMove, Exact);
  128.             return pos;
  129.         }
  130.  
  131.         if (depth == 0)
  132.             return 1;
  133.  
  134.         for (int i = 0; i < pos; i++)
  135.         {
  136.             board.MakeMove(moves[i]);
  137.             nodes += Perft(depth - 1, board);
  138.             board.UndoMove(moves[i]);
  139.         }
  140.  
  141.         board.Table.Save(board.zobrist, depth, nodes, Constants::NullMove, Exact);
  142.         return nodes;
  143.     }
  144.  
  145. }