2008-05-14 15:32:17 +04:00
|
|
|
#include "Point.h"
|
|
|
|
#include<cmath>
|
2008-05-14 18:25:07 +04:00
|
|
|
#include <cassert>
|
2008-05-14 15:32:17 +04:00
|
|
|
using namespace std;
|
2008-05-15 14:57:20 +04:00
|
|
|
|
|
|
|
|
|
|
|
vector<unsigned> Point::optindices;
|
|
|
|
|
|
|
|
unsigned Point::dim=0;
|
|
|
|
|
|
|
|
map<unsigned,lambda> Point::fixedweights;
|
|
|
|
|
|
|
|
unsigned Point::pdim=0;
|
2008-05-15 18:17:34 +04:00
|
|
|
unsigned Point::ncall=0;
|
2008-05-15 14:57:20 +04:00
|
|
|
|
|
|
|
void Point::Randomize(const vector<lambda>& min,const vector<lambda>& max){
|
2008-05-14 15:32:17 +04:00
|
|
|
for (int i=0; i<size(); i++)
|
|
|
|
operator[](i)= min[i] + (float)random()/RAND_MAX * (max[i]-min[i]);
|
|
|
|
}
|
|
|
|
|
2008-05-15 14:57:20 +04:00
|
|
|
void Point::Normalize(){
|
2008-05-14 15:32:17 +04:00
|
|
|
lambda norm=0.0;
|
|
|
|
for (int i=0; i<size(); i++)
|
|
|
|
norm+= operator[](i)*operator[](i);
|
|
|
|
if(norm!=0.0){
|
|
|
|
norm=sqrt(norm);
|
|
|
|
for (int i=0; i<size(); i++)
|
|
|
|
operator[](i)/=norm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-15 14:57:20 +04:00
|
|
|
double Point::operator*(const FeatureStats& F)const{
|
2008-05-15 18:17:34 +04:00
|
|
|
ncall++;//to track performance
|
2008-05-14 15:32:17 +04:00
|
|
|
double prod=0.0;
|
2008-05-15 14:57:20 +04:00
|
|
|
if(OptimizeAll())
|
|
|
|
for (unsigned i=0; i<size(); i++)
|
|
|
|
prod+= operator[](i)*F.get(i);
|
|
|
|
else{
|
|
|
|
for (unsigned i=0; i<size(); i++)
|
|
|
|
prod+= operator[](i)*F.get(optindices[i]);
|
|
|
|
for(map<unsigned,float >::iterator it=fixedweights.begin();it!=fixedweights.end();it++)
|
|
|
|
prod+=it->second*F.get(it->first);
|
|
|
|
}
|
|
|
|
return prod;
|
2008-05-14 15:32:17 +04:00
|
|
|
}
|
2008-05-14 18:25:07 +04:00
|
|
|
Point Point::operator+(const Point& p2)const{
|
|
|
|
assert(p2.size()==size());
|
|
|
|
Point Res(*this);
|
|
|
|
for(unsigned i=0;i<size();i++)
|
|
|
|
Res[i]+=p2[i];
|
|
|
|
Res.score=numeric_limits<statscore>::max();
|
|
|
|
return Res;
|
|
|
|
};
|
|
|
|
|
2008-05-15 14:57:20 +04:00
|
|
|
Point Point::operator*(float l)const{
|
2008-05-14 18:25:07 +04:00
|
|
|
Point Res(*this);
|
|
|
|
for(unsigned i=0;i<size();i++)
|
|
|
|
Res[i]*=l;
|
|
|
|
Res.score=numeric_limits<statscore>::max();
|
|
|
|
return Res;
|
|
|
|
};
|
2008-05-15 14:57:20 +04:00
|
|
|
|
2008-05-15 18:04:42 +04:00
|
|
|
ostream& operator<<(ostream& o,const Point& P){
|
|
|
|
vector<lambda> w=P.GetAllWeights();
|
|
|
|
for(int i=0;i<Point::pdim;i++)
|
|
|
|
o<<w[i]<<' ';
|
|
|
|
o<<endl;
|
|
|
|
return o;
|
|
|
|
};
|
2008-05-15 14:57:20 +04:00
|
|
|
|
|
|
|
vector<lambda> Point::GetAllWeights()const{
|
|
|
|
vector<lambda> w;
|
|
|
|
if(OptimizeAll()){
|
|
|
|
w=*this;
|
|
|
|
}else{
|
|
|
|
w.resize(pdim);
|
|
|
|
for (int i=0; i<size(); i++)
|
|
|
|
w[optindices[i]]=operator[](i);
|
|
|
|
for(map<unsigned,float >::iterator it=fixedweights.begin();it!=fixedweights.end();it++)
|
|
|
|
w[it->first]=it->second;
|
|
|
|
}
|
|
|
|
return w;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|