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 static unsigned int dynamic_widening_width;
00085
00086 private:
00088 NtesukiRecord::IWScheme iwscheme;
00090 NtesukiRecord::PSScheme psscheme;
00092 NtesukiRecord::ISScheme isscheme;
00093
00095 int tsumero_cost;
00096
00098 int tsumero_estimate;
00099
00101 double gc_ratio;
00102
00103 template<class Search,Player T> class AttackHelper;
00104 template<class Search,Player T> class DefenseHelper;
00105 template<class Search,Player T> class CallSimulationAttack;
00106 template<class Search,Player T> class CallSimulationDefense;
00107 template<class Search,Player T> class CallSimulationDefenseDisproof;
00108
00109
00110 unsigned int blockByAttackBack;
00111 unsigned int blockByPass;
00112 unsigned int attack_node_count;
00113 unsigned int attack_node_under_attack_count;
00114 unsigned int attack_node_moves_count;
00115 unsigned int defense_node_count;
00116 unsigned int defense_node_under_attack_count;
00117 unsigned int defense_node_moves_count;
00118 unsigned int pass_count, pass_success_count;
00119 unsigned int pass_attack_count, pass_attack_success_count;
00120 unsigned int sibling_defense_count, sibling_defense_success_count;
00121 unsigned int sibling_attack_count, sibling_attack_success_count;
00122 unsigned int isshogi_defense_count, isshogi_defense_success_count;
00123 unsigned int isshogi_attack_count, isshogi_attack_success_count;
00124 unsigned int immediate_win, immediate_lose;
00125 unsigned int attack_back_count;
00126 unsigned int proof_without_inversion_count, proof_AND_count, disproof_by_inversion_count;
00127
00128 public:
00129 static const int NtesukiNotFound = -1;
00130 static const int ReadLimitReached = -2;
00131 static const int TableLimitReached = -3;
00132 private:
00133 static const unsigned int INITIAL_PROOF_LIMIT =
00134 ProofDisproof::PROOF_LIMIT / 4;
00135 static const unsigned int INITIAL_DISPROOF_LIMIT =
00136 ProofDisproof::DISPROOF_LIMIT / 4;
00137
00141 template <Player T>
00142 NtesukiResult attack(NtesukiRecord* record,
00143 const NtesukiRecord* oracle_attack,
00144 const NtesukiRecord* oracle_defense,
00145 unsigned int proofLimit,
00146 unsigned int disproofLimit,
00147 int pass_left,
00148 const Move last_move);
00149 template <Player T>
00150 void attackWithOrder(NtesukiRecord* record,
00151 const NtesukiRecord* oracle_attack,
00152 const NtesukiRecord* oracle_defense,
00153 unsigned int proofLimit,
00154 unsigned int disproofLimit,
00155 int pass_left,
00156 const Move last_move);
00157
00158 template <Player T>
00159 NtesukiMove* selectMoveAttack(NtesukiRecord* record,
00160 unsigned int& best_proof,
00161 unsigned int& sum_disproof,
00162 unsigned int& second_proof,
00163 unsigned int& best_disproof,
00164 unsigned int& step_cost,
00165 NtesukiMoveList& moves,
00166 const int pass_left);
00167
00171 template <Player T>
00172 NtesukiResult defense(NtesukiRecord* record,
00173 const NtesukiRecord* oracle_attack,
00174 const NtesukiRecord* oracle_defense,
00175 unsigned int proofLimit,
00176 unsigned int disproofLimit,
00177 int pass_left,
00178 const Move last_move);
00179
00180
00181 template <Player T>
00182 void defenseWithPlayer(NtesukiRecord* record,
00183 const NtesukiRecord* oracle_attack,
00184 const NtesukiRecord* oracle_defense,
00185 unsigned int proofLimit,
00186 unsigned int disproofLimit,
00187 int pass_left,
00188 const Move last_move);
00189
00190 template <Player T>
00191 NtesukiMove* selectMoveDefense(NtesukiRecord* record,
00192 unsigned int& best_disproof,
00193 unsigned int& sum_proof,
00194 unsigned int& second_disproof,
00195 unsigned int& best_proof,
00196 unsigned int& step_cost,
00197 NtesukiMoveList& moves,
00198 const int pass_left,
00199 const Move last_move);
00200
00208 template <Player T> void simulateSiblingsSuccess(NtesukiRecord *record,
00209 NtesukiRecord *record_best,
00210 int pass_left,
00211 unsigned int& success_count,
00212 unsigned int& total_count);
00213
00218 template <Player T> void simulateSiblingsFail(NtesukiRecord *record,
00219 NtesukiRecord *record_best,
00220 int pass_left,
00221 unsigned int& success_count,
00222 unsigned int& total_count);
00226 template <Player T> void handleNonAttack(NtesukiRecord *record,
00227 int pass_left);
00231 template <Player T> void handleTonshi(NtesukiRecord *record,
00232 int pass_left,
00233 const Move last_move);
00237 template <Player T> void handleInterpose(NtesukiRecord *record,
00238 int pass_left);
00239
00240 public:
00241
00242
00243
00244 NtesukiSearcher(State& state,
00245 NtesukiMoveGenerator *mg,
00246 unsigned int table_limit,
00247 volatile int *stop_flag,
00248 bool verbose,
00249 int maxPass = NtesukiRecord::SIZE,
00250 NtesukiRecord::IWScheme iwscheme = NtesukiRecord::pn_iw,
00251 NtesukiRecord::PSScheme psscheme = NtesukiRecord::no_ps,
00252 NtesukiRecord::ISScheme isscheme = NtesukiRecord::no_is,
00253 int tsumero_cost = 0,
00254 int tsumero_estimate = 0,
00255 double gc_ratio = 0.33);
00256 ~NtesukiSearcher();
00257
00258
00259 template <Player T> int search();
00260
00261 int searchSlow(Player attacker, int rnl = 160000)
00262 {
00263 read_node_limit = rnl;
00264 if (attacker == BLACK)
00265 return search<BLACK>();
00266 else
00267 return search<WHITE>();
00268 }
00269
00270 NtesukiTable& getTable();
00271 int getNodeCount() const { return node_count; }
00272 bool exceedReadNodeLimit() const {return node_count > read_node_limit;}
00273
00274 };
00275 }
00276 }
00277
00278 #endif
00279
00280
00281
00282