00001 #include "Point.h"
00002
00003 #include <cmath>
00004 #include <cstdlib>
00005 #include "util/exception.hh"
00006 #include "util/random.hh"
00007 #include "FeatureStats.h"
00008 #include "Optimizer.h"
00009
00010 using namespace std;
00011
00012 namespace MosesTuning
00013 {
00014
00015 vector<unsigned> Point::m_opt_indices;
00016
00017 unsigned Point::m_dim = 0;
00018
00019 map<unsigned,statscore_t> Point::m_fixed_weights;
00020
00021 unsigned Point::m_pdim = 0;
00022 unsigned Point::m_ncall = 0;
00023
00024 vector<parameter_t> Point::m_min;
00025 vector<parameter_t> Point::m_max;
00026
00027 Point::Point() : vector<parameter_t>(m_dim), m_score(0.0) {}
00028
00029
00030 Point::Point(const vector<parameter_t>& init,
00031 const vector<parameter_t>& min,
00032 const vector<parameter_t>& max)
00033 : vector<parameter_t>(Point::m_dim), m_score(0.0)
00034 {
00035 m_min.resize(Point::m_dim);
00036 m_max.resize(Point::m_dim);
00037 if (init.size() == m_dim) {
00038 for (unsigned int i = 0; i < Point::m_dim; i++) {
00039 operator[](i) = init[i];
00040 m_min[i] = min[i];
00041 m_max[i] = max[i];
00042 }
00043 } else {
00044 UTIL_THROW_IF(init.size() != m_pdim, util::Exception, "Error");
00045 UTIL_THROW_IF(m_opt_indices.size() != Point::m_dim, util::Exception, "Error");
00046 for (unsigned int i = 0; i < Point::m_dim; i++) {
00047 operator[](i) = init[m_opt_indices[i]];
00048 m_min[i] = min[m_opt_indices[i]];
00049 m_max[i] = max[m_opt_indices[i]];
00050 }
00051 }
00052 }
00053
00054 Point::~Point() {}
00055
00056 void Point::Randomize()
00057 {
00058 UTIL_THROW_IF(m_min.size() != Point::m_dim, util::Exception, "Error");
00059 UTIL_THROW_IF(m_max.size() != Point::m_dim, util::Exception, "Error");
00060
00061 for (unsigned int i = 0; i < size(); i++)
00062 operator[](i) = util::rand_incl(m_min[i], m_max[i]);
00063 }
00064
00065 double Point::operator*(const FeatureStats& F) const
00066 {
00067 m_ncall++;
00068 double prod = 0.0;
00069 if (OptimizeAll())
00070 for (unsigned i=0; i<size(); i++)
00071 prod += operator[](i) * F.get(i);
00072 else {
00073 for (unsigned i = 0; i < size(); i++)
00074 prod += operator[](i) * F.get(m_opt_indices[i]);
00075 for(map<unsigned, float>::iterator it = m_fixed_weights.begin();
00076 it != m_fixed_weights.end(); ++it)
00077 prod += it->second * F.get(it->first);
00078 }
00079 return prod;
00080 }
00081
00082 const Point Point::operator+(const Point& p2) const
00083 {
00084 UTIL_THROW_IF(p2.size() != size(), util::Exception, "Error");
00085
00086 Point Res(*this);
00087 for (unsigned i = 0; i < size(); i++) {
00088 Res[i] += p2[i];
00089 }
00090
00091 Res.m_score = kMaxFloat;
00092 return Res;
00093 }
00094
00095 void Point::operator+=(const Point& p2)
00096 {
00097 UTIL_THROW_IF(p2.size() != size(), util::Exception, "Error");
00098 for (unsigned i = 0; i < size(); i++) {
00099 operator[](i) += p2[i];
00100 }
00101 m_score = kMaxFloat;
00102 }
00103
00104 const Point Point::operator*(float l) const
00105 {
00106 Point Res(*this);
00107 for (unsigned i = 0; i < size(); i++) {
00108 Res[i] *= l;
00109 }
00110 Res.m_score = kMaxFloat;
00111 return Res;
00112 }
00113
00114 ostream& operator<<(ostream& o, const Point& P)
00115 {
00116 vector<parameter_t> w;
00117 P.GetAllWeights(w);
00118 for (unsigned int i = 0; i < Point::m_pdim; i++) {
00119 o << w[i] << " ";
00120 }
00121 return o;
00122 }
00123
00124 void Point::NormalizeL2()
00125 {
00126 parameter_t norm=0.0;
00127 for (unsigned int i = 0; i < size(); i++)
00128 norm += operator[](i) * operator[](i);
00129 if (norm != 0.0) {
00130 norm = sqrt(norm);
00131 for (unsigned int i = 0; i < size(); i++)
00132 operator[](i) /= norm;
00133 }
00134 }
00135
00136
00137 void Point::NormalizeL1()
00138 {
00139 parameter_t norm = 0.0;
00140 for (unsigned int i = 0; i < size(); i++)
00141 norm += abs(operator[](i));
00142 if (norm != 0.0) {
00143 for (unsigned int i = 0; i < size(); i++)
00144 operator[](i) /= norm;
00145 }
00146 }
00147
00148
00149 void Point::GetAllWeights(vector<parameter_t>& w) const
00150 {
00151 if (OptimizeAll()) {
00152 w = *this;
00153 } else {
00154 w.resize(m_pdim);
00155 for (unsigned int i = 0; i < size(); i++)
00156 w[m_opt_indices[i]] = operator[](i);
00157 for (map<unsigned,float>::const_iterator it = m_fixed_weights.begin();
00158 it != m_fixed_weights.end(); ++it) {
00159 w[it->first]=it->second;
00160 }
00161 }
00162 }
00163
00164 }
00165