51 std::string
name()
const;
54 template<
class VISITOR>
58 typename GM::ValueType
bound()
const;
59 typename GM::ValueType
value()
const;
65 const GraphicalModelType&
gm_;
88 : gm_(gm), parameter_(para), pdInference_(NULL), labelCosts_(NULL),
89 numPairs_(0), pairs_(NULL), distance_(NULL), weights_(NULL) {
100 throw std::runtime_error(
"Error: Tables are not proportional");
104 gm_.numberOfVariables(),
105 gm_.numberOfLabels(0),
125 delete[] labelCosts_;
150 EmptyVisitorType visitor;
151 return this->infer(visitor);
155 template<
class VISITOR>
157 visitor.begin(*
this);
160 if(pdInference_ != NULL) {
162 std::vector<LabelType> result;
164 value_ = gm_.evaluate(result);
174 arg.resize(gm_.numberOfVariables());
175 for(
IndexType i = 0; i < gm_.numberOfVariables(); i++) {
176 arg[i] = pdInference_->_pinfo[i].label;
192 std::vector<IndexType> factorId2pwId(gm_.numberOfFactors(),std::numeric_limits<IndexType>::max());
193 for (
IndexType factorId=0;factorId<gm_.numberOfFactors();++factorId)
194 if (gm_[factorId].numberOfVariables()==2)
195 factorId2pwId[factorId]=pwId++;
197 for (
IndexType factorId=0;factorId<gm_.numberOfFactors();++factorId)
199 const typename GM::FactorType& f=gm_[factorId];
200 ValueType res=std::numeric_limits<ValueType>::infinity(), res1;
201 if (f.numberOfVariables()==1)
204 for (
LabelType label=0;label<gm_.numberOfLabels(varId);++label)
207 for (
IndexType i=0;i<gm_.numberOfFactors(varId);++i)
209 IndexType fId=gm_.factorOfVariable(varId,i);
210 if (gm_[fId].numberOfVariables()==2)
212 OPENGM_ASSERT(factorId2pwId[fId]<std::numeric_limits<IndexType>::max());
213 if (gm_[fId].variableIndex(0)==varId)
214 res1+=pdInference_->_y[label*numPairs_+factorId2pwId[fId]];
216 res1-=pdInference_->_y[label*numPairs_+factorId2pwId[fId]];
219 res=std::min(res,res1);
221 }
else if (f.numberOfVariables()==2)
223 pwId=factorId2pwId[factorId];
225 IndexType varId0=f.variableIndex(0),varId1=f.variableIndex(1);
226 for (
LabelType label0=0;label0<gm_.numberOfLabels(varId0);++label0)
227 for (
LabelType label1=0;label1<gm_.numberOfLabels(varId1);++label1)
229 std::vector<LabelType> labels(2); labels[0]=label0; labels[1]=label1;
230 res1=f(labels.begin())-pdInference_->_y[label0*numPairs_+pwId]+pdInference_->_y[label1*numPairs_+pwId];
231 res=std::min(res,res1);
234 AccumulationType::ineutral(boundValue);
252 LabelType numLabels = gm_.numberOfLabels(0);
253 for(
IndexType i = 1; i < gm_.numberOfVariables(); i++) {
254 if(gm_.numberOfLabels(i) != numLabels) {
263 labelCosts_ =
new fastPDLib::CV_Fast_PD::Real[gm_.numberOfVariables() * gm_.numberOfLabels(0)];
264 for(
IndexType i = 0; i < gm_.numberOfVariables() * gm_.numberOfLabels(0); i++) {
268 for(
IndexType i = 0; i < gm_.numberOfVariables(); i++) {
269 for(
IndexType j = 0; j < gm_.numberOfFactors(i); j++) {
270 IndexType gmFactorIndex = gm_.factorOfVariable(i, j);
271 if(gm_.numberOfVariables(gmFactorIndex) == 1) {
272 for(
IndexType k = 0; k < gm_.numberOfLabels(0); k++) {
273 labelCosts_[k * gm_.numberOfVariables() + i ] += gm_[gmFactorIndex](&k);
282 for(
IndexType i = 0; i < gm_.numberOfFactors(); i++) {
283 if(gm_.numberOfVariables(i) == 2) {
291 pairs_ =
new int[numPairs_ * 2];
293 for(
IndexType i = 0; i < gm_.numberOfFactors(); i++) {
294 if(gm_.numberOfVariables(i) == 2) {
295 pairs_[currentPair * 2] = gm_[i].variableIndex(0);
296 pairs_[(currentPair * 2) + 1] = gm_[i].variableIndex(1);
304 distance_ =
new fastPDLib::CV_Fast_PD::Real[gm_.numberOfLabels(0) * gm_.numberOfLabels(0)];
305 for(
IndexType k = 0; k < gm_.numberOfLabels(0); k++) {
306 for(
IndexType l = 0; l < gm_.numberOfLabels(0); l++) {
307 distance_[(l * gm_.numberOfLabels(0)) + k] = 0;
310 for(
IndexType i = 0; i < gm_.numberOfFactors(); i++) {
311 if(gm_.numberOfVariables(i) == 2) {
312 for(
IndexType k = 0; k < gm_.numberOfLabels(0); k++) {
313 for(
IndexType l = 0; l < gm_.numberOfLabels(0); l++) {
315 distance_[(l * gm_.numberOfLabels(0)) + k] = gm_[i](index);
325 weights_ =
new fastPDLib::CV_Fast_PD::Real[numPairs_];
327 for(
IndexType i = 0; i < gm_.numberOfFactors(); i++) {
328 if(gm_.numberOfVariables(i) == 2) {
331 for(k = 0; k < gm_.numberOfLabels(0); k++) {
333 for(l = 0; l < gm_.numberOfLabels(0); l++) {
335 if((gm_[i](index) != 0) && (distance_[(l * gm_.numberOfLabels(0)) + k] != 0)) {
336 double currentWeight =
static_cast<double>(gm_[i](index)) /
static_cast<double>(distance_[(l * gm_.numberOfLabels(0)) + k]);
337 weights_[currentPair] =
static_cast<fastPDLib::CV_Fast_PD::Real
>(currentWeight);
338 if(fabs(currentWeight - static_cast<double>(weights_[currentPair])) >
OPENGM_FLOAT_TOL) {
345 if(l != gm_.numberOfLabels(0)) {
349 if(k == gm_.numberOfLabels(0)) {
350 weights_[currentPair] = 0;
361 for(
IndexType i = 0; i < gm_.numberOfFactors(); i++) {
362 if(gm_.numberOfVariables(i) == 2) {
363 for(
IndexType k = 0; k < gm_.numberOfLabels(0); k++) {
364 for(
IndexType l = 0; l < gm_.numberOfLabels(0); l++) {
366 if(fabs(gm_[i](index) - (distance_[(l * gm_.numberOfLabels(0)) + k] * weights_[currentPair])) >
OPENGM_FLOAT_TOL) {
381 ReparametrizerType* pReparametrizer=
new ReparametrizerType(gm_);
383 ReparametrizerType lpreparametrizer(gm_);
385 typedef typename ReparametrizerType::RepaStorageType::uIterator uIterator;
389 for (
IndexType factorId=0;factorId<gm_.numberOfFactors();++factorId)
390 if (gm_[factorId].numberOfVariables()==2) ++pwNum;
392 const fastPDLib::CV_Fast_PD::Real* y=pdInference_->_y;
395 for (
IndexType factorId=0;factorId<gm_.numberOfFactors();++factorId)
397 if (gm_[factorId].numberOfVariables()!=2)
continue;
400 std::pair<uIterator,uIterator> iter=reparametrization.getIterators(factorId,i);
403 for (;iter.first!=iter.second;++iter.first)
405 *iter.first=-mul*y[label*pwNum+pwId];
414 return pReparametrizer;
InferenceTermination arg(std::vector< LabelType > &, const size_t &=1) const
const GraphicalModelType & graphicalModel() const
fastPDLib::CV_Fast_PD * pdInference_
visitors::TimingVisitor< FastPD< GM > > TimingVisitorType
FastPD(const GraphicalModelType &gm, const Parameter ¶=Parameter())
GM::ValueType bound() const
return a bound on the solution
#define OPENGM_ASSERT(expression)
GM::ValueType value() const
return the solution (value)
LPReparametrisationStorage< GM > RepaStorageType
const GraphicalModelType & gm_
GraphicalModelType::IndexType IndexType
LPReparametrizer< GM, opengm::Minimizer > ReparametrizerType
FastPD FastPD inference algorithm class.
visitors::VerboseVisitor< FastPD< GM > > VerboseVisitorType
ReparametrizerType * getReparametrizer(const typename ReparametrizerType::Parameter ¶ms=typename ReparametrizerType::Parameter()) const
GraphicalModelType::ValueType ValueType
static T ineutral()
inverse neutral element (with return)
Inference algorithm interface.
opengm::Minimizer AccumulationType
InferenceTermination infer()
bool sameNumberOfLabels() const
static T neutral()
neutral element (with return)
size_t numberOfIterations_
number of iterations
visitors::EmptyVisitor< FastPD< GM > > EmptyVisitorType
fastPDLib::CV_Fast_PD::Real * labelCosts_
RepaStorageType & Reparametrization()
GraphicalModelType::LabelType LabelType
Minimization as a unary accumulation.
fastPDLib::CV_Fast_PD::Real * weights_
fastPDLib::CV_Fast_PD::Real * distance_