00001 //---------------------------------------------------------------------------- 00002 /** @file WolveEngine.cpp 00003 */ 00004 //---------------------------------------------------------------------------- 00005 00006 #include "SgSystem.h" 00007 00008 #include "Misc.hpp" 00009 #include "WolveEngine.hpp" 00010 #include "WolvePlayer.hpp" 00011 #include "PlayAndSolve.hpp" 00012 #include "SwapCheck.hpp" 00013 00014 using namespace benzene; 00015 00016 //---------------------------------------------------------------------------- 00017 00018 namespace { 00019 00020 //---------------------------------------------------------------------------- 00021 00022 template<typename TYPE> 00023 void ParseDashSeparatedString(const std::string& str, std::vector<TYPE>& out) 00024 { 00025 // remove the '-' separators 00026 std::string widths(str); 00027 for (std::size_t i=0; i<widths.size(); ++i) 00028 if (widths[i] == '-') widths[i] = ' '; 00029 00030 // parse the ' ' separated widths 00031 std::istringstream is; 00032 is.str(widths); 00033 while (is) 00034 { 00035 TYPE j; 00036 if (is >> j) 00037 out.push_back(j); 00038 } 00039 } 00040 00041 //---------------------------------------------------------------------------- 00042 00043 } // namespace 00044 00045 //---------------------------------------------------------------------------- 00046 00047 WolveEngine::WolveEngine(int boardsize, WolvePlayer& player) 00048 : BenzeneHtpEngine(boardsize), 00049 m_player(player), 00050 m_book(0), 00051 m_bookCheck(m_book), 00052 m_bookCommands(m_game, m_pe, m_book, m_bookCheck), 00053 m_cacheBook() 00054 { 00055 m_bookCommands.Register(*this); 00056 RegisterCmd("param_wolve", &WolveEngine::WolveParam); 00057 } 00058 00059 WolveEngine::~WolveEngine() 00060 { 00061 } 00062 00063 //---------------------------------------------------------------------------- 00064 00065 void WolveEngine::RegisterCmd(const std::string& name, 00066 GtpCallback<WolveEngine>::Method method) 00067 { 00068 Register(name, new GtpCallback<WolveEngine>(this, method)); 00069 } 00070 00071 //---------------------------------------------------------------------------- 00072 00073 double WolveEngine::TimeForMove(HexColor color) 00074 { 00075 return m_game.TimeRemaining(color); 00076 } 00077 00078 /** Generates a move. */ 00079 HexPoint WolveEngine::GenMove(HexColor color, bool useGameClock) 00080 { 00081 SG_UNUSED(useGameClock); 00082 if (SwapCheck::PlaySwap(m_game, color)) 00083 return SWAP_PIECES; 00084 HexState state(m_game.Board(), color); 00085 HexPoint bookMove = m_bookCheck.BestMove(state); 00086 if (bookMove != INVALID_POINT) 00087 return bookMove; 00088 if (m_cacheBook.Exists(state)) 00089 return m_cacheBook[state]; 00090 00091 double maxTime = TimeForMove(color); 00092 if (m_useParallelSolver) 00093 { 00094 PlayAndSolve ps(*m_pe.brd, *m_se.brd, m_player, m_dfpnSolver, 00095 m_dfpnPositions, m_game); 00096 return ps.GenMove(state, maxTime); 00097 } 00098 double score; 00099 return m_player.GenMove(state, m_game, m_pe.SyncBoard(m_game.Board()), 00100 maxTime, score); 00101 } 00102 00103 void WolveEngine::WolveParam(HtpCommand& cmd) 00104 { 00105 WolveSearch& search = m_player.Search(); 00106 00107 if (cmd.NuArg() == 0) 00108 { 00109 cmd << '\n' 00110 << "[bool] backup_ice_info " 00111 << search.BackupIceInfo() << '\n' 00112 << "[bool] use_guifx " 00113 << search.GuiFx() << '\n' 00114 << "[string] panic_time " 00115 << m_player.PanicTime() << '\n' 00116 << "[string] ply_width " 00117 << MiscUtil::PrintVector(m_player.PlyWidth()) << '\n' 00118 << "[string] search_depths " 00119 << MiscUtil::PrintVector(m_player.SearchDepths()) << '\n' 00120 << "[bool] search_singleton " 00121 << m_player.SearchSingleton() << '\n' 00122 << "[bool] use_parallel_solver " 00123 << m_useParallelSolver << '\n'; 00124 } 00125 else if (cmd.NuArg() == 2) 00126 { 00127 std::string name = cmd.Arg(0); 00128 if (name == "backup_ice_info") 00129 search.SetBackupIceInfo(cmd.BoolArg(1)); 00130 else if (name == "panic_time") 00131 m_player.SetPanicTime(cmd.FloatArg(1)); 00132 else if (name == "ply_width") 00133 { 00134 std::vector<int> plywidth; 00135 ParseDashSeparatedString(cmd.Arg(1), plywidth); 00136 m_player.SetPlyWidth(plywidth); 00137 } 00138 else if (name == "search_depths") 00139 { 00140 std::vector<int> depths; 00141 ParseDashSeparatedString(cmd.Arg(1), depths); 00142 m_player.SetSearchDepths(depths); 00143 } 00144 else if (name == "use_guifx") 00145 search.SetGuiFx(cmd.BoolArg(1)); 00146 else if (name == "search_singleton") 00147 m_player.SetSearchSingleton(cmd.BoolArg(1)); 00148 else if (name == "use_parallel_solver") 00149 m_useParallelSolver = cmd.BoolArg(1); 00150 else 00151 throw HtpFailure() << "Unknown parameter: " << name; 00152 } 00153 else 00154 throw HtpFailure("Expected 0 or 2 arguments"); 00155 } 00156 00157 //---------------------------------------------------------------------------- 00158 // Pondering 00159 00160 #if GTPENGINE_PONDER 00161 00162 void WolveEngine::InitPonder() 00163 { 00164 } 00165 00166 void WolveEngine::Ponder() 00167 { 00168 } 00169 00170 void WolveEngine::StopPonder() 00171 { 00172 } 00173 00174 #endif // GTPENGINE_PONDER 00175 00176 //----------------------------------------------------------------------------