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 ;
2008-05-15 18:04:42 +04:00
/**abstract virtual class*/
2011-02-24 15:42:19 +03:00
class Optimizer
{
protected :
2011-11-11 15:40:59 +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 ) ;
}
2008-05-14 20:19:00 +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-02-24 15:42:19 +03:00
/**main function that perform an optimization*/
2008-05-15 23:09:01 +04:00
virtual statscore_t TrueRun ( Point & ) const = 0 ;
2008-05-14 15:00:59 +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 ;
2008-05-14 15:00:59 +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 ) ;
} ;
2008-05-14 15:00:59 +04:00
/**given a set of lambdas, get the total statistical score*/
2011-02-24 15:42:19 +03:00
statscore_t GetStatScore ( const Point & param ) const ;
2008-05-15 23:09:01 +04:00
vector < statscore_t > GetIncStatScore ( vector < unsigned > ref , vector < vector < pair < unsigned , unsigned > > > ) const ;
statscore_t LineOptimize ( const Point & start , const Point & direction , Point & best ) const ; //Get the optimal Lambda and the best score in a particular direction from a given Point
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-07-23 04:24:45 +04:00
/**default basic optimizer*/
2011-02-24 15:42:19 +03:00
class SimpleOptimizer : public Optimizer
{
2008-05-15 23:09:01 +04:00
private :
2011-07-23 04:24:45 +04:00
static float eps ;
2008-05-14 15:00:59 +04:00
public :
2011-07-23 04:24:45 +04:00
SimpleOptimizer ( unsigned dim , vector < unsigned > i2O , vector < parameter_t > start , unsigned int nrandom ) : Optimizer ( dim , i2O , start , nrandom ) { } ;
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-07-23 04:24:45 +04:00
/**optimizer with random directions*/
class RandomDirectionOptimizer : public Optimizer
2011-02-24 15:42:19 +03:00
{
2011-07-23 04:24:45 +04:00
private :
static float eps ;
2008-05-16 14:57:24 +04:00
public :
2011-07-23 04:24:45 +04:00
RandomDirectionOptimizer ( unsigned dim , vector < unsigned > i2O , vector < parameter_t > start , unsigned int nrandom ) : Optimizer ( dim , i2O , start , nrandom ) { } ;
2008-05-16 14:57:24 +04:00
virtual statscore_t TrueRun ( Point & ) const ;
} ;
2011-07-23 04:24:45 +04:00
/**dumb baseline optimizer: just picks a random point and quits*/
class RandomOptimizer : public Optimizer
{
public :
RandomOptimizer ( unsigned dim , vector < unsigned > i2O , vector < parameter_t > start , unsigned int nrandom ) : Optimizer ( dim , i2O , start , nrandom ) { } ;
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-02-24 15:42:19 +03:00
private :
2011-07-23 04:24:45 +04:00
enum OptType { POWELL = 0 , RANDOM_DIRECTION = 1 , RANDOM , NOPTIMIZER } ; //Add new optimizer here BEFORE NOPTIMZER
2008-05-16 14:57:24 +04:00
static OptType GetOType ( string ) ;
static vector < string > typenames ;
static void SetTypeNames ( ) ;
2011-02-24 15:42:19 +03:00
2008-05-16 14:57:24 +04:00
} ;
2011-11-12 02:59:50 +04:00
# endif // OPTIMIZER_H