1 #ifndef OPENGM_LP_INFERENCE_BASE_HXX_ 2 #define OPENGM_LP_INFERENCE_BASE_HXX_ 25 template <
class LP_INFERENCE_TYPE>
28 template <
class LP_INFERENCE_TYPE>
29 class LPInferenceBase :
public Inference<typename LPInferenceTraits<LP_INFERENCE_TYPE>::GraphicalModelType, typename LPInferenceTraits<LP_INFERENCE_TYPE>::AccumulationType> {
53 typedef typename LPInferenceTraitsType::SolverType
SolverType;
89 template<
class VISITOR_TYPE>
117 template<
class LINEAR_CONSTRAINT_FUNCTION_TYPE>
118 void operator()(
const LINEAR_CONSTRAINT_FUNCTION_TYPE& linearConstraintFunction);
120 template<
class FUNCTION_TYPE,
bool IS_LINEAR_CONSTRAINT_FUNCTION>
124 template<
class FUNCTION_TYPE>
133 template<
class LINEAR_CONSTRAINT_FUNCTION_TYPE>
134 void operator()(
const LINEAR_CONSTRAINT_FUNCTION_TYPE& linearConstraintFunction);
136 template<
class FUNCTION_TYPE,
bool IS_LINEAR_CONSTRAINT_FUNCTION>
140 template<
class FUNCTION_TYPE>
149 template<
class LINEAR_CONSTRAINT_FUNCTION_TYPE>
150 void operator()(
const LINEAR_CONSTRAINT_FUNCTION_TYPE& linearConstraintFunction);
152 template<
class FUNCTION_TYPE,
bool IS_LINEAR_CONSTRAINT_FUNCTION>
156 template<
class FUNCTION_TYPE>
165 template<
class LINEAR_CONSTRAINT_FUNCTION_TYPE>
166 void operator()(
const LINEAR_CONSTRAINT_FUNCTION_TYPE& linearConstraintFunction);
168 template<
class FUNCTION_TYPE,
bool IS_LINEAR_CONSTRAINT_FUNCTION>
172 template<
class FUNCTION_TYPE>
185 template<
class LINEAR_CONSTRAINT_FUNCTION_TYPE>
186 void operator()(
const LINEAR_CONSTRAINT_FUNCTION_TYPE& linearConstraintFunction);
188 template<
class FUNCTION_TYPE,
bool IS_LINEAR_CONSTRAINT_FUNCTION>
192 template<
class FUNCTION_TYPE>
205 template<
class LINEAR_CONSTRAINT_FUNCTION_TYPE>
206 void operator()(
const LINEAR_CONSTRAINT_FUNCTION_TYPE& linearConstraintFunction);
208 template<
class FUNCTION_TYPE,
bool IS_LINEAR_CONSTRAINT_FUNCTION>
212 template<
class FUNCTION_TYPE>
224 const GraphicalModelType&
gm_;
271 template<
class LABELING_ITERATOR_TYPE>
272 SolverIndexType
factorLPVariableIndex(
const IndexType factorID, LABELING_ITERATOR_TYPE labelingBegin,
const LABELING_ITERATOR_TYPE labelingEnd)
const;
273 template <
class HIGHER_ORDER_FACTORS_MAP_TYPE,
class INDICATOR_VARIABLES_MAP_TYPE>
274 bool getLPVariableIndexFromIndicatorVariable(
const HIGHER_ORDER_FACTORS_MAP_TYPE& higherOrderFactorVariablesLookupTable,
const INDICATOR_VARIABLES_MAP_TYPE& indicatorVariablesLookupTable,
const IndicatorVariableType& indicatorVariable,
const IndexType linearConstraintFactorIndex, SolverIndexType& lpVariableIndex)
const;
283 template <
class VISITOR_TYPE>
285 template <
class VISITOR_TYPE,
typename Parameter::Relaxation RELAXATION>
287 template <
class VISITOR_TYPE,
typename Parameter::Relaxation RELAXATION,
typename Parameter::ChallengeHeuristic HEURISTIC>
289 template <
class VISITOR_TYPE,
typename Parameter::Relaxation RELAXATION,
typename Parameter::ChallengeHeuristic HEURISTIC,
bool USE_INFINITE_ITERATIONS>
291 template <
class VISITOR_TYPE,
typename Parameter::Relaxation RELAXATION,
typename Parameter::ChallengeHeuristic HEURISTIC,
bool USE_INFINITE_ITERATIONS,
bool ADD_ALL_VIOLATED_CONSTRAINTS>
293 template <
class VISITOR_TYPE,
typename Parameter::Relaxation RELAXATION,
typename Parameter::ChallengeHeuristic HEURISTIC,
bool USE_INFINITE_ITERATIONS,
bool ADD_ALL_VIOLATED_CONSTRAINTS,
bool USE_INTEGER_CONSTRAINTS>
295 template <
typename Parameter::Relaxation RELAXATION,
typename Parameter::ChallengeHeuristic HEURISTIC,
bool ADD_ALL_VIOLATED_CONSTRAINTS>
297 template <
typename Parameter::Relaxation RELAXATION,
typename Parameter::ChallengeHeuristic HEURISTIC,
bool ADD_ALL_VIOLATED_CONSTRAINTS>
303 template <
class LP_INFERENCE_BASE_TYPE,
typename LP_INFERENCE_BASE_TYPE::Parameter::ChallengeHeuristic HEURISTIC>
305 template <
class LP_INFERENCE_BASE_TYPE,
typename LP_INFERENCE_BASE_TYPE::Parameter::ChallengeHeuristic HEURISTIC>
309 template <
class LP_INFERENCE_BASE_TYPE,
typename LP_INFERENCE_BASE_TYPE::Parameter::ChallengeHeuristic HEURISTIC>
313 typename LP_INFERENCE_BASE_TYPE::IntegerSolutionSubsequenceIterator
labelingBegin_;
319 template<
class LINEAR_CONSTRAINT_FUNCTION_TYPE>
320 void operator()(
const LINEAR_CONSTRAINT_FUNCTION_TYPE& linearConstraintFunction);
322 template<
class FUNCTION_TYPE,
bool IS_LINEAR_CONSTRAINT_FUNCTION>
326 template<
class FUNCTION_TYPE>
331 template <
class LP_INFERENCE_BASE_TYPE,
typename LP_INFERENCE_BASE_TYPE::Parameter::ChallengeHeuristic HEURISTIC>
335 typename LP_INFERENCE_BASE_TYPE::RelaxedSolutionSubsequenceIterator
labelingBegin_;
341 template<
class LINEAR_CONSTRAINT_FUNCTION_TYPE>
342 void operator()(
const LINEAR_CONSTRAINT_FUNCTION_TYPE& linearConstraintFunction);
344 template<
class FUNCTION_TYPE,
bool IS_LINEAR_CONSTRAINT_FUNCTION>
348 template<
class FUNCTION_TYPE>
1751 template <
class LP_INFERENCE_TYPE>
1762 template <
class LP_INFERENCE_TYPE>
1767 template <
class LP_INFERENCE_TYPE>
1769 EmptyVisitorType visitor;
1770 return infer(visitor);
1773 template <
class LP_INFERENCE_TYPE>
1774 template<
class VISITOR_TYPE>
1782 template <
class LP_INFERENCE_TYPE>
1784 if(inferenceStarted_) {
1785 return static_cast<const LPInferenceType*
>(
this)->objectiveFunctionValueBound() +
constValue_;
1788 return AccumulationType::template ineutral<ValueType>();
1792 template <
class LP_INFERENCE_TYPE>
1794 std::vector<LabelType> states;
1796 return gm_.evaluate(states);
1799 template <
class LP_INFERENCE_TYPE>
1801 x.resize(gm_.numberOfVariables());
1802 if(inferenceStarted_) {
1803 SolverSolutionIteratorType solutionIterator =
static_cast<const LPInferenceType*
>(
this)->solutionBegin();
1804 for(
IndexType node = 0; node < gm_.numberOfVariables(); ++node) {
1805 SolverIndexType currentNodeLPVariable = nodesLPVariablesOffset_[node];
1806 SolverValueType bestValue = solutionIterator[currentNodeLPVariable];
1808 for(
LabelType i = 1; i < gm_.numberOfLabels(node); ++i) {
1809 ++currentNodeLPVariable;
1810 const SolverValueType currentValue = solutionIterator[currentNodeLPVariable];
1811 if(currentValue > bestValue) {
1812 bestValue = currentValue;
1820 for(
IndexType node = 0; node < gm_.numberOfVariables(); ++node) {
1827 template <
class LP_INFERENCE_TYPE>
1829 : gm_(gm), parameter_(parameter), constValue_(0.0), unaryFactors_(),
1830 higherOrderFactors_(), linearConstraintFactors_(), transferableFactors_(),
1831 inferenceStarted_(false), numLPVariables_(0), numNodesLPVariables_(0),
1832 numFactorsLPVariables_(0), numLinearConstraintsLPVariables_(0),
1833 numTransferedFactorsLPVariables(0), numSlackVariables_(0),
1834 nodesLPVariablesOffset_(gm_.numberOfVariables()),
1835 factorsLPVariablesOffset_(gm_.numberOfFactors()),
1836 linearConstraintsLPVariablesIndicesLookupTable_(),
1837 transferedFactorsLPVariablesIndicesLookupTable_(),
1838 linearConstraintLPVariablesSubsequenceIndices_(),
1839 addLocalPolytopeFactorConstraintCachePreviousFactorID_(gm_.numberOfFactors()),
1840 addLocalPolytopeFactorConstraintCacheFactorLPVariableIDs_(),
1841 inactiveConstraints_() {
1842 if(!opengm::meta::Compare<OperatorType, opengm::Adder>::value) {
1843 throw RuntimeError(
"This implementation does only supports Min-Sum-Semiring and Max-Sum-Semiring.");
1852 fillLinearConstraintLPVariablesSubsequenceIndices();
1861 createObjectiveFunction();
1864 switch(parameter_.relaxation_){
1865 case Parameter::LocalPolytope : {
1866 addLocalPolytopeConstraints();
1869 case Parameter::LoosePolytope : {
1870 addLoosePolytopeConstraints();
1873 case Parameter::TightPolytope: {
1874 addTightPolytopeConstraints();
1883 static_cast<LPInferenceType*
>(
this)->addConstraintsFinished();
1887 addLocalPolytopeFactorConstraintCachePreviousFactorID_ = gm_.numberOfFactors();
1890 template <
class LP_INFERENCE_TYPE>
1895 template <
class LP_INFERENCE_TYPE>
1898 for(
IndexType factorIndex = 0; factorIndex < gm_.numberOfFactors(); ++factorIndex){
1899 gm_[factorIndex].callFunctor(isTransferableFunctor);
1900 if((!parameter_.useSoftConstraints_) && gm_[factorIndex].isLinearConstraint()) {
1901 linearConstraintFactors_.push_back(factorIndex);
1902 }
else if(parameter_.useFunctionTransfer_ && isTransferableFunctor.
isTransferable_) {
1903 transferableFactors_.push_back(factorIndex);
1904 }
else if(gm_[factorIndex].numberOfVariables() == 0) {
1906 constValue_ += gm_[factorIndex](&l);
1907 }
else if(gm_[factorIndex].numberOfVariables() == 1) {
1908 unaryFactors_.push_back(factorIndex);
1909 }
else if(gm_[factorIndex].numberOfVariables() > 1) {
1910 higherOrderFactors_.push_back(factorIndex);
1915 template <
class LP_INFERENCE_TYPE>
1918 for(
IndexType node = 0; node < gm_.numberOfVariables(); ++node){
1919 numNodesLPVariables_ += gm_.numberOfLabels(node);
1920 nodesLPVariablesOffset_[node] = numLPVariables_;
1921 numLPVariables_ += gm_.numberOfLabels(node);
1925 for(
IndexType i = 0; i < unaryFactors_.size(); ++i) {
1926 const IndexType factorIndex = unaryFactors_[i];
1927 const IndexType node = gm_[factorIndex].variableIndex(0);
1928 factorsLPVariablesOffset_[factorIndex] = nodesLPVariablesOffset_[node];
1934 std::map<std::vector<IndexType>,
IndexType> higherOrderFactorVariablesLookupTable;
1935 for(
IndexType i = 0; i < higherOrderFactors_.size(); ++i) {
1936 const IndexType factorIndex = higherOrderFactors_[i];
1937 bool duplicate =
false;
1938 if(parameter_.mergeParallelFactors_) {
1940 std::vector<IndexType> currentFactorVariables(gm_[factorIndex].numberOfVariables());
1941 for(
IndexType j = 0; j < gm_[factorIndex].numberOfVariables(); ++j) {
1942 currentFactorVariables[j] = gm_[factorIndex].variableIndex(j);
1944 const typename std::map<std::vector<IndexType>,
IndexType>::const_iterator iter = higherOrderFactorVariablesLookupTable.find(currentFactorVariables);
1945 if(iter != higherOrderFactorVariablesLookupTable.end()) {
1947 factorsLPVariablesOffset_[factorIndex] = factorsLPVariablesOffset_[iter->second];
1950 higherOrderFactorVariablesLookupTable[currentFactorVariables] = factorIndex;
1954 const size_t currentSize = gm_[factorIndex].size();
1955 numFactorsLPVariables_ += currentSize;
1956 factorsLPVariablesOffset_[factorIndex] = numLPVariables_;
1957 numLPVariables_ += currentSize;
1961 OPENGM_ASSERT(numLPVariables_ == numNodesLPVariables_ + numFactorsLPVariables_);
1966 std::map<std::pair<typename IndicatorVariableType::LogicalOperatorType, std::vector<std::pair<IndexType, LabelType> > >, SolverIndexType> linearConstraintIndicatorVariablesLookupTable;
1967 GetIndicatorVariablesOrderBeginFunctor getIndicatorVariablesOrderBegin;
1968 GetIndicatorVariablesOrderEndFunctor getIndicatorVariablesOrderEnd;
1969 for(
IndexType i = 0; i < linearConstraintFactors_.size(); ++i) {
1970 const IndexType currentLinearConstraintFactorIndex = linearConstraintFactors_[i];
1971 factorsLPVariablesOffset_[currentLinearConstraintFactorIndex] = numLPVariables_;
1972 gm_[currentLinearConstraintFactorIndex].callFunctor(getIndicatorVariablesOrderBegin);
1973 IndicatorVariablesIteratorType currentIndicatorVariablesBegin = getIndicatorVariablesOrderBegin.indicatorVariablesOrderBegin_;
1974 gm_[currentLinearConstraintFactorIndex].callFunctor(getIndicatorVariablesOrderEnd);
1975 const IndicatorVariablesIteratorType currentIndicatorVariablesEnd = getIndicatorVariablesOrderEnd.indicatorVariablesOrderEnd_;
1977 linearConstraintsLPVariablesIndicesLookupTable_.push_back(std::map<const IndicatorVariableType, SolverIndexType>());
1978 const size_t numIndicatorVariables = std::distance(currentIndicatorVariablesBegin, currentIndicatorVariablesEnd);
1979 for(
size_t j = 0; j < numIndicatorVariables; ++j) {
1980 const IndicatorVariableType& currentIndicatorVariable = *currentIndicatorVariablesBegin;
1982 SolverIndexType lpVariableIndex = std::numeric_limits<SolverIndexType>::max();
1983 const bool matchingLPVariableIndexFound = getLPVariableIndexFromIndicatorVariable(higherOrderFactorVariablesLookupTable, linearConstraintIndicatorVariablesLookupTable, currentIndicatorVariable, currentLinearConstraintFactorIndex, lpVariableIndex);
1985 if(matchingLPVariableIndexFound) {
1986 linearConstraintsLPVariablesIndicesLookupTable_.back()[currentIndicatorVariable] = lpVariableIndex;
1989 linearConstraintsLPVariablesIndicesLookupTable_.back()[currentIndicatorVariable] = numLPVariables_;
1990 const size_t currentIndicatorVariableSize = std::distance(currentIndicatorVariable.
begin(), currentIndicatorVariable.
end());
1991 std::vector<std::pair<IndexType, LabelType> > currentVariableLabelPairs(currentIndicatorVariableSize);
1992 for(
size_t k = 0; k < currentIndicatorVariableSize; ++k) {
1993 currentVariableLabelPairs[k] = std::pair<IndexType, LabelType>(gm_[currentLinearConstraintFactorIndex].variableIndex((currentIndicatorVariable.
begin() + k)->first), (currentIndicatorVariable.
begin() + k)->second);
1995 linearConstraintIndicatorVariablesLookupTable[make_pair(currentIndicatorVariable.
getLogicalOperatorType(), currentVariableLabelPairs)] = numLPVariables_;
1996 ++numLinearConstraintsLPVariables_;
1999 ++currentIndicatorVariablesBegin;
2001 OPENGM_ASSERT(currentIndicatorVariablesBegin == currentIndicatorVariablesEnd);
2004 OPENGM_ASSERT(linearConstraintFactors_.size() == linearConstraintsLPVariablesIndicesLookupTable_.size());
2005 OPENGM_ASSERT(numLPVariables_ == numNodesLPVariables_ + numFactorsLPVariables_ + numLinearConstraintsLPVariables_);
2010 for(
IndexType i = 0; i < transferableFactors_.size(); ++i) {
2011 const IndexType currentTransferableFactorIndex = transferableFactors_[i];
2012 factorsLPVariablesOffset_[currentTransferableFactorIndex] = numLPVariables_;
2015 gm_[currentTransferableFactorIndex].callFunctor(numSlackVariablesFunctor);
2019 transferedFactorsLPVariablesIndicesLookupTable_.push_back(std::map<const IndicatorVariableType, SolverIndexType>());
2022 IndicatorVariablesContainerType currentIndicatorVariables;
2023 getIndicatorVariablesFunctor.
variables_ = ¤tIndicatorVariables;
2024 gm_[currentTransferableFactorIndex].callFunctor(getIndicatorVariablesFunctor);
2027 for(IndicatorVariablesIteratorType iter = currentIndicatorVariables.begin(); iter != currentIndicatorVariables.end(); ++iter) {
2028 const IndicatorVariableType& currentIndicatorVariable = *iter;
2029 const size_t currentIndicatorVariableSize = std::distance(currentIndicatorVariable.
begin(), currentIndicatorVariable.
end());
2031 if(currentIndicatorVariableSize == 1) {
2032 if(currentIndicatorVariable.
begin()->first >= gm_[currentTransferableFactorIndex].numberOfVariables()) {
2034 transferedFactorsLPVariablesIndicesLookupTable_.back()[currentIndicatorVariable] = numSlackVariables_ + currentNumSlackVariables;
2035 ++currentNumSlackVariables;
2040 SolverIndexType lpVariableIndex;
2041 const bool matchingLPVariableIndexFound = getLPVariableIndexFromIndicatorVariable(higherOrderFactorVariablesLookupTable, linearConstraintIndicatorVariablesLookupTable, currentIndicatorVariable, currentTransferableFactorIndex, lpVariableIndex);
2043 if(matchingLPVariableIndexFound) {
2044 transferedFactorsLPVariablesIndicesLookupTable_.back()[currentIndicatorVariable] = lpVariableIndex;
2047 transferedFactorsLPVariablesIndicesLookupTable_.back()[currentIndicatorVariable] = numLPVariables_;
2048 const size_t currentIndicatorVariableSize = std::distance(currentIndicatorVariable.
begin(), currentIndicatorVariable.
end());
2049 std::vector<std::pair<IndexType, LabelType> > currentVariableLabelPairs(currentIndicatorVariableSize);
2050 for(
size_t j = 0; j < currentIndicatorVariableSize; ++j) {
2051 currentVariableLabelPairs[j] = std::pair<IndexType, LabelType>(gm_[currentTransferableFactorIndex].variableIndex((currentIndicatorVariable.
begin() + j)->first), (currentIndicatorVariable.
begin() + j)->second);
2053 linearConstraintIndicatorVariablesLookupTable[make_pair(currentIndicatorVariable.
getLogicalOperatorType(), currentVariableLabelPairs)] = numLPVariables_;
2054 ++numTransferedFactorsLPVariables;
2065 for(
IndexType i = 0; i < transferableFactors_.size(); ++i) {
2066 const IndexType currentTransferableFactorIndex = transferableFactors_[i];
2069 IndicatorVariablesContainerType slackVariables;
2070 getSlackVariablesOrderFunctor.
order_ = &slackVariables;
2071 gm_[currentTransferableFactorIndex].callFunctor(getSlackVariablesOrderFunctor);
2074 for(IndicatorVariablesIteratorType iter = slackVariables.begin(); iter != slackVariables.end(); ++iter) {
2075 const IndicatorVariableType& currentSlackVariable = *iter;
2076 transferedFactorsLPVariablesIndicesLookupTable_[i][currentSlackVariable] += numLPVariables_;
2080 OPENGM_ASSERT(transferableFactors_.size() == transferedFactorsLPVariablesIndicesLookupTable_.size());
2081 OPENGM_ASSERT(numLPVariables_ == numNodesLPVariables_ + numFactorsLPVariables_ + numLinearConstraintsLPVariables_ + numTransferedFactorsLPVariables);
2084 template <
class LP_INFERENCE_TYPE>
2086 linearConstraintLPVariablesSubsequenceIndices_.resize(linearConstraintFactors_.size());
2087 if(parameter_.integerConstraintNodeVar_ || parameter_.integerConstraintFactorVar_) {
2088 for(
size_t i = 0; i < linearConstraintFactors_.size(); ++i) {
2089 const IndexType currentFactor = linearConstraintFactors_[i];
2090 const size_t numVariables = gm_[currentFactor].numberOfVariables();
2091 linearConstraintLPVariablesSubsequenceIndices_[i].resize(numVariables);
2092 for(
size_t j = 0; j < numVariables; ++j) {
2093 linearConstraintLPVariablesSubsequenceIndices_[i][j] = gm_[currentFactor].variableIndex(j);
2097 GetIndicatorVariablesOrderBeginFunctor getIndicatorVariablesOrderBegin;
2098 GetIndicatorVariablesOrderEndFunctor getIndicatorVariablesOrderEnd;
2099 for(
size_t i = 0; i < linearConstraintFactors_.size(); ++i) {
2100 const IndexType currentFactor = linearConstraintFactors_[i];
2101 gm_[currentFactor].callFunctor(getIndicatorVariablesOrderBegin);
2102 gm_[currentFactor].callFunctor(getIndicatorVariablesOrderEnd);
2103 const IndicatorVariablesIteratorType currentIndicatorVariablesEnd = getIndicatorVariablesOrderEnd.indicatorVariablesOrderEnd_;
2104 for(IndicatorVariablesIteratorType iter = getIndicatorVariablesOrderBegin.indicatorVariablesOrderBegin_; iter != currentIndicatorVariablesEnd; ++iter) {
2105 linearConstraintLPVariablesSubsequenceIndices_[i].push_back(linearConstraintsLPVariablesIndicesLookupTable_[i][*iter]);
2111 template <
class LP_INFERENCE_TYPE>
2113 if(meta::Compare<AccumulationType, Minimizer>::value) {
2114 static_cast<LPInferenceType*
>(
this)->setObjective(LPInferenceType::Minimize);
2115 }
else if(meta::Compare<AccumulationType, Maximizer>::value) {
2116 static_cast<LPInferenceType*
>(
this)->setObjective(LPInferenceType::Maximize);
2118 throw RuntimeError(
"This implementation of lp inference does only support Minimizer or Maximizer accumulators");
2122 template <
class LP_INFERENCE_TYPE>
2124 if(parameter_.integerConstraintNodeVar_) {
2125 static_cast<LPInferenceType*
>(
this)->addBinaryVariables(numNodesLPVariables_);
2127 static_cast<LPInferenceType*
>(
this)->addContinuousVariables(numNodesLPVariables_, 0.0, 1.0);
2130 if(parameter_.integerConstraintFactorVar_) {
2131 static_cast<LPInferenceType*
>(
this)->addBinaryVariables(numFactorsLPVariables_);
2132 static_cast<LPInferenceType*
>(
this)->addBinaryVariables(numLinearConstraintsLPVariables_);
2133 static_cast<LPInferenceType*
>(
this)->addBinaryVariables(numTransferedFactorsLPVariables);
2135 static_cast<LPInferenceType*
>(
this)->addContinuousVariables(numFactorsLPVariables_, 0.0, 1.0);
2136 static_cast<LPInferenceType*
>(
this)->addContinuousVariables(numLinearConstraintsLPVariables_, 0.0, 1.0);
2137 static_cast<LPInferenceType*
>(
this)->addContinuousVariables(numTransferedFactorsLPVariables, 0.0, 1.0);
2141 static_cast<LPInferenceType*
>(
this)->addContinuousVariables(numSlackVariables_, 0.0, LPInferenceType::infinity());
2144 template <
class LP_INFERENCE_TYPE>
2146 std::vector<SolverValueType> objective(numNodesLPVariables_ + numFactorsLPVariables_ + numLinearConstraintsLPVariables_ + numTransferedFactorsLPVariables + numSlackVariables_, 0.0);
2149 for(
IndexType i = 0; i < unaryFactors_.size(); i++) {
2150 const IndexType factorIndex = unaryFactors_[i];
2151 const IndexType node = gm_[factorIndex].variableIndex(0);
2152 for(
LabelType j = 0; j < gm_.numberOfLabels(node); j++) {
2153 objective[nodeLPVariableIndex(node, j)] +=
static_cast<SolverValueType
>(gm_[factorIndex](&j));
2158 for(
IndexType i = 0; i < higherOrderFactors_.size(); i++) {
2159 const IndexType factorIndex = higherOrderFactors_[i];
2161 std::vector<ValueType> tempValues(gm_[factorIndex].size());
2162 gm_[factorIndex].copyValues(tempValues.begin());
2163 for(
size_t j = 0; j < tempValues.size(); ++j) {
2164 objective[factorLPVariableIndex(factorIndex, j)] +=
static_cast<SolverValueType
>(tempValues[j]);
2171 for(
IndexType i = 0; i < transferableFactors_.size(); ++i) {
2172 const IndexType currentTransferableFactorIndex = transferableFactors_[i];
2175 IndicatorVariablesContainerType slackVariables;
2176 getSlackVariablesOrderFunctor.
order_ = &slackVariables;
2177 gm_[currentTransferableFactorIndex].callFunctor(getSlackVariablesOrderFunctor);
2181 getSlackVariablesObjectiveCoefficientsFunctor.
coefficients_ = &coefficients;
2182 gm_[currentTransferableFactorIndex].callFunctor(getSlackVariablesObjectiveCoefficientsFunctor);
2184 OPENGM_ASSERT(coefficients.size() == slackVariables.size());
2187 for(
size_t j = 0; j < slackVariables.size(); ++j) {
2188 const IndicatorVariableType& currentSlackVariable = slackVariables[j];
2189 const ValueType currentCoefficient = coefficients[j];
2190 const SolverIndexType currentSlackVariableLPVariableIndex = transferedFactorsLPVariablesIndicesLookupTable_[i][currentSlackVariable];
2191 objective[currentSlackVariableLPVariableIndex] +=
static_cast<SolverValueType
>(currentCoefficient);
2194 static_cast<LPInferenceType*
>(
this)->setObjectiveValue(objective.begin(), objective.end());
2197 template <
class LP_INFERENCE_TYPE>
2200 for(
IndexType i = 0; i < gm_.numberOfVariables(); ++i) {
2201 addLocalPolytopeVariableConstraint(i,
true);
2205 for(
IndexType i = 0; i < higherOrderFactors_.size(); ++i) {
2206 const IndexType factorIndex = higherOrderFactors_[i];
2207 for(
IndexType j = 0; j < gm_[factorIndex].numberOfVariables(); ++j) {
2208 const IndexType node = gm_[factorIndex].variableIndex(j);
2209 for(
LabelType k = 0; k < gm_.numberOfLabels(node); k++) {
2210 addLocalPolytopeFactorConstraint(i, j, k,
true);
2216 GetIndicatorVariablesOrderBeginFunctor getIndicatorVariablesOrderBegin;
2217 GetIndicatorVariablesOrderEndFunctor getIndicatorVariablesOrderEnd;
2218 for(
IndexType i = 0; i < linearConstraintFactors_.size(); ++i) {
2219 gm_[linearConstraintFactors_[i]].callFunctor(getIndicatorVariablesOrderBegin);
2220 gm_[linearConstraintFactors_[i]].callFunctor(getIndicatorVariablesOrderEnd);
2221 const size_t linearConstraintNumIndicatorVariables = std::distance(getIndicatorVariablesOrderBegin.indicatorVariablesOrderBegin_, getIndicatorVariablesOrderEnd.indicatorVariablesOrderEnd_);
2222 for(
size_t j = 0; j < linearConstraintNumIndicatorVariables; ++j) {
2223 const IndexType currentFactor = linearConstraintFactors_[i];
2224 const IndicatorVariableType& currentIndicatorVariable = getIndicatorVariablesOrderBegin.indicatorVariablesOrderBegin_[j];
2225 const SolverIndexType currentLPVariable = linearConstraintsLPVariablesIndicesLookupTable_[i][currentIndicatorVariable];
2226 addIndicatorVariableConstraints(currentFactor, currentIndicatorVariable, currentLPVariable,
true);
2232 for(
IndexType i = 0; i < transferableFactors_.size(); ++i) {
2234 getIndicatorVariablesFunctor.
variables_ = ¤tIndicatorVariables;
2235 gm_[transferableFactors_[i]].callFunctor(getIndicatorVariablesFunctor);
2236 const size_t transformedFactorsNumIndicatorVariables = currentIndicatorVariables.size();
2237 for(
size_t j = 0; j < transformedFactorsNumIndicatorVariables; ++j) {
2238 const IndexType currentFactor = transferableFactors_[i];
2239 const IndicatorVariableType& currentIndicatorVariable = currentIndicatorVariables[j];
2240 const SolverIndexType currentLPVariable = transferedFactorsLPVariablesIndicesLookupTable_[i][currentIndicatorVariable];
2241 addIndicatorVariableConstraints(currentFactor, currentIndicatorVariable, currentLPVariable,
true);
2247 for(
IndexType i = 0; i < transferableFactors_.size(); ++i) {
2248 const IndexType currentTransferableFactorIndex = transferableFactors_[i];
2251 getLinearConstraintsFunctor.
constraints_ = &constraints;
2252 gm_[currentTransferableFactorIndex].callFunctor(getLinearConstraintsFunctor);
2253 for(
size_t j = 0; j < constraints.size(); ++j) {
2254 const LinearConstraintType& currentConstraint = constraints[j];
2256 for(
size_t k = 0; k < lpVariables.size(); ++k) {
2257 lpVariables[k] = transferedFactorsLPVariablesIndicesLookupTable_[i][currentConstraint.
indicatorVariablesBegin()[k]];
2260 case LinearConstraintType::LinearConstraintOperatorType::LessEqual : {
2261 static_cast<LPInferenceType*
>(
this)->addLessEqualConstraint(lpVariables.begin(), lpVariables.end(), currentConstraint.
coefficientsBegin(),
static_cast<const SolverValueType
>(currentConstraint.
getBound()));
2264 case LinearConstraintType::LinearConstraintOperatorType::Equal : {
2265 static_cast<LPInferenceType*
>(
this)->addEqualityConstraint(lpVariables.begin(), lpVariables.end(), currentConstraint.
coefficientsBegin(),
static_cast<const SolverValueType
>(currentConstraint.
getBound()));
2270 static_cast<LPInferenceType*
>(
this)->addGreaterEqualConstraint(lpVariables.begin(), lpVariables.end(), currentConstraint.
coefficientsBegin(),
static_cast<const SolverValueType
>(currentConstraint.
getBound()));
2277 template <
class LP_INFERENCE_TYPE>
2280 for(
IndexType i = 0; i < gm_.numberOfVariables(); ++i) {
2281 addLocalPolytopeVariableConstraint(i,
true);
2285 for(
IndexType i = 0; i < higherOrderFactors_.size(); ++i) {
2286 const IndexType factorIndex = higherOrderFactors_[i];
2287 for(
IndexType j = 0; j < gm_[factorIndex].numberOfVariables(); ++j) {
2288 const IndexType node = gm_[factorIndex].variableIndex(j);
2289 for(
LabelType k = 0; k < gm_.numberOfLabels(node); k++) {
2290 addLocalPolytopeFactorConstraint(i, j, k,
false);
2296 GetIndicatorVariablesOrderBeginFunctor getIndicatorVariablesOrderBegin;
2297 GetIndicatorVariablesOrderEndFunctor getIndicatorVariablesOrderEnd;
2298 for(
IndexType i = 0; i < linearConstraintFactors_.size(); ++i) {
2299 gm_[linearConstraintFactors_[i]].callFunctor(getIndicatorVariablesOrderBegin);
2300 gm_[linearConstraintFactors_[i]].callFunctor(getIndicatorVariablesOrderEnd);
2301 const size_t linearConstraintNumIndicatorVariables = std::distance(getIndicatorVariablesOrderBegin.indicatorVariablesOrderBegin_, getIndicatorVariablesOrderEnd.indicatorVariablesOrderEnd_);
2302 for(
size_t j = 0; j < linearConstraintNumIndicatorVariables; ++j) {
2303 const IndexType currentFactor = linearConstraintFactors_[i];
2304 const IndicatorVariableType& currentIndicatorVariable = getIndicatorVariablesOrderBegin.indicatorVariablesOrderBegin_[j];
2305 const SolverIndexType currentLPVariable = linearConstraintsLPVariablesIndicesLookupTable_[i][currentIndicatorVariable];
2306 addIndicatorVariableConstraints(currentFactor, currentIndicatorVariable, currentLPVariable,
false);
2312 for(
IndexType i = 0; i < transferableFactors_.size(); ++i) {
2314 getIndicatorVariablesFunctor.
variables_ = ¤tIndicatorVariables;
2315 gm_[transferableFactors_[i]].callFunctor(getIndicatorVariablesFunctor);
2316 const size_t transformedFactorsNumIndicatorVariables = currentIndicatorVariables.size();
2317 for(
size_t j = 0; j < transformedFactorsNumIndicatorVariables; ++j) {
2318 const IndexType currentFactor = transferableFactors_[i];
2319 const IndicatorVariableType& currentIndicatorVariable = currentIndicatorVariables[j];
2320 const SolverIndexType currentLPVariable = transferedFactorsLPVariablesIndicesLookupTable_[i][currentIndicatorVariable];
2321 addIndicatorVariableConstraints(currentFactor, currentIndicatorVariable, currentLPVariable,
false);
2327 for(
IndexType i = 0; i < transferableFactors_.size(); ++i) {
2328 const IndexType currentTransferableFactorIndex = transferableFactors_[i];
2331 getLinearConstraintsFunctor.
constraints_ = &constraints;
2332 gm_[currentTransferableFactorIndex].callFunctor(getLinearConstraintsFunctor);
2333 for(
size_t j = 0; j < constraints.size(); ++j) {
2334 const LinearConstraintType& currentConstraint = constraints[j];
2336 for(
size_t k = 0; k < lpVariables.size(); ++k) {
2337 lpVariables[k] = transferedFactorsLPVariablesIndicesLookupTable_[i][currentConstraint.
indicatorVariablesBegin()[k]];
2340 std::stringstream constraintName;
2341 if(parameter_.nameConstraints_) {
2342 constraintName <<
"transfered factor " << currentTransferableFactorIndex <<
" constraint " << j <<
" of " << constraints.size();
2345 constraint.variableIDs_ = lpVariables;
2347 constraint.bound_ = currentConstraint.
getBound();
2349 constraint.name_ = constraintName.str();
2350 inactiveConstraints_.push_back(constraint);
2355 template <
class LP_INFERENCE_TYPE>
2357 addLocalPolytopeConstraints();
2360 GetLinearConstraintsBeginFunctor getLinearConstraintsBegin;
2361 GetLinearConstraintsEndFunctor getLinearConstraintsEnd;
2362 for(
IndexType i = 0; i < linearConstraintFactors_.size(); ++i) {
2363 const IndexType currentLinearConstraintFactorIndex = linearConstraintFactors_[i];
2364 gm_[currentLinearConstraintFactorIndex].callFunctor(getLinearConstraintsBegin);
2365 LinearConstraintsIteratorType currentLinearConstraintsBegin = getLinearConstraintsBegin.linearConstraintsBegin_;
2366 gm_[currentLinearConstraintFactorIndex].callFunctor(getLinearConstraintsEnd);
2367 const LinearConstraintsIteratorType currentLinearConstraintsEnd = getLinearConstraintsEnd.linearConstraintsEnd_;
2368 while(currentLinearConstraintsBegin != currentLinearConstraintsEnd) {
2369 addLinearConstraint(i, *currentLinearConstraintsBegin);
2370 ++currentLinearConstraintsBegin;
2375 template <
class LP_INFERENCE_TYPE>
2379 return nodesLPVariablesOffset_[nodeID] + label;
2382 template <
class LP_INFERENCE_TYPE>
2387 return factorsLPVariablesOffset_[factorID] + labelingIndex;
2390 template <
class LP_INFERENCE_TYPE>
2391 template<
class LABELING_ITERATOR_TYPE>
2394 OPENGM_ASSERT(static_cast<IndexType>(std::distance(labelingBegin, labelingEnd)) == gm_[factorID].numberOfVariables());
2396 const size_t numVar = gm_[factorID].numberOfVariables();
2397 size_t labelingIndex = *labelingBegin;
2399 size_t strides = gm_[factorID].numberOfLabels(0);
2400 for(
size_t i = 1; i < numVar; i++){
2401 OPENGM_ASSERT(*labelingBegin < gm_[factorID].numberOfLabels(i));
2402 labelingIndex += strides * (*labelingBegin);
2403 strides *= gm_[factorID].numberOfLabels(i);
2409 return factorLPVariableIndex(factorID, labelingIndex);
2412 template <
class LP_INFERENCE_TYPE>
2413 template <
class HIGHER_ORDER_FACTORS_MAP_TYPE,
class INDICATOR_VARIABLES_MAP_TYPE>
2415 const size_t indicatorVariableSize = std::distance(indicatorVariable.
begin(), indicatorVariable.
end());
2418 if(indicatorVariableSize == 1) {
2419 const IndexType currentNode = gm_[linearConstraintFactorIndex].variableIndex(indicatorVariable.
begin()->first);
2420 const LabelType currentLabel = indicatorVariable.
begin()->second;
2422 if(gm_.numberOfLabels(currentNode) == 2) {
2425 lpVariableIndex = nodeLPVariableIndex(currentNode, currentLabel == 0 ?
LabelType(1) :
LabelType(0));
2431 lpVariableIndex = nodeLPVariableIndex(currentNode, currentLabel);
2437 std::vector<IndexType> currentVariables(indicatorVariableSize);
2438 std::vector<LabelType> currentLabeling(indicatorVariableSize);
2439 for(
size_t i = 0; i < indicatorVariableSize; ++i) {
2440 currentVariables[i] = gm_[linearConstraintFactorIndex].variableIndex((indicatorVariable.
begin() + i)->first);
2441 currentLabeling[i] = (indicatorVariable.
begin() + i)->second;
2443 const typename HIGHER_ORDER_FACTORS_MAP_TYPE::const_iterator iter = higherOrderFactorVariablesLookupTable.find(currentVariables);
2444 if(iter != higherOrderFactorVariablesLookupTable.end()) {
2446 lpVariableIndex = factorLPVariableIndex(iter->second, currentLabeling.begin(), currentLabeling.end());
2451 std::vector<std::pair<IndexType, LabelType> > currentVariableLabelPairs(indicatorVariableSize);
2452 for(
size_t i = 0; i < indicatorVariableSize; ++i) {
2453 currentVariableLabelPairs[i] = std::pair<IndexType, LabelType>(gm_[linearConstraintFactorIndex].variableIndex((indicatorVariable.
begin() + i)->first), (indicatorVariable.
begin() + i)->second);
2455 const typename INDICATOR_VARIABLES_MAP_TYPE::const_iterator iter = indicatorVariablesLookupTable.find(make_pair(indicatorVariable.
getLogicalOperatorType(), currentVariableLabelPairs));
2456 if(iter != indicatorVariablesLookupTable.end()) {
2458 lpVariableIndex = iter->second;
2466 template <
class LP_INFERENCE_TYPE>
2469 static std::vector<SolverIndexType> variableIDs;
2470 static std::vector<SolverValueType> coefficients;
2473 const LabelType size = gm_.numberOfLabels(variableID);
2474 if(variableIDs.size() != size) {
2475 variableIDs.resize(size);
2476 coefficients.resize(size, 1.0);
2479 variableIDs[j] = nodeLPVariableIndex(variableID, j);
2482 std::stringstream constraintName;
2483 if(parameter_.nameConstraints_) {
2484 constraintName <<
"local polytope variable constraint of variable " << variableID;
2487 static_cast<LPInferenceType*
>(
this)->addEqualityConstraint(variableIDs.begin(), variableIDs.end(), coefficients.begin(), 1.0, constraintName.str());
2490 constraint.variableIDs_ = variableIDs;
2491 constraint.coefficients_ = coefficients;
2492 constraint.bound_ = 1.0;
2493 constraint.operator_ = LinearConstraintType::LinearConstraintOperatorType::Equal;
2494 constraint.name_ = constraintName.str();
2495 inactiveConstraints_.push_back(constraint);
2499 template <
class LP_INFERENCE_TYPE>
2502 OPENGM_ASSERT(variable < gm_[higherOrderFactors_[factor]].numberOfVariables());
2503 OPENGM_ASSERT(label < gm_[higherOrderFactors_[factor]].shape(variable));
2505 static std::vector<SolverIndexType> variableIDs;
2506 static std::vector<SolverValueType> coefficients;
2507 if(addLocalPolytopeFactorConstraintCachePreviousFactorID_ != higherOrderFactors_[factor]) {
2509 addLocalPolytopeFactorConstraintCachePreviousFactorID_ = higherOrderFactors_[factor];
2510 addLocalPolytopeFactorConstraintCacheFactorLPVariableIDs_.
resize(gm_[higherOrderFactors_[factor]].shapeBegin(), gm_[higherOrderFactors_[factor]].shapeEnd());
2511 SolverIndexType counter = factorLPVariableIndex(higherOrderFactors_[factor], 0);
2512 for(
typename marray::Marray<SolverIndexType>::iterator factorLPVariableIDsIter = addLocalPolytopeFactorConstraintCacheFactorLPVariableIDs_.
begin(); factorLPVariableIDsIter != addLocalPolytopeFactorConstraintCacheFactorLPVariableIDs_.
end(); ++factorLPVariableIDsIter) {
2513 *factorLPVariableIDsIter = counter++;
2518 const IndexType node = gm_[higherOrderFactors_[factor]].variableIndex(variable);
2519 const size_t size = view.
size() + 1;
2520 const size_t containerSize = variableIDs.size();
2521 if(containerSize != size) {
2522 variableIDs.resize(size);
2524 if(containerSize > 0) {
2525 coefficients[containerSize - 1] = 1.0;
2527 coefficients.resize(size, 1.0);
2528 coefficients[size - 1] = -1.0;
2530 SolverIndexType currentVariableIDsIndex = 0;
2532 variableIDs[currentVariableIDsIndex] = *viewIter;
2533 currentVariableIDsIndex++;
2535 OPENGM_ASSERT(static_cast<size_t>(currentVariableIDsIndex) == size - 1);
2536 variableIDs[size - 1] = nodeLPVariableIndex(node, label);
2538 std::stringstream constraintName;
2539 if(parameter_.nameConstraints_) {
2540 constraintName <<
"local polytope factor constraint of higher order factor " << factor <<
" variable " << variable <<
" and label " << label;
2543 static_cast<LPInferenceType*
>(
this)->addEqualityConstraint(variableIDs.begin(), variableIDs.end(), coefficients.begin(), 0.0, constraintName.str());
2546 constraint.variableIDs_ = variableIDs;
2547 constraint.coefficients_ = coefficients;
2548 constraint.bound_ = 0.0;
2549 constraint.operator_ = LinearConstraintType::LinearConstraintOperatorType::Equal;
2550 constraint.name_ = constraintName.str();
2551 inactiveConstraints_.push_back(constraint);
2555 template <
class LP_INFERENCE_TYPE>
2558 OPENGM_ASSERT(indicatorVariableLPVariable < numLPVariables_ + numSlackVariables_);
2559 if(indicatorVariableLPVariable >= numLPVariables_) {
2562 if(indicatorVariableLPVariable >= factorsLPVariablesOffset_[factor]) {
2566 const SolverIndexType numVariables =
static_cast<const SolverIndexType
>(std::distance(indicatorVariable.
begin(), indicatorVariable.
end()));
2567 if(numVariables == 1) {
2571 std::vector<SolverValueType> coefficients(2, 1.0);
2572 std::vector<SolverIndexType> lpVariableIDs(2, indicatorVariableLPVariable);
2573 lpVariableIDs[0] = nodeLPVariableIndex(gm_[factor].variableIndex(indicatorVariable.
begin()->first), indicatorVariable.
begin()->second);
2574 std::stringstream constraintName;
2575 if(parameter_.nameConstraints_) {
2576 constraintName <<
"indicator variable constraint of factor " << factor <<
"of type Not for node" << indicatorVariable.
begin()->first <<
" and label " << indicatorVariable.
begin()->second;
2579 static_cast<LPInferenceType*
>(
this)->addEqualityConstraint(lpVariableIDs.begin(), lpVariableIDs.end(), coefficients.begin(), 1.0, constraintName.str());
2582 constraint.variableIDs_ = lpVariableIDs;
2583 constraint.coefficients_ = coefficients;
2584 constraint.bound_ = 1.0;
2585 constraint.operator_ = LinearConstraintType::LinearConstraintOperatorType::Equal;
2586 constraint.name_ = constraintName.str();
2587 inactiveConstraints_.push_back(constraint);
2595 std::vector<SolverValueType> coefficients(2, 1.0);
2597 coefficients.back() = -1.0;
2599 std::vector<SolverIndexType> lpVariableIDs(2, indicatorVariableLPVariable);
2600 for(VariableLabelPairsIteratorType iter = indicatorVariable.
begin(); iter != indicatorVariable.
end(); ++iter) {
2601 lpVariableIDs[1] = nodeLPVariableIndex(gm_[factor].variableIndex(iter->first), iter->second);
2602 std::stringstream constraintName;
2603 if(parameter_.nameConstraints_) {
2604 constraintName <<
"indicator variable constraint of factor " << factor <<
"of type ";
2606 case IndicatorVariableType::And : {
2607 constraintName <<
"And";
2610 case IndicatorVariableType::Or : {
2611 constraintName <<
"Or";
2616 constraintName <<
"Not";
2619 constraintName <<
" for node" << iter->first <<
" and label " << iter->second;
2624 case IndicatorVariableType::And : {
2625 static_cast<LPInferenceType*
>(
this)->addLessEqualConstraint(lpVariableIDs.begin(), lpVariableIDs.end(), coefficients.begin(), 0.0, constraintName.str());
2628 case IndicatorVariableType::Or : {
2629 static_cast<LPInferenceType*
>(
this)->addGreaterEqualConstraint(lpVariableIDs.begin(), lpVariableIDs.end(), coefficients.begin(), 0.0, constraintName.str());
2634 static_cast<LPInferenceType*
>(
this)->addLessEqualConstraint(lpVariableIDs.begin(), lpVariableIDs.end(), coefficients.begin(), 1.0, constraintName.str());
2639 constraint.variableIDs_ = lpVariableIDs;
2640 constraint.coefficients_ = coefficients;
2642 case IndicatorVariableType::And : {
2643 constraint.bound_ = 0.0;
2644 constraint.operator_ = LinearConstraintType::LinearConstraintOperatorType::LessEqual;
2647 case IndicatorVariableType::Or : {
2648 constraint.bound_ = 0.0;
2649 constraint.operator_ = LinearConstraintType::LinearConstraintOperatorType::GreaterEqual;
2654 constraint.bound_ = 1.0;
2655 constraint.operator_ = LinearConstraintType::LinearConstraintOperatorType::LessEqual;
2658 constraint.name_ = constraintName.str();
2659 inactiveConstraints_.push_back(constraint);
2667 coefficients.back() = 1.0;
2669 coefficients.resize(numVariables + 1, 1.0);
2671 coefficients.back() = -1.0;
2673 lpVariableIDs.clear();
2674 for(VariableLabelPairsIteratorType iter = indicatorVariable.
begin(); iter != indicatorVariable.
end(); ++iter) {
2675 lpVariableIDs.push_back(nodeLPVariableIndex(gm_[factor].variableIndex(iter->first), iter->second));
2677 lpVariableIDs.push_back(indicatorVariableLPVariable);
2678 std::stringstream constraintName;
2679 if(parameter_.nameConstraints_) {
2680 constraintName <<
"indicator variable sum constraint of factor " << factor <<
"of type ";
2682 case IndicatorVariableType::And : {
2683 constraintName <<
"And";
2686 case IndicatorVariableType::Or : {
2687 constraintName <<
"Or";
2692 constraintName <<
"Not";
2695 constraintName <<
" for lp variable" << indicatorVariableLPVariable;
2699 case IndicatorVariableType::And : {
2700 static_cast<LPInferenceType*
>(
this)->addLessEqualConstraint(lpVariableIDs.begin(), lpVariableIDs.end(), coefficients.begin(),
static_cast<const SolverValueType
>(numVariables - 1.0), constraintName.str());
2703 case IndicatorVariableType::Or : {
2704 static_cast<LPInferenceType*
>(
this)->addGreaterEqualConstraint(lpVariableIDs.begin(), lpVariableIDs.end(), coefficients.begin(),
static_cast<const SolverValueType
>(0.0), constraintName.str());
2709 static_cast<LPInferenceType*
>(
this)->addGreaterEqualConstraint(lpVariableIDs.begin(), lpVariableIDs.end(), coefficients.begin(),
static_cast<const SolverValueType
>(1.0), constraintName.str());
2714 constraint.variableIDs_ = lpVariableIDs;
2715 constraint.coefficients_ = coefficients;
2717 case IndicatorVariableType::And : {
2718 constraint.bound_ =
static_cast<const SolverValueType
>(numVariables - 1.0);
2719 constraint.operator_ = LinearConstraintType::LinearConstraintOperatorType::LessEqual;
2722 case IndicatorVariableType::Or : {
2723 constraint.bound_ =
static_cast<const SolverValueType
>(0.0);
2724 constraint.operator_ = LinearConstraintType::LinearConstraintOperatorType::GreaterEqual;
2729 constraint.bound_ =
static_cast<const SolverValueType
>(1.0);
2730 constraint.operator_ = LinearConstraintType::LinearConstraintOperatorType::GreaterEqual;
2733 constraint.name_ = constraintName.str();
2734 inactiveConstraints_.push_back(constraint);
2741 template <
class LP_INFERENCE_TYPE>
2743 OPENGM_ASSERT(linearConstraintFactor < linearConstraintsLPVariablesIndicesLookupTable_.size());
2745 for(
size_t i = 0; i < lpVariables.size(); ++i) {
2746 lpVariables[i] = linearConstraintsLPVariablesIndicesLookupTable_[linearConstraintFactor][constraint.
indicatorVariablesBegin()[i]];
2749 case LinearConstraintType::LinearConstraintOperatorType::LessEqual : {
2750 static_cast<LPInferenceType*
>(
this)->addLessEqualConstraint(lpVariables.begin(), lpVariables.end(), constraint.
coefficientsBegin(),
static_cast<const SolverValueType
>(constraint.
getBound()));
2753 case LinearConstraintType::LinearConstraintOperatorType::Equal : {
2754 static_cast<LPInferenceType*
>(
this)->addEqualityConstraint(lpVariables.begin(), lpVariables.end(), constraint.
coefficientsBegin(),
static_cast<const SolverValueType
>(constraint.
getBound()));
2759 static_cast<LPInferenceType*
>(
this)->addGreaterEqualConstraint(lpVariables.begin(), lpVariables.end(), constraint.
coefficientsBegin(),
static_cast<const SolverValueType
>(constraint.
getBound()));
2764 template <
class LP_INFERENCE_TYPE>
2765 template <
class VISITOR_TYPE>
2767 switch(parameter_.relaxation_){
2768 case Parameter::LocalPolytope : {
2769 return infer_impl_selectHeuristic<VISITOR_TYPE, Parameter::LocalPolytope>(visitor);
2771 case Parameter::LoosePolytope : {
2772 return infer_impl_selectHeuristic<VISITOR_TYPE, Parameter::LoosePolytope>(visitor);
2774 case Parameter::TightPolytope: {
2775 return infer_impl_selectHeuristic<VISITOR_TYPE, Parameter::TightPolytope>(visitor);
2783 template <
class LP_INFERENCE_TYPE>
2784 template <class VISITOR_TYPE, typename LPInferenceBase<LP_INFERENCE_TYPE>::Parameter::Relaxation RELAXATION>
2786 switch(parameter_.challengeHeuristic_){
2787 case Parameter::Random : {
2788 return infer_impl_selectIterations<VISITOR_TYPE, RELAXATION, Parameter::Random>(visitor);
2790 case Parameter::Weighted : {
2791 return infer_impl_selectIterations<VISITOR_TYPE, RELAXATION, Parameter::Weighted>(visitor);
2799 template <
class LP_INFERENCE_TYPE>
2802 if(parameter_.maxNumIterations_ == 0) {
2803 return infer_impl_selectViolatedConstraints<VISITOR_TYPE, RELAXATION, HEURISTIC, true>(visitor);
2805 return infer_impl_selectViolatedConstraints<VISITOR_TYPE, RELAXATION, HEURISTIC, false>(visitor);
2809 template <
class LP_INFERENCE_TYPE>
2812 if(parameter_.maxNumConstraintsPerIter_ == 0) {
2813 return infer_impl_selectLPType<VISITOR_TYPE, RELAXATION, HEURISTIC, USE_INFINITE_ITERATIONS, true>(visitor);
2815 return infer_impl_selectLPType<VISITOR_TYPE, RELAXATION, HEURISTIC, USE_INFINITE_ITERATIONS, false>(visitor);
2819 template <
class LP_INFERENCE_TYPE>
2822 if(parameter_.integerConstraintNodeVar_ || parameter_.integerConstraintFactorVar_) {
2823 return infer_impl<VISITOR_TYPE, RELAXATION, HEURISTIC, USE_INFINITE_ITERATIONS, ADD_ALL_VIOLATED_CONSTRAINTS, true>(visitor);
2825 return infer_impl<VISITOR_TYPE, RELAXATION, HEURISTIC, USE_INFINITE_ITERATIONS, ADD_ALL_VIOLATED_CONSTRAINTS, false>(visitor);
2829 template <
class LP_INFERENCE_TYPE>
2832 if(meta::Compare<VISITOR_TYPE, TimingVisitorType>::value) {
2833 visitor.addLog(
"LP Solver Time");
2834 visitor.addLog(
"Search Violated Constraints Time");
2835 visitor.addLog(
"Add Violated Constraints Time");
2838 visitor.begin(*
this);
2839 inferenceStarted_ =
true;
2840 for(
size_t i = 0; USE_INFINITE_ITERATIONS || (i < parameter_.maxNumIterations_);) {
2842 if(meta::Compare<VISITOR_TYPE, TimingVisitorType>::value) {
2843 SolverTimingType solverTime;
2844 const bool solveSuccess =
static_cast<LPInferenceType*
>(
this)->solve(solverTime);
2849 const size_t visitorReturnFlag = visitor(*
this);
2850 visitor.log(
"LP Solver Time", solverTime);
2851 if(visitorReturnFlag != visitors::VisitorReturnFlag::ContinueInf) {
2856 if(!static_cast<LPInferenceType*>(
this)->solve()) {
2860 if(visitor(*
this) != visitors::VisitorReturnFlag::ContinueInf) {
2866 if(meta::Compare<VISITOR_TYPE, TimingVisitorType>::value) {
2867 static Timer searchViolatedConstraintsTimer;
2868 searchViolatedConstraintsTimer.
reset();
2869 searchViolatedConstraintsTimer.
tic();
2870 const bool newViolatedConstraintsFound = USE_INTEGER_CONSTRAINTS ? tightenPolytope<RELAXATION, HEURISTIC, ADD_ALL_VIOLATED_CONSTRAINTS>() : tightenPolytopeRelaxed<RELAXATION, HEURISTIC, ADD_ALL_VIOLATED_CONSTRAINTS>();
2871 searchViolatedConstraintsTimer.
toc();
2872 visitor.log(
"Search Violated Constraints Time", searchViolatedConstraintsTimer.
elapsedTime());
2873 if(newViolatedConstraintsFound){
2874 SolverTimingType addConstraintsTime;
2875 static_cast<LPInferenceType*
>(
this)->addConstraintsFinished(addConstraintsTime);
2876 visitor.log(
"Add Violated Constraints Time", addConstraintsTime);
2882 if(USE_INTEGER_CONSTRAINTS ? tightenPolytope<RELAXATION, HEURISTIC, ADD_ALL_VIOLATED_CONSTRAINTS>() : tightenPolytopeRelaxed<RELAXATION, HEURISTIC, ADD_ALL_VIOLATED_CONSTRAINTS>()){
2883 static_cast<LPInferenceType*
>(
this)->addConstraintsFinished();
2889 if(!USE_INFINITE_ITERATIONS) {
2897 template <
class LP_INFERENCE_TYPE>
2900 if(RELAXATION == Parameter::TightPolytope) {
2906 static std::vector<LabelType> currentArg;
2909 if(ADD_ALL_VIOLATED_CONSTRAINTS) {
2910 bool violatedConstraintAdded =
false;
2911 if(RELAXATION == Parameter::LoosePolytope) {
2912 double currentWeight;
2913 InactiveConstraintsListIteratorType inactiveConstraintsBegin = inactiveConstraints_.begin();
2914 InactiveConstraintsListIteratorType inactiveConstraintsEnd = inactiveConstraints_.end();
2915 while(inactiveConstraintsBegin != inactiveConstraintsEnd) {
2916 checkInactiveConstraint(*inactiveConstraintsBegin, currentWeight);
2917 if(currentWeight > parameter_.tolerance_) {
2918 addInactiveConstraint(*inactiveConstraintsBegin);
2919 violatedConstraintAdded =
true;
2920 const InactiveConstraintsListIteratorType removeInactiveConstraintIterator = inactiveConstraintsBegin;
2921 ++inactiveConstraintsBegin;
2922 inactiveConstraints_.erase(removeInactiveConstraintIterator);
2925 ++inactiveConstraintsBegin;
2928 while(inactiveConstraintsBegin != inactiveConstraintsEnd) {
2929 checkInactiveConstraint(*inactiveConstraintsBegin, currentWeight);
2930 if(currentWeight > parameter_.tolerance_) {
2931 addInactiveConstraint(*inactiveConstraintsBegin);
2932 const InactiveConstraintsListIteratorType removeInactiveConstraintIterator = inactiveConstraintsBegin;
2933 ++inactiveConstraintsBegin;
2934 inactiveConstraints_.erase(removeInactiveConstraintIterator);
2936 ++inactiveConstraintsBegin;
2943 AddAllViolatedLinearConstraintsFunctor addAllViolatedLinearConstraintsFunctor;
2944 addAllViolatedLinearConstraintsFunctor.tolerance_ = parameter_.tolerance_;
2945 addAllViolatedLinearConstraintsFunctor.lpInference_ =
this;
2946 addAllViolatedLinearConstraintsFunctor.violatedConstraintAdded_ =
false;
2947 if(!violatedConstraintAdded) {
2948 for(; i < linearConstraintFactors_.size(); ++i) {
2949 addAllViolatedLinearConstraintsFunctor.labelingBegin_ = IntegerSolutionSubsequenceIterator(currentArg.begin(), linearConstraintLPVariablesSubsequenceIndices_[i].begin());
2950 addAllViolatedLinearConstraintsFunctor.linearConstraintID_ = i;
2951 const IndexType currentFactor = linearConstraintFactors_[i];
2952 gm_[currentFactor].callFunctor(addAllViolatedLinearConstraintsFunctor);
2953 if(addAllViolatedLinearConstraintsFunctor.violatedConstraintAdded_) {
2954 violatedConstraintAdded =
true;
2959 for(; i < linearConstraintFactors_.size(); ++i) {
2960 for(; i < linearConstraintFactors_.size(); ++i) {
2961 addAllViolatedLinearConstraintsFunctor.labelingBegin_ = IntegerSolutionSubsequenceIterator(currentArg.begin(), linearConstraintLPVariablesSubsequenceIndices_[i].begin());
2962 addAllViolatedLinearConstraintsFunctor.linearConstraintID_ = i;
2963 const IndexType currentFactor = linearConstraintFactors_[i];
2964 gm_[currentFactor].callFunctor(addAllViolatedLinearConstraintsFunctor);
2967 return violatedConstraintAdded;
2969 size_t numConstraintsAdded = 0;
2970 SortedViolatedConstraintsListType sortedViolatedConstraints;
2972 if(RELAXATION == Parameter::LoosePolytope) {
2973 double currentWeight;
2974 InactiveConstraintsListIteratorType inactiveConstraintsBegin = inactiveConstraints_.begin();
2975 InactiveConstraintsListIteratorType inactiveConstraintsEnd = inactiveConstraints_.end();
2976 while(inactiveConstraintsBegin != inactiveConstraintsEnd) {
2977 checkInactiveConstraint(*inactiveConstraintsBegin, currentWeight);
2978 if(currentWeight > parameter_.tolerance_) {
2979 if(HEURISTIC == Parameter::Random) {
2980 addInactiveConstraint(*inactiveConstraintsBegin);
2981 ++numConstraintsAdded;
2982 const InactiveConstraintsListIteratorType removeInactiveConstraintIterator = inactiveConstraintsBegin;
2983 ++inactiveConstraintsBegin;
2984 inactiveConstraints_.erase(removeInactiveConstraintIterator);
2985 if(numConstraintsAdded == parameter_.maxNumConstraintsPerIter_) {
2989 sortedViolatedConstraints.insert(
typename SortedViolatedConstraintsListType::value_type(currentWeight, std::make_pair(inactiveConstraintsBegin, std::make_pair(linearConstraintFactors_.size(),
static_cast<const LinearConstraintType*
>(NULL)))));
2990 if(sortedViolatedConstraints.size() > parameter_.maxNumConstraintsPerIter_) {
2992 sortedViolatedConstraints.erase(sortedViolatedConstraints.begin());
2993 OPENGM_ASSERT(sortedViolatedConstraints.size() == parameter_.maxNumConstraintsPerIter_);
2995 ++inactiveConstraintsBegin;
2998 ++inactiveConstraintsBegin;
3005 addViolatedLinearConstraintsFunctor.
tolerance_ = parameter_.tolerance_;
3006 addViolatedLinearConstraintsFunctor.
lpInference_ =
this;
3009 for(
size_t i = 0; i < linearConstraintFactors_.size(); ++i) {
3010 addViolatedLinearConstraintsFunctor.
labelingBegin_ = IntegerSolutionSubsequenceIterator(currentArg.begin(), linearConstraintLPVariablesSubsequenceIndices_[i].begin());
3012 const IndexType currentFactor = linearConstraintFactors_[i];
3013 gm_[currentFactor].callFunctor(addViolatedLinearConstraintsFunctor);
3014 if(addViolatedLinearConstraintsFunctor.
numConstraintsAdded_ == parameter_.maxNumConstraintsPerIter_) {
3020 typename SortedViolatedConstraintsListType::reverse_iterator sortedViolatedConstraintsListRBegin = sortedViolatedConstraints.rbegin();
3021 const typename SortedViolatedConstraintsListType::reverse_iterator sortedViolatedConstraintsListREnd = sortedViolatedConstraints.rend();
3022 OPENGM_ASSERT(sortedViolatedConstraints.size() <= parameter_.maxNumConstraintsPerIter_);
3023 while(sortedViolatedConstraintsListRBegin != sortedViolatedConstraintsListREnd) {
3024 if(sortedViolatedConstraintsListRBegin->second.first == inactiveConstraints_.end()) {
3025 addLinearConstraint(sortedViolatedConstraintsListRBegin->second.second.first, *(sortedViolatedConstraintsListRBegin->second.second.second));
3027 addInactiveConstraint(*(sortedViolatedConstraintsListRBegin->second.first));
3028 inactiveConstraints_.erase(sortedViolatedConstraintsListRBegin->second.first);
3030 ++numConstraintsAdded;
3031 ++sortedViolatedConstraintsListRBegin;
3033 if(numConstraintsAdded == 0) {
3041 template <
class LP_INFERENCE_TYPE>
3044 if(RELAXATION == Parameter::TightPolytope) {
3050 SolverSolutionIteratorType relaxedArgBegin =
static_cast<const LPInferenceType*
>(
this)->solutionBegin();
3052 if(ADD_ALL_VIOLATED_CONSTRAINTS) {
3053 bool violatedConstraintAdded =
false;
3054 if(RELAXATION == Parameter::LoosePolytope) {
3055 double currentWeight;
3056 InactiveConstraintsListIteratorType inactiveConstraintsBegin = inactiveConstraints_.begin();
3057 InactiveConstraintsListIteratorType inactiveConstraintsEnd = inactiveConstraints_.end();
3058 while(inactiveConstraintsBegin != inactiveConstraintsEnd) {
3059 checkInactiveConstraint(*inactiveConstraintsBegin, currentWeight);
3060 if(currentWeight > parameter_.tolerance_) {
3061 addInactiveConstraint(*inactiveConstraintsBegin);
3062 violatedConstraintAdded =
true;
3063 const InactiveConstraintsListIteratorType removeInactiveConstraintIterator = inactiveConstraintsBegin;
3064 ++inactiveConstraintsBegin;
3065 inactiveConstraints_.erase(removeInactiveConstraintIterator);
3068 ++inactiveConstraintsBegin;
3071 while(inactiveConstraintsBegin != inactiveConstraintsEnd) {
3072 checkInactiveConstraint(*inactiveConstraintsBegin, currentWeight);
3073 if(currentWeight > parameter_.tolerance_) {
3074 addInactiveConstraint(*inactiveConstraintsBegin);
3075 const InactiveConstraintsListIteratorType removeInactiveConstraintIterator = inactiveConstraintsBegin;
3076 ++inactiveConstraintsBegin;
3077 inactiveConstraints_.erase(removeInactiveConstraintIterator);
3079 ++inactiveConstraintsBegin;
3086 AddAllViolatedLinearConstraintsRelaxedFunctor addAllViolatedLinearConstraintsRelaxedFunctor;
3087 addAllViolatedLinearConstraintsRelaxedFunctor.tolerance_ = parameter_.tolerance_;
3088 addAllViolatedLinearConstraintsRelaxedFunctor.lpInference_ =
this;
3089 addAllViolatedLinearConstraintsRelaxedFunctor.violatedConstraintAdded_ =
false;
3090 if(!violatedConstraintAdded) {
3091 for(; i < linearConstraintFactors_.size(); ++i) {
3092 addAllViolatedLinearConstraintsRelaxedFunctor.labelingBegin_ = RelaxedSolutionSubsequenceIterator(relaxedArgBegin, linearConstraintLPVariablesSubsequenceIndices_[i].begin());
3093 addAllViolatedLinearConstraintsRelaxedFunctor.linearConstraintID_ = i;
3094 const IndexType currentFactor = linearConstraintFactors_[i];
3095 gm_[currentFactor].callFunctor(addAllViolatedLinearConstraintsRelaxedFunctor);
3096 if(addAllViolatedLinearConstraintsRelaxedFunctor.violatedConstraintAdded_) {
3097 violatedConstraintAdded =
true;
3102 for(; i < linearConstraintFactors_.size(); ++i) {
3103 for(; i < linearConstraintFactors_.size(); ++i) {
3104 addAllViolatedLinearConstraintsRelaxedFunctor.labelingBegin_ = RelaxedSolutionSubsequenceIterator(relaxedArgBegin, linearConstraintLPVariablesSubsequenceIndices_[i].begin());
3105 addAllViolatedLinearConstraintsRelaxedFunctor.linearConstraintID_ = i;
3106 const IndexType currentFactor = linearConstraintFactors_[i];
3107 gm_[currentFactor].callFunctor(addAllViolatedLinearConstraintsRelaxedFunctor);
3110 return violatedConstraintAdded;
3112 size_t numConstraintsAdded = 0;
3113 SortedViolatedConstraintsListType sortedViolatedConstraints;
3115 if(RELAXATION == Parameter::LoosePolytope) {
3116 double currentWeight;
3117 InactiveConstraintsListIteratorType inactiveConstraintsBegin = inactiveConstraints_.begin();
3118 InactiveConstraintsListIteratorType inactiveConstraintsEnd = inactiveConstraints_.end();
3119 while(inactiveConstraintsBegin != inactiveConstraintsEnd) {
3120 checkInactiveConstraint(*inactiveConstraintsBegin, currentWeight);
3121 if(currentWeight > parameter_.tolerance_) {
3122 if(HEURISTIC == Parameter::Random) {
3123 addInactiveConstraint(*inactiveConstraintsBegin);
3124 ++numConstraintsAdded;
3125 const InactiveConstraintsListIteratorType removeInactiveConstraintIterator = inactiveConstraintsBegin;
3126 ++inactiveConstraintsBegin;
3127 inactiveConstraints_.erase(removeInactiveConstraintIterator);
3128 if(numConstraintsAdded == parameter_.maxNumConstraintsPerIter_) {
3132 sortedViolatedConstraints.insert(
typename SortedViolatedConstraintsListType::value_type(currentWeight, std::make_pair(inactiveConstraintsBegin, std::make_pair(linearConstraintFactors_.size(),
static_cast<LinearConstraintType*
>(NULL)))));
3133 if(sortedViolatedConstraints.size() > parameter_.maxNumConstraintsPerIter_) {
3135 sortedViolatedConstraints.erase(sortedViolatedConstraints.begin());
3136 OPENGM_ASSERT(sortedViolatedConstraints.size() == parameter_.maxNumConstraintsPerIter_);
3138 ++inactiveConstraintsBegin;
3141 ++inactiveConstraintsBegin;
3148 addViolatedLinearConstraintsRelaxedFunctor.
tolerance_ = parameter_.tolerance_;
3149 addViolatedLinearConstraintsRelaxedFunctor.
lpInference_ =
this;
3152 for(
size_t i = 0; i < linearConstraintFactors_.size(); ++i) {
3153 addViolatedLinearConstraintsRelaxedFunctor.
labelingBegin_ = RelaxedSolutionSubsequenceIterator(relaxedArgBegin, linearConstraintLPVariablesSubsequenceIndices_[i].begin());
3155 const IndexType currentFactor = linearConstraintFactors_[i];
3156 gm_[currentFactor].callFunctor(addViolatedLinearConstraintsRelaxedFunctor);
3157 if(addViolatedLinearConstraintsRelaxedFunctor.
numConstraintsAdded_ == parameter_.maxNumConstraintsPerIter_) {
3163 typename SortedViolatedConstraintsListType::reverse_iterator sortedViolatedConstraintsListRBegin = sortedViolatedConstraints.rbegin();
3164 const typename SortedViolatedConstraintsListType::reverse_iterator sortedViolatedConstraintsListREnd = sortedViolatedConstraints.rend();
3165 OPENGM_ASSERT(sortedViolatedConstraints.size() <= parameter_.maxNumConstraintsPerIter_);
3166 while(sortedViolatedConstraintsListRBegin != sortedViolatedConstraintsListREnd) {
3167 if(sortedViolatedConstraintsListRBegin->second.first == inactiveConstraints_.end()) {
3168 addLinearConstraint(sortedViolatedConstraintsListRBegin->second.second.first, *(sortedViolatedConstraintsListRBegin->second.second.second));
3170 addInactiveConstraint(*(sortedViolatedConstraintsListRBegin->second.first));
3171 inactiveConstraints_.erase(sortedViolatedConstraintsListRBegin->second.first);
3173 ++numConstraintsAdded;
3174 if(numConstraintsAdded == parameter_.maxNumConstraintsPerIter_) {
3177 ++sortedViolatedConstraintsListRBegin;
3179 if(numConstraintsAdded == 0) {
3187 template <
class LP_INFERENCE_TYPE>
3189 const SolverSolutionIteratorType currentSolution =
static_cast<const LPInferenceType*
>(
this)->solutionBegin();
3191 for(
size_t i = 0; i < constraint.variableIDs_.size(); ++i) {
3192 sum += constraint.coefficients_[i] * currentSolution[constraint.variableIDs_[i]];
3194 switch(constraint.operator_) {
3195 case LinearConstraintType::LinearConstraintOperatorType::LessEqual : {
3196 if(sum <= constraint.bound_) {
3199 weight = sum - constraint.bound_;
3203 case LinearConstraintType::LinearConstraintOperatorType::Equal : {
3204 if(sum == constraint.bound_) {
3207 weight =
std::abs(sum - constraint.bound_);
3213 if(sum >= constraint.bound_) {
3216 weight = constraint.bound_ - sum;
3223 template <
class LP_INFERENCE_TYPE>
3225 switch(constraint.operator_) {
3226 case LinearConstraintType::LinearConstraintOperatorType::LessEqual : {
3227 static_cast<LPInferenceType*
>(
this)->addLessEqualConstraint(constraint.variableIDs_.begin(), constraint.variableIDs_.end(), constraint.coefficients_.begin(), constraint.bound_, constraint.name_);
3230 case LinearConstraintType::LinearConstraintOperatorType::Equal : {
3231 static_cast<LPInferenceType*
>(
this)->addEqualityConstraint(constraint.variableIDs_.begin(), constraint.variableIDs_.end(), constraint.coefficients_.begin(), constraint.bound_, constraint.name_);
3236 static_cast<LPInferenceType*
>(
this)->addGreaterEqualConstraint(constraint.variableIDs_.begin(), constraint.variableIDs_.end(), constraint.coefficients_.begin(), constraint.bound_, constraint.name_);
3242 template <
class LP_INFERENCE_TYPE>
3243 template<
class LINEAR_CONSTRAINT_FUNCTION_TYPE>
3248 template <
class LP_INFERENCE_TYPE>
3249 template<
class FUNCTION_TYPE,
bool IS_LINEAR_CONSTRAINT_FUNCTION>
3251 throw RuntimeError(std::string(
"GetIndicatorVariablesOrderBeginFunctor: Unsupported linear constraint function type") +
typeid(FUNCTION_TYPE).name());
3254 template <
class LP_INFERENCE_TYPE>
3255 template<
class FUNCTION_TYPE>
3260 template <
class LP_INFERENCE_TYPE>
3261 template<
class LINEAR_CONSTRAINT_FUNCTION_TYPE>
3266 template <
class LP_INFERENCE_TYPE>
3267 template<
class FUNCTION_TYPE,
bool IS_LINEAR_CONSTRAINT_FUNCTION>
3269 throw RuntimeError(std::string(
"GetIndicatorVariablesOrderEnd: Unsupported linear constraint function type") +
typeid(FUNCTION_TYPE).name());
3272 template <
class LP_INFERENCE_TYPE>
3273 template<
class FUNCTION_TYPE>
3278 template <
class LP_INFERENCE_TYPE>
3279 template<
class LINEAR_CONSTRAINT_FUNCTION_TYPE>
3284 template <
class LP_INFERENCE_TYPE>
3285 template<
class FUNCTION_TYPE,
bool IS_LINEAR_CONSTRAINT_FUNCTION>
3287 throw RuntimeError(std::string(
"GetLinearConstraintsBeginFunctor: Unsupported linear constraint function type") +
typeid(FUNCTION_TYPE).name());
3290 template <
class LP_INFERENCE_TYPE>
3291 template<
class FUNCTION_TYPE>
3296 template <
class LP_INFERENCE_TYPE>
3297 template<
class LINEAR_CONSTRAINT_FUNCTION_TYPE>
3302 template <
class LP_INFERENCE_TYPE>
3303 template<
class FUNCTION_TYPE,
bool IS_LINEAR_CONSTRAINT_FUNCTION>
3305 throw RuntimeError(std::string(
"GetLinearConstraintsEndFunctor: Unsupported linear constraint function type") +
typeid(FUNCTION_TYPE).name());
3308 template <
class LP_INFERENCE_TYPE>
3309 template<
class FUNCTION_TYPE>
3314 template <
class LP_INFERENCE_TYPE>
3315 template<
class LINEAR_CONSTRAINT_FUNCTION_TYPE>
3320 template <
class LP_INFERENCE_TYPE>
3321 template<
class FUNCTION_TYPE,
bool IS_LINEAR_CONSTRAINT_FUNCTION>
3323 throw RuntimeError(std::string(
"AddAllViolatedLinearConstraintsFunctor: Unsupported linear constraint function type") +
typeid(FUNCTION_TYPE).name());
3326 template <
class LP_INFERENCE_TYPE>
3327 template<
class FUNCTION_TYPE>
3329 typename FUNCTION_TYPE::ViolatedLinearConstraintsIteratorType violatedConstraintsBegin;
3330 typename FUNCTION_TYPE::ViolatedLinearConstraintsIteratorType violatedConstraintsEnd;
3331 typename FUNCTION_TYPE::ViolatedLinearConstraintsWeightsIteratorType violatedConstraintsWeightsBegin;
3332 function.challenge(violatedConstraintsBegin, violatedConstraintsEnd, violatedConstraintsWeightsBegin, myself.
labelingBegin_, myself.
tolerance_);
3333 if(std::distance(violatedConstraintsBegin, violatedConstraintsEnd) > 0) {
3334 while(violatedConstraintsBegin != violatedConstraintsEnd) {
3336 ++violatedConstraintsBegin;
3342 template <
class LP_INFERENCE_TYPE>
3343 template<
class LINEAR_CONSTRAINT_FUNCTION_TYPE>
3348 template <
class LP_INFERENCE_TYPE>
3349 template<
class FUNCTION_TYPE,
bool IS_LINEAR_CONSTRAINT_FUNCTION>
3351 throw RuntimeError(std::string(
"AddAllViolatedLinearConstraintsRelaxedFunctor: Unsupported linear constraint function type") +
typeid(FUNCTION_TYPE).name());
3354 template <
class LP_INFERENCE_TYPE>
3355 template<
class FUNCTION_TYPE>
3357 typename FUNCTION_TYPE::ViolatedLinearConstraintsIteratorType violatedConstraintsBegin;
3358 typename FUNCTION_TYPE::ViolatedLinearConstraintsIteratorType violatedConstraintsEnd;
3359 typename FUNCTION_TYPE::ViolatedLinearConstraintsWeightsIteratorType violatedConstraintsWeightsBegin;
3360 function.challengeRelaxed(violatedConstraintsBegin, violatedConstraintsEnd, violatedConstraintsWeightsBegin, myself.
labelingBegin_, myself.
tolerance_);
3361 if(std::distance(violatedConstraintsBegin, violatedConstraintsEnd) > 0) {
3362 while(violatedConstraintsBegin != violatedConstraintsEnd) {
3364 ++violatedConstraintsBegin;
3370 template <
class LP_INFERENCE_BASE_TYPE,
typename LP_INFERENCE_BASE_TYPE::Parameter::ChallengeHeuristic HEURISTIC>
3371 template<
class LINEAR_CONSTRAINT_FUNCTION_TYPE>
3376 template <
class LP_INFERENCE_BASE_TYPE,
typename LP_INFERENCE_BASE_TYPE::Parameter::ChallengeHeuristic HEURISTIC>
3377 template<
class FUNCTION_TYPE,
bool IS_LINEAR_CONSTRAINT_FUNCTION>
3379 throw RuntimeError(std::string(
"AddViolatedLinearConstraintsFunctor: Unsupported linear constraint function type") +
typeid(FUNCTION_TYPE).name());
3382 template <
class LP_INFERENCE_BASE_TYPE,
typename LP_INFERENCE_BASE_TYPE::Parameter::ChallengeHeuristic HEURISTIC>
3383 template<
class FUNCTION_TYPE>
3385 typename FUNCTION_TYPE::ViolatedLinearConstraintsIteratorType violatedConstraintsBegin;
3386 typename FUNCTION_TYPE::ViolatedLinearConstraintsIteratorType violatedConstraintsEnd;
3387 typename FUNCTION_TYPE::ViolatedLinearConstraintsWeightsIteratorType violatedConstraintsWeightsBegin;
3388 function.challenge(violatedConstraintsBegin, violatedConstraintsEnd, violatedConstraintsWeightsBegin, myself.
labelingBegin_, myself.
tolerance_);
3389 if(std::distance(violatedConstraintsBegin, violatedConstraintsEnd) > 0) {
3390 while(violatedConstraintsBegin != violatedConstraintsEnd) {
3391 if(HEURISTIC == LP_INFERENCE_BASE_TYPE::Parameter::Random) {
3393 ++violatedConstraintsBegin;
3405 ++violatedConstraintsBegin;
3406 ++violatedConstraintsWeightsBegin;
3412 template <
class LP_INFERENCE_BASE_TYPE,
typename LP_INFERENCE_BASE_TYPE::Parameter::ChallengeHeuristic HEURISTIC>
3413 template<
class LINEAR_CONSTRAINT_FUNCTION_TYPE>
3418 template <
class LP_INFERENCE_BASE_TYPE,
typename LP_INFERENCE_BASE_TYPE::Parameter::ChallengeHeuristic HEURISTIC>
3419 template<
class FUNCTION_TYPE,
bool IS_LINEAR_CONSTRAINT_FUNCTION>
3421 throw RuntimeError(std::string(
"AddViolatedLinearConstraintsRelaxedFunctor: Unsupported linear constraint function type") +
typeid(FUNCTION_TYPE).name());
3424 template <
class LP_INFERENCE_BASE_TYPE,
typename LP_INFERENCE_BASE_TYPE::Parameter::ChallengeHeuristic HEURISTIC>
3425 template<
class FUNCTION_TYPE>
3427 typename FUNCTION_TYPE::ViolatedLinearConstraintsIteratorType violatedConstraintsBegin;
3428 typename FUNCTION_TYPE::ViolatedLinearConstraintsIteratorType violatedConstraintsEnd;
3429 typename FUNCTION_TYPE::ViolatedLinearConstraintsWeightsIteratorType violatedConstraintsWeightsBegin;
3430 function.challengeRelaxed(violatedConstraintsBegin, violatedConstraintsEnd, violatedConstraintsWeightsBegin, myself.
labelingBegin_, myself.
tolerance_);
3431 if(std::distance(violatedConstraintsBegin, violatedConstraintsEnd) > 0) {
3432 while(violatedConstraintsBegin != violatedConstraintsEnd) {
3433 if(HEURISTIC == LP_INFERENCE_BASE_TYPE::Parameter::Random) {
3435 ++violatedConstraintsBegin;
3447 ++violatedConstraintsBegin;
3448 ++violatedConstraintsWeightsBegin;
IndicatorVariablesIteratorType indicatorVariablesBegin() const
Get the begin iterator to the set of indicator variables.
LPInferenceBase(const GraphicalModelType &gm, const Parameter ¶meter=Parameter())
LPInferenceBase constructor.
InactiveConstraintsListType::iterator InactiveConstraintsListIteratorType
Typedef of the iterator type used to iterate over a set of LPInferenceBase::ConstraintStorage objects...
Helper struct to distinguish between linear constraint functions and other function types...
LPInferenceTraitsType::SolverIndexType SolverIndexType
Typedef of the index type used by the LP/MIP solver.
Functor to call LPFunctionTransfer::getLinearConstraints() for a factor of the graphical model...
virtual ValueType bound() const
Get the current bound.
Functor used to access the method challengeRelaxed() of the underlying linear constraint function of ...
LinearConstraintsContainerType::const_iterator LinearConstraintsIteratorType
Typedef of the iterator type used to iterate over a set of linear constraints.
IndexType linearConstraintID_
Index of the linear constraint factor.
LinearConstraintType::IndicatorVariableType IndicatorVariableType
Typedef of the indicator variable type used within linear constraints.
CoefficientsIteratorType coefficientsBegin() const
Get the begin iterator to the set of coefficients for the indicator variables.
LPInferenceTraitsType::GraphicalModelType GraphicalModelType
Typedef of the graphical model type.
LPInferenceBase< LPInferenceType > LPInferenceBaseType
Typedef of the opengm::LPInferenceBase class with appropriate template parameter. ...
IndicatorVariablesIteratorType indicatorVariablesOrderEnd_
Storage for the iterator returned by the method indicatorVariablesOrderEnd().
LinearConstraintType::IndicatorVariablesIteratorType IndicatorVariablesIteratorType
Typedef of the iterator type used to iterate over a set of indicator variables.
Base class for Linear Programming based inference.
LocalPolytope will use a first order local polytope approximation of the marginal polytope...
InferenceTermination infer_impl_selectHeuristic(VISITOR_TYPE &visitor)
Helper function for LPInferenceBase::infer_impl to select the challenge heuristic template parameter...
std::vector< IndexType > linearConstraintFactors_
List of all linear constraint factors.
std::vector< SolverIndexType > variableIDs_
The variables of the LP/MIP model which are used in the constraint.
STL-compliant random access iterator for View and Marray.
LinearConstraintType::IndicatorVariablesContainerType IndicatorVariablesContainerType
Typedef of the container type used to store a set of indicator variables.
IndicatorVariablesContainerType * order_
Pointer to the storage for the return value of the LPFunctionTransfer::getSlackVariablesOrder() metho...
LoosePolytope will add no constraints at all. All linear constraints will be added iteratively only i...
LPInferenceTraitsType::SolverValueType SolverValueType
Typedef of the value type used by the LP/MIP solver.
TightPolytope will add all constraints of the LocalPolytope relaxation and furthermore all constraint...
IndicatorVariablesContainerType::const_iterator IndicatorVariablesIteratorType
Defines the const iterator type to iterate over the set of indicator variables.
bool useFunctionTransfer_
Use function transfer if available to generate more efficient LP/MIP models.
IteratorType begin() const
Get the iterator over the sequence of variable label pairs from the indicator variable.
void addLoosePolytopeConstraints()
Add all constraints to the lp model which are required by the loose polytope relaxation.
SolverIndexType numSlackVariables_
The number of slack variables for the transferable factors of the graphical model.
Provides interface for liner constraint functions.
void addLinearConstraint(const IndexType linearConstraintFactor, const LinearConstraintType &constraint)
Add a new linear constraint from a linear constraint function to the lp model.
IndicatorVariableType::IteratorType VariableLabelPairsIteratorType
Defines the const iterator type to iterate over the variable label pairs of an indicator variable...
virtual ~LPInferenceBase()
LPInferenceBase destructor.
Helper struct to distinguish between linear constraint functions and other function types...
const GraphicalModelType & gm_
Reference to the graphical model.
Functor used to access the method linearConstraintsBegin() of the underlying linear constraint functi...
iterator end()
Get the end-iterator.
LP_INFERENCE_BASE_TYPE::RelaxedSolutionSubsequenceIterator labelingBegin_
Iterator used to iterate over the current solution.
Helper struct to distinguish between linear constraint functions and other function types...
CoefficientsIteratorType coefficientsEnd() const
Get the end iterator to the set of coefficients for the indicator variables.
std::vector< LinearConstraintType > LinearConstraintsContainerType
Typedef of the container type used to store a set of linear constraints.
bool tightenPolytope()
Search for linear constraints which are violated by the current integer solution and add them to the ...
std::vector< IndexType > higherOrderFactors_
List of all higher order factors.
void addLPVariables()
Add the number of lp variables computed by LPInferenceBase::countLPVariables to the lp model...
virtual ValueType value() const
Get the current value.
std::vector< IndicatorVariableType > IndicatorVariablesContainerType
Defines the storage type for the set of indicator variables.
void addInactiveConstraint(const ConstraintStorage &constraint)
Add a linear constraint from the local polytope constraint to the LP/MIP model.
LP_INFERENCE_BASE_TYPE::ValueType tolerance_
The tolerance used for the method challenge() of the underlying linear constraint function of a graph...
Helper struct to distinguish between linear constraint functions and other function types...
LP_INFERENCE_BASE_TYPE * lpInference_
Pointer pointing to the instance of opengm::LPInferenceBase to get access to the LP/MIP model...
Array-Interface to an interval of memory.
void countLPVariables()
Count the number of lp variables required to build a lp model for inference of the graphical model...
Defines the const iterator type to iterate over the subset of a sequence.
Platform-independent runtime measurements.
Functor used to access the method linearConstraintsEnd() of the underlying linear constraint function...
Helper struct to distinguish between linear constraint functions and other function types...
size_t numConstraintsAdded_
Indicator used to tell how many constraints were added to the LP/MIP model.
visitors::TimingVisitor< LPInferenceBaseType > TimingVisitorType
Typedef of the opengm::visitors::TimingVisitor class with appropriate template parameter.
Functor to call LPFunctionTransfer::numSlackVariables() for a graphical model factor.
LPInferenceTraitsType::SolverType SolverType
Typedef of the solver type used to solve the LP or MIP which is generated from the graphical model...
bool mergeParallelFactors_
Merge factors which are connected to the same set of variables. Might increase construction time but ...
#define OPENGM_ASSERT(expression)
visitors::VerboseVisitor< LPInferenceBaseType > VerboseVisitorType
Typedef of the opengm::visitors::VerboseVisitor class with appropriate template parameter.
SolverIndexType numTransferedFactorsLPVariables
The number of lp variables for the transferable factors of the graphical model.
virtual InferenceTermination infer()
Run inference with empty visitor.
ValueType tolerance_
Tolerance for violation of linear constraints.
std::list< ConstraintStorage > InactiveConstraintsListType
Typedef of the container type used to sore a set of LPInferenceBase::ConstraintStorage objects...
InferenceTermination infer_impl_selectLPType(VISITOR_TYPE &visitor)
Helper function for LPInferenceBase::infer_impl to select the use integer constraints template parame...
View< T, isConst, A > boundView(const size_t, const size_t=0) const
Get a View where one coordinate is bound to a value.
LP_INFERENCE_BASE_TYPE::IndexType linearConstraintID_
Index of the linear constraint factor.
Storage class for linear constraints representing the local polytope constraints. They are generated ...
std::vector< ValueType > SlackVariablesObjectiveCoefficientsContainerType
Defines the container type which is used to store the coefficients of the slack variables for the obj...
void setAccumulation()
Set the accumulation for the lp solver.
Relaxation
This enum defines the type of the linear programming model which is used for inference.
virtual InferenceTermination arg(std::vector< LabelType > &x, const size_t N=1) const
Get the current argument.
LPInferenceTraitsType::SolverTimingType SolverTimingType
Typedef of the type used by the LP/MIP solver to measure timings.
LPInferenceTraitsType::SolverParameterType SolverParameterType
Typedef of the parameter class used by the LP/MIP solver.
void createObjectiveFunction()
Create the objective function for the lp model.
visitors::EmptyVisitor< LPInferenceBaseType > EmptyVisitorType
Typedef of the opengm::visitors::EmptyVisitor class with appropriate template parameter.
LP_INFERENCE_TYPE LPInferenceType
Typedef of the child class which inherits from opengm::LPInferenceBase.
std::string name_
The name of the constraint.
bool tightenPolytopeRelaxed()
Search for linear constraints which are violated by the current relaxed solution and add them to the ...
iterator begin()
Get an iterator to the beginning.
size_t maxNumIterations_
Maximum number of tightening iterations (infinite if set to 0).
ChallengeHeuristic challengeHeuristic_
Heuristic on how to select violated constraints.
InactiveConstraintsListType inactiveConstraints_
Storage for all linear constraints representing the local polytope constraints. They are generated an...
Helper struct to distinguish between linear constraint functions and other function types...
IndicatorVariablesIteratorType indicatorVariablesOrderBegin_
Storage for the iterator returned by the method indicatorVariablesOrderBegin().
LinearConstraintsContainerType * constraints_
Pointer to the storage for the return value of the LPFunctionTransfer::getLinearConstraints() method...
IndexType numSlackVariables_
Storage for the return value of the LPFunctionTransfer::numSlackVariables() method.
LP_INFERENCE_BASE_TYPE * lpInference_
Pointer pointing to the instance of opengm::LPInferenceBase to get access to the LP/MIP model...
void addLocalPolytopeVariableConstraint(const IndexType variableID, const bool addToModel)
Add a new variable constraint to the lp model.
SolverIndexType numLinearConstraintsLPVariables_
The number of lp variables for the linear constraint factors of the graphical model.
size_t maxNumConstraintsPerIter_
Maximum number of violated constraints which are added per tightening iteration (all if set to 0)...
LPInferenceBaseType * lpInference_
Pointer pointing to the instance of opengm::LPInferenceBase to get access to the LP/MIP model...
Parameter()
Parameter constructor setting default value for all options.
SolverValueType bound_
The value for the right hand side of the constraint.
GraphicalModelType::IndexType IndexType
void sortFactors()
Sorts the factors of the graphical model into the lists unaryFactors_, higherOrderFactors_, linearConstraintFactors_ and transferableFactors_.
LPInferenceBaseType * lpInference_
Pointer pointing to the instance of opengm::LPInferenceBase to get access to the LP/MIP model...
size_t numConstraintsAdded_
Indicator used to tell how many constraints were added to the LP/MIP model.
SlackVariablesObjectiveCoefficientsContainerType * coefficients_
Pointer to the storage for the return value of the LPFunctionTransfer::getSlackVariablesObjectiveCoef...
Provides transformations for some function types when they are used in inference algorithms which use...
ValueType tolerance_
The tolerance used for the method challenge() of the underlying linear constraint function of a graph...
SolverIndexType numLPVariables_
The total number of lp variables except slack variables.
SubsequenceIterator< typename std::vector< LabelType >::const_iterator, typename std::vector< size_t >::const_iterator > IntegerSolutionSubsequenceIterator
Typedef of the iterator type used to iterate over a subset of the computed solution. This iterator type is used to challenge the linear constraint functions present in the model. Only used when the problem is solved as a MIP.
Relaxation relaxation_
Selected relaxation method.
Functor to call LPFunctionTransfer::getSlackVariablesOrder() for a factor of the graphical model...
LogicalOperatorType getLogicalOperatorType() const
Get the logical operator type of the indicator variable.
void addTightPolytopeConstraints()
Add all constraints to the lp model which are required by the tight polytope relaxation.
std::vector< IndicatorVariableType > IndicatorVariablesContainerType
Defines the indicator variables container type which is used to store multiple indicator variables...
meta::GetLinearConstraintFunctionTypeList< typename GraphicalModelType::FunctionTypeList >::type LinearConstraintFunctionTypeList
Typelist of all linear constraint function types which are present in the graphical model type...
bool violatedConstraintAdded_
Indicator used to tell if at least one constraint was added to the LP/MIP model.
IteratorType end() const
Get the end iterator of the sequence of variable label pairs from the indicator variable.
LPFunctionTransfer< ValueType, IndexType, LabelType > LPFunctionTransferType
Typedef of the opengm::LPFunctionTransfer class with appropriate template parameter.
InferenceTermination infer_impl_selectViolatedConstraints(VISITOR_TYPE &visitor)
Helper function for LPInferenceBase::infer_impl to select the add all violated constraints template p...
IndicatorVariablesContainerType * variables_
Pointer to the storage for the return value of the LPFunctionTransfer::getIndicatorVariables() method...
bool integerConstraintNodeVar_
Use integer constraints for node variables.
IndicatorVariablesIteratorType indicatorVariablesEnd() const
Get the end iterator to the set of indicator variables.
LPInferenceTraitsType::SolverSolutionIteratorType SolverSolutionIteratorType
Typedef of the iterator type used to iterate over the computed solution from the LP/MIP solver...
GraphicalModelType::ValueType ValueType
LPInferenceTraits< LPInferenceType > LPInferenceTraitsType
Typedef of the opengm::LPInferenceTraits class with appropriate template parameter.
ValueType tolerance_
The tolerance used for the method challengeRelaxed() of the underlying linear constraint function of ...
ValueType constValue_
Constant value offset.
Combine a group of variables to a new variable.
Inference algorithm interface.
std::vector< IndexType > unaryFactors_
List of all unary factors.
void addLocalPolytopeFactorConstraint(const IndexType factor, const IndexType variable, const LabelType label, const bool addToModel)
Add a new factor constraint to the lp model.
LinearConstraintsIteratorType linearConstraintsEnd_
Storage for the iterator returned by the method linearConstraintsEnd().
Weighted will add constraints sorted by their weights. This is only meaningful if the maximum number ...
void checkInactiveConstraint(const ConstraintStorage &constraint, double &weight) const
Check if a given linear constraint from the local polytope constraints is violated.
bool integerConstraintFactorVar_
Use integer constraints for factor variables.
marray::Marray< SolverIndexType > addLocalPolytopeFactorConstraintCacheFactorLPVariableIDs_
Lookup table for the factor lp variable ids required by the LPInferenceBase::addLocalPolytopeFactorCo...
SolverIndexType factorLPVariableIndex(const IndexType factorID, const size_t labelingIndex) const
Get the lp variable which corresponds to the labeling of the factor.
Provides implementation for class LinearConstraint.
LPInferenceTraitsType::AccumulationType AccumulationType
Typedef of the Accumulation type.
SolverIndexType numFactorsLPVariables_
The number of lp variables for the factors of the graphical model.
std::vector< std::map< const IndicatorVariableType, SolverIndexType > > transferedFactorsLPVariablesIndicesLookupTable_
Lookup table for the lp variable indices of each transferable factor.
std::multimap< double, InactiveConstraintFactorConstraintPairType > SortedViolatedConstraintsListType
Typedef of the map type used to store a set of violated constraints sorted by their weights...
Functor used to access the method indicatorVariablesOrderEnd() of the underlying linear constraint fu...
Functor used to access the method challengeRelaxed() of the underlying linear constraint function of ...
bool isTransferable_
Storage for the return value of the LPFunctionTransfer::isTransferable() method.
bool nameConstraints_
Create unique names for the linear constraints added to the model (might be helpful for debugging mod...
InferenceTermination infer_impl(VISITOR_TYPE &visitor)
The implementation of the inference method.
LP_INFERENCE_BASE_TYPE::SortedViolatedConstraintsListType * sortedViolatedConstraintsList_
Storage for the violated linear constraints sorted by their weights. Only used when LPInferenceBase::...
double elapsedTime() const
std::vector< SolverIndexType > nodesLPVariablesOffset_
The offsets for the indices of the lp variables for each node of the graphical model.
Functor used to access the method challenge() of the underlying linear constraint function of a graph...
Functor to call LPFunctionTransfer::getSlackVariablesObjectiveCoefficients() for a factor of the grap...
LP_INFERENCE_BASE_TYPE::IntegerSolutionSubsequenceIterator labelingBegin_
Iterator used to iterate over the current solution.
LinearConstraint< ValueType, IndexType, LabelType > LinearConstraintType
Typedef of the opengm::LinearConstraint class with appropriate template parameter. Used to represent linear constraints.
LinearConstraintOperatorValueType getConstraintOperator() const
Get the constraint operator of the linear constraint.
Functor to call LPFunctionTransfer::isTransferable() for a factor of the graphical model...
InferenceTermination infer_impl_selectIterations(VISITOR_TYPE &visitor)
Helper function for LPInferenceBase::infer_impl to select the use infinite iterations template parame...
IndexType addLocalPolytopeFactorConstraintCachePreviousFactorID_
Cache for the function LPInferenceBase::addLocalPolytopeFactorConstraint. It is used to store the fac...
bool useSoftConstraints_
If constraint factors are present in the model add them as soft constraints e.g. treat them as normal...
Functor used to access the method indicatorVariablesOrderBegin() of the underlying linear constraint ...
ChallengeHeuristic
This enum defines the heuristic by which the violated constraints are added to the LP/MIP model...
const Parameter parameter_
Parameter which stores the settings for the inference.
RelaxedSolutionSubsequenceIterator labelingBegin_
Iterator used to iterate over the current solution.
LinearConstraintType::LinearConstraintOperatorValueType operator_
The operator type used to compare the left hand side of the constraint against the right hand side (<...
void addIndicatorVariableConstraints(const IndexType factor, const IndicatorVariableType &indicatorVariable, const SolverIndexType indicatorVariableLPVariable, const bool addToModel)
Add constraints for an indicator variable to the lp model.
Provides implementation for class SubsequenceIterator.
Functor used to access the method challenge() of the underlying linear constraint function of a graph...
void fillLinearConstraintLPVariablesSubsequenceIndices()
Fill the variable LPInferenceBase::linearConstraintLPVariablesSubsequenceIndices_ with the appropriat...
bool getLPVariableIndexFromIndicatorVariable(const HIGHER_ORDER_FACTORS_MAP_TYPE &higherOrderFactorVariablesLookupTable, const INDICATOR_VARIABLES_MAP_TYPE &indicatorVariablesLookupTable, const IndicatorVariableType &indicatorVariable, const IndexType linearConstraintFactorIndex, SolverIndexType &lpVariableIndex) const
Get the index of the lp variable associated with an indicator variable.
SubsequenceIterator< SolverSolutionIteratorType, typename std::vector< size_t >::const_iterator > RelaxedSolutionSubsequenceIterator
Typedef of the iterator type used to iterate over a subset of the computed solution. This iterator type is used to challenge the linear constraint functions present in the model. Only used when the problem is solved as a LP.
Random will add violated constraints in a random order.
ValueType
This enum defines the operator type for the linear constraint.
SolverIndexType nodeLPVariableIndex(const IndexType nodeID, const LabelType label) const
Get the lp variable which corresponds to the variable label pair of the graphical model...
std::vector< LinearConstraintType > LinearConstraintsContainerType
Defines the linear constraints container type which is used to store multiple linear constraints...
Parameter class for opengm::LPInferenceBase.
SolverIndexType numNodesLPVariables_
The number of lp variables for the nodes of the graphical model.
void addLocalPolytopeConstraints()
Add all constraints to the lp model which are required by the local polytope relaxation.
virtual const GraphicalModelType & graphicalModel() const
Get graphical model.
LP_INFERENCE_BASE_TYPE::IndexType linearConstraintID_
Index of the linear constraint factor.
Traits class for lp inference classes.
IndexType linearConstraintID_
Index of the linear constraint factor.
Helper struct to distinguish between linear constraint functions and other function types...
LP_INFERENCE_BASE_TYPE::SortedViolatedConstraintsListType * sortedViolatedConstraintsList_
Storage for the violated linear constraints sorted by their weights. Only used when LPInferenceBase::...
InferenceTermination infer_impl_selectRelaxation(VISITOR_TYPE &visitor)
Helper function for LPInferenceBase::infer_impl to select the relaxation template parameter...
GraphicalModelType::LabelType LabelType
LP_INFERENCE_BASE_TYPE::ValueType tolerance_
The tolerance used for the method challengeRelaxed() of the underlying linear constraint function of ...
std::pair< IndexType, const LinearConstraintType * > FactorIndexConstraintPointerPairType
Typedef of the pair type used to store a pointer to a linear constraint in combination with the linea...
std::vector< IndexType > transferableFactors_
List of all transferable factors.
Functor to call LPFunctionTransfer::getIndicatorVariables() for a factor of the graphical model...
bool violatedConstraintAdded_
Indicator used to tell if at least one constraint was added to the LP/MIP model.
std::vector< std::map< const IndicatorVariableType, SolverIndexType > > linearConstraintsLPVariablesIndicesLookupTable_
Lookup table for the lp variable indices of each linear constraint.
Define a linear constraint for a set of indicatorVariables.
std::vector< SolverValueType > coefficients_
The coefficients for the variables of the LP/MIP model which are used in the constraint.
bool inferenceStarted_
Tell if inference was already started.
std::vector< SolverIndexType > factorsLPVariablesOffset_
The offsets for the indices of the lp variables for each factor of the graphical model.
LinearConstraintType::VariableLabelPairsIteratorType VariableLabelPairsIteratorType
Typedef of the iterator type used to iterate over a set of varible label pairs used within an indicat...
void resize(ShapeIterator, ShapeIterator, const T &=T())
Resize (existing entries are preserved, new entries are initialized).
LinearConstraintsIteratorType linearConstraintsBegin_
Storage for the iterator returned by the method linearConstraintsBegin().
IntegerSolutionSubsequenceIterator labelingBegin_
Iterator used to iterate over the current solution.
const size_t size() const
Get the number of data items.
Helper struct to distinguish between linear constraint functions and other function types...
std::pair< InactiveConstraintsListIteratorType, FactorIndexConstraintPointerPairType > InactiveConstraintFactorConstraintPairType
Typedef of the pair type used to store a pointer to an inactive constraint of the local polytope cons...
BoundType getBound() const
Get the bound of the linear constraint.
std::vector< std::vector< size_t > > linearConstraintLPVariablesSubsequenceIndices_
The indices of the subset of the solution variables which are relevant for each linear constraint...