00001
00002
00003 #include "osl/search/searchState.h"
00004 #include "osl/search/simpleHashRecord.h"
00005 #include "osl/search/sacrificeCheck.h"
00006 #include "osl/record/csa.h"
00007 #include "osl/misc/realTime.h"
00008 #include <iostream>
00009
00010 osl::search::
00011 SearchStateShared::SearchStateShared()
00012 {
00013 #ifdef OSL_SMP_DEBUG
00014 std::cerr << ">> SearchStateShared " << this << "\n";
00015 #endif
00016 }
00017
00018 osl::search::
00019 SearchStateShared::~SearchStateShared()
00020 {
00021 #ifdef OSL_SMP_DEBUG
00022 std::cerr << "<< SearchStateShared " << this << "\n";
00023 #endif
00024 }
00025
00026
00027 osl::search::
00028 SearchStateCore::SearchStateCore(const NumEffectState& s, checkmate_t& c)
00029 : current_state(s), checkmate_searcher(&c), worker_cache(0),
00030 current_path(s.getTurn()), root_depth(0),
00031 stop_flag(Alarm::UNAVAILABLE)
00032 {
00033 setState(s);
00034 assert(hasLastRecord());
00035 }
00036 osl::search::
00037 SearchStateCore::~SearchStateCore()
00038 {
00039 }
00040
00041 void osl::search::
00042 SearchStateCore::setTimeOut(int seconds)
00043 {
00044 if (seconds <= 0)
00045 {
00046 if (seconds < 0)
00047 std::cerr << "warning: setTimeOut negative " << seconds << "\n";
00048 seconds = 1;
00049 }
00050 const bool success = Alarm::set(seconds, AlarmSwitch(&stop_flag));
00051 if (! success)
00052 {
00053 std::cerr << "warning: search without alarm\n";
00054 }
00055 }
00056 void osl::search::
00057 SearchStateCore::resetTimeOut()
00058 {
00059 Alarm::reset(AlarmSwitch(&stop_flag));
00060 }
00061
00062 void osl::search::
00063 SearchStateCore::throwTimeOut()
00064 {
00065 resetTimeOut();
00066 throw misc::NoMoreTime();
00067 }
00068
00069 void osl::search::
00070 SearchStateCore::setState(const NumEffectState& s)
00071 {
00072 if (¤t_state != &s)
00073 current_state = s;
00074 current_path = PathEncoding(s.getTurn(), move_history.size());
00075 killer_moves.clear();
00076 shared.reset(new SearchStateShared());
00077 repetition_counter.clear();
00078 const HashKey key = HashKey::calcHash(s);
00079 repetition_counter.push(key, s);
00080 move_history.clear();
00081 record_stack.clear();
00082 root_depth = 0;
00083 }
00084
00085 void osl::search::
00086 SearchStateCore::setHistory(const MoveStack& h)
00087 {
00088 move_history = h;
00089 current_path = PathEncoding(current_path.turn(), h.size());
00090 root_depth = history().size();
00091 }
00092
00093 void osl::search::
00094 SearchStateCore::setBigramKillerMove(const BigramKillerMove& killers)
00095 {
00096 shared.reset(new SearchStateShared());
00097 shared->bigram_killers = killers;
00098 }
00099
00100 bool osl::search::
00101 SearchStateCore::abort(Move best_move) const
00102 {
00103 std::cerr << state();
00104 history().dump();
00105 const SimpleHashRecord *record = record_stack.lastRecord();
00106 std::cerr << "best move " << record::csa::show(best_move)
00107 << "\n";
00108 std::cerr << "record " << record << "\n";
00109 if (record)
00110 {
00111 record->dump(std::cerr);
00112 }
00113 record_stack.dump();
00114 repetition_counter.history().dump();
00115 return false;
00116 }
00117
00118
00119
00120 osl::search::
00121 SearchState::SearchState(const NumEffectState& s, checkmate_t& c)
00122 : SearchStateCore(s, c), root_limit(0), cur_limit(0)
00123 {
00124 }
00125
00126 osl::search::
00127 SearchState::~SearchState()
00128 {
00129 }
00130
00131 void osl::search::
00132 SearchState::setState(const NumEffectState& s)
00133 {
00134 SearchStateCore::setState(s);
00135 root_limit = cur_limit = 0;
00136 }
00137
00138 int osl::search::
00139 SearchState::countSacrificeCheck2(int history_max) const
00140 {
00141 return SacrificeCheck::count2(recordHistory(), history(), history_max);
00142 }
00143
00144 bool osl::search::
00145 SearchState::abort(Move best_move) const
00146 {
00147 std::cerr << "cur limit " << cur_limit
00148 << " root limit " << root_limit << "\n";
00149 SearchStateCore::abort(best_move);
00150 return false;
00151 }
00152
00153 void osl::search::
00154 SearchState::checkPointSearchAllMoves()
00155 {
00156
00157 }
00158
00159
00160
00161
00162
00163