OpenGM  2.3.x
Discrete Graphical Model Library
linear_constraint.hxx
Go to the documentation of this file.
1 #ifndef OPENGM_LINEAR_CONSTRAINT_HXX_
2 #define OPENGM_LINEAR_CONSTRAINT_HXX_
3 
4 #include <iterator>
5 #include <vector>
6 
8 
9 namespace opengm {
10 
11 /*********************
12  * class definition *
13  *********************/
15 public:
16  // typedefs
18 };
19 
20 template<class VALUE_TYPE, class INDEX_TYPE = size_t, class LABEL_TYPE = size_t>
22 public:
23  // typedefs
24  typedef VALUE_TYPE ValueType;
25  typedef INDEX_TYPE IndexType;
26  typedef LABEL_TYPE LabelType;
27 
29  typedef std::vector<IndicatorVariableType> IndicatorVariablesContainerType;
30  typedef std::vector<ValueType> CoefficientsContainerType;
31  typedef ValueType BoundType;
34 
36  typedef typename IndicatorVariablesContainerType::const_iterator IndicatorVariablesIteratorType;
37  typedef typename CoefficientsContainerType::const_iterator CoefficientsIteratorType;
38 
39  // constructors
41  LinearConstraint(const IndicatorVariablesContainerType& indicatorVariables, const CoefficientsContainerType& coefficients, const BoundType bound = 0.0, const LinearConstraintOperatorValueType constraintOperator = LinearConstraintOperatorType::LessEqual);
42  template<class INDICATOR_VARIABLES_ITERATOR_TYPE, class COEFFICIENTS_ITERATOR_TYPE>
43  LinearConstraint(const INDICATOR_VARIABLES_ITERATOR_TYPE indicatorVariablesBegin, const INDICATOR_VARIABLES_ITERATOR_TYPE indicatorVariablesEnd, const COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const BoundType bound = 0.0, const LinearConstraintOperatorValueType constraintOperator = LinearConstraintOperatorType::LessEqual);
45 
46  // modify
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>
51  void add(const INDICATOR_VARIABLES_ITERATOR_TYPE indicatorVariablesBegin, const INDICATOR_VARIABLES_ITERATOR_TYPE indicatorVariablesEnd, const COEFFICIENTS_ITERATOR_TYPE coefficientsBegin);
52  void setBound(const BoundType bound);
53  void setConstraintOperator(const LinearConstraintOperatorValueType constraintOperator);
54 
55  // evaluate
56  template<class ITERATOR_TYPE>
57  ValueType operator()(const ITERATOR_TYPE statesBegin) const;
58 
59  // const access
60  IndicatorVariablesIteratorType indicatorVariablesBegin() const;
61  IndicatorVariablesIteratorType indicatorVariablesEnd() const;
62  CoefficientsIteratorType coefficientsBegin() const;
63  CoefficientsIteratorType coefficientsEnd() const;
64  BoundType getBound() const;
65  LinearConstraintOperatorValueType getConstraintOperator() const;
66 protected:
67  // storage
68  IndicatorVariablesContainerType indicatorVariables_;
69  CoefficientsContainerType coefficients_;
70  BoundType bound_;
71  LinearConstraintOperatorValueType constraintOperator_;
72 };
73 
74 /***********************
75  * class documentation *
76  ***********************/
386 /******************
387  * implementation *
388  ******************/
389 
390 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
392  coefficients_(), bound_(0.0),
393  constraintOperator_(LinearConstraintOperatorType::LessEqual) {
394 
395 }
396 
397 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
399  : indicatorVariables_(indicatorVariables), coefficients_(coefficients),
400  bound_(bound), constraintOperator_(constraintOperator) {
401 
402 }
403 
404 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
405 template<class INDICATOR_VARIABLES_ITERATOR_TYPE, class COEFFICIENTS_ITERATOR_TYPE>
406 inline LinearConstraint<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::LinearConstraint(const INDICATOR_VARIABLES_ITERATOR_TYPE indicatorVariablesBegin, const INDICATOR_VARIABLES_ITERATOR_TYPE indicatorVariablesEnd, const COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const BoundType bound, const LinearConstraintOperatorValueType constraintOperator)
407  : indicatorVariables_(indicatorVariablesBegin, indicatorVariablesEnd),
408  coefficients_(coefficientsBegin, coefficientsBegin + std::distance(indicatorVariablesBegin, indicatorVariablesEnd)),
409  bound_(bound), constraintOperator_(constraintOperator) {
410 
411 }
412 
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_) {
419 
420 }
421 
422 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
423 inline void LinearConstraint<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::reserve(const size_t numIndicatorVariables) {
424  indicatorVariables_.reserve(numIndicatorVariables);
425  coefficients_.reserve(numIndicatorVariables);
426 }
427 
428 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
429 inline void LinearConstraint<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::add(const IndicatorVariableType& indicatorVariable, const ValueType coefficient) {
430  indicatorVariables_.push_back(indicatorVariable);
431  coefficients_.push_back(coefficient);
432 }
433 
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());
438 }
439 
440 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
441 template<class INDICATOR_VARIABLES_ITERATOR_TYPE, class COEFFICIENTS_ITERATOR_TYPE>
442 inline void LinearConstraint<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::add(const INDICATOR_VARIABLES_ITERATOR_TYPE indicatorVariablesBegin, const INDICATOR_VARIABLES_ITERATOR_TYPE indicatorVariablesEnd, const COEFFICIENTS_ITERATOR_TYPE coefficientsBegin) {
444  coefficients_.insert(coefficients_.end(), coefficientsBegin, coefficientsBegin + std::distance(indicatorVariablesBegin, indicatorVariablesEnd));
445 }
446 
447 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
449  bound_ = bound;
450 }
451 
452 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
454  constraintOperator_ = constraintOperator;
455 }
456 
457 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
458 template<class ITERATOR_TYPE>
460  ValueType leftHandSide = 0.0;
461  for(IndexType i = 0; i < indicatorVariables_.size(); ++i) {
462  if(indicatorVariables_[i](statesBegin)) {
463  leftHandSide += coefficients_[i];
464  }
465  }
466 
467  // compare left hand side against bound
468  const ValueType weight = leftHandSide - bound_;
469  switch(constraintOperator_) {
471  if(weight > 0.0) {
472  return weight;
473  } else {
474  return 0.0;
475  }
476  break;
477  }
479  if(weight > 0.0) {
480  return weight;
481  } else if(weight < 0.0) {
482  return -weight;
483  } else {
484  return 0.0;
485  }
486  break;
487  }
488  /*case LinearConstraintOperatorType::GreaterEqual : {
489  if(weight < 0.0) {
490  return -weight;
491  } else {
492  return 0.0;
493  }
494  break;
495  } */
496  default : { // default corresponds to GreaterEqual case
497  if(weight < 0.0) {
498  return -weight;
499  } else {
500  return 0.0;
501  }
502  }
503  }
504 }
505 
506 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
508  return indicatorVariables_.begin();
509 }
510 
511 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
513  return indicatorVariables_.end();
514 }
515 
516 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
518  return coefficients_.begin();
519 }
520 
521 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
523  return coefficients_.end();
524 }
525 
526 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
528  return bound_;
529 }
530 
531 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
533  return constraintOperator_;
534 }
535 
536 } // namespace opengm
537 
538 #endif /* OPENGM_LINEAR_CONSTRAINT_HXX_ */
IndicatorVariablesIteratorType indicatorVariablesBegin() const
Get the begin iterator to the set of indicator variables.
CoefficientsContainerType::const_iterator CoefficientsIteratorType
Defines the const iterator type to iterate over the set of coefficients for the indicator variables...
CoefficientsIteratorType coefficientsBegin() const
Get the begin iterator to the set of coefficients for the indicator variables.
The OpenGM namespace.
Definition: config.hxx:43
IndicatorVariablesContainerType::const_iterator IndicatorVariablesIteratorType
Defines the const iterator type to iterate over the set of indicator variables.
ValueType operator()(const ITERATOR_TYPE statesBegin) const
Evaluation operator to check if the linear constraint is violated by the given labeling.
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...
CoefficientsIteratorType coefficientsEnd() const
Get the end iterator to the set of coefficients for the indicator variables.
CoefficientsContainerType coefficients_
Storage for the set of coefficients for the indicator variables.
STL namespace.
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.
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...
std::vector< ValueType > CoefficientsContainerType
Defines the storage type for the set of coefficients for the indicator variables. ...
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.
LABEL_TYPE LabelType
Typedef of the LABEL_TYPE template parameter type from the class LinearConstraint.
BoundType bound_
Storage for the bound of the linear constraint.
LinearConstraintOperatorType::ValueType LinearConstraintOperatorValueType
Defines the linear constraint operator type.
LinearConstraintOperatorValueType getConstraintOperator() const
Get the constraint operator of the linear constraint.
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.
BoundType getBound() const
Get the bound of the linear constraint.