12 #ifndef ERRORSURFACE_H_ 13 #define ERRORSURFACE_H_ 22 #include <boost/thread/mutex.hpp> 28 template<
typename IntervalBoundary>
30 const IntervalBoundary& b2) {
31 return b1.gamma < b2.gamma;
40 typedef typename ErrorStats::Error
Error;
44 ErrorStats MergeInitials()
const {
46 for (
typename std::vector<IntervalBoundary>::const_iterator it =
47 ++ (initials.begin() ); it != initials.end(); ++it) {
48 es = es + it->errorStats;
57 void MergeInitialScores (
double scores[])
const {
60 for (
typename std::vector<IntervalBoundary>::const_iterator it =
61 ++ (initials.begin() ); it != initials.end(); ++it) {
62 scores[0] += it->deltaScore;
63 scores[1] += it->deltaExpScore;
69 std::vector<ErrorStats> prev;
72 std::vector<IntervalBoundary>
74 std::vector<IntervalBoundary> prevBestIBs;
81 initials.resize (noOfSentences + 1);
82 prevBestIBs.resize (noOfSentences + 1);
83 prev.resize (noOfSentences + 1);
87 optimalGamma (other.optimalGamma), optimalError (other.optimalError), prev (
88 other.prev), refs (other.refs), unbounded (other.unbounded), initials (
89 other.initials), prevBestIBs (other.prevBestIBs) {
96 optimalGamma = rhs.optimalGamma;
97 optimalError = rhs.optimalError;
100 unbounded = rhs.unbounded;
101 initials = rhs.initials;
102 prevBestIBs = rhs.prevBestIBs;
119 std::vector<IntervalBoundary> currentIBs (initials);
121 MergeInitialScores (scores);
122 ErrorStats aggregateErrorStats = MergeInitials();
124 IntervalBoundarySortPredicate<IntervalBoundary>);
126 optimalError = aggregateErrorStats.ComputeError();
127 typename std::vector<IntervalBoundary>::iterator itNext =
130 for (
typename std::vector<IntervalBoundary>::iterator it =
132 scores[0] = it->score + it->deltaScore;
133 scores[1] = it->expScore + it->deltaExpScore;
134 it->score = scores[0];
135 it->expScore = scores[1];
136 aggregateErrorStats = aggregateErrorStats + it->errorStats;
137 currentIBs[it->sentence].errorStats =
138 currentIBs[it->sentence].errorStats + it->errorStats;
139 Error current = aggregateErrorStats.ComputeError();
140 if (current > optimalError) {
141 copy (++ (currentIBs.begin() ), currentIBs.end(),
142 ++ (prevBestIBs.begin() ) );
143 optimalError = current;
145 optimalGamma = it->gamma + 1;
148 optimalGamma = 0.5 * (it->gamma + itNext->gamma);
161 copy (++ (prevBestIBs.begin() ), prevBestIBs.end(), ++ (initials.begin() ) );
165 ErrorStats aggregate;
166 os << optimalGamma <<
" " << optimalError << endl;
167 for (
typename std::vector<IntervalBoundary>::const_iterator it =
168 initials.begin(); it != initials.end(); ++it) {
169 aggregate = aggregate + it->errorStats;
172 IntervalBoundary temp;
173 for (
typename std::vector<IntervalBoundary>::const_iterator it =
183 static unsigned int lineOptCount = 0;
184 std::stringstream sstream;
185 sstream << filename << lineOptCount;
186 std::ofstream ofs (sstream.str().c_str() );
188 cerr <<
"ERROR: can't write: " << sstream.str() <<
'\n';
191 tracer <<
"writing error surface to " << sstream.str() <<
'\n';
198 const double modelScore,
const double expScore) {
199 prev[sid] = refs->ComputeErrorStats (sid, h);
205 const double modelScore,
const double expScore) {
206 ErrorStats next = refs->ComputeErrorStats (sid, h);
207 IntervalBoundary b (sid, gamma, next - prev[sid], modelScore, expScore);
214 std::vector<IntervalBoundary>
IntervalData< ErrorStats > IntervalBoundary
bool IntervalBoundarySortPredicate(const IntervalBoundary &b1, const IntervalBoundary &b2)
void CreateInterval(Sid sid, const double gamma, const Sentence h, const double modelScore, const double expScore)
void PrintErrorSurface(ostream &os) const
void CreateInitial(Sid sid, const double gamma, const Sentence h, const double modelScore, const double expScore)
ErrorSurface(unsigned int noOfSentences, RefData *refs)
std::vector< Wid > Sentence
std::vector< IntervalBoundary > boundaries
RefData::ErrorStats ErrorStats
ErrorSurface & operator=(const ErrorSurface &rhs)
ErrorSurface(const ErrorSurface &other)
void WriteErrorSurface(const std::string &filename) const