00001
00002
00003 #ifndef _NTESUKI_SEACHER_H
00004 #define _NTESUKI_SEACHER_H
00005 #include "osl/state/numEffectState.h"
00006 #include "osl/ntesuki/ntesukiTable.h"
00007 #include "osl/ntesuki/ntesukiRecord.h"
00008 #include "osl/ntesuki/ntesukiMoveGenerator.h"
00009
00010 #include "osl/ntesuki/ntesukiExceptions.h"
00011 #include "osl/container/moveVector.h"
00012 #include "osl/ntesuki/ntesukiSimulationSearcher.h"
00013
00014 #include <osl/stl/vector.h>
00015
00016 namespace osl
00017 {
00018 namespace ntesuki
00019 {
00020 class
00021 NtesukiSearcher
00022 {
00023 public:
00024 typedef NumEffectState State;
00025
00026 private:
00027 State& state;
00028
00030 NtesukiMoveGenerator *mg;
00031
00033 NtesukiTable table;
00034
00036 NtesukiSimulationSearcher simulator;
00037
00039 unsigned int node_count;
00040
00042 unsigned int read_node_limit;
00043
00045 bool verbose;
00046
00048 volatile int *stop_flag;
00049
00051 PathEncoding path;
00052
00054 typedef std::vector<Move> moves_t;
00055 moves_t moves_played;
00056
00058 typedef std::vector<NtesukiRecord *> nodes_t;
00059 nodes_t nodes_played;
00060
00062 unsigned int max_pass;
00063
00064
00065 public:
00069 static bool delay_non_pass;
00070 static bool ptt_invalid_defense;
00071
00072 static bool delay_interpose;
00073 static bool delay_nopromote;
00074 static bool delay_non_attack;
00075 static bool read_attack_only;
00076 static bool ptt_non_attack;
00077
00078 static bool ptt_siblings_fail;
00079 static bool ptt_siblings_success;
00080
00081 static bool ptt_uncle;
00082 static bool ptt_aunt;
00083
00084 private:
00086 NtesukiRecord::IWScheme iwscheme;
00088 NtesukiRecord::PSScheme psscheme;
00090 NtesukiRecord::ISScheme isscheme;
00091
00093 int tsumero_cost;
00094
00096 int tsumero_estimate;
00097
00099 double gc_ratio;
00100
00101 template<class Search,Player T> class AttackHelper;
00102 template<class Search,Player T> class DefenseHelper;
00103 template<class Search,Player T> class CallSimulationAttack;
00104 template<class Search,Player T> class CallSimulationDefense;
00105 template<class Search,Player T> class CallSimulationDefenseDisproof;
00106
00107
00108 unsigned int blockByAttackBack;
00109 unsigned int blockByPass;
00110 unsigned int attack_node_count;
00111 unsigned int attack_node_under_attack_count;
00112 unsigned int attack_node_moves_count;
00113 unsigned int defense_node_count;
00114 unsigned int defense_node_under_attack_count;
00115 unsigned int defense_node_moves_count;
00116 unsigned int pass_count, pass_success_count;
00117 unsigned int pass_attack_count, pass_attack_success_count;
00118 unsigned int sibling_defense_count, sibling_defense_success_count;
00119 unsigned int sibling_attack_count, sibling_attack_success_count;
00120 unsigned int isshogi_defense_count, isshogi_defense_success_count;
00121 unsigned int isshogi_attack_count, isshogi_attack_success_count;
00122 unsigned int immediate_win, immediate_lose;
00123 unsigned int attack_back_count;
00124 unsigned int proof_without_inversion_count, proof_AND_count, disproof_by_inversion_count;
00125
00126 public:
00127 static const int NtesukiNotFound = -1;
00128 static const int ReadLimitReached = -2;
00129 static const int TableLimitReached = -3;
00130 private:
00131 static const unsigned int INITIAL_PROOF_LIMIT =
00132 ProofDisproof::PROOF_LIMIT / 4;
00133 static const unsigned int INITIAL_DISPROOF_LIMIT =
00134 ProofDisproof::DISPROOF_LIMIT / 4;
00135
00139 template <Player T>
00140 NtesukiResult attack(NtesukiRecord* record,
00141 const NtesukiRecord* oracle_attack,
00142 const NtesukiRecord* oracle_defense,
00143 unsigned int proofLimit,
00144 unsigned int disproofLimit,
00145 int pass_left,
00146 const Move last_move);
00147 template <Player T>
00148 void attackWithOrder(NtesukiRecord* record,
00149 const NtesukiRecord* oracle_attack,
00150 const NtesukiRecord* oracle_defense,
00151 unsigned int proofLimit,
00152 unsigned int disproofLimit,
00153 int pass_left,
00154 const Move last_move);
00155
00156 template <Player T>
00157 NtesukiMove* selectMoveAttack(NtesukiRecord* record,
00158 unsigned int& best_proof,
00159 unsigned int& sum_disproof,
00160 unsigned int& second_proof,
00161 unsigned int& best_disproof,
00162 unsigned int& step_cost,
00163 NtesukiMoveList& moves,
00164 const int pass_left);
00165
00169 template <Player T>
00170 NtesukiResult defense(NtesukiRecord* record,
00171 const NtesukiRecord* oracle_attack,
00172 const NtesukiRecord* oracle_defense,
00173 unsigned int proofLimit,
00174 unsigned int disproofLimit,
00175 int pass_left,
00176 const Move last_move);
00177
00178
00179 template <Player T>
00180 void defenseWithPlayer(NtesukiRecord* record,
00181 const NtesukiRecord* oracle_attack,
00182 const NtesukiRecord* oracle_defense,
00183 unsigned int proofLimit,
00184 unsigned int disproofLimit,
00185 int pass_left,
00186 const Move last_move);
00187
00188 template <Player T>
00189 NtesukiMove* selectMoveDefense(NtesukiRecord* record,
00190 unsigned int& best_disproof,
00191 unsigned int& sum_proof,
00192 unsigned int& second_disproof,
00193 unsigned int& best_proof,
00194 unsigned int& step_cost,
00195 NtesukiMoveList& moves,
00196 const int pass_left,
00197 const Move last_move);
00198
00206 template <Player T> void simulateSiblingsSuccess(NtesukiRecord *record,
00207 NtesukiRecord *record_best,
00208 int pass_left,
00209 unsigned int& success_count,
00210 unsigned int& total_count);
00211
00216 template <Player T> void simulateSiblingsFail(NtesukiRecord *record,
00217 NtesukiRecord *record_best,
00218 int pass_left,
00219 unsigned int& success_count,
00220 unsigned int& total_count);
00224 template <Player T> void handleNonAttack(NtesukiRecord *record,
00225 int pass_left);
00229 template <Player T> void handleTonshi(NtesukiRecord *record,
00230 int pass_left,
00231 const Move last_move);
00235 template <Player T> void handleInterpose(NtesukiRecord *record,
00236 int pass_left);
00237
00238 public:
00239
00240
00241
00242 NtesukiSearcher(State& state,
00243 NtesukiMoveGenerator *mg,
00244 unsigned int table_limit,
00245 volatile int *stop_flag,
00246 bool verbose,
00247 int maxPass = NtesukiRecord::SIZE,
00248 NtesukiRecord::IWScheme iwscheme = NtesukiRecord::pn_iw,
00249 NtesukiRecord::PSScheme psscheme = NtesukiRecord::no_ps,
00250 NtesukiRecord::ISScheme isscheme = NtesukiRecord::no_is,
00251 int tsumero_cost = 0,
00252 int tsumero_estimate = 0,
00253 double gc_ratio = 0.33);
00254 ~NtesukiSearcher();
00255
00256
00257 template <Player T> int search();
00258
00259 int searchSlow(Player attacker, int rnl = 160000)
00260 {
00261 read_node_limit = rnl;
00262 if (attacker == BLACK)
00263 return search<BLACK>();
00264 else
00265 return search<WHITE>();
00266 }
00267
00268 NtesukiTable& getTable();
00269 int getNodeCount() const { return node_count; }
00270 bool exceedReadNodeLimit() const {return node_count > read_node_limit;}
00271
00272 };
00273 }
00274 }
00275
00276 #endif
00277
00278
00279
00280