Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

BookBuilderCommands.hpp

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file BookBuilderCommands.hpp
00003  */
00004 //----------------------------------------------------------------------------
00005 
00006 #ifndef BOOKBUILDERCOMMANDS_HPP
00007 #define BOOKBUILDERCOMMANDS_HPP
00008 
00009 #include "BookBuilder.hpp"
00010 #include "BookCommands.hpp"
00011 #include "HexHtpEngine.hpp"
00012 
00013 _BEGIN_BENZENE_NAMESPACE_
00014 
00015 //----------------------------------------------------------------------------
00016 
00017 /** Commands for building opening books. */
00018 template<class PLAYER>
00019 class BookBuilderCommands : public BookCommands
00020 {
00021 public:
00022     BookBuilderCommands(Game& game, HexEnvironment& env, 
00023                         boost::scoped_ptr<Book>& book,
00024                         BookCheck& bookCheck, PLAYER& player);
00025 
00026     ~BookBuilderCommands();
00027 
00028     void Register(GtpEngine& engine);
00029 
00030 private:
00031 
00032     BookBuilder<PLAYER> m_bookBuilder;
00033     
00034     typedef BookBuilderCommands<PLAYER> BookBuilderType;
00035     void Register(GtpEngine& engine, const std::string& command,
00036                   typename GtpCallback<BookBuilderType>::Method method);
00037 
00038     void CmdBookPriorities(HtpCommand& cmd);
00039     void CmdBookExpand(HtpCommand& cmd);
00040     void CmdBookRefresh(HtpCommand& cmd);
00041     void CmdBookIncreaseWidth(HtpCommand& cmd);
00042     void CmdParamBookBuilder(HtpCommand& cmd);
00043 };
00044 
00045 //----------------------------------------------------------------------------
00046 
00047 template<class PLAYER>
00048 BookBuilderCommands<PLAYER>::BookBuilderCommands(Game& game, 
00049                                                  HexEnvironment& env,
00050                                                  boost::scoped_ptr<Book>& book,
00051                                                  BookCheck& bookCheck, 
00052                                                  PLAYER& player)
00053     : BookCommands(game, env, book, bookCheck),
00054       m_bookBuilder(player)
00055 {
00056 }
00057 
00058 template<class PLAYER>
00059 BookBuilderCommands<PLAYER>::~BookBuilderCommands()
00060 {
00061 }
00062 
00063 template<class PLAYER>
00064 void BookBuilderCommands<PLAYER>::Register(GtpEngine& e)
00065 {
00066     BookCommands::Register(e);
00067     Register(e, "book-expand", &BookBuilderCommands<PLAYER>::CmdBookExpand);
00068     Register(e, "book-priorities", 
00069              &BookBuilderCommands<PLAYER>::CmdBookPriorities);
00070     Register(e, "book-refresh", &BookBuilderCommands<PLAYER>::CmdBookRefresh);
00071     Register(e, "book-increase-width", 
00072              &BookBuilderCommands<PLAYER>::CmdBookIncreaseWidth);
00073     Register(e, "param_book_builder", 
00074              &BookBuilderCommands<PLAYER>::CmdParamBookBuilder);
00075 }
00076 
00077 template<class PLAYER>
00078 void BookBuilderCommands<PLAYER>::Register(GtpEngine& engine, 
00079                                            const std::string& command,
00080                 typename GtpCallback<BookBuilderType>::Method method)
00081 {
00082     engine.Register(command, new GtpCallback<BookBuilderType>(this, method));
00083 }
00084 
00085 //----------------------------------------------------------------------------
00086 
00087 template<class PLAYER>
00088 void BookBuilderCommands<PLAYER>::CmdParamBookBuilder(HtpCommand& cmd)
00089 {
00090     if (cmd.NuArg() == 0)
00091     {
00092         cmd << '\n'
00093             << "[bool] use_widening " 
00094             << m_bookBuilder.UseWidening() << '\n'
00095             << "[bool] use_ice "
00096             << m_bookBuilder.UseICE() << '\n'
00097             << "[string] alpha "
00098             << m_bookBuilder.Alpha() << '\n'
00099             << "[string] expand_width "
00100             << m_bookBuilder.ExpandWidth() << '\n'
00101             << "[string] expand_threshold " 
00102             << m_bookBuilder.ExpandThreshold() << '\n'
00103             << "[string] num_threads " 
00104             << m_bookBuilder.NumThreads() << '\n';
00105     }
00106     else if (cmd.NuArg() == 2)
00107     {
00108         std::string name = cmd.Arg(0);
00109         if (name == "alpha")
00110             m_bookBuilder.SetAlpha(cmd.FloatArg(1));
00111         else if (name == "expand_width")
00112             m_bookBuilder.SetExpandWidth(cmd.SizeTypeArg(1, 1));
00113         else if (name == "expand_threshold")
00114             m_bookBuilder.SetExpandThreshold(cmd.SizeTypeArg(1, 1));
00115         else if (name == "num_threads")
00116             m_bookBuilder.SetNumThreads(cmd.SizeTypeArg(1));
00117         else if (name == "use_ice")
00118             m_bookBuilder.SetUseICE(cmd.BoolArg(1));
00119         else if (name == "use_widening")
00120             m_bookBuilder.SetUseWidening(cmd.BoolArg(1));
00121         else
00122             throw HtpFailure() << "unknown parameter: " << name;
00123     }
00124     else
00125         throw HtpFailure("Expected 0 or 2 arguments.");
00126 }
00127 
00128 /** Expands the current node in the current opening book.
00129     "book-expand [iterations]"
00130 */
00131 template<class PLAYER>
00132 void BookBuilderCommands<PLAYER>::CmdBookExpand(HtpCommand& cmd)
00133 {
00134     if (m_book.get() == 0) 
00135         throw HtpFailure() << "No open book.";
00136     cmd.CheckNuArg(1);
00137     int iterations = cmd.IntArg(0, 1);
00138     HexState state(m_game.Board(), m_game.Board().WhoseTurn());
00139     HexBoard& brd = m_env.SyncBoard(m_game.Board());
00140     m_bookBuilder.SetState(*m_book, state);
00141     m_bookBuilder.SetWorkBoard(brd);
00142     m_bookBuilder.Expand(iterations);
00143 }
00144 
00145 /** Refreshes the current book. See BookBuilder::Refresh(). */
00146 template<class PLAYER>
00147 void BookBuilderCommands<PLAYER>::CmdBookRefresh(HtpCommand& cmd)
00148 {
00149     UNUSED(cmd);
00150     if (m_book.get() == 0) 
00151         throw HtpFailure() << "No open book.";
00152     HexState state(m_game.Board(), m_game.Board().WhoseTurn());
00153     HexBoard& brd = m_env.SyncBoard(m_game.Board());
00154     m_bookBuilder.SetState(*m_book, state);
00155     m_bookBuilder.SetWorkBoard(brd);
00156     m_bookBuilder.Refresh();
00157 }
00158 
00159 /** Increases the width of all internal nodes that need to be
00160     increased. See BookBuilder::IncreaseWidth().  */
00161 template<class PLAYER>
00162 void BookBuilderCommands<PLAYER>::CmdBookIncreaseWidth(HtpCommand& cmd)
00163 {
00164     UNUSED(cmd);
00165     if (m_book.get() == 0) 
00166         throw HtpFailure() << "No open book.";
00167     HexState state(m_game.Board(), m_game.Board().WhoseTurn());
00168     HexBoard& brd = m_env.SyncBoard(m_game.Board());
00169     m_bookBuilder.SetState(*m_book, state);
00170     m_bookBuilder.SetWorkBoard(brd);
00171     m_bookBuilder.IncreaseWidth();
00172 }
00173 
00174 template<class PLAYER>
00175 void BookBuilderCommands<PLAYER>::CmdBookPriorities(HtpCommand& cmd)
00176 {
00177     if (m_book.get() == 0) 
00178         throw HtpFailure() << "No open book.";
00179     HexState state(m_game.Board(), m_game.Board().WhoseTurn());
00180     HexBookNode parent;
00181     if (!m_book->Get(state, parent))
00182         return;
00183     for (BitsetIterator p(state.Position().GetEmpty()); p; ++p) 
00184     {
00185         state.PlayMove(*p);
00186         HexBookNode succ;
00187         if (m_book->Get(state, succ))
00188         {
00189             cmd << " " << *p;
00190             float priority = m_bookBuilder.ComputePriority(parent, 
00191                                                            succ.m_value,
00192                                                            succ.m_priority);
00193             float value = BookUtil::InverseEval(succ.m_value);
00194             if (HexEvalUtil::IsWin(value))
00195                 cmd << " W";
00196             else if (HexEvalUtil::IsLoss(value))
00197                 cmd << " L";
00198             else
00199                 cmd << " " << std::fixed << std::setprecision(1) << priority;
00200         }
00201         state.UndoMove(*p);
00202     }
00203 }
00204 
00205 //----------------------------------------------------------------------------
00206 
00207 _END_BENZENE_NAMESPACE_
00208 
00209 #endif // BOOKBUILDERCOMMANDS_HPP


6 Jan 2011 Doxygen 1.6.3