1 #ifndef OPENGM_LINEAR_CONSTRAINT_HXX_
2 #define OPENGM_LINEAR_CONSTRAINT_HXX_
20 template<
class VALUE_TYPE,
class INDEX_TYPE =
size_t,
class LABEL_TYPE =
size_t>
42 template<
class INDICATOR_VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
47 void reserve(
const size_t numIndicatorVariables);
48 void add(
const IndicatorVariableType& indicatorVariable,
const ValueType coefficient);
49 void add(
const IndicatorVariablesContainerType& indicatorVariables,
const CoefficientsContainerType& coefficients);
50 template<
class INDICATOR_VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
52 void setBound(
const BoundType bound);
56 template<
class ITERATOR_TYPE>
57 ValueType
operator()(
const ITERATOR_TYPE statesBegin)
const;
390 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
392 coefficients_(), bound_(0.0),
397 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
399 : indicatorVariables_(indicatorVariables), coefficients_(coefficients),
400 bound_(bound), constraintOperator_(constraintOperator) {
404 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
405 template<
class INDICATOR_VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
407 : indicatorVariables_(indicatorVariablesBegin, indicatorVariablesEnd),
408 coefficients_(coefficientsBegin, coefficientsBegin +
std::distance(indicatorVariablesBegin, indicatorVariablesEnd)),
409 bound_(bound), constraintOperator_(constraintOperator) {
413 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
415 : indicatorVariables_(linearConstraint.indicatorVariables_),
416 coefficients_(linearConstraint.coefficients_),
417 bound_(linearConstraint.bound_),
418 constraintOperator_(linearConstraint.constraintOperator_) {
422 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
424 indicatorVariables_.reserve(numIndicatorVariables);
425 coefficients_.reserve(numIndicatorVariables);
428 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
430 indicatorVariables_.push_back(indicatorVariable);
431 coefficients_.push_back(coefficient);
434 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
436 indicatorVariables_.insert(indicatorVariables_.end(), indicatorVariables.begin(), indicatorVariables.end());
437 coefficients_.insert(coefficients_.end(), coefficients.begin(), coefficients.end());
440 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
441 template<
class INDICATOR_VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
443 indicatorVariables_.insert(indicatorVariables_.end(), indicatorVariablesBegin, indicatorVariablesEnd);
444 coefficients_.insert(coefficients_.end(), coefficientsBegin, coefficientsBegin + std::distance(indicatorVariablesBegin, indicatorVariablesEnd));
447 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
452 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
454 constraintOperator_ = constraintOperator;
457 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
458 template<
class ITERATOR_TYPE>
461 for(
IndexType i = 0; i < indicatorVariables_.size(); ++i) {
462 if(indicatorVariables_[i](statesBegin)) {
463 leftHandSide += coefficients_[i];
468 const ValueType weight = leftHandSide - bound_;
469 switch(constraintOperator_) {
470 case LinearConstraintOperatorType::LessEqual : {
478 case LinearConstraintOperatorType::Equal : {
481 }
else if(weight < 0.0) {
506 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
508 return indicatorVariables_.begin();
511 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
513 return indicatorVariables_.end();
516 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
518 return coefficients_.begin();
521 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
523 return coefficients_.end();
526 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
531 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
533 return constraintOperator_;
CoefficientsContainerType::const_iterator CoefficientsIteratorType
Defines the const iterator type to iterate over the set of coefficients for the indicator variables...
IndicatorVariablesContainerType::const_iterator IndicatorVariablesIteratorType
Defines the const iterator type to iterate over the set of indicator variables.
ValueType BoundType
Defines the data type for the bound.
IndicatorVariableType::IteratorType VariableLabelPairsIteratorType
Defines the const iterator type to iterate over the variable label pairs of an indicator variable...
Defines the linear constraint operator type to be . Hence the left hand side of the constraint will b...
CoefficientsContainerType coefficients_
Storage for the set of coefficients for the indicator variables.
std::vector< IndicatorVariableType > IndicatorVariablesContainerType
Defines the storage type for the set of indicator variables.
VariableLabelPairContainerType::const_iterator IteratorType
A const iterator to iterate over the VariableLabelPair elements.
Provides implementation for class IndicatorVariable.
BoundType getBound() const
Get the bound of the linear constraint.
INDEX_TYPE IndexType
Typedef of the INDEX_TYPE template parameter type from the class LinearConstraint.
This struct is used to create an own scope for the LinearConstraintTraits::LinearConstraintOperator::...
Defines the linear constraint operator type to be . Hence the left hand side of the constraint will b...
IndicatorVariablesIteratorType indicatorVariablesBegin() const
Get the begin iterator to the set of indicator variables.
std::vector< ValueType > CoefficientsContainerType
Defines the storage type for the set of coefficients for the indicator variables. ...
LinearConstraintOperatorValueType getConstraintOperator() const
Get the constraint operator of the linear constraint.
IndicatorVariable< IndexType, LabelType > IndicatorVariableType
Typedef of the IndicatorVariable class with appropriate template parameter.
LinearConstraintTraits::LinearConstraintOperator LinearConstraintOperatorType
Defines the linear constraint operator.
void add(const IndicatorVariableType &indicatorVariable, const ValueType coefficient)
Add a single indicator variable and the corresponding coefficient to the linear constraint.
IndicatorVariablesIteratorType indicatorVariablesEnd() const
Get the end iterator to the set of indicator variables.
Combine a group of variables to a new variable.
LinearConstraint()
LinearConstraint constructor.
CoefficientsIteratorType coefficientsBegin() const
Get the begin iterator to the set of coefficients for the indicator variables.
LABEL_TYPE LabelType
Typedef of the LABEL_TYPE template parameter type from the class LinearConstraint.
CoefficientsIteratorType coefficientsEnd() const
Get the end iterator to the set of coefficients for the indicator variables.
ValueType operator()(const ITERATOR_TYPE statesBegin) const
Evaluation operator to check if the linear constraint is violated by the given labeling.
BoundType bound_
Storage for the bound of the linear constraint.
LinearConstraintOperatorType::ValueType LinearConstraintOperatorValueType
Defines the linear constraint operator type.
void reserve(const size_t numIndicatorVariables)
Preallocate memory.
ValueType
This enum defines the operator type for the linear constraint.
LinearConstraintOperatorValueType constraintOperator_
Storage for the constraint operator of the linear constraint.
IndicatorVariablesContainerType indicatorVariables_
Storage for the set of indicator variables.
void setConstraintOperator(const LinearConstraintOperatorValueType constraintOperator)
Set the constraint operator for the linear constraint.
Defines the linear constraint operator type to be . Hence the left hand side of the constraint will b...
Define a linear constraint for a set of indicatorVariables.
VALUE_TYPE ValueType
Typedef of the VALUE_TYPE template parameter type from the class LinearConstraint.
void setBound(const BoundType bound)
Set the bound of the linear constraint.
Traits class for LinearConstraint to provide template independent enum for ConstraintOperatorType.