12 #include "GCoptimization.h" 56 : inferenceType_(inferenceType), energyType_(energyType), numberOfIterations_(numberOfIterations), randomLabelOrder_(false), useAdaptiveCycles_(false), doNotUseGrid_(false) {
64 std::string
name()
const;
67 template<
class VISITOR>
71 typename GM::ValueType
bound()
const;
72 typename GM::ValueType
value()
const;
77 const GraphicalModelType&
gm_;
143 throw(
RuntimeError(
"GCOLIB only supports graphical models where each variable has the same number of states."));
156 std::cout <<
"GRID"<<std::endl;
162 std::cout <<
"NO GRID"<<std::endl;
171 throw(
RuntimeError(
"Singleton policy: GCOLIB only supports one instance with energy type \"VIEW\" at a time."));
179 throw(
RuntimeError(
"GCOLIB only supports energy type \"TABLES\" if model is a grid."));
182 throw(
RuntimeError(
"Singleton policy: GCOLIB only supports one instance with energy type \"TABLES\" at a time."));
190 throw(
RuntimeError(
"GCOLIB only supports energy type \"WEIGHTEDTABLE\" if model is a grid."));
203 std::cout <<
"~~"<<std::endl;
242 return this->
infer(visitor);
246 template<
class VISITOR>
248 visitor.begin(*
this);
340 arg.resize(
gm_.numberOfVariables());
342 for(
IndexType i = 0; i <
gm_.numberOfVariables(); i++) {
346 for(
IndexType i = 0; i <
gm_.numberOfVariables(); i++) {
380 if(
gm_[i].numberOfVariables() == 2) {
415 throw(
RuntimeError(
"All energy tables have to be equal with respect to a scaling factor."));
423 if(
gm_[i].numberOfVariables() == 2) {
433 if((
V_[(l * numLabels_) + m] != 0) && (
gm_[i](index) != 0)) {
434 weight =
gm_[i](index) /
V_[(l * numLabels_) + m];
438 if(m != numLabels_) {
448 throw(
RuntimeError(
"All energy tables have to be equal with respect to a scaling factor."));
471 for(
IndexType i = 0; i <
gm_.numberOfVariables(); i++) {
473 for(
IndexType j = 0; j <
gm_.numberOfFactors(gmVariableIndex); j++) {
474 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, j);
475 if(
gm_.numberOfVariables(gmFactorIndex) == 1) {
477 D_[i * numLabels_ + k] +=
gm_[gmFactorIndex](&k);
489 for(
IndexType i = 0; i <
gm_.numberOfFactors(gmVariableIndex); i++) {
490 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, i);
491 if(
gm_.numberOfVariables(gmFactorIndex) == 2) {
510 for(
IndexType k = 0; k <
gm_.numberOfFactors(gmVariableIndex); k++) {
511 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, k);
512 if(
gm_.numberOfVariables(gmFactorIndex) == 2) {
513 if((i < sizeX_ - 1) &&
gm_.variableFactorConnection(
grid_(i + 1, j), gmFactorIndex)) {
520 if((
V_[(l * numLabels_) + m] != 0) && (
gm_[gmFactorIndex](index) != 0)) {
525 if(m != numLabels_) {
529 }
else if((j < sizeY_ -1 ) &&
gm_.variableFactorConnection(
grid_(i, j + 1), gmFactorIndex)) {
536 if((
V_[(l * numLabels_) + m] != 0) && (
gm_[gmFactorIndex](index) != 0)) {
541 if(m != numLabels_) {
545 }
else if((i != 0) &&
gm_.variableFactorConnection(
grid_(i - 1, j), gmFactorIndex)) {
547 }
else if((j != 0) &&
gm_.variableFactorConnection(
grid_(i, j - 1), gmFactorIndex)) {
561 for(
IndexType i = 1; i <
gm_.numberOfVariables(); i++) {
575 for(
IndexType k = 0; k <
gm_.numberOfFactors(gmVariableIndex); k++) {
576 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, k);
577 if(
gm_.numberOfVariables(gmFactorIndex) == 2) {
578 if(
gm_.variableFactorConnection(
grid_(i + 1, j), gmFactorIndex)) {
582 if(fabs((
V_[(l * numLabels_) + m] *
hCue_[i + (j * sizeX_)]) -
gm_[gmFactorIndex](index)) > eps) {
587 }
else if(
gm_.variableFactorConnection(
grid_(i, j + 1), gmFactorIndex)) {
591 if(fabs((
V_[(l * numLabels_) + m] *
vCue_[i + (j * sizeX_)]) -
gm_[gmFactorIndex](index)) > eps) {
596 }
else if((i != 0) &&
gm_.variableFactorConnection(
grid_(i - 1, j), gmFactorIndex)) {
598 }
else if((j != 0) &&
gm_.variableFactorConnection(
grid_(i, j - 1), gmFactorIndex)) {
615 if (
V_[(i * numLabels_) + j] !=
V_[(j * numLabels_) + i]) {
625 std::vector<LabelType> state;
638 for(
IndexType i = 0; i <
gm_.numberOfVariables(); i++) {
640 for(
IndexType j = 0; j <
gm_.numberOfFactors(gmVariableIndex); j++) {
641 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, j);
642 if(
gm_.numberOfVariables(gmFactorIndex) == 1) {
649 if(
gm_[i].numberOfVariables() == 1) {
666 for(
IndexType k = 0; k <
gm_.numberOfFactors(gmVariableIndex); k++) {
667 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, k);
668 if(
gm_.numberOfVariables(gmFactorIndex) == 2) {
669 if((i < sizeX_ - 1) &&
gm_.variableFactorConnection(
grid_(i + 1, j), gmFactorIndex)) {
671 }
else if((j < sizeY_ -1 ) &&
gm_.variableFactorConnection(
grid_(i, j + 1), gmFactorIndex)) {
673 }
else if((i != 0) &&
gm_.variableFactorConnection(
grid_(i - 1, j), gmFactorIndex)) {
675 }
else if((j != 0) &&
gm_.variableFactorConnection(
grid_(i, j - 1), gmFactorIndex)) {
687 if(
gm_[i].numberOfVariables() == 2) {
691 const std::pair<IndexType, IndexType> variables(a, b);
694 const std::pair<IndexType, IndexType> variables(b, a);
706 typename std::vector<IndexType>::const_iterator iter;
707 for(iter =
mySelfView_->firstOrderFactorLookupTable_[pix].begin(); iter !=
mySelfView_->firstOrderFactorLookupTable_[pix].end(); iter++) {
719 typedef typename std::vector<IndexType>::const_iterator vecIter;
721 if(pix2 == pix1 + 1) {
723 for(vecIter iter =
mySelfView_->horizontalSecondOrderFactorLookupTable_[pix1].begin(); iter !=
mySelfView_->horizontalSecondOrderFactorLookupTable_[pix1].end(); iter++) {
728 for(vecIter iter =
mySelfView_->verticalSecondOrderFactorLookupTable_[pix1].begin(); iter !=
mySelfView_->verticalSecondOrderFactorLookupTable_[pix1].end(); iter++) {
733 if(pix1 == pix2 + 1) {
735 for(vecIter iter =
mySelfView_->horizontalSecondOrderFactorLookupTable_[pix2].begin(); iter !=
mySelfView_->horizontalSecondOrderFactorLookupTable_[pix2].end(); iter++) {
740 for(vecIter iter =
mySelfView_->verticalSecondOrderFactorLookupTable_[pix2].begin(); iter !=
mySelfView_->verticalSecondOrderFactorLookupTable_[pix2].end(); iter++) {
754 typedef typename std::vector<IndexType>::const_iterator vecIter;
755 typedef typename std::map<std::pair<IndexType, IndexType>, std::vector<IndexType> >::const_iterator mapIter;
757 const std::pair<IndexType, IndexType> variables(pix1, pix2);
758 mapIter generalSecondOrderFactors =
mySelfView_->generalSecondOrderFactorLookupTable_.find(variables);
759 if(generalSecondOrderFactors !=
mySelfView_->generalSecondOrderFactorLookupTable_.end()) {
760 for(vecIter iter = generalSecondOrderFactors->second.begin(); iter != generalSecondOrderFactors->second.end(); iter++) {
765 const std::pair<IndexType, IndexType> variables(pix2, pix1);
766 mapIter generalSecondOrderFactors =
mySelfView_->generalSecondOrderFactorLookupTable_.find(variables);
767 if(generalSecondOrderFactors !=
mySelfView_->generalSecondOrderFactorLookupTable_.end()) {
768 for(vecIter iter = generalSecondOrderFactors->second.begin(); iter != generalSecondOrderFactors->second.end(); iter++) {
781 for(
IndexType i = 0; i <
gm_.numberOfVariables(); i++) {
783 for(
IndexType j = 0; j <
gm_.numberOfFactors(gmVariableIndex); j++) {
784 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, j);
785 if(
gm_.numberOfVariables(gmFactorIndex) == 1) {
800 for(
IndexType k = 0; k <
gm_.numberOfFactors(gmVariableIndex); k++) {
801 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, k);
802 if(
gm_.numberOfVariables(gmFactorIndex) == 2) {
803 if((i < sizeX_ - 1) &&
gm_.variableFactorConnection(
grid_(i + 1, j), gmFactorIndex)) {
812 }
else if((j < sizeY_ -1 ) &&
gm_.variableFactorConnection(
grid_(i, j + 1), gmFactorIndex)) {
821 }
else if((i != 0) &&
gm_.variableFactorConnection(
grid_(i - 1, j), gmFactorIndex)) {
824 }
else if((j != 0) &&
gm_.variableFactorConnection(
grid_(i, j - 1), gmFactorIndex)) {
849 if(pix2 == pix1 + 1) {
857 if(pix1 == pix2 + 1) {
std::map< std::pair< IndexType, IndexType >, std::vector< IndexType > > generalSecondOrderFactorLookupTable_
bool doNotUseGrid_
Do not use grid structure.
void generateEnergyView()
GM::ValueType value() const
return the solution (value)
InferenceType
possible optimization algorithms for GCOLIB
std::vector< std::vector< IndexType > > verticalSecondOrderFactorLookupTable_
const GraphicalModelType & graphicalModel() const
visitors::EmptyVisitor< GCOLIB< GM > > EmptyVisitorType
marray::Matrix< size_t > grid_
static const IndexType down_
opengm::Minimizer AccumulationType
static EnergyTermType firstOrderFactorViewAccess(int pix, int i)
virtual ValueType bound() const
return a bound on the solution
void setWeightedTableWeights()
#define OPENGM_ASSERT(expression)
GM::ValueType bound() const
return a bound on the solution
static EnergyTermType secondOrderFactorViewGeneralAccess(int pix1, int pix2, int i, int j)
static EnergyTermType secondOrderFactorViewGridAccess(int pix1, int pix2, int i, int j)
InferenceTermination arg(std::vector< LabelType > &, const size_t &=1) const
EnergyType energyType_
selected energy type
gcoLib::GCoptimizationGridGraph * GCOGridGraph_
const GraphicalModelType & gm_
void generateFirstOrderFactorLookupTable()
GraphicalModelType::IndexType IndexType
gcoLib::GCoptimization::EnergyTermType EnergyTermType
std::vector< std::vector< IndexType > > firstOrderFactorLookupTable_
static const IndexType right_
InferenceTermination infer()
std::vector< EnergyTermType > secondOrderFactorGridValues
GCOLIB(const GraphicalModelType &gm, const Parameter ¶)
EnergyType
possible energy types for GCOLIB
bool symmetricEnergyTable() const
bool useAdaptiveCycles_
Use adaptive cycles for alpha-expansion.
GraphicalModelType::ValueType ValueType
bool sameEnergyTable() const
Inference algorithm interface.
static EnergyTermType firstOrderFactorTablesAccess(int pix, int i)
bool randomLabelOrder_
Enable random label order. By default, the labels for the swap and expansion algorithms are visited i...
Parameter(const InferenceType inferenceType=EXPANSION, const EnergyType energyType=VIEW, const size_t numberOfIterations=1000)
void generateEnergyWeightedTable()
static GCOLIB< GM > * mySelfView_
gcoLib::GCoptimizationGeneralGraph * GCOGeneralGraph_
bool hasSameLabelNumber() const
visitors::VerboseVisitor< GCOLIB< GM > > VerboseVisitorType
static GCOLIB< GM > * mySelfTables_
void generateEnergyTables()
const IndexType numNodes_
std::vector< EnergyTermType > firstOrderFactorValues
GCOLIB GCOLIB inference algorithm class.
const size_t shape(const size_t) const
Get the shape in one dimension.
Minimization as a unary accumulation.
const LabelType numLabels_
InferenceType inferenceType_
selected optimization algorithm
static const size_t ContinueInf
std::vector< std::vector< IndexType > > horizontalSecondOrderFactorLookupTable_
size_t numberOfIterations_
number of iterations
void generateSecondOrderFactorLookupTables()
static EnergyTermType secondOrderFactorTablesGridAccess(int pix1, int pix2, int i, int j)
visitors::TimingVisitor< GCOLIB< GM > > TimingVisitorType