group.h
Go to the documentation of this file.
00001 /* group.h
00002  */
00003 #ifndef _GROUP_H
00004 #define _GROUP_H
00005 
00006 #include "osl/rating/feature.h"
00007 #include "osl/rating/range.h"
00008 #include "osl/stl/vector.h"
00009 #include <boost/ptr_container/ptr_vector.hpp>
00010 
00011 namespace osl
00012 {
00013   namespace rating
00014   {
00016     class Group : public boost::ptr_vector<Feature> 
00017     {
00018     public:
00019       std::string group_name;
00020 
00021       Group(const std::string& name);
00022       Group(Feature *f) : group_name(f->name()) { push_back(f); }
00023       virtual ~Group();
00024       virtual void show(std::ostream&, int name_width, const range_t& range, 
00025                         const vector<double>& weights) const;
00026 
00028       virtual int findMatch(const NumEffectState& state, Move m, const RatingEnv& env) const;
00029       void showMinMax(std::ostream& os, int name_width, const range_t& range, 
00030                       const vector<double>& weights) const;
00031       void showAll(std::ostream& os, int name_width, const range_t& range, 
00032                    const vector<double>& weights) const;
00033       void showTopN(std::ostream& os, int name_width, const range_t& range, 
00034                     const vector<double>& weights, int n) const;
00035       void saveResult(const std::string& directory, const range_t& range, 
00036                       const vector<double>& weights) const;
00037       bool load(const std::string& directory, const range_t& range, 
00038                 vector<double>& weights) const;
00039       virtual bool effectiveInCheck() const { return (*this)[0].effectiveInCheck(); }
00040     };
00041 
00042     struct TakeBackGroup : public Group
00043     {
00044       TakeBackGroup() : Group("TakeBack")
00045       {
00046         push_back(new TakeBack());
00047         push_back(new TakeBack2());
00048       }
00049 #ifndef MINIMAL
00050       void show(std::ostream& os, int name_width, const range_t& range, 
00051                 const vector<double>& weights) const
00052       {
00053         showAll(os, name_width, range, weights);
00054       }
00055 #endif
00056       int findMatch(const NumEffectState&, Move move, const RatingEnv& env) const
00057       {
00058         const Square to = move.to();
00059         if (! env.history.hasLastMove() || env.history.lastMove().to() != to)
00060           return -1;
00061         if (! env.history.hasLastMove(2) || env.history.lastMove(2).to() != to)
00062           return 0;
00063         return 1;
00064       }
00065       bool effectiveInCheck() const { return true; }
00066     };
00067 
00068     struct CheckGroup : public Group
00069     {
00070       CheckGroup() : Group("Check")
00071       {
00072         for (int i=0; i<4; ++i)
00073           for (int p=0; p<8; ++p)       // progress8
00074             push_back(new Check(i));
00075       }
00076       void show(std::ostream& os, int name_width, const range_t& range, 
00077                 const vector<double>& weights) const
00078       {
00079         showAll(os, name_width, range, weights);
00080       }
00081       int findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
00082       {
00083         using namespace osl::move_classifier;
00084         const bool direct = PlayerMoveAdaptor<osl::move_classifier::DirectCheck>::isMember(state, move);
00085         const bool open = ConditionAdaptor<osl::move_classifier::OpenCheck>::isMember(state, move);
00086         int index = -1;
00087         if (direct && !open)
00088           index = Check::openLong(state, move);
00089         else if (open)
00090           index = direct + 2;
00091         const int progress8 = env.progress.value()/2;
00092         return index*8 + progress8;
00093       }
00094       bool effectiveInCheck() const { return true; }
00095     };
00096 
00097     class SendOffGroup : public Group
00098     {
00099     public:
00100       SendOffGroup() : Group("SendOff")
00101       {
00102         for (int p=0; p<8; ++p) // progress8
00103           push_back(new SendOff(0));
00104         for (int p=0; p<8; ++p) // progress8
00105           push_back(new SendOff(1));
00106       }
00107       void show(std::ostream& os, int name_width, const range_t& range, 
00108                 const vector<double>& weights) const
00109       {
00110         showAll(os, name_width, range, weights);
00111       }
00112       int findMatch(const NumEffectState&, Move move, const RatingEnv& env) const
00113       {
00114         if (! env.sendoffs.isMember(move.to()))
00115           return -1;
00116         const int progress8 = env.progress.value()/2;
00117         return (move.capturePtype() != PTYPE_EMPTY)*8 + progress8;
00118       }
00119     };
00120 
00121     struct BlockGroup : public Group
00122     {
00123       BlockGroup() : Group("Block")
00124       {
00125         for (int s=0; s<=3; ++s) {
00126           for (int o=0; o<=3; ++o) {
00127             push_back(new Block(s, o));
00128           }
00129         }
00130       }
00131       void show(std::ostream& os, int name_width, const range_t& range, 
00132                 const vector<double>& weights) const
00133       {
00134         showAll(os, name_width, range, weights);
00135       }
00136       int findMatch(const NumEffectState& state, Move move, const RatingEnv& ) const
00137       {
00138         const int index = Block::count(state, move.to(), state.turn())*4
00139           + Block::count(state, move.to(), alt(state.turn()));
00140         return index;
00141       }
00142       bool effectiveInCheck() const { return true; }
00143     };
00144 
00145     struct OpenGroup : public Group
00146     {
00147       OpenGroup() : Group("Open")
00148       {
00149         for (int i=0; i<16; ++i)
00150           push_back(new Open(i));
00151       }
00152       void show(std::ostream& os, int name_width, const range_t& range, 
00153                 const vector<double>& weights) const
00154       {
00155         showTopN(os, name_width, range, weights, 3);
00156       }
00157       int findMatch(const NumEffectState& state, Move move, const RatingEnv& ) const
00158       {
00159         const int index = Open::index(state, move);
00160         return index;
00161       }
00162       bool effectiveInCheck() const { return true; }
00163     };
00164 
00165     struct ChaseGroup : public Group
00166     {
00167       ChaseGroup();
00168       void show(std::ostream& os, int name_width, const range_t& range, 
00169                 const vector<double>& weights) const
00170       {
00171         showTopN(os, name_width, range, weights, 3);
00172       }
00173       int findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const;
00174     };
00175 
00176     struct KaranariGroup : public Group
00177     {
00178       KaranariGroup();
00179       void show(std::ostream& os, int name_width, const range_t& range, 
00180                 const vector<double>& weights) const
00181       {
00182         showAll(os, name_width, range, weights);
00183       }
00184       int findMatch(const NumEffectState& state, Move move, const RatingEnv&) const;
00185     };
00186 
00187     struct ImmediateAddSupportGroup : public Group
00188     {
00189       ImmediateAddSupportGroup();
00190       void show(std::ostream& os, int name_width, const range_t& range, 
00191                 const vector<double>& weights) const
00192       {
00193         showTopN(os, name_width, range, weights, 3);
00194       }
00195       int findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
00196       {
00197         const int index = ImmediateAddSupport::index(state, move, env);
00198         if (index < 0)
00199           return index; 
00200         const int progress8 = env.progress.value()/2;
00201         return index*8 + progress8;
00202       }
00203     };
00204 
00205     struct BadLanceGroup : public Group
00206     {
00207       BadLanceGroup() : Group("BadLance")
00208       {
00209         push_back(new BadLance(false));
00210         push_back(new BadLance(true));
00211       }
00212       void show(std::ostream& os, int name_width, const range_t& range, 
00213                 const vector<double>& weights) const
00214       {
00215         showAll(os, name_width, range, weights);
00216       }
00217       int findMatch(const NumEffectState& state, Move move, const RatingEnv&) const
00218       {
00219         const Square front = Board_Table.nextSquare(move.player(), move.to(), U);
00220         if (! BadLance::basicMatch(state, move, front))
00221           return -1;
00222         const int index = state.hasEffectAt(alt(move.player()), front);
00223         return index;
00224       }
00225     };
00226 
00227     struct PawnAttackGroup : public Group
00228     {
00229       PawnAttackGroup() : Group("PawnAttack")
00230       {
00231         for (int p=0; p<8; ++p) // progress8
00232           push_back(new PawnAttack());
00233       }
00234       void show(std::ostream& os, int name_width, const range_t& range, 
00235                 const vector<double>& weights) const
00236       {
00237         showAll(os, name_width, range, weights);
00238       }
00239       int findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
00240       {
00241         if (! (*this)[0].match(state, move, env))
00242           return -1;
00243         const int progress8 = env.progress.value()/2;
00244         return progress8;
00245       }
00246     };
00247   }
00248 }
00249 
00250 #endif /* _GROUP_H */
00251 // ;;; Local Variables:
00252 // ;;; mode:c++
00253 // ;;; c-basic-offset:2
00254 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines