File: featureSet.h

package info (click to toggle)
gpsshogi 0.7.0-3.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 111,280 kB
  • sloc: cpp: 80,962; perl: 12,610; ruby: 3,929; javascript: 1,631; makefile: 1,202; sh: 473; tcl: 166; ansic: 67
file content (83 lines) | stat: -rw-r--r-- 3,221 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* featureSet.h
 */
#ifndef GPSSHOGI_MOVE_PROBABILITY_FEATURESET_H
#define GPSSHOGI_MOVE_PROBABILITY_FEATURESET_H

#include "indexList.h"
#include "stateInfo.h"
#include "osl/numEffectState.h"
#include "osl/container/moveLogProbVector.h"
#include <boost/ptr_container/ptr_vector.hpp>
#include <valarray>
namespace gpsshogi
{
  class Feature;
  typedef std::valarray<weight_t> valarray_t;
  typedef std::pair<double,Move> WeightedMove;
  typedef FixedCapacityVector<WeightedMove,Move::MaxUniqMoves> WeightedMoveVector;

  class FeatureSet
  {
    boost::ptr_vector<Feature> features;
    std::vector<int> offsets, light_features;
  protected:
    FeatureSet();
  public:
    ~FeatureSet();
    void pushBack(Feature *, bool light=false);
    void addFinished();
    int dimension() const { return offsets.back(); }
  public:
    weight_t match(const StateInfo&, Move, index_list_t&, const valarray_t& weights) const;    
    weight_t matchExp(const StateInfo&, Move, index_list_t&, const valarray_t& weights) const;    
    weight_t matchLight(const StateInfo&, Move, index_list_t&, const valarray_t& weights) const;    
    void generateLogProb(const StateInfo& state,
			 MoveLogProbVector& out, const valarray_t& weights) const;
    static weight_t accumulate(const index_list_t& features, const valarray_t& weights);
    static weight_t accumulateExp(const index_list_t& features, const valarray_t& weights);
    void save(const char *base_filename, const valarray_t& weights) const;
    bool load(const char *base_filename, valarray_t& weights) const;
    int logLikelihood(const StateInfo& state,
		      Move move, const valarray_t& weights) const;
    void showSummary(const valarray_t& weights) const;
    static weight_t sumToProbability(double sum) { return sum/(1.0+sum); }
    void analyze(const StateInfo& state, Move move, const valarray_t& weights) const;
    // for fine control
    weight_t generateRating(const StateInfo& state,
			WeightedMoveVector& out, const valarray_t& weights) const;
    static void ratingToLogProb(const WeightedMoveVector& rating,
				weight_t sum,
				MoveLogProbVector& out);
  };

  class StandardFeatureSet : public FeatureSet
  {
  public:
    StandardFeatureSet();
  };  

  struct PredictionModelLight 
  {
    static int logProbTakeBack(const StateInfo&, double sum, const valarray_t& w);
    static int logProbSeePlus(const StateInfo&, double sum, const valarray_t& w);
    static int predict(int offset, double sum, const valarray_t& w);

    static size_t dimension() { return 4*8; }
    static bool load(const char *filename, valarray_t&);
    static void save(const char *filename, const valarray_t&);
    static weight_t addGradientTakeBack(int progress8, Move next, Move target, double sum, const valarray_t& w,
				    valarray_t& partial);
    static weight_t addGradientSeePlus(const StateInfo&, Move next, Move target, double sum, const valarray_t& w,
				   valarray_t& partial);
  private:
    static weight_t addGradient(Move next, Move target, double sum, const valarray_t& w,
				valarray_t& partial, int offset);
  };
  
}

#endif /* GPSSHOGI_MOVE_PROBABILITY_FEATURESET_H */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End: