Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

GoUctBookBuilderCommands.h

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file GoUctBookBuilderCommands.h
00003 */
00004 //----------------------------------------------------------------------------
00005 
00006 #ifndef GOUCT_BOOKBUILDERCOMMANDS_H
00007 #define GOUCT_BOOKBUILDERCOMMANDS_H
00008 
00009 #include <string>
00010 #include <typeinfo>
00011 #include "GtpEngine.h"
00012 #include "GoUctPlayoutPolicy.h"
00013 #include "GoUctGlobalSearch.h"
00014 #include "GoUctPlayer.h"
00015 #include "GoUctBookBuilder.h"
00016 
00017 class GoBoard;
00018 class GoPlayer;
00019 class GoUctBoard;
00020 class GoUctSearch;
00021 
00022 //----------------------------------------------------------------------------
00023 
00024 template<class PLAYER>
00025 class GoUctBookBuilderCommands
00026 {
00027 public:
00028     /** Constructor.
00029         @param bd The game board.
00030         @param player Reference to pointer to current player, this player can
00031         be null or a different player, but those commands of this class that
00032         need a GoUctPlayer will fail, if the current player is not a
00033         PLAYER.
00034     */
00035     GoUctBookBuilderCommands(GoBoard& bd, GoPlayer*& player,
00036                              boost::scoped_ptr<GoAutoBook>& book);
00037 
00038     void AddGoGuiAnalyzeCommands(GtpCommand& cmd);
00039 
00040     /** @page gouctbookbuildergtpcommands GoUctBookBuilderCommands Commands
00041         - @link CmdOpen() @c autobook_open @endlink
00042         - @link CmdClose() @c autobook_close @endlink
00043         - @link CmdExpand() @c autobook_expand @endlink
00044         - @link CmdRefresh() @c autobook_refresh @endlink
00045         - @link CmdParam()  @c autobook_param @endlink
00046         - @link CmdScores() @c autobook_scores @endlink
00047         - @link CmdCounts() @c autobook_counts @endlink
00048         - @link CmdPriority() @c autobook_priority @endlink
00049     */
00050     /** @name Command Callbacks */
00051     // @{
00052     void CmdOpen(GtpCommand& cmd);
00053     void CmdClose(GtpCommand& cmd);
00054     void CmdExpand(GtpCommand& cmd);
00055     void CmdRefresh(GtpCommand& cmd);
00056     void CmdParam(GtpCommand& cmd);
00057     void CmdScores(GtpCommand& cmd);
00058     void CmdCounts(GtpCommand& cmd);
00059     void CmdPriority(GtpCommand& cmd);
00060     // @} // @name
00061 
00062     void Register(GtpEngine& engine);
00063 
00064 private:
00065     GoBoard& m_bd;
00066 
00067     GoPlayer*& m_player;
00068 
00069     boost::scoped_ptr<GoAutoBook>& m_book;
00070 
00071     GoUctBookBuilder<PLAYER> m_bookBuilder;
00072 
00073     PLAYER& Player();
00074 
00075     void Register(GtpEngine& e, const std::string& command,
00076                 typename GtpCallback<GoUctBookBuilderCommands>::Method method);
00077 
00078     void ShowInfluence(GtpCommand& cmd, GoAutoBookState& state);
00079 };
00080 
00081 //----------------------------------------------------------------------------
00082 
00083 template<class PLAYER>
00084 GoUctBookBuilderCommands<PLAYER>
00085 ::GoUctBookBuilderCommands(GoBoard& bd, GoPlayer*& player, 
00086                            boost::scoped_ptr<GoAutoBook>& book)
00087     : m_bd(bd),
00088       m_player(player),
00089       m_book(book),
00090       m_bookBuilder(bd)
00091 {
00092 }
00093 
00094 template<class PLAYER>
00095 void GoUctBookBuilderCommands<PLAYER>::AddGoGuiAnalyzeCommands(GtpCommand& cmd)
00096 {
00097     cmd << 
00098         "none/AutoBook Close/autobook_close\n"
00099         "none/AutoBook Expand/autobook_expand\n"
00100         "none/AutoBook Open/autobook_open %r\n"
00101         "param/AutoBook Param/autobook_param\n"
00102         "none/AutoBook Refresh/autobook_refresh\n"
00103         "gfx/AutoBook Scores/autobook_scores\n"
00104         "gfx/AutoBook Counts/autobook_counts\n"
00105         "gfx/AutoBook Priority/autobook_priority\n";
00106 }
00107 
00108 template<class PLAYER>
00109 void GoUctBookBuilderCommands<PLAYER>::Register(GtpEngine& e)
00110 {
00111     Register(e, "autobook_close", &GoUctBookBuilderCommands<PLAYER>::CmdClose);
00112     Register(e, "autobook_counts", 
00113              &GoUctBookBuilderCommands<PLAYER>::CmdCounts);
00114     Register(e, "autobook_expand", 
00115              &GoUctBookBuilderCommands<PLAYER>::CmdExpand);
00116     Register(e, "autobook_open", &GoUctBookBuilderCommands<PLAYER>::CmdOpen);
00117     Register(e, "autobook_param", &GoUctBookBuilderCommands<PLAYER>::CmdParam);
00118     Register(e, "autobook_priority", 
00119              &GoUctBookBuilderCommands<PLAYER>::CmdPriority);
00120     Register(e, "autobook_refresh", 
00121              &GoUctBookBuilderCommands<PLAYER>::CmdRefresh);
00122     Register(e, "autobook_scores", 
00123              &GoUctBookBuilderCommands<PLAYER>::CmdScores);
00124 }
00125 
00126 template<class PLAYER>
00127 void GoUctBookBuilderCommands<PLAYER>::
00128 Register(GtpEngine& engine, const std::string& command,
00129          typename GtpCallback<GoUctBookBuilderCommands>::Method method)
00130 {
00131     engine.Register(command, 
00132                     new GtpCallback<GoUctBookBuilderCommands>(this, method));
00133 }
00134 
00135 template<class PLAYER>
00136 PLAYER& GoUctBookBuilderCommands<PLAYER>::Player()
00137 {
00138     if (m_player == 0)
00139         throw GtpFailure("player not GoUctPlayer");
00140     try
00141     {
00142         return dynamic_cast<PLAYER&>(*m_player);
00143     }
00144     catch (const std::bad_cast&)
00145     {
00146         throw GtpFailure("player not of right type!");
00147     }
00148 }
00149 
00150 //----------------------------------------------------------------------------
00151 
00152 template<class PLAYER>
00153 void GoUctBookBuilderCommands<PLAYER>::ShowInfluence(GtpCommand& cmd,
00154                                                      GoAutoBookState& state)
00155 {
00156     cmd << "INFLUENCE ";
00157     for (GoBoard::Iterator it(m_bd); it; ++it)
00158     {
00159         if (m_bd.IsLegal(*it))
00160         {
00161             state.Play(*it);
00162             SgBookNode node;
00163             if (m_book->Get(state, node))
00164             {
00165                 float value 
00166                     = m_bookBuilder.InverseEval(m_bookBuilder.Value(node));
00167                 float scaledValue = (value * 2 - 1);
00168                 if (m_bd.ToPlay() != SG_BLACK)
00169                     scaledValue *= -1;
00170                 cmd << ' ' << SgWritePoint(*it) << ' ' << scaledValue;
00171             }
00172             state.Undo();
00173         }
00174     }
00175 }
00176 
00177 //----------------------------------------------------------------------------
00178 
00179 template<class PLAYER>
00180 void GoUctBookBuilderCommands<PLAYER>::CmdOpen(GtpCommand& cmd)
00181 {
00182     cmd.CheckNuArg(1);
00183     m_book.reset(new GoAutoBook(cmd.Arg(0)));
00184 }
00185 
00186 template<class PLAYER>
00187 void GoUctBookBuilderCommands<PLAYER>::CmdClose(GtpCommand& cmd)
00188 {
00189     cmd.CheckArgNone();
00190     m_book.reset(0);
00191 }
00192 
00193 template<class PLAYER>
00194 void GoUctBookBuilderCommands<PLAYER>::CmdExpand(GtpCommand& cmd)
00195 {
00196     if (m_book.get() == 0)
00197         throw GtpFailure() << "No opened autobook!\n";
00198     cmd.CheckNuArg(1);
00199     int numExpansions = cmd.IntArg(0, 1);
00200     m_bookBuilder.SetPlayer(Player());
00201     m_bookBuilder.SetState(*m_book);
00202     m_bookBuilder.Expand(numExpansions);
00203 }
00204 
00205 template<class PLAYER>
00206 void GoUctBookBuilderCommands<PLAYER>::CmdRefresh(GtpCommand& cmd)
00207 {
00208     if (m_book.get() == 0)
00209         throw GtpFailure() << "No opened autobook!\n";
00210     cmd.CheckArgNone();
00211     m_bookBuilder.SetPlayer(Player());
00212     m_bookBuilder.SetState(*m_book);
00213     m_bookBuilder.Refresh();
00214 }
00215 
00216 template<class PLAYER>
00217 void GoUctBookBuilderCommands<PLAYER>::CmdParam(GtpCommand& cmd)
00218 {
00219     if (cmd.NuArg() == 0)
00220     {
00221         cmd << "[bool] use_widening " << m_bookBuilder.UseWidening() << '\n'
00222             << "[string] alpha " << m_bookBuilder.Alpha() << '\n'
00223             << "[string] expand_width " << m_bookBuilder.ExpandWidth() << '\n'
00224             << "[string] expand_threshold " 
00225             << m_bookBuilder.ExpandThreshold() << '\n'
00226             << "[string] num_threads " << m_bookBuilder.NumThreads() << '\n'
00227             << "[string] num_games_per_evaluation " 
00228             << m_bookBuilder.NumGamesPerEvaluation() << '\n'
00229             << "[string] num_games_per_sort "
00230             << m_bookBuilder.NumGamesPerSort() << '\n';
00231     }
00232     else if (cmd.NuArg() == 2)
00233     {
00234         std::string name = cmd.Arg(0);
00235         if (name == "num_threads")
00236             m_bookBuilder.SetNumThreads(cmd.IntArg(1, 1));
00237         else if (name == "num_games_per_evaluation")
00238             m_bookBuilder.SetNumGamesPerEvaluation(cmd.SizeTypeArg(1, 1));
00239         else if (name == "num_games_per_sort")
00240             m_bookBuilder.SetNumGamesPerSort(cmd.SizeTypeArg(1, 1));
00241         else if (name == "use_widening")
00242             m_bookBuilder.SetUseWidening(cmd.BoolArg(1));
00243         else if (name == "expand_width")
00244             m_bookBuilder.SetExpandWidth(cmd.IntArg(1, 1));
00245         else if (name == "expand_threshold")
00246             m_bookBuilder.SetExpandThreshold(cmd.IntArg(1, 1));
00247         else if (name == "alpha")
00248         {
00249             float alpha = cmd.FloatArg(1);
00250             if (alpha < 0)
00251                 throw GtpFailure("Alpha must be greater than 0!");
00252             m_bookBuilder.SetAlpha(alpha);
00253         }
00254     }
00255     else
00256         throw GtpFailure() << "Expected 0 or 2 arguments!\n";
00257 }
00258 
00259 template<class PLAYER>
00260 void GoUctBookBuilderCommands<PLAYER>::CmdScores(GtpCommand& cmd)
00261 {
00262     if (m_book.get() == 0)
00263         throw GtpFailure() << "No opened autobook!\n";
00264     cmd.CheckArgNone();
00265     GoAutoBookState state(m_bd);
00266     state.Synchronize();
00267     ShowInfluence(cmd, state);
00268     cmd << "\nLABEL ";
00269     for (GoBoard::Iterator it(m_bd); it; ++it)
00270     {
00271         if (m_bd.IsLegal(*it))
00272         {
00273             state.Play(*it);
00274             SgBookNode node;
00275             if (m_book->Get(state, node))
00276             {
00277                 float value 
00278                     = m_bookBuilder.InverseEval(m_bookBuilder.Value(node));
00279                 cmd << ' ' << SgWritePoint(*it) 
00280                     << ' ' << std::fixed << std::setprecision(3) << value;
00281             }
00282             state.Undo();
00283         }
00284     }
00285     cmd << '\n';
00286 }
00287 
00288 template<class PLAYER>
00289 void GoUctBookBuilderCommands<PLAYER>::CmdCounts(GtpCommand& cmd)
00290 {
00291     if (m_book.get() == 0)
00292         throw GtpFailure() << "No opened autobook!\n";
00293     cmd.CheckArgNone();
00294     GoAutoBookState state(m_bd);
00295     state.Synchronize();
00296     ShowInfluence(cmd, state);
00297     cmd << "\nLABEL ";
00298     for (GoBoard::Iterator it(m_bd); it; ++it)
00299     {
00300         if (m_bd.IsLegal(*it))
00301         {
00302             state.Play(*it);
00303             SgBookNode node;
00304             if (m_book->Get(state, node))
00305                 cmd << ' ' << SgWritePoint(*it) << ' ' << node.m_count;
00306             state.Undo();
00307         }
00308     }
00309     cmd << '\n';
00310 }
00311 
00312 template<class PLAYER>
00313 void GoUctBookBuilderCommands<PLAYER>::CmdPriority(GtpCommand& cmd)
00314 {
00315     if (m_book.get() == 0)
00316         throw GtpFailure() << "No opened autobook!\n";
00317     cmd.CheckArgNone();
00318     GoAutoBookState state(m_bd);
00319     state.Synchronize();
00320     SgBookNode parent;
00321     if (!m_book->Get(state, parent))
00322         throw GtpFailure("Current state not in book!");
00323     ShowInfluence(cmd, state);
00324     cmd << "\nLABEL ";
00325     for (GoBoard::Iterator it(m_bd); it; ++it)
00326     {
00327         if (m_bd.IsLegal(*it))
00328         {
00329             state.Play(*it);
00330             SgBookNode child;
00331             if (m_book->Get(state, child))
00332             {
00333                 float priority = m_bookBuilder.ComputePriority
00334                     (parent, child.m_value, child.m_priority);
00335                 cmd << ' ' << SgWritePoint(*it) << ' ' 
00336                     << std::fixed << std::setprecision(1) << priority;
00337             }
00338             state.Undo();
00339         }
00340     }
00341     cmd << '\n';
00342 }
00343 
00344 //----------------------------------------------------------------------------
00345 
00346 #endif // GOUCT_BOOKBUILDERCOMMANDS_H


17 Jun 2010 Doxygen 1.4.7