usiResponse.cc
Go to the documentation of this file.
00001 /* usiResponse.cc
00002  */
00003 #include "osl/game_playing/usiResponse.h"
00004 #include "osl/game_playing/gameState.h"
00005 #include "osl/move_probability/featureSet.h"
00006 #include "osl/rating/ratingEnv.h"
00007 #include "osl/rating/featureSet.h"
00008 #include "osl/progress/ml/newProgress.h"
00009 #include "osl/sennichite.h"
00010 #include "osl/record/csa.h"
00011 #include "osl/record/usi.h"
00012 #include "osl/record/ki2.h"
00013 #include <boost/foreach.hpp>
00014 #include <boost/lexical_cast.hpp>
00015 #include <sstream>
00016 #include <iostream>
00017 
00018 osl::game_playing::
00019 UsiResponse::UsiResponse(const UsiState& u, bool n, bool v)
00020   : usi_state(u), new_move_probability(n), verbose(v)
00021 {
00022 }
00023 osl::game_playing::
00024 UsiResponse::~UsiResponse()
00025 {
00026 }
00027 
00028 osl::MoveVector osl::game_playing::
00029 UsiResponse::generateGoodMoves()
00030 {
00031   GameState state(usi_state.initial_state);
00032   BOOST_FOREACH(Move m, usi_state.moves) 
00033     state.pushMove(m);
00034 
00035   MoveVector normal_or_win_or_draw, loss;
00036   state.generateNotLosingMoves(normal_or_win_or_draw, loss);
00037   if (verbose && ! loss.empty()) {
00038     std::cerr << "  removed losing move ";
00039     BOOST_FOREACH(Move m, loss)
00040       std::cerr << record::csa::show(m);
00041     std::cerr << "\n";
00042   }
00043   return normal_or_win_or_draw;
00044 }
00045 
00046 void osl::game_playing::
00047 UsiResponse::genmoveProbability(int limit, MoveLogProbVector& out)
00048 {
00049   GameState gstate(usi_state.initial_state);
00050   BOOST_FOREACH(Move m, usi_state.moves) 
00051     gstate.pushMove(m);
00052   const NumEffectState& state= gstate.state();
00053   const MoveStack& history = gstate.moveHistory();
00054   progress::ml::NewProgress progress(state);
00055   MoveLogProbVector moves;
00056   if (new_move_probability) {
00057     const move_probability::StandardFeatureSet& feature_set
00058       = move_probability::StandardFeatureSet::instance();
00059     Move threatmate = move_probability::StateInfo::findShortThreatmate
00060       (state, history.lastMove());
00061     move_probability::StateInfo info(state, progress.progress16(),
00062                                      history, threatmate);
00063     feature_set.generateLogProb(info, moves);
00064   } else {
00065     const rating::StandardFeatureSet& feature_set
00066       = rating::StandardFeatureSet::instance();
00067     rating::RatingEnv env;
00068     env.history = history;
00069     env.make(state, state.pin(state.turn()), state.pin(alt(state.turn())),
00070              progress.progress16());
00071     feature_set.generateLogProb(state, env, limit, moves);
00072   }
00073   for (size_t i=1; i<moves.size(); ++i)
00074     if (moves[i].logProb() <= moves[i-1].logProb() && moves[i-1].logProb()+1<=limit)
00075       moves[i].setLogProb(moves[i-1].logProb()+1);
00076 
00077   MoveVector good_moves = generateGoodMoves();
00078   BOOST_FOREACH(MoveLogProb move, moves) 
00079     if (good_moves.isMember(move.move())) 
00080       out.push_back(move);
00081 }
00082 
00083 void osl::game_playing::
00084 UsiResponse::genmoveProbability(int limit, std::string& out)
00085 {
00086   MoveLogProbVector moves;
00087   genmoveProbability(limit, moves);
00088   out = "genmove_probability";
00089   BOOST_FOREACH(MoveLogProb move, moves) {
00090     out += " ";
00091     out += record::usi::show(move.move());
00092     out += " ";
00093     out += boost::lexical_cast<std::string>(move.logProb());
00094   }
00095 }
00096 
00097 void osl::game_playing::
00098 UsiResponse::genmove(std::string& out)
00099 {
00100   MoveVector moves = generateGoodMoves();
00101   out = "genmove";
00102   BOOST_FOREACH(Move move, moves) {
00103     out += " ";
00104     out += record::usi::show(move);
00105   }
00106 }
00107 
00108 void osl::game_playing::
00109 UsiResponse::csashow(const NumEffectState& state, std::string& out)
00110 {
00111   std::ostringstream os;
00112   os << state;
00113   os << "csashowok";
00114   out = os.str();
00115 }
00116 
00117 void osl::game_playing::
00118 UsiResponse::csamove(const NumEffectState& state, const std::string& str,
00119                      std::string& out)
00120 {
00121   const Move move = record::usi::strToMove(str, state);
00122   out = "csamove ";
00123   out += record::csa::show(move);
00124 }
00125 
00129 void osl::game_playing::
00130 UsiResponse::ki2moves(const NumEffectState& current,
00131                       const std::string& moves_str, std::string& out)
00132 {
00133   NumEffectState state(current);
00134   vector<Move> moves;
00135   std::istringstream is(moves_str);
00136   std::string s;
00137   while (is >> s) {
00138     const Move move = record::usi::strToMove(s, state);
00139     moves.push_back(move);
00140     state.makeMove(move);
00141   }
00142   assert(!moves.empty());
00143   
00144   Move last_move;
00145   if (! usi_state.moves.empty()) {
00146     last_move = usi_state.moves.back();
00147   }
00148   out = "ki2moves ";
00149   out += record::ki2::show(&*moves.begin(), &*moves.end(),
00150                            current, last_move);
00151 }
00152 
00156 void osl::game_playing::
00157 UsiResponse::ki2currentinfo(const NumEffectState& current, std::string& out)
00158 {
00159   Move last_last_move, last_move;
00160   if (1 <= usi_state.moves.size()) {
00161     last_move = usi_state.moves.back();
00162   }
00163   if (2 <= usi_state.moves.size()) {
00164     last_last_move = usi_state.moves[usi_state.moves.size()-2];
00165   }
00166  
00167   if (last_move.isValid()) {
00168     out = "ki2currentinfo ";
00169     out += boost::lexical_cast<std::string>(usi_state.moves.size());
00170     out += " ";
00171     out += record::ki2::show(last_move, current, last_last_move);
00172   } else {
00173     out = "ki2currentinfo";
00174   }
00175 }
00176 
00177 void osl::game_playing::
00178 UsiResponse::isValidPosition(const std::string& line, std::string& out)
00179 {
00180   out = "invalid";
00181   if (line.find("position") == 0) {
00182     try {
00183       SimpleState initial_state;
00184       vector<Move> moves;
00185       record::usi::parse(line.substr(8), initial_state, moves);
00186       out = "valid";
00187     }
00188     catch (std::exception& e) {
00189       // fall through
00190     }
00191   }
00192 }
00193 
00194 bool osl::game_playing::
00195 UsiResponse::hasImmediateResponse(const std::string& command,
00196                                   std::string& out)
00197 {
00198   if (command.find("genmove_probability") == 0) {
00199     int limit = 2000, value;
00200     std::istringstream is(command.substr(strlen("genmove_probability")));
00201     if (is >> value)
00202       limit = value;
00203     genmoveProbability(limit, out);
00204     return true;
00205   }
00206   if (command.find("genmove") == 0) {
00207     genmove(out);
00208     return true;
00209   }
00210   const NumEffectState state = usi_state.currentState();
00211   if (command.find("csashow") == 0) {
00212     csashow(state, out);
00213     return true;
00214   }
00215   if (command.find("csamove ") == 0) {
00216     csamove(state, command.substr(8), out);
00217     return true;
00218   }
00219   if (command.find("ki2moves ") == 0) {
00220     ki2moves(state, command.substr(9), out);
00221     return true;
00222   }
00223   if (command.find("ki2currentinfo") == 0) {
00224     ki2currentinfo(state, out);
00225     return true;
00226   }
00227   if (command.find("isvalidposition ") == 0) {
00228     isValidPosition(command.substr(16), out);
00229     return true;
00230   }
00231   if (command.find("echo ") == 0) {
00232     out = command.substr(5);
00233     return true;
00234   }
00235   return false;
00236 }
00237 
00238 // ;;; Local Variables:
00239 // ;;; mode:c++
00240 // ;;; c-basic-offset:2
00241 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines