usiResponse.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
usiResponse.cc
Go to the documentation of this file.
1 /* usiResponse.cc
2 */
3 #include "osl/game_playing/usiResponse.h"
4 #include "osl/game_playing/gameState.h"
5 #include "osl/move_probability/featureSet.h"
6 #include "osl/rating/ratingEnv.h"
7 #include "osl/rating/featureSet.h"
8 #include "osl/progress/ml/newProgress.h"
9 #include "osl/sennichite.h"
10 #include "osl/record/csa.h"
11 #include "osl/record/usi.h"
12 #ifndef MINIMAL
13 # include "osl/record/ki2.h"
14 #endif
15 #include <boost/foreach.hpp>
16 #include <boost/lexical_cast.hpp>
17 #include <sstream>
18 #include <iostream>
19
20 osl::game_playing::
21 UsiResponse::UsiResponse(const UsiState& u, bool n, bool v)
22 : usi_state(u), new_move_probability(n), verbose(v)
23 {
24 }
25 osl::game_playing::
26 UsiResponse::~UsiResponse()
27 {
28 }
29
30 osl::MoveVector osl::game_playing::
31 UsiResponse::generateGoodMoves()
32 {
33 GameState state(usi_state.initial_state);
34 BOOST_FOREACH(Move m, usi_state.moves)
35 state.pushMove(m);
36
37 MoveVector normal_or_win_or_draw, loss;
38 state.generateNotLosingMoves(normal_or_win_or_draw, loss);
39 if (verbose && ! loss.empty()) {
40 std::cerr << " removed losing move ";
41 BOOST_FOREACH(Move m, loss)
42 std::cerr << record::csa::show(m);
43 std::cerr << "\n";
44 }
45 return normal_or_win_or_draw;
46 }
47
48 void osl::game_playing::
49 UsiResponse::genmoveProbability(int limit, MoveLogProbVector& out)
50 {
51 GameState gstate(usi_state.initial_state);
52 BOOST_FOREACH(Move m, usi_state.moves)
53 gstate.pushMove(m);
54 const NumEffectState& state= gstate.state();
55 const MoveStack& history = gstate.moveHistory();
56 progress::ml::NewProgress progress(state);
57 MoveLogProbVector moves;
58 if (new_move_probability) {
59 const move_probability::StandardFeatureSet& feature_set
60 = move_probability::StandardFeatureSet::instance();
61 Move threatmate = move_probability::StateInfo::findShortThreatmate
62 (state, history.lastMove());
63 move_probability::StateInfo info(state, progress.progress16(),
64 history, threatmate);
65 feature_set.generateLogProb(info, moves);
66 } else {
67 const rating::StandardFeatureSet& feature_set
68 = rating::StandardFeatureSet::instance();
69 rating::RatingEnv env;
70 env.history = history;
71 env.make(state, state.pin(state.turn()), state.pin(alt(state.turn())),
72 progress.progress16());
73 feature_set.generateLogProb(state, env, limit, moves);
74 }
75 for (size_t i=1; i<moves.size(); ++i)
76 if (moves[i].logProb() <= moves[i-1].logProb() && moves[i-1].logProb()+1<=limit)
77 moves[i].setLogProb(moves[i-1].logProb()+1);
78
79 MoveVector good_moves = generateGoodMoves();
80 BOOST_FOREACH(MoveLogProb move, moves)
81 if (good_moves.isMember(move.move()))
82 out.push_back(move);
83 }
84
85 void osl::game_playing::
86 UsiResponse::genmoveProbability(int limit, std::string& out)
87 {
88 MoveLogProbVector moves;
89 genmoveProbability(limit, moves);
90 out = "genmove_probability";
91 BOOST_FOREACH(MoveLogProb move, moves) {
92 out += " ";
93 out += record::usi::show(move.move());
94 out += " ";
95 out += boost::lexical_cast<std::string>(move.logProb());
96 }
97 }
98
99 void osl::game_playing::
100 UsiResponse::genmove(std::string& out)
101 {
102 MoveVector moves = generateGoodMoves();
103 out = "genmove";
104 BOOST_FOREACH(Move move, moves) {
105 out += " ";
106 out += record::usi::show(move);
107 }
108 }
109
110 void osl::game_playing::
111 UsiResponse::csashow(const NumEffectState& state, std::string& out)
112 {
113 std::ostringstream os;
114 os << state;
115 os << "csashowok";
116 out = os.str();
117 }
118
119 void osl::game_playing::
120 UsiResponse::csamove(const NumEffectState& state, const std::string& str,
121 std::string& out)
122 {
123 const Move move = record::usi::strToMove(str, state);
124 out = "csamove ";
125 out += record::csa::show(move);
126 }
127
128 #ifndef MINIMAL
129
132 void osl::game_playing::
133 UsiResponse::ki2moves(const NumEffectState& current,
134 const std::string& moves_str, std::string& out)
135 {
136 NumEffectState state(current);
137 vector<Move> moves;
138 std::istringstream is(moves_str);
139 std::string s;
140 while (is >> s) {
141 const Move move = record::usi::strToMove(s, state);
142 moves.push_back(move);
143 state.makeMove(move);
144 }
145 assert(!moves.empty());
146
147 Move last_move;
148 if (! usi_state.moves.empty()) {
149 last_move = usi_state.moves.back();
150 }
151 out = "ki2moves ";
152 out += record::ki2::show(&*moves.begin(), &*moves.end(),
153 current, last_move);
154 }
155
159 void osl::game_playing::
160 UsiResponse::ki2currentinfo(const NumEffectState& current, std::string& out)
161 {
162 Move last_last_move, last_move;
163 if (1 <= usi_state.moves.size()) {
164 last_move = usi_state.moves.back();
165 }
166 if (2 <= usi_state.moves.size()) {
167 last_last_move = usi_state.moves[usi_state.moves.size()-2];
168 }
169
170 if (last_move.isValid()) {
171 out = "ki2currentinfo ";
172 out += boost::lexical_cast<std::string>(usi_state.moves.size());
173 out += " ";
174 out += record::ki2::show(last_move, current, last_last_move);
175 } else {
176 out = "ki2currentinfo";
177 }
178 }
179 #endif
180
181 void osl::game_playing::
182 UsiResponse::isValidPosition(const std::string& line, std::string& out)
183 {
184 out = "invalid";
185 if (line.find("position") == 0) {
186 try {
187 SimpleState initial_state;
188 vector<Move> moves;
189 record::usi::parse(line.substr(8), initial_state, moves);
190 out = "valid";
191 }
192 catch (std::exception& e) {
193 // fall through
194 }
195 }
196 }
197
198 bool osl::game_playing::
199 UsiResponse::hasImmediateResponse(const std::string& command,
200 std::string& out)
201 {
202 if (command.find("genmove_probability") == 0) {
203 int limit = 2000, value;
204 std::istringstream is(command.substr(strlen("genmove_probability")));
205 if (is >> value)
206 limit = value;
207 genmoveProbability(limit, out);
208 return true;
209 }
210 if (command.find("genmove") == 0) {
211 genmove(out);
212 return true;
213 }
214 const NumEffectState state = usi_state.currentState();
215 if (command.find("csashow") == 0) {
216 csashow(state, out);
217 return true;
218 }
219 if (command.find("csamove ") == 0) {
220 csamove(state, command.substr(8), out);
221 return true;
222 }
223 #ifndef MINIMAL
224 if (command.find("ki2moves ") == 0) {
225 ki2moves(state, command.substr(9), out);
226 return true;
227 }
228 if (command.find("ki2currentinfo") == 0) {
229 ki2currentinfo(state, out);
230 return true;
231 }
232 #endif
233 if (command.find("isvalidposition ") == 0) {
234 isValidPosition(command.substr(16), out);
235 return true;
236 }
237 if (command.find("echo ") == 0) {
238 out = command.substr(5);
239 return true;
240 }
241 return false;
242 }
243
244 // ;;; Local Variables:
245 // ;;; mode:c++
246 // ;;; c-basic-offset:2
247 // ;;; End: