diff options
author | Tetsuo Kiso <tetsuo-s@is.naist.jp> | 2011-11-12 11:39:57 +0400 |
---|---|---|
committer | Tetsuo Kiso <tetsuo-s@is.naist.jp> | 2011-11-12 11:39:57 +0400 |
commit | df0874df89429dc940df75b125a02362e3b1b539 (patch) | |
tree | bb4717e3b488c8be15b813395514dfa07a9921d1 /mert | |
parent | fdcd148cac32bca8993e5e8b10e56f86f5f7e522 (diff) |
Make Point::score a private by defining accessor/mutator.
Diffstat (limited to 'mert')
-rw-r--r-- | mert/Optimizer.cpp | 39 | ||||
-rw-r--r-- | mert/Point.cpp | 129 | ||||
-rw-r--r-- | mert/Point.h | 21 |
3 files changed, 97 insertions, 92 deletions
diff --git a/mert/Optimizer.cpp b/mert/Optimizer.cpp index 52aecc4b1..fb762cee1 100644 --- a/mert/Optimizer.cpp +++ b/mert/Optimizer.cpp @@ -297,7 +297,7 @@ statscore_t Optimizer::LineOptimize(const Point& origin,const Point& direction,P // cerr<<"end Lineopt, bestx="<<bestx<<endl; } bestpoint=direction*bestx+origin; - bestpoint.score=bestscore; + bestpoint.SetScore(bestscore); return bestscore; } @@ -338,16 +338,19 @@ statscore_t Optimizer::Run(Point& P)const exit(2); } - statscore_t score=GetStatScore(P); - P.score=score; + P.SetScore(GetStatScore(P)); - if(verboselevel()>2) - cerr<<"Starting point: "<< P << " => "<< P.score << endl; - statscore_t s=TrueRun(P); - P.score=s;//just in case its not done in TrueRun - if (verboselevel()>2) - cerr<<"Ending point: "<< P <<" => "<< s << endl; - return s; + if (verboselevel () > 2) { + cerr << "Starting point: " << P << " => " << P.GetScore() << endl; + } + statscore_t score = TrueRun(P); + + // just in case its not done in TrueRun + P.SetScore(score); + if (verboselevel() > 2) { + cerr << "Ending point: " << P << " => " << score << endl; + } + return score; } @@ -366,13 +369,13 @@ statscore_t SimpleOptimizer::TrueRun(Point& P)const { statscore_t prevscore=0; statscore_t bestscore=MIN_FLOAT; - Point best; + Point best; // If P is already defined and provides a score, // We must improve over this score. - if(P.score>bestscore) { - bestscore=P.score; - best=P; + if(P.GetScore() > bestscore) { + bestscore = P.GetScore(); + best = P; } int nrun=0; @@ -427,7 +430,7 @@ statscore_t SimpleOptimizer::TrueRun(Point& P)const statscore_t RandomDirectionOptimizer::TrueRun(Point& P)const { - statscore_t prevscore=P.score; + statscore_t prevscore = P.GetScore(); // do specified number of random direction optimizations unsigned int nrun = 0; @@ -458,11 +461,11 @@ statscore_t RandomDirectionOptimizer::TrueRun(Point& P)const } -statscore_t RandomOptimizer::TrueRun(Point& P)const +statscore_t RandomOptimizer::TrueRun(Point& P) const { P.Randomize(); - statscore_t score=GetStatScore(P); - P.score=score; + statscore_t score = GetStatScore(P); + P.SetScore(score); return score; } diff --git a/mert/Point.cpp b/mert/Point.cpp index 590225358..58f1e4fb1 100644 --- a/mert/Point.cpp +++ b/mert/Point.cpp @@ -1,60 +1,27 @@ #include "Point.h" -#include<cmath> -#include<cstdlib> +#include <cmath> +#include <cstdlib> #include <cassert> -using namespace std; +using namespace std; vector<unsigned> Point::optindices; -unsigned Point::dim=0; +unsigned Point::dim = 0; map<unsigned,statscore_t> Point::fixedweights; -unsigned Point::pdim=0; -unsigned Point::ncall=0; +unsigned Point::pdim = 0; +unsigned Point::ncall = 0; vector<parameter_t> Point::m_min; vector<parameter_t> Point::m_max; -void Point::Randomize() -{ - assert(m_min.size()==Point::dim); - assert(m_max.size()==Point::dim); - for (unsigned int i=0; i<size(); i++) - operator[](i)= m_min[i] - + (float)random()/(float)RAND_MAX * (float)(m_max[i]-m_min[i]); -} - -void Point::NormalizeL2() -{ - parameter_t norm=0.0; - for (unsigned int i=0; i<size(); i++) - norm+= operator[](i)*operator[](i); - if(norm!=0.0) { - norm=sqrt(norm); - for (unsigned int i=0; i<size(); i++) - operator[](i)/=norm; - } -} - - -void Point::NormalizeL1() -{ - parameter_t norm=0.0; - for (unsigned int i=0; i<size(); i++) - norm+= abs(operator[](i)); - if(norm!=0.0) { - for (unsigned int i=0; i<size(); i++) - operator[](i)/=norm; - } -} - //Can initialize from a vector of dim or pdim Point::Point(const vector<parameter_t>& init, - const vector<parameter_t>& min, - const vector<parameter_t>& max - ):vector<parameter_t>(Point::dim) + const vector<parameter_t>& min, + const vector<parameter_t>& max) + : vector<parameter_t>(Point::dim), score_(0.0f) { m_min.resize(Point::dim); m_max.resize(Point::dim); @@ -74,8 +41,17 @@ Point::Point(const vector<parameter_t>& init, } } +void Point::Randomize() +{ + assert(m_min.size()==Point::dim); + assert(m_max.size()==Point::dim); + for (unsigned int i=0; i<size(); i++) { + operator[](i) = m_min[i] + + (float)random()/(float)RAND_MAX * (float)(m_max[i]-m_min[i]); + } +} -double Point::operator*(const FeatureStats& F)const +double Point::operator*(const FeatureStats& F) const { ncall++; // to track performance double prod=0.0; @@ -91,43 +67,71 @@ double Point::operator*(const FeatureStats& F)const return prod; } -Point Point::operator+(const Point& p2)const +Point Point::operator+(const Point& p2) const { - assert(p2.size()==size()); + assert(p2.size() == size()); Point Res(*this); - for(unsigned i=0; i<size(); i++) - Res[i]+=p2[i]; - Res.score=numeric_limits<statscore_t>::max(); + for (unsigned i = 0; i < size(); i++) { + Res[i] += p2[i]; + } + + Res.score_ = numeric_limits<statscore_t>::max(); return Res; } void Point::operator+=(const Point& p2) { - assert(p2.size()==size()); - for(unsigned i=0; i<size(); i++) - operator[](i)+=p2[i]; - score=numeric_limits<statscore_t>::max(); + assert(p2.size() == size()); + for (unsigned i = 0; i < size(); i++) { + operator[](i) += p2[i]; + } + score_ = numeric_limits<statscore_t>::max(); } -Point Point::operator*(float l)const +Point Point::operator*(float l) const { Point Res(*this); - for(unsigned i=0; i<size(); i++) - Res[i]*=l; - Res.score=numeric_limits<statscore_t>::max(); + for (unsigned i = 0; i < size(); i++) { + Res[i] *= l; + } + Res.score_ = numeric_limits<statscore_t>::max(); return Res; } -ostream& operator<<(ostream& o,const Point& P) +ostream& operator<<(ostream& o, const Point& P) { - vector<parameter_t> w=P.GetAllWeights(); -// o << "[" << Point::pdim << "] "; - for(unsigned int i=0; i<Point::pdim; i++) + vector<parameter_t> w = P.GetAllWeights(); + for (unsigned int i = 0; i < Point::pdim; i++) { o << w[i] << " "; -// o << "=> " << P.GetScore(); + } return o; } +void Point::NormalizeL2() +{ + parameter_t norm=0.0; + for (unsigned int i=0; i<size(); i++) + norm+= operator[](i)*operator[](i); + if(norm!=0.0) { + norm=sqrt(norm); + for (unsigned int i=0; i<size(); i++) + operator[](i)/=norm; + } +} + + +void Point::NormalizeL1() +{ + parameter_t norm=0.0; + for (unsigned int i=0; i<size(); i++) + norm+= abs(operator[](i)); + if(norm!=0.0) { + for (unsigned int i=0; i<size(); i++) + operator[](i)/=norm; + } +} + + vector<parameter_t> Point::GetAllWeights()const { vector<parameter_t> w; @@ -142,6 +146,3 @@ vector<parameter_t> Point::GetAllWeights()const } return w; } - - - diff --git a/mert/Point.h b/mert/Point.h index 4e06bfd26..a836a94b1 100644 --- a/mert/Point.h +++ b/mert/Point.h @@ -4,8 +4,6 @@ #include <vector> #include "Types.h" #include "FeatureStats.h" -#include <cassert> - class Optimizer; @@ -45,6 +43,8 @@ private: static vector<parameter_t> m_min; static vector<parameter_t> m_max; + statscore_t score_; + public: static unsigned int getdim() { return dim; @@ -62,8 +62,6 @@ public: return fixedweights.empty(); } - statscore_t score; - Point() : vector<parameter_t>(dim) {} Point(const vector<parameter_t>& init, const vector<parameter_t>& min, @@ -71,10 +69,10 @@ public: void Randomize(); // Compute the feature function - double operator*(const FeatureStats&)const; - Point operator+(const Point&)const; + double operator*(const FeatureStats&) const; + Point operator+(const Point&) const; void operator+=(const Point&); - Point operator*(float)const; + Point operator*(float) const; /** * Write the Whole featureweight to a stream (ie pdim float). @@ -89,10 +87,13 @@ public: * Return a vector of size pdim where all weights have been * put (including fixed ones). */ - vector<parameter_t> GetAllWeights()const; - statscore_t GetScore()const { - return score; + vector<parameter_t> GetAllWeights() const; + + statscore_t GetScore() const { + return score_; } + + void SetScore(statscore_t score) { score_ = score; } }; #endif // POINT_H |