2008-05-14 15:32:17 +04:00
|
|
|
#include "Point.h"
|
2011-11-14 10:15:30 +04:00
|
|
|
|
2011-11-12 11:39:57 +04:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstdlib>
|
2013-11-18 22:13:10 +04:00
|
|
|
#include "util/exception.hh"
|
2011-11-14 10:15:30 +04:00
|
|
|
#include "FeatureStats.h"
|
2012-04-12 00:19:11 +04:00
|
|
|
#include "Optimizer.h"
|
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
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
namespace MosesTuning
|
|
|
|
{
|
|
|
|
|
2012-03-10 12:12:34 +04:00
|
|
|
vector<unsigned> Point::m_opt_indices;
|
2008-05-15 14:57:20 +04:00
|
|
|
|
2012-03-10 12:12:34 +04:00
|
|
|
unsigned Point::m_dim = 0;
|
2008-05-15 14:57:20 +04:00
|
|
|
|
2012-03-10 12:12:34 +04:00
|
|
|
map<unsigned,statscore_t> Point::m_fixed_weights;
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2012-03-10 12:12:34 +04:00
|
|
|
unsigned Point::m_pdim = 0;
|
|
|
|
unsigned Point::m_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;
|
|
|
|
|
2012-03-10 12:12:34 +04:00
|
|
|
Point::Point() : vector<parameter_t>(m_dim), m_score(0.0) {}
|
2011-11-12 11:50:18 +04:00
|
|
|
|
2012-03-10 12:12:34 +04:00
|
|
|
//Can initialize from a vector of dim or m_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)
|
2013-05-29 21:16:15 +04:00
|
|
|
: vector<parameter_t>(Point::m_dim), m_score(0.0)
|
2011-02-24 15:42:19 +03:00
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
m_min.resize(Point::m_dim);
|
|
|
|
m_max.resize(Point::m_dim);
|
|
|
|
if (init.size() == m_dim) {
|
|
|
|
for (unsigned int i = 0; i < Point::m_dim; i++) {
|
|
|
|
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 {
|
2014-01-15 19:49:57 +04:00
|
|
|
UTIL_THROW_IF(init.size() != m_pdim, util::Exception, "Error");
|
|
|
|
UTIL_THROW_IF(m_opt_indices.size() != Point::m_dim, util::Exception, "Error");
|
2012-03-10 12:12:34 +04:00
|
|
|
for (unsigned int i = 0; i < Point::m_dim; i++) {
|
|
|
|
operator[](i) = init[m_opt_indices[i]];
|
|
|
|
m_min[i] = min[m_opt_indices[i]];
|
|
|
|
m_max[i] = max[m_opt_indices[i]];
|
2011-07-04 01:01:16 +04:00
|
|
|
}
|
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:50:18 +04:00
|
|
|
Point::~Point() {}
|
|
|
|
|
2011-11-12 11:39:57 +04:00
|
|
|
void Point::Randomize()
|
|
|
|
{
|
2013-11-18 22:13:10 +04:00
|
|
|
UTIL_THROW_IF(m_min.size() != Point::m_dim, util::Exception, "Error");
|
|
|
|
UTIL_THROW_IF(m_max.size() != Point::m_dim, util::Exception, "Error");
|
|
|
|
|
2012-03-10 12:12:34 +04:00
|
|
|
for (unsigned int i = 0; i < size(); i++) {
|
2011-11-12 11:39:57 +04:00
|
|
|
operator[](i) = m_min[i] +
|
2012-02-01 12:17:58 +04:00
|
|
|
static_cast<float>(random()) / static_cast<float>(RAND_MAX) * (m_max[i] - m_min[i]);
|
2011-11-12 11:39:57 +04:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
m_ncall++; // to track performance
|
|
|
|
double prod = 0.0;
|
|
|
|
if (OptimizeAll())
|
2008-05-15 14:57:20 +04:00
|
|
|
for (unsigned i=0; i<size(); i++)
|
2012-03-10 12:12:34 +04:00
|
|
|
prod += operator[](i) * F.get(i);
|
2011-02-24 15:42:19 +03:00
|
|
|
else {
|
2012-03-10 12:12:34 +04:00
|
|
|
for (unsigned i = 0; i < size(); i++)
|
|
|
|
prod += operator[](i) * F.get(m_opt_indices[i]);
|
|
|
|
for(map<unsigned, float>::iterator it = m_fixed_weights.begin();
|
|
|
|
it != m_fixed_weights.end(); ++it)
|
|
|
|
prod += it->second * F.get(it->first);
|
2008-05-15 14:57:20 +04:00
|
|
|
}
|
|
|
|
return prod;
|
2008-05-14 15:32:17 +04:00
|
|
|
}
|
2011-11-12 04:40:01 +04:00
|
|
|
|
2012-04-12 04:16:27 +04:00
|
|
|
const Point Point::operator+(const Point& p2) const
|
2011-02-24 15:42:19 +03:00
|
|
|
{
|
2013-11-18 22:13:10 +04:00
|
|
|
UTIL_THROW_IF(p2.size() != size(), util::Exception, "Error");
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2012-04-12 00:19:11 +04:00
|
|
|
Res.m_score = kMaxFloat;
|
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)
|
|
|
|
{
|
2013-11-18 22:13:10 +04:00
|
|
|
UTIL_THROW_IF(p2.size() != size(), util::Exception, "Error");
|
2011-11-12 11:39:57 +04:00
|
|
|
for (unsigned i = 0; i < size(); i++) {
|
|
|
|
operator[](i) += p2[i];
|
|
|
|
}
|
2012-04-12 00:19:11 +04:00
|
|
|
m_score = kMaxFloat;
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2011-09-15 21:45:35 +04:00
|
|
|
|
2012-04-12 04:16:27 +04:00
|
|
|
const 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;
|
|
|
|
}
|
2012-04-12 00:19:11 +04:00
|
|
|
Res.m_score = kMaxFloat;
|
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
|
|
|
{
|
2012-04-12 04:40:52 +04:00
|
|
|
vector<parameter_t> w;
|
|
|
|
P.GetAllWeights(w);
|
2012-03-10 12:12:34 +04:00
|
|
|
for (unsigned int i = 0; i < Point::m_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;
|
2012-03-10 12:12:34 +04:00
|
|
|
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;
|
2011-11-12 11:39:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Point::NormalizeL1()
|
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
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-11-12 11:39:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-12 04:40:52 +04:00
|
|
|
void Point::GetAllWeights(vector<parameter_t>& w) const
|
2011-02-24 15:42:19 +03:00
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
if (OptimizeAll()) {
|
|
|
|
w = *this;
|
2011-02-24 15:42:19 +03:00
|
|
|
} else {
|
2012-03-10 12:12:34 +04:00
|
|
|
w.resize(m_pdim);
|
|
|
|
for (unsigned int i = 0; i < size(); i++)
|
|
|
|
w[m_opt_indices[i]] = operator[](i);
|
2012-04-12 04:40:52 +04:00
|
|
|
for (map<unsigned,float>::const_iterator it = m_fixed_weights.begin();
|
2012-03-10 12:12:34 +04:00
|
|
|
it != m_fixed_weights.end(); ++it) {
|
2011-02-24 15:42:19 +03:00
|
|
|
w[it->first]=it->second;
|
2012-03-10 12:12:34 +04:00
|
|
|
}
|
2008-05-15 14:57:20 +04:00
|
|
|
}
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2012-06-30 23:23:45 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|