mosesdecoder/contrib/other-builds/moses2/HypothesisColl.cpp

127 lines
3.1 KiB
C++
Raw Normal View History

2016-02-29 16:10:55 +03:00
/*
* HypothesisColl.cpp
*
* Created on: 26 Feb 2016
* Author: hieu
*/
#include <algorithm>
#include <boost/foreach.hpp>
#include "HypothesisColl.h"
#include "ManagerBase.h"
#include "System.h"
2016-03-31 23:00:16 +03:00
namespace Moses2
{
2016-02-29 16:10:55 +03:00
2016-03-31 23:00:16 +03:00
HypothesisColl::HypothesisColl(const ManagerBase &mgr) :
m_coll(MemPoolAllocator<const HypothesisBase*>(mgr.GetPool())), m_sortedHypos(
NULL)
{
}
2016-02-29 16:10:55 +03:00
StackAdd HypothesisColl::Add(const HypothesisBase *hypo)
{
std::pair<_HCType::iterator, bool> addRet = m_coll.insert(hypo);
// CHECK RECOMBINATION
if (addRet.second) {
2016-03-31 23:00:16 +03:00
// equiv hypo doesn't exists
return StackAdd(true, NULL);
2016-02-29 16:10:55 +03:00
}
else {
2016-03-31 23:00:16 +03:00
HypothesisBase *hypoExisting = const_cast<HypothesisBase*>(*addRet.first);
if (hypo->GetFutureScore() > hypoExisting->GetFutureScore()) {
// incoming hypo is better than the one we have
const HypothesisBase * const &hypoExisting1 = *addRet.first;
const HypothesisBase *&hypoExisting2 =
const_cast<const HypothesisBase *&>(hypoExisting1);
hypoExisting2 = hypo;
return StackAdd(true, hypoExisting);
}
else {
// already storing the best hypo. discard incoming hypo
return StackAdd(false, hypoExisting);
}
2016-02-29 16:10:55 +03:00
}
assert(false);
}
2016-05-06 17:41:50 +03:00
Hypotheses &HypothesisColl::GetSortedAndPruneHypos(
const ManagerBase &mgr,
2016-03-31 23:00:16 +03:00
ArcLists &arcLists) const
2016-02-29 16:10:55 +03:00
{
if (m_sortedHypos == NULL) {
// create sortedHypos first
MemPool &pool = mgr.GetPool();
2016-03-31 23:00:16 +03:00
m_sortedHypos = new (pool.Allocate<Hypotheses>()) Hypotheses(pool,
m_coll.size());
2016-02-29 16:10:55 +03:00
2016-03-31 23:00:16 +03:00
size_t ind = 0;
BOOST_FOREACH(const HypothesisBase *hypo, m_coll){
(*m_sortedHypos)[ind] = hypo;
++ind;
}
2016-02-29 16:10:55 +03:00
2016-03-31 22:00:55 +03:00
SortAndPruneHypos(mgr, arcLists);
2016-02-29 16:10:55 +03:00
}
return *m_sortedHypos;
}
2016-03-31 23:00:16 +03:00
void HypothesisColl::SortAndPruneHypos(const ManagerBase &mgr,
ArcLists &arcLists) const
2016-02-29 16:10:55 +03:00
{
2016-04-07 16:51:43 +03:00
size_t stackSize = mgr.system.options.search.stack_size;
2016-02-29 16:10:55 +03:00
Recycler<HypothesisBase*> &recycler = mgr.GetHypoRecycle();
/*
2016-03-31 23:00:16 +03:00
cerr << "UNSORTED hypos:" << endl;
for (size_t i = 0; i < hypos.size(); ++i) {
const Hypothesis *hypo = hypos[i];
cerr << *hypo << endl;
}
cerr << endl;
*/
2016-02-29 16:10:55 +03:00
Hypotheses::iterator iterMiddle;
2016-03-31 23:00:16 +03:00
iterMiddle =
(stackSize == 0 || m_sortedHypos->size() < stackSize) ?
m_sortedHypos->end() : m_sortedHypos->begin() + stackSize;
2016-02-29 16:10:55 +03:00
std::partial_sort(m_sortedHypos->begin(), iterMiddle, m_sortedHypos->end(),
2016-03-31 23:00:16 +03:00
HypothesisFutureScoreOrderer());
2016-02-29 16:10:55 +03:00
// prune
if (stackSize && m_sortedHypos->size() > stackSize) {
2016-03-31 23:00:16 +03:00
for (size_t i = stackSize; i < m_sortedHypos->size(); ++i) {
HypothesisBase *hypo = const_cast<HypothesisBase*>((*m_sortedHypos)[i]);
recycler.Recycle(hypo);
// delete from arclist
2016-04-07 18:23:14 +03:00
if (mgr.system.options.nbest.nbest_size) {
2016-03-31 23:00:16 +03:00
arcLists.Delete(hypo);
}
}
m_sortedHypos->resize(stackSize);
2016-02-29 16:10:55 +03:00
}
/*
2016-03-31 23:00:16 +03:00
cerr << "sorted hypos:" << endl;
for (size_t i = 0; i < hypos.size(); ++i) {
const Hypothesis *hypo = hypos[i];
cerr << hypo << " " << *hypo << endl;
}
cerr << endl;
*/
2016-02-29 16:10:55 +03:00
}
void HypothesisColl::Clear()
{
2016-03-31 23:00:16 +03:00
m_sortedHypos = NULL;
m_coll.clear();
2016-02-29 16:10:55 +03:00
}
} /* namespace Moses2 */