00001
00002
00003 #include "osl/game_playing/speculativeSearchPlayer.h"
00004 #include "osl/game_playing/speculativeAllMoves.h"
00005 #include "osl/game_playing/gameState.h"
00006 #include "osl/game_playing/searchPlayer.h"
00007 #include "osl/container/moveStack.h"
00008 #include "osl/sennichite.h"
00009 #include "osl/misc/nonBlockDelete.h"
00010 #include <iostream>
00011 #include <ctime>
00012 #include <unistd.h>
00013
00014
00015 osl::game_playing::SpeculativeSearchPlayer::
00016 SpeculativeSearchPlayer(Player my_turn, SearchPlayer *player)
00017 : main_player(player),
00018 speculative(new SpeculativeAllMoves()),
00019 my_turn(my_turn)
00020 {
00021 }
00022
00023 osl::game_playing::
00024 SpeculativeSearchPlayer::~SpeculativeSearchPlayer()
00025 {
00026 }
00027
00028 osl::game_playing::ComputerPlayer*
00029 osl::game_playing::SpeculativeSearchPlayer::clone() const
00030 {
00031 return new SpeculativeSearchPlayer(my_turn,
00032 dynamic_cast<SearchPlayer*>(main_player->clone()));
00033 }
00034
00035 void osl::game_playing::SpeculativeSearchPlayer::
00036 setMaxThreads(int new_max_threads)
00037 {
00038 speculative->setMaxThreads(new_max_threads);
00039 }
00040
00041 void osl::game_playing::SpeculativeSearchPlayer::
00042 pushMove(Move m)
00043 {
00044 main_player->pushMove(m);
00045 if (m.player() == my_turn)
00046 {
00047 if (previous_state.get() && speculative_search_allowed)
00048 {
00049 try
00050 {
00051 previous_state->pushMove(m);
00052 speculative->startSpeculative(previous_state, *main_player);
00053 }
00054 catch (std::exception& e)
00055 {
00056 std::cerr << e.what() << " in SpeculativeSearchPlayer::pushMove\n";
00057 speculative->clearResource();
00058 }
00059 NonBlockDelete::reset(previous_state);
00060 }
00061 }
00062 else
00063 {
00064 if (speculative_search_allowed)
00065 speculative->stopOtherThan(m);
00066 }
00067 }
00068
00069 void osl::game_playing::SpeculativeSearchPlayer::
00070 popMove()
00071 {
00072 main_player->popMove();
00073 previous_state.reset();
00074 speculative->stopAll();
00075 }
00076
00077 bool osl::game_playing::SpeculativeSearchPlayer::
00078 stopSearchNow()
00079 {
00080 return main_player->stopSearchNow();
00081 }
00082
00083 int osl::game_playing::SpeculativeSearchPlayer::
00084 standardSearchSeconds(const GameState& state, int total_left, int byoyomi) const
00085 {
00086 return
00087 std::max(1, main_player->secondsForThisMove(state, total_left, byoyomi)-1);
00088 }
00089
00090 const osl::game_playing::MoveWithComment
00091 osl::game_playing::SpeculativeSearchPlayer::
00092 selectBestMove(const GameState& state, int total_left, int byoyomi)
00093 {
00094 const time_t start_time = time(0);
00095 MoveWithComment result = MoveWithComment(Move::INVALID());
00096 const Move last_move = state.moveHistory().lastMove();
00097
00098 const int wait_for = standardSearchSeconds(state, total_left, byoyomi);
00099
00100 if (last_move.isNormal())
00101 result = speculative->waitResult(last_move, wait_for, *main_player,
00102 byoyomi);
00103
00104 const time_t now = time(0);
00105 char ctime_buf[64];
00106 if (result.move.isNormal()) {
00107 #ifdef DEBUG_SPECULATIVE_EXECUTION
00108 std::cerr << "returned " << record::csa::show(result.move)
00109 << " " << ctime_r(&now, ctime_buf);
00110 #endif
00111 selectBestMoveCleanUp(state);
00112 return result;
00113 }
00114 std::cerr << "search again " << ctime_r(&now, ctime_buf);
00115
00116 const int consumed = (now - start_time);
00117 if (total_left)
00118 total_left = std::max(1, total_left - consumed);
00119 if (byoyomi)
00120 byoyomi = std::max(1, byoyomi - consumed);
00121
00122 result = main_player->selectBestMove(state, total_left, byoyomi);
00123 selectBestMoveCleanUp(state);
00124 return result;
00125 }
00126
00127 void osl::game_playing::SpeculativeSearchPlayer::
00128 selectBestMoveCleanUp(const GameState& state)
00129 {
00130 try
00131 {
00132 previous_state = state.clone();
00133 }
00134 catch (std::exception& e)
00135 {
00136 std::cerr << e.what() << std::endl;
00137 previous_state.reset();
00138 }
00139 speculative->selectBestMoveCleanUp();
00140 }
00141
00142
00143
00144
00145