2008-05-14 15:32:17 +04:00
|
|
|
#include "Point.h"
|
2011-11-12 11:39:57 +04:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstdlib>
|
2008-05-14 18:25:07 +04:00
|
|
|
#include <cassert>
|
2008-05-15 14:57:20 +04:00
|
|
|
|
2011-11-12 11:39:57 +04:00
|
|
|
using namespace std;
|
2008-05-15 14:57:20 +04:00
|
|
|
|
|
|
|
vector<unsigned> Point::optindices;
|
|
|
|
|
2011-11-12 11:39:57 +04:00
|
|
|
unsigned Point::dim = 0;
|
2008-05-15 14:57:20 +04:00
|
|
|
|
2008-05-15 23:09:01 +04:00
|
|
|
map<unsigned,statscore_t> Point::fixedweights;
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2011-11-12 11:39:57 +04:00
|
|
|
unsigned Point::pdim = 0;
|
|
|
|
unsigned Point::ncall = 0;
|
2008-05-15 14:57:20 +04:00
|
|
|
|
2011-07-04 01:01:16 +04:00
|
|
|
vector<parameter_t> Point::m_min;
|
|
|
|
vector<parameter_t> Point::m_max;
|
|
|
|
|
2008-05-16 14:57:24 +04:00
|
|
|
//Can initialize from a vector of dim or pdim
|
2011-07-04 01:01:16 +04:00
|
|
|
Point::Point(const vector<parameter_t>& init,
|
2011-11-12 11:39:57 +04:00
|
|
|
const vector<parameter_t>& min,
|
|
|
|
const vector<parameter_t>& max)
|
|
|
|
: vector<parameter_t>(Point::dim), score_(0.0f)
|
2011-02-24 15:42:19 +03:00
|
|
|
{
|
2011-07-04 01:01:16 +04:00
|
|
|
m_min.resize(Point::dim);
|
|
|
|
m_max.resize(Point::dim);
|
2011-02-24 15:42:19 +03:00
|
|
|
if(init.size()==dim) {
|
2011-07-04 01:01:16 +04:00
|
|
|
for (unsigned int i=0; i<Point::dim; i++) {
|
2008-05-16 14:57:24 +04:00
|
|
|
operator[](i)=init[i];
|
2011-07-04 01:01:16 +04:00
|
|
|
m_min[i] = min[i];
|
|
|
|
m_max[i] = max[i];
|
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
} else {
|
2008-05-16 14:57:24 +04:00
|
|
|
assert(init.size()==pdim);
|
2011-07-04 01:01:16 +04:00
|
|
|
for (unsigned int i=0; i<Point::dim; i++) {
|
2008-05-16 14:57:24 +04:00
|
|
|
operator[](i)=init[optindices[i]];
|
2011-07-04 01:01:16 +04:00
|
|
|
m_min[i] = min[optindices[i]];
|
|
|
|
m_max[i] = max[optindices[i]];
|
|
|
|
}
|
2008-05-16 14:57:24 +04:00
|
|
|
}
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2008-05-16 14:57:24 +04:00
|
|
|
|
2011-11-12 11:39:57 +04:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
2008-05-16 14:57:24 +04:00
|
|
|
|
2011-11-12 11:39:57 +04:00
|
|
|
double Point::operator*(const FeatureStats& F) const
|
2011-02-24 15:42:19 +03:00
|
|
|
{
|
2011-11-12 03:58:23 +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);
|
2011-02-24 15:42:19 +03:00
|
|
|
else {
|
2008-05-15 14:57:20 +04:00
|
|
|
for (unsigned i=0; i<size(); i++)
|
|
|
|
prod+= operator[](i)*F.get(optindices[i]);
|
2011-02-24 15:42:19 +03:00
|
|
|
for(map<unsigned,float >::iterator it=fixedweights.begin(); it!=fixedweights.end(); it++)
|
2008-05-15 14:57:20 +04:00
|
|
|
prod+=it->second*F.get(it->first);
|
|
|
|
}
|
|
|
|
return prod;
|
2008-05-14 15:32:17 +04:00
|
|
|
}
|
2011-11-12 04:40:01 +04:00
|
|
|
|
2011-11-12 11:39:57 +04:00
|
|
|
Point Point::operator+(const Point& p2) const
|
2011-02-24 15:42:19 +03:00
|
|
|
{
|
2011-11-12 11:39:57 +04:00
|
|
|
assert(p2.size() == size());
|
2008-05-14 18:25:07 +04:00
|
|
|
Point Res(*this);
|
2011-11-12 11:39:57 +04:00
|
|
|
for (unsigned i = 0; i < size(); i++) {
|
|
|
|
Res[i] += p2[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
Res.score_ = numeric_limits<statscore_t>::max();
|
2008-05-14 18:25:07 +04:00
|
|
|
return Res;
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2008-05-14 18:25:07 +04:00
|
|
|
|
2011-09-15 21:45:35 +04:00
|
|
|
void Point::operator+=(const Point& p2)
|
|
|
|
{
|
2011-11-12 11:39:57 +04:00
|
|
|
assert(p2.size() == size());
|
|
|
|
for (unsigned i = 0; i < size(); i++) {
|
|
|
|
operator[](i) += p2[i];
|
|
|
|
}
|
|
|
|
score_ = numeric_limits<statscore_t>::max();
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2011-09-15 21:45:35 +04:00
|
|
|
|
2011-11-12 11:39:57 +04:00
|
|
|
Point Point::operator*(float l) const
|
2011-02-24 15:42:19 +03:00
|
|
|
{
|
2008-05-14 18:25:07 +04:00
|
|
|
Point Res(*this);
|
2011-11-12 11:39:57 +04:00
|
|
|
for (unsigned i = 0; i < size(); i++) {
|
|
|
|
Res[i] *= l;
|
|
|
|
}
|
|
|
|
Res.score_ = numeric_limits<statscore_t>::max();
|
2008-05-14 18:25:07 +04:00
|
|
|
return Res;
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2008-05-15 14:57:20 +04:00
|
|
|
|
2011-11-12 11:39:57 +04:00
|
|
|
ostream& operator<<(ostream& o, const Point& P)
|
2011-02-24 15:42:19 +03:00
|
|
|
{
|
2011-11-12 11:39:57 +04:00
|
|
|
vector<parameter_t> w = P.GetAllWeights();
|
|
|
|
for (unsigned int i = 0; i < Point::pdim; i++) {
|
2011-02-24 15:42:19 +03:00
|
|
|
o << w[i] << " ";
|
2011-11-12 11:39:57 +04:00
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
return o;
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2008-05-15 14:57:20 +04:00
|
|
|
|
2011-11-12 11:39:57 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
vector<parameter_t> Point::GetAllWeights()const
|
|
|
|
{
|
2008-05-16 14:57:24 +04:00
|
|
|
vector<parameter_t> w;
|
2011-02-24 15:42:19 +03:00
|
|
|
if(OptimizeAll()) {
|
2008-05-15 14:57:20 +04:00
|
|
|
w=*this;
|
2011-02-24 15:42:19 +03:00
|
|
|
} else {
|
2008-05-15 14:57:20 +04:00
|
|
|
w.resize(pdim);
|
2008-05-23 15:48:16 +04:00
|
|
|
for (unsigned int i=0; i<size(); i++)
|
2008-05-15 14:57:20 +04:00
|
|
|
w[optindices[i]]=operator[](i);
|
2011-02-24 15:42:19 +03:00
|
|
|
for(map<unsigned,float >::iterator it=fixedweights.begin(); it!=fixedweights.end(); it++)
|
|
|
|
w[it->first]=it->second;
|
2008-05-15 14:57:20 +04:00
|
|
|
}
|
|
|
|
return w;
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|