2008-05-14 15:00:59 +04:00
|
|
|
#ifndef OPTIMIZER_H
|
|
|
|
#define OPTIMIZER_H
|
2011-11-12 02:59:50 +04:00
|
|
|
|
2008-05-15 20:03:49 +04:00
|
|
|
#include <vector>
|
2008-05-14 15:00:59 +04:00
|
|
|
#include "FeatureStats.h"
|
|
|
|
#include "FeatureData.h"
|
|
|
|
#include "FeatureArray.h"
|
2008-05-14 16:49:45 +04:00
|
|
|
#include "Scorer.h"
|
|
|
|
#include "Point.h"
|
2008-05-15 23:09:01 +04:00
|
|
|
#include "Types.h"
|
2008-05-14 15:00:59 +04:00
|
|
|
|
|
|
|
|
|
|
|
typedef float featurescore;
|
|
|
|
|
|
|
|
using namespace std;
|
2011-11-12 03:58:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract optimizer class.
|
|
|
|
*/
|
2011-02-24 15:42:19 +03:00
|
|
|
class Optimizer
|
|
|
|
{
|
|
|
|
protected:
|
2011-11-12 03:58:23 +04:00
|
|
|
Scorer *scorer; // no accessor for them only child can use them
|
|
|
|
FeatureData *FData; // no accessor for them only child can use them
|
2011-07-23 04:24:45 +04:00
|
|
|
unsigned int number_of_random_directions;
|
2011-11-11 15:40:59 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
public:
|
2011-07-23 04:24:45 +04:00
|
|
|
Optimizer(unsigned Pd,vector<unsigned> i2O,vector<parameter_t> start,unsigned int nrandom);
|
2011-11-11 15:40:59 +04:00
|
|
|
void SetScorer(Scorer *_scorer);
|
|
|
|
void SetFData(FeatureData *_FData);
|
2008-05-15 14:57:20 +04:00
|
|
|
virtual ~Optimizer();
|
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
unsigned size()const {
|
|
|
|
return (FData?FData->size():0);
|
|
|
|
}
|
2011-11-12 03:58:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic wrapper around TrueRun to check a few things. Non virtual.
|
|
|
|
*/
|
2008-05-15 23:09:01 +04:00
|
|
|
statscore_t Run(Point&)const;
|
2011-11-12 03:58:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Main function that performs an optimization.
|
|
|
|
*/
|
2008-05-15 23:09:01 +04:00
|
|
|
virtual statscore_t TrueRun(Point&)const=0;
|
2011-11-12 03:58:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a set of lambdas, get the nbest for each sentence.
|
|
|
|
*/
|
2008-05-15 14:57:20 +04:00
|
|
|
void Get1bests(const Point& param,vector<unsigned>& bests)const;
|
2011-11-12 03:58:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a set of nbests, get the Statistical score.
|
|
|
|
*/
|
2011-02-24 15:42:19 +03:00
|
|
|
statscore_t GetStatScore(const vector<unsigned>& nbests)const {
|
|
|
|
return scorer->score(nbests);
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
statscore_t GetStatScore(const Point& param)const;
|
2011-11-12 03:58:23 +04:00
|
|
|
|
2008-05-15 23:09:01 +04:00
|
|
|
vector<statscore_t > GetIncStatScore(vector<unsigned> ref,vector<vector <pair<unsigned,unsigned> > >)const;
|
2011-11-12 03:58:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the optimal Lambda and the best score in a particular direction from a given Point.
|
|
|
|
*/
|
|
|
|
statscore_t LineOptimize(const Point& start,const Point& direction,Point& best)const;
|
2008-05-14 18:25:07 +04:00
|
|
|
};
|
2008-05-14 15:00:59 +04:00
|
|
|
|
2008-05-15 14:57:20 +04:00
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
/**
|
|
|
|
* Default basic optimizer.
|
|
|
|
* This class implements Powell's method.
|
|
|
|
*/
|
2011-02-24 15:42:19 +03:00
|
|
|
class SimpleOptimizer: public Optimizer
|
|
|
|
{
|
2008-05-15 23:09:01 +04:00
|
|
|
private:
|
2011-11-12 05:16:31 +04:00
|
|
|
const float kEPS;
|
2008-05-14 15:00:59 +04:00
|
|
|
public:
|
2011-11-12 05:16:31 +04:00
|
|
|
SimpleOptimizer(unsigned dim,vector<unsigned> i2O,vector<parameter_t> start,unsigned int nrandom): Optimizer(dim, i2O, start,nrandom), kEPS(0.0001) {}
|
2008-05-15 23:09:01 +04:00
|
|
|
virtual statscore_t TrueRun(Point&)const;
|
2008-05-14 18:25:07 +04:00
|
|
|
};
|
2008-05-14 15:00:59 +04:00
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
/**
|
|
|
|
* An optimizer with random directions.
|
|
|
|
*/
|
2011-07-23 04:24:45 +04:00
|
|
|
class RandomDirectionOptimizer: public Optimizer
|
2011-02-24 15:42:19 +03:00
|
|
|
{
|
2011-07-23 04:24:45 +04:00
|
|
|
private:
|
2011-11-12 05:16:31 +04:00
|
|
|
const float kEPS;
|
2008-05-16 14:57:24 +04:00
|
|
|
public:
|
2011-11-12 05:16:31 +04:00
|
|
|
RandomDirectionOptimizer(unsigned dim,vector<unsigned> i2O,vector<parameter_t> start,unsigned int nrandom): Optimizer(dim,i2O,start,nrandom), kEPS(0.0001) {}
|
2008-05-16 14:57:24 +04:00
|
|
|
virtual statscore_t TrueRun(Point&)const;
|
|
|
|
};
|
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
/**
|
|
|
|
* Dumb baseline optimizer: just picks a random point and quits.
|
|
|
|
*/
|
2011-07-23 04:24:45 +04:00
|
|
|
class RandomOptimizer: public Optimizer
|
|
|
|
{
|
|
|
|
public:
|
2011-11-12 04:40:01 +04:00
|
|
|
RandomOptimizer(unsigned dim,vector<unsigned> i2O,vector<parameter_t> start, unsigned int nrandom): Optimizer(dim,i2O,start,nrandom) {}
|
2011-07-23 04:24:45 +04:00
|
|
|
virtual statscore_t TrueRun(Point&)const;
|
|
|
|
};
|
2008-05-16 14:57:24 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
class OptimizerFactory
|
|
|
|
{
|
|
|
|
public:
|
2008-05-16 14:57:24 +04:00
|
|
|
// unsigned dim;
|
|
|
|
//Point Start;
|
|
|
|
static vector<string> GetTypeNames();
|
2011-07-23 04:24:45 +04:00
|
|
|
static Optimizer* BuildOptimizer(unsigned dim,vector<unsigned>tooptimize,vector<parameter_t> start,string type,unsigned int nrandom);
|
2011-11-12 04:56:21 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
private:
|
2011-11-12 04:56:21 +04:00
|
|
|
OptimizerFactory() {}
|
|
|
|
~OptimizerFactory() {}
|
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
// Add new optimizer here BEFORE NOPTIMZER
|
|
|
|
enum OptType {POWELL=0,RANDOM_DIRECTION=1,RANDOM,NOPTIMIZER};
|
2008-05-16 14:57:24 +04:00
|
|
|
static OptType GetOType(string);
|
|
|
|
static vector<string> typenames;
|
|
|
|
static void SetTypeNames();
|
|
|
|
};
|
|
|
|
|
2011-11-12 02:59:50 +04:00
|
|
|
#endif // OPTIMIZER_H
|