|
#include "Cube.h" |
|
|
|
#include "moses/FF/FFState.h" |
|
#include "moses/FF/StatefulFeatureFunction.h" |
|
#include "moses/FF/StatelessFeatureFunction.h" |
|
#include "moses/StaticData.h" |
|
|
|
#include "SVertex.h" |
|
|
|
namespace Moses |
|
{ |
|
|
|
namespace Syntax |
|
{ |
|
|
|
Cube::Cube(const SHyperedgeBundle &bundle) |
|
: m_bundle(bundle) |
|
{ |
|
|
|
std::vector<int> coordinates(bundle.stacks.size()+1, 0); |
|
SHyperedge *hyperedge = CreateHyperedge(coordinates); |
|
|
|
std::pair<CoordinateSet::iterator, bool> p = m_visited.insert(coordinates); |
|
const std::vector<int> &storedCoordinates = *p.first; |
|
|
|
|
|
m_queue.push(QueueItem(hyperedge, &storedCoordinates)); |
|
} |
|
|
|
Cube::~Cube() |
|
{ |
|
|
|
|
|
|
|
while (!m_queue.empty()) { |
|
QueueItem item = m_queue.top(); |
|
m_queue.pop(); |
|
|
|
delete item.first->head; |
|
} |
|
} |
|
|
|
SHyperedge *Cube::Pop() |
|
{ |
|
QueueItem item = m_queue.top(); |
|
m_queue.pop(); |
|
CreateNeighbours(*item.second); |
|
return item.first; |
|
} |
|
|
|
void Cube::CreateNeighbours(const std::vector<int> &coordinates) |
|
{ |
|
|
|
|
|
std::vector<int> tmpCoordinates(coordinates); |
|
|
|
|
|
for (std::size_t i = 0; i < coordinates.size()-1; ++i) { |
|
const std::size_t x = coordinates[i]; |
|
if (m_bundle.stacks[i]->size() > x+1) { |
|
++tmpCoordinates[i]; |
|
CreateNeighbour(tmpCoordinates); |
|
--tmpCoordinates[i]; |
|
} |
|
} |
|
|
|
const std::size_t x = coordinates.back(); |
|
if (m_bundle.translations->GetSize() > x+1) { |
|
++tmpCoordinates.back(); |
|
CreateNeighbour(tmpCoordinates); |
|
--tmpCoordinates.back(); |
|
} |
|
} |
|
|
|
void Cube::CreateNeighbour(const std::vector<int> &coordinates) |
|
{ |
|
|
|
|
|
std::pair<CoordinateSet::iterator, bool> p = m_visited.insert(coordinates); |
|
if (!p.second) { |
|
|
|
return; |
|
} |
|
SHyperedge *hyperedge = CreateHyperedge(coordinates); |
|
const std::vector<int> &storedCoordinates = *p.first; |
|
m_queue.push(QueueItem(hyperedge, &storedCoordinates)); |
|
} |
|
|
|
SHyperedge *Cube::CreateHyperedge(const std::vector<int> &coordinates) |
|
{ |
|
SHyperedge *hyperedge = new SHyperedge(); |
|
|
|
SVertex *head = new SVertex(); |
|
head->best = hyperedge; |
|
head->pvertex = 0; |
|
head->states.resize( |
|
StatefulFeatureFunction::GetStatefulFeatureFunctions().size()); |
|
hyperedge->head = head; |
|
|
|
hyperedge->tail.resize(coordinates.size()-1); |
|
for (std::size_t i = 0; i < coordinates.size()-1; ++i) { |
|
boost::shared_ptr<SVertex> pred = (*m_bundle.stacks[i])[coordinates[i]]; |
|
hyperedge->tail[i] = pred.get(); |
|
} |
|
|
|
hyperedge->label.inputWeight = m_bundle.inputWeight; |
|
|
|
hyperedge->label.translation = |
|
*(m_bundle.translations->begin()+coordinates.back()); |
|
|
|
|
|
|
|
const StaticData &staticData = StaticData::Instance(); |
|
|
|
|
|
|
|
const std::vector<const StatelessFeatureFunction*>& sfs = |
|
StatelessFeatureFunction::GetStatelessFeatureFunctions(); |
|
for (unsigned i = 0; i < sfs.size(); ++i) { |
|
if (!staticData.IsFeatureFunctionIgnored(*sfs[i])) { |
|
sfs[i]->EvaluateWhenApplied(*hyperedge, &hyperedge->label.deltas); |
|
} |
|
} |
|
|
|
const std::vector<const StatefulFeatureFunction*>& ffs = |
|
StatefulFeatureFunction::GetStatefulFeatureFunctions(); |
|
for (unsigned i = 0; i < ffs.size(); ++i) { |
|
if (!staticData.IsFeatureFunctionIgnored(*ffs[i])) { |
|
head->states[i] = |
|
ffs[i]->EvaluateWhenApplied(*hyperedge, i, &hyperedge->label.deltas); |
|
} |
|
} |
|
|
|
|
|
|
|
hyperedge->label.futureScore = |
|
hyperedge->label.translation->GetScoreBreakdown().GetWeightedScore(); |
|
|
|
hyperedge->label.futureScore += hyperedge->label.deltas.GetWeightedScore(); |
|
|
|
for (std::vector<SVertex*>::const_iterator p = hyperedge->tail.begin(); |
|
p != hyperedge->tail.end(); ++p) { |
|
const SVertex *pred = *p; |
|
if (pred->best) { |
|
hyperedge->label.futureScore += pred->best->label.futureScore; |
|
} |
|
} |
|
|
|
return hyperedge; |
|
} |
|
|
|
} |
|
} |
|
|