00001 #include "Cube.h"
00002
00003 #include "moses/FF/FFState.h"
00004 #include "moses/FF/StatefulFeatureFunction.h"
00005 #include "moses/FF/StatelessFeatureFunction.h"
00006 #include "moses/StaticData.h"
00007
00008 #include "SVertex.h"
00009
00010 namespace Moses
00011 {
00012
00013 namespace Syntax
00014 {
00015
00016 Cube::Cube(const SHyperedgeBundle &bundle)
00017 : m_bundle(bundle)
00018 {
00019
00020 std::vector<int> coordinates(bundle.stacks.size()+1, 0);
00021 SHyperedge *hyperedge = CreateHyperedge(coordinates);
00022
00023 std::pair<CoordinateSet::iterator, bool> p = m_visited.insert(coordinates);
00024 const std::vector<int> &storedCoordinates = *p.first;
00025
00026
00027 m_queue.push(QueueItem(hyperedge, &storedCoordinates));
00028 }
00029
00030 Cube::~Cube()
00031 {
00032
00033
00034
00035 while (!m_queue.empty()) {
00036 QueueItem item = m_queue.top();
00037 m_queue.pop();
00038
00039 delete item.first->head;
00040 }
00041 }
00042
00043 SHyperedge *Cube::Pop()
00044 {
00045 QueueItem item = m_queue.top();
00046 m_queue.pop();
00047 CreateNeighbours(*item.second);
00048 return item.first;
00049 }
00050
00051 void Cube::CreateNeighbours(const std::vector<int> &coordinates)
00052 {
00053
00054
00055 std::vector<int> tmpCoordinates(coordinates);
00056
00057
00058 for (std::size_t i = 0; i < coordinates.size()-1; ++i) {
00059 const std::size_t x = coordinates[i];
00060 if (m_bundle.stacks[i]->size() > x+1) {
00061 ++tmpCoordinates[i];
00062 CreateNeighbour(tmpCoordinates);
00063 --tmpCoordinates[i];
00064 }
00065 }
00066
00067 const std::size_t x = coordinates.back();
00068 if (m_bundle.translations->GetSize() > x+1) {
00069 ++tmpCoordinates.back();
00070 CreateNeighbour(tmpCoordinates);
00071 --tmpCoordinates.back();
00072 }
00073 }
00074
00075 void Cube::CreateNeighbour(const std::vector<int> &coordinates)
00076 {
00077
00078
00079 std::pair<CoordinateSet::iterator, bool> p = m_visited.insert(coordinates);
00080 if (!p.second) {
00081
00082 return;
00083 }
00084 SHyperedge *hyperedge = CreateHyperedge(coordinates);
00085 const std::vector<int> &storedCoordinates = *p.first;
00086 m_queue.push(QueueItem(hyperedge, &storedCoordinates));
00087 }
00088
00089 SHyperedge *Cube::CreateHyperedge(const std::vector<int> &coordinates)
00090 {
00091 SHyperedge *hyperedge = new SHyperedge();
00092
00093 SVertex *head = new SVertex();
00094 head->best = hyperedge;
00095 head->pvertex = 0;
00096 head->states.resize(
00097 StatefulFeatureFunction::GetStatefulFeatureFunctions().size());
00098 hyperedge->head = head;
00099
00100 hyperedge->tail.resize(coordinates.size()-1);
00101 for (std::size_t i = 0; i < coordinates.size()-1; ++i) {
00102 boost::shared_ptr<SVertex> pred = (*m_bundle.stacks[i])[coordinates[i]];
00103 hyperedge->tail[i] = pred.get();
00104 }
00105
00106 hyperedge->label.inputWeight = m_bundle.inputWeight;
00107
00108 hyperedge->label.translation =
00109 *(m_bundle.translations->begin()+coordinates.back());
00110
00111
00112
00113 const StaticData &staticData = StaticData::Instance();
00114
00115
00116
00117 const std::vector<const StatelessFeatureFunction*>& sfs =
00118 StatelessFeatureFunction::GetStatelessFeatureFunctions();
00119 for (unsigned i = 0; i < sfs.size(); ++i) {
00120 if (!staticData.IsFeatureFunctionIgnored(*sfs[i])) {
00121 sfs[i]->EvaluateWhenApplied(*hyperedge, &hyperedge->label.deltas);
00122 }
00123 }
00124
00125 const std::vector<const StatefulFeatureFunction*>& ffs =
00126 StatefulFeatureFunction::GetStatefulFeatureFunctions();
00127 for (unsigned i = 0; i < ffs.size(); ++i) {
00128 if (!staticData.IsFeatureFunctionIgnored(*ffs[i])) {
00129 head->states[i] =
00130 ffs[i]->EvaluateWhenApplied(*hyperedge, i, &hyperedge->label.deltas);
00131 }
00132 }
00133
00134
00135
00136 hyperedge->label.futureScore =
00137 hyperedge->label.translation->GetScoreBreakdown().GetWeightedScore();
00138
00139 hyperedge->label.futureScore += hyperedge->label.deltas.GetWeightedScore();
00140
00141 for (std::vector<SVertex*>::const_iterator p = hyperedge->tail.begin();
00142 p != hyperedge->tail.end(); ++p) {
00143 const SVertex *pred = *p;
00144 if (pred->best) {
00145 hyperedge->label.futureScore += pred->best->label.futureScore;
00146 }
00147 }
00148
00149 return hyperedge;
00150 }
00151
00152 }
00153 }