OpenGM  2.3.x
Discrete Graphical Model Library
linear_constraint_function.hxx
Go to the documentation of this file.
1 #ifndef OPENGM_LINEAR_CONSTRAINT_FUNCTION_HXX_
2 #define OPENGM_LINEAR_CONSTRAINT_FUNCTION_HXX_
3 
4 #include <algorithm>
5 #include <vector>
6 #include <cmath>
7 #include <limits>
8 
9 #include <opengm/opengm.hxx>
11 
15 
16 namespace opengm {
17 
18 /*********************
19  * class definition *
20  *********************/
21 template<class VALUE_TYPE, class INDEX_TYPE = size_t, class LABEL_TYPE = size_t>
22 class LinearConstraintFunction : public LinearConstraintFunctionBase<LinearConstraintFunction<VALUE_TYPE,INDEX_TYPE, LABEL_TYPE> > {
23 public:
24  // typedefs
28  typedef typename LinearConstraintFunctionTraitsType::ValueType ValueType;
29  typedef typename LinearConstraintFunctionTraitsType::IndexType IndexType;
30  typedef typename LinearConstraintFunctionTraitsType::LabelType LabelType;
31  typedef typename LinearConstraintFunctionTraitsType::LinearConstraintType LinearConstraintType;
32  typedef typename LinearConstraintFunctionTraitsType::LinearConstraintsContainerType LinearConstraintsContainerType;
33  typedef typename LinearConstraintFunctionTraitsType::LinearConstraintsIteratorType LinearConstraintsIteratorType;
34  typedef typename LinearConstraintFunctionTraitsType::IndicatorVariablesContainerType IndicatorVariablesContainerType;
35  typedef typename LinearConstraintFunctionTraitsType::IndicatorVariablesIteratorType IndicatorVariablesIteratorType;
36  typedef typename LinearConstraintFunctionTraitsType::VariableLabelPairsIteratorType VariableLabelPairsIteratorType;
37  typedef typename LinearConstraintFunctionTraitsType::ViolatedLinearConstraintsIteratorType ViolatedLinearConstraintsIteratorType;
38  typedef typename LinearConstraintFunctionTraitsType::ViolatedLinearConstraintsWeightsContainerType ViolatedLinearConstraintsWeightsContainerType;
39  typedef typename LinearConstraintFunctionTraitsType::ViolatedLinearConstraintsWeightsIteratorType ViolatedLinearConstraintsWeightsIteratorType;
40 
41  // constructors
43  template <class SHAPE_ITERATOR_TYPE>
44  LinearConstraintFunction(SHAPE_ITERATOR_TYPE shapeBegin, SHAPE_ITERATOR_TYPE shapeEnd, const LinearConstraintsContainerType& constraints, const ValueType returnValid = 0.0, const ValueType returnInvalid = 1.0);
45  template <class SHAPE_ITERATOR_TYPE, class CONSTRAINTS_ITERATOR_TYPE>
46  LinearConstraintFunction(SHAPE_ITERATOR_TYPE shapeBegin, SHAPE_ITERATOR_TYPE shapeEnd, CONSTRAINTS_ITERATOR_TYPE constraintsBegin, CONSTRAINTS_ITERATOR_TYPE constraintsEnd, const ValueType returnValid = 0.0, const ValueType returnInvalid = 1.0);
48 
49  // function access
50  template<class STATES_ITERATOR_TYPE>
51  ValueType operator()(STATES_ITERATOR_TYPE statesBegin) const; // function evaluation
52  size_t shape(const size_t i) const; // number of labels of the indicated input variable
53  size_t dimension() const; // number of input variables
54  size_t size() const; // number of parameters
55 
56  // specializations
57  ValueType min() const;
58  ValueType max() const;
59  MinMaxFunctor<ValueType> minMax() const;
60 
61 protected:
62  // storage
63  std::vector<LabelType> shape_;
64  size_t size_;
65  LinearConstraintsContainerType constraints_;
66  mutable std::vector<size_t> violatedConstraintsIds_;
67  mutable ViolatedLinearConstraintsWeightsContainerType violatedConstraintsWeights_;
68  ValueType returnValid_;
69  ValueType returnInvalid_;
70  IndicatorVariablesContainerType indicatorVariableList_;
71  std::vector<std::vector<size_t> > indicatorVariableLookupTable_;
72 
73  // implementations for LinearConstraintFunctionBase
74  LinearConstraintsIteratorType linearConstraintsBegin_impl() const;
75  LinearConstraintsIteratorType linearConstraintsEnd_impl() const;
76  IndicatorVariablesIteratorType indicatorVariablesOrderBegin_impl() const;
77  IndicatorVariablesIteratorType indicatorVariablesOrderEnd_impl() const;
78  template <class LABEL_ITERATOR>
79  void challenge_impl(ViolatedLinearConstraintsIteratorType& violatedConstraintsBegin, ViolatedLinearConstraintsIteratorType& violatedConstraintsEnd, ViolatedLinearConstraintsWeightsIteratorType& violatedConstraintsWeightsBegin, LABEL_ITERATOR labelingBegin, const ValueType tolerance = 0.0) const;
80  template <class LABEL_ITERATOR>
81  void challengeRelaxed_impl(ViolatedLinearConstraintsIteratorType& violatedConstraintsBegin, ViolatedLinearConstraintsIteratorType& violatedConstraintsEnd, ViolatedLinearConstraintsWeightsIteratorType& violatedConstraintsWeightsBegin, LABEL_ITERATOR labelingBegin, const ValueType tolerance = 0.0) const;
82 
83  // sanity check
84  bool checkConstraints() const;
85 
86  // helper functions
89 
90  // friends
91  friend class FunctionSerialization<LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE> >;
92  friend class opengm::LinearConstraintFunctionBase<LinearConstraintFunction<VALUE_TYPE,INDEX_TYPE, LABEL_TYPE> >;
93 };
94 
95 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
96 struct LinearConstraintFunctionTraits<LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE> > {
97  // typedefs
98  typedef VALUE_TYPE ValueType;
99  typedef INDEX_TYPE IndexType;
100  typedef LABEL_TYPE LabelType;
102  typedef std::vector<LinearConstraintType> LinearConstraintsContainerType;
103  typedef typename LinearConstraintsContainerType::const_iterator LinearConstraintsIteratorType;
109  typedef typename ViolatedLinearConstraintsWeightsContainerType::const_iterator ViolatedLinearConstraintsWeightsIteratorType;
110 };
111 
114 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
115 struct FunctionRegistration<LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE> > {
116  enum ID {
117  // TODO set final Id
119  };
120 };
121 
123 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
124 class FunctionSerialization<LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE> > {
125 public:
126  typedef typename LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::ValueType ValueType;
127 
128  static size_t indexSequenceSize(const LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>&);
129  static size_t valueSequenceSize(const LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>&);
130  template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR>
131  static void serialize(const LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>&, INDEX_OUTPUT_ITERATOR, VALUE_OUTPUT_ITERATOR);
132  template<class INDEX_INPUT_ITERATOR, class VALUE_INPUT_ITERATOR>
133  static void deserialize( INDEX_INPUT_ITERATOR, VALUE_INPUT_ITERATOR, LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>&);
134 };
136 
137 /***********************
138  * class documentation *
139  ***********************/
427 /******************
428  * implementation *
429  ******************/
430 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
432  size_(), constraints_(), violatedConstraintsIds_(),
433  violatedConstraintsWeights_(), returnValid_(), returnInvalid_(),
434  indicatorVariableList_(), indicatorVariableLookupTable_() {
435 
436 }
437 
438 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
439 template <class SHAPE_ITERATOR_TYPE>
440 inline LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::LinearConstraintFunction(SHAPE_ITERATOR_TYPE shapeBegin, SHAPE_ITERATOR_TYPE shapeEnd, const LinearConstraintsContainerType& constraints, const ValueType returnValid, const ValueType returnInvalid)
441  : shape_(shapeBegin, shapeEnd),
442  size_(std::accumulate(shapeBegin, shapeEnd, 1, std::multiplies<typename std::iterator_traits<SHAPE_ITERATOR_TYPE>::value_type>())),
443  constraints_(constraints), violatedConstraintsIds_(constraints_.size()),
444  violatedConstraintsWeights_(constraints_.size()),
445  returnValid_(returnValid), returnInvalid_(returnInvalid),
446  indicatorVariableList_(), indicatorVariableLookupTable_() {
448 
449  // fill indicatorVariableList_
451 
452  // create indicatorVariableLookupTable_
454 }
455 
456 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
457 template <class SHAPE_ITERATOR_TYPE, class CONSTRAINTS_ITERATOR_TYPE>
458 inline LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::LinearConstraintFunction(SHAPE_ITERATOR_TYPE shapeBegin, SHAPE_ITERATOR_TYPE shapeEnd, CONSTRAINTS_ITERATOR_TYPE constraintsBegin, CONSTRAINTS_ITERATOR_TYPE constraintsEnd, const ValueType returnValid, const ValueType returnInvalid)
459  : shape_(shapeBegin, shapeEnd),
460  size_(std::accumulate(shapeBegin, shapeEnd, 1, std::multiplies<typename std::iterator_traits<SHAPE_ITERATOR_TYPE>::value_type>())),
461  constraints_(constraintsBegin, constraintsEnd),
462  violatedConstraintsIds_(constraints_.size()),
463  violatedConstraintsWeights_(constraints_.size()),
464  returnValid_(returnValid), returnInvalid_(returnInvalid),
465  indicatorVariableList_(), indicatorVariableLookupTable_() {
467 
468  // fill indicatorVariableList_
470 
471  // create indicatorVariableLookupTable_
473 }
474 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
476 
477 }
478 
479 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
480 template<class STATES_ITERATOR_TYPE>
482  for(LinearConstraintsIteratorType constraintsIter = constraints_.begin(); constraintsIter != constraints_.end(); ++constraintsIter) {
483  // compare result against bound with floating point tolerance
484  const ValueType weight = constraintsIter->operator()(statesBegin);
485  if(weight > OPENGM_FLOAT_TOL) {
486  return returnInvalid_;
487  }
488  }
489  return returnValid_;
490 }
491 
492 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
494  OPENGM_ASSERT(i < shape_.size());
495  return shape_[i];
496 }
497 
498 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
500  return shape_.size();
501 }
502 
503 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
505  return size_;
506 }
507 
508 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
510  return returnValid_ < returnInvalid_ ? returnValid_ : returnInvalid_;
511 }
512 
513 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
515  return returnValid_ > returnInvalid_ ? returnValid_ : returnInvalid_;
516 }
517 
518 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
519 inline MinMaxFunctor<typename LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::ValueType> LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::minMax() const {
520  if(returnValid_ < returnInvalid_) {
521  return MinMaxFunctor<ValueType>(returnValid_, returnInvalid_);
522  }
523  else {
524  return MinMaxFunctor<ValueType>(returnInvalid_, returnValid_);
525  }
526 }
527 
528 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
530  return constraints_.begin();
531 }
532 
533 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
535  return constraints_.end();
536 }
537 
538 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
540  return indicatorVariableList_.begin();
541 }
542 
543 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
545  return indicatorVariableList_.end();
546 }
547 
548 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
549 template <class LABEL_ITERATOR>
550 inline void LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::challenge_impl(ViolatedLinearConstraintsIteratorType& violatedConstraintsBegin, ViolatedLinearConstraintsIteratorType& violatedConstraintsEnd, ViolatedLinearConstraintsWeightsIteratorType& violatedConstraintsWeightsBegin, LABEL_ITERATOR labelingBegin, const ValueType tolerance) const {
551  size_t numViolatedConstraints = 0;
552  for(LinearConstraintsIteratorType constraintsIter = constraints_.begin(); constraintsIter != constraints_.end(); ++constraintsIter) {
553  // compare result against bound with floating point tolerance
554  const double weight = static_cast<double>(constraintsIter->operator()(labelingBegin));
555  if(weight > tolerance) {
556  violatedConstraintsIds_[numViolatedConstraints] = std::distance(constraints_.begin(), constraintsIter);
557  violatedConstraintsWeights_[numViolatedConstraints] = weight;
558  ++numViolatedConstraints;
559  }
560  }
561  violatedConstraintsBegin = ViolatedLinearConstraintsIteratorType(constraints_.begin(), violatedConstraintsIds_.begin(), 0);
562  violatedConstraintsEnd = ViolatedLinearConstraintsIteratorType(constraints_.begin(), violatedConstraintsIds_.begin(), numViolatedConstraints);
563  violatedConstraintsWeightsBegin = violatedConstraintsWeights_.begin();
564 }
565 
566 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
567 template <class LABEL_ITERATOR>
568 inline void LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::challengeRelaxed_impl(ViolatedLinearConstraintsIteratorType& violatedConstraintsBegin, ViolatedLinearConstraintsIteratorType& violatedConstraintsEnd, ViolatedLinearConstraintsWeightsIteratorType& violatedConstraintsWeightsBegin, LABEL_ITERATOR labelingBegin, const ValueType tolerance) const {
569  size_t numViolatedConstraints = 0;
570  std::vector<std::vector<size_t> >::const_iterator indicatorVariableLookupTableIter = indicatorVariableLookupTable_.begin();
571  for(LinearConstraintsIteratorType constraintsIter = constraints_.begin(); constraintsIter != constraints_.end(); ++constraintsIter) {
572  double result = 0.0;
573  typename LinearConstraintType::CoefficientsIteratorType coefficientsIter = constraintsIter->coefficientsBegin();
574  for(std::vector<size_t>::const_iterator variableIDsIter = indicatorVariableLookupTableIter->begin(); variableIDsIter != indicatorVariableLookupTableIter->end(); ++variableIDsIter) {
575  result += labelingBegin[*variableIDsIter] * (*coefficientsIter);
576  ++coefficientsIter;
577  }
578 
579  // compare result against bound with floating point tolerance
580  const double weight = result - constraintsIter->getBound();
581  switch(constraintsIter->getConstraintOperator()) {
582  case LinearConstraintType::LinearConstraintOperatorType::LessEqual : {
583  if(weight > tolerance) {
584  violatedConstraintsIds_[numViolatedConstraints] = std::distance(constraints_.begin(), constraintsIter);
585  violatedConstraintsWeights_[numViolatedConstraints] = weight;
586  ++numViolatedConstraints;
587  }
588  break;
589  }
590  case LinearConstraintType::LinearConstraintOperatorType::Equal : {
591  if(weight > tolerance) {
592  violatedConstraintsIds_[numViolatedConstraints] = std::distance(constraints_.begin(), constraintsIter);
593  violatedConstraintsWeights_[numViolatedConstraints] = weight;
594  ++numViolatedConstraints;
595  } else if(weight < -tolerance) {
596  violatedConstraintsIds_[numViolatedConstraints] = std::distance(constraints_.begin(), constraintsIter);
597  violatedConstraintsWeights_[numViolatedConstraints] = -weight;
598  ++numViolatedConstraints;
599  }
600  break;
601  }
602  /*case LinearConstraintType::LinearConstraintOperatorType::GreaterEqual : {
603  if(weight < -tolerance) {
604  violatedConstraintsIds_[numViolatedConstraints] = std::distance(constraints_.begin(), constraintsIter);
605  violatedConstraintsWeights_[numViolatedConstraints] = -weight;
606  ++numViolatedConstraints;
607  }
608  break;
609  } */
610  default : { // default corresponds to GreaterEqual case
611  if(weight < -tolerance) {
612  violatedConstraintsIds_[numViolatedConstraints] = std::distance(constraints_.begin(), constraintsIter);
613  violatedConstraintsWeights_[numViolatedConstraints] = -weight;
614  ++numViolatedConstraints;
615  }
616  }
617  }
618  ++indicatorVariableLookupTableIter;
619  }
620  violatedConstraintsBegin = ViolatedLinearConstraintsIteratorType(constraints_.begin(), violatedConstraintsIds_.begin(), 0);
621  violatedConstraintsEnd = ViolatedLinearConstraintsIteratorType(constraints_.begin(), violatedConstraintsIds_.begin(), numViolatedConstraints);
622  violatedConstraintsWeightsBegin = violatedConstraintsWeights_.begin();
623 }
624 
625 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
627  for(LinearConstraintsIteratorType constraintsIter = constraints_.begin(); constraintsIter != constraints_.end(); ++constraintsIter) {
628  if(std::distance(constraintsIter->indicatorVariablesBegin(), constraintsIter->indicatorVariablesEnd()) == 0) {
629  // no empty constraint allowed
630  std::cout << "empty constraint" << std::endl;
631  return false;
632  }
633  for(IndicatorVariablesIteratorType variablesIter = constraintsIter->indicatorVariablesBegin(); variablesIter != constraintsIter->indicatorVariablesEnd(); ++variablesIter) {
634  if(std::distance(variablesIter->begin(), variablesIter->end()) == 0) {
635  // no empty indicator variable allowed
636  std::cout << "empty indicator variable" << std::endl;
637  return false;
638  }
639  for(VariableLabelPairsIteratorType indicatorVariablesIter = variablesIter->begin(); indicatorVariablesIter != variablesIter->end(); ++indicatorVariablesIter) {
640  if(indicatorVariablesIter->first >= shape_.size()) {
641  return false;
642  } else if(indicatorVariablesIter->second >= shape_[indicatorVariablesIter->first]) {
643  return false;
644  }
645  }
646  }
647  }
648  return true;
649 }
650 
651 
652 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
654  // fill indicatorVariableList_
655  for(LinearConstraintsIteratorType constraintsIter = constraints_.begin(); constraintsIter != constraints_.end(); ++constraintsIter) {
656  for(IndicatorVariablesIteratorType variablesIter = constraintsIter->indicatorVariablesBegin(); variablesIter != constraintsIter->indicatorVariablesEnd(); ++variablesIter) {
657  if(std::find(indicatorVariableList_.begin(), indicatorVariableList_.end(), *variablesIter) == indicatorVariableList_.end()) {
658  indicatorVariableList_.push_back(*variablesIter) ;
659  }
660  }
661  }
662 
663  // sort indicatorVariableList_
664  std::sort(indicatorVariableList_.begin(), indicatorVariableList_.end());
665 }
666 
667 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
669  // create indicatorVariableLookupTable_
670  for(LinearConstraintsIteratorType constraintsIter = constraints_.begin(); constraintsIter != constraints_.end(); ++constraintsIter) {
671  std::vector<size_t> currentConstraintLookup;
672  for(IndicatorVariablesIteratorType variablesIter = constraintsIter->indicatorVariablesBegin(); variablesIter != constraintsIter->indicatorVariablesEnd(); ++variablesIter) {
673  OPENGM_ASSERT(std::find(indicatorVariableList_.begin(), indicatorVariableList_.end(), *variablesIter) != indicatorVariableList_.end());
674  currentConstraintLookup.push_back(std::distance(indicatorVariableList_.begin(), std::find(indicatorVariableList_.begin(), indicatorVariableList_.end(), *variablesIter)));
675  }
676  indicatorVariableLookupTable_.push_back(currentConstraintLookup);
677  }
678 }
679 
681 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
683  const size_t dimensionSize = 1;
684  const size_t shapeSize = src.dimension();
685  const size_t numConstraintsSize = 1;
686  const size_t operatorTypeSize = src.constraints_.size();
687  const size_t numIndicatorVariablesPerConstraintSize = src.constraints_.size();
688 
690 
691  size_t numVariablesPerIndicatorVariablePerConstraintSize = 0;
692  for(LinearConstraintsIteratorType constraintsIter = src.constraints_.begin(); constraintsIter != src.constraints_.end(); ++constraintsIter) {
693  numVariablesPerIndicatorVariablePerConstraintSize += std::distance(constraintsIter->indicatorVariablesBegin(), constraintsIter->indicatorVariablesEnd());
694  }
695  numVariablesPerIndicatorVariablePerConstraintSize *= 2;
696 
697  typedef typename LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::IndicatorVariablesIteratorType IndicatorVariablesIteratorType;
698 
699  size_t variableStatePairSize = 0;
700  for(LinearConstraintsIteratorType constraintsIter = src.constraints_.begin(); constraintsIter != src.constraints_.end(); ++constraintsIter) {
701  for(IndicatorVariablesIteratorType variablesIter = constraintsIter->indicatorVariablesBegin(); variablesIter != constraintsIter->indicatorVariablesEnd(); ++variablesIter) {
702  variableStatePairSize += std::distance(variablesIter->begin(), variablesIter->end());
703  }
704  }
705  variableStatePairSize *= 2;
706 
707  const size_t totalIndexSize = dimensionSize + shapeSize + operatorTypeSize + numConstraintsSize + numIndicatorVariablesPerConstraintSize + numVariablesPerIndicatorVariablePerConstraintSize + variableStatePairSize;
708  return totalIndexSize;
709 }
710 
711 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
712 inline size_t FunctionSerialization<LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE> >::valueSequenceSize(const LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>& src) {
713  const size_t boundsSize = src.constraints_.size();
714  const size_t validSize = 1;
715  const size_t invalidSize = 1;
716 
717  typedef typename LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::LinearConstraintsIteratorType LinearConstraintsIteratorType;
718 
719  size_t coefficientsSize = 0;
720  for(LinearConstraintsIteratorType constraintsIter = src.constraints_.begin(); constraintsIter != src.constraints_.end(); ++constraintsIter) {
721  coefficientsSize += std::distance(constraintsIter->coefficientsBegin(), constraintsIter->coefficientsEnd());
722  }
723 
724  const size_t totalValueSize = boundsSize + validSize + invalidSize + coefficientsSize;
725  return totalValueSize;
726 }
727 
728 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
729 template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR>
730 inline void FunctionSerialization<LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE> >::serialize(const LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>& src, INDEX_OUTPUT_ITERATOR indexOutIterator, VALUE_OUTPUT_ITERATOR valueOutIterator) {
731  // index output
732  // dimension
733  *indexOutIterator = src.dimension();
734  ++indexOutIterator;
735 
736  // shape
737  for(size_t i = 0; i < src.dimension(); i++) {
738  *indexOutIterator = src.shape_[i];
739  ++indexOutIterator;
740  }
741 
742  // number of constraints
743  *indexOutIterator = src.constraints_.size();
744  ++indexOutIterator;
745 
746  // operator type
747  typedef typename LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::LinearConstraintsIteratorType LinearConstraintsIteratorType;
748 
749  for(LinearConstraintsIteratorType constraintsIter = src.constraints_.begin(); constraintsIter != src.constraints_.end(); ++constraintsIter) {
750  *indexOutIterator = constraintsIter->getConstraintOperator();
751  ++indexOutIterator;
752  }
753 
754  // number of indicator variables per constraint
755  for(LinearConstraintsIteratorType constraintsIter = src.constraints_.begin(); constraintsIter != src.constraints_.end(); ++constraintsIter) {
756  *indexOutIterator = std::distance(constraintsIter->indicatorVariablesBegin(), constraintsIter->indicatorVariablesEnd());
757  ++indexOutIterator;
758  }
759 
760  // number of variables per indicator variable per constraint
761  typedef typename LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::IndicatorVariablesIteratorType IndicatorVariablesIteratorType;
762 
763  for(LinearConstraintsIteratorType constraintsIter = src.constraints_.begin(); constraintsIter != src.constraints_.end(); ++constraintsIter) {
764  for(IndicatorVariablesIteratorType variablesIter = constraintsIter->indicatorVariablesBegin(); variablesIter != constraintsIter->indicatorVariablesEnd(); ++variablesIter) {
765  *indexOutIterator = static_cast<size_t>(variablesIter->getLogicalOperatorType());
766  ++indexOutIterator;
767  *indexOutIterator = std::distance(variablesIter->begin(), variablesIter->end());
768  ++indexOutIterator;
769  }
770  }
771 
772  // variable state pairs
773  typedef typename LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::VariableLabelPairsIteratorType VariableLabelPairsIteratorType;
774 
775  for(LinearConstraintsIteratorType constraintsIter = src.constraints_.begin(); constraintsIter != src.constraints_.end(); ++constraintsIter) {
776  for(IndicatorVariablesIteratorType variablesIter = constraintsIter->indicatorVariablesBegin(); variablesIter != constraintsIter->indicatorVariablesEnd(); ++variablesIter) {
777  for(VariableLabelPairsIteratorType indicatorVariablesIter = variablesIter->begin(); indicatorVariablesIter != variablesIter->end(); ++indicatorVariablesIter) {
778  *indexOutIterator = indicatorVariablesIter->first;
779  ++indexOutIterator;
780  *indexOutIterator = indicatorVariablesIter->second;
781  ++indexOutIterator;
782  }
783  }
784  }
785 
786  // value output
787  // bound
788  for(LinearConstraintsIteratorType constraintsIter = src.constraints_.begin(); constraintsIter != src.constraints_.end(); ++constraintsIter) {
789  *valueOutIterator = constraintsIter->getBound();
790  ++valueOutIterator;
791  }
792 
793  // valid value
794  *valueOutIterator = src.returnValid_;
795  ++valueOutIterator;
796 
797  // invalid value
798  *valueOutIterator = src.returnInvalid_;
799  ++valueOutIterator;
800 
801  // coefficients
802  typedef typename LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::LinearConstraintType::CoefficientsIteratorType CoefficientsIteratorType;
803  for(LinearConstraintsIteratorType constraintsIter = src.constraints_.begin(); constraintsIter != src.constraints_.end(); ++constraintsIter) {
804  for(CoefficientsIteratorType coefficientsIter = constraintsIter->coefficientsBegin(); coefficientsIter != constraintsIter->coefficientsEnd(); ++coefficientsIter) {
805  *valueOutIterator = *coefficientsIter;
806  ++valueOutIterator;
807  }
808  }
809 }
810 
811 template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
812 template<class INDEX_INPUT_ITERATOR, class VALUE_INPUT_ITERATOR>
813 inline void FunctionSerialization<LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE> >::deserialize( INDEX_INPUT_ITERATOR indexInIterator, VALUE_INPUT_ITERATOR valueInIterator, LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>& dst) {
814  // index input
815  // dimension
816  const size_t dimension = *indexInIterator;
817  ++indexInIterator;
818  // shape
819  INDEX_INPUT_ITERATOR shapeBegin = indexInIterator;
820  INDEX_INPUT_ITERATOR shapeEnd = indexInIterator + dimension;
821  indexInIterator += dimension;
822 
823  // constraints
824  typename LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::LinearConstraintsContainerType constraints(*indexInIterator);
825  ++indexInIterator;
826 
827  // operator type
828  typedef typename LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::LinearConstraintsContainerType::iterator LinearConstraintsIteratorType;
829 
830  for(LinearConstraintsIteratorType constraintsIter = constraints.begin(); constraintsIter != constraints.end(); ++constraintsIter) {
831  constraintsIter->setConstraintOperator(static_cast<typename LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::LinearConstraintType::LinearConstraintOperatorValueType>(*indexInIterator));
832  ++indexInIterator;
833  }
834 
835  // number of indicator variables per constraint
836  std::vector<INDEX_TYPE> numIndicatorariablesPerConstraint;
837  for(LinearConstraintsIteratorType constraintsIter = constraints.begin(); constraintsIter != constraints.end(); ++constraintsIter) {
838  numIndicatorariablesPerConstraint.push_back(*indexInIterator);
839  ++indexInIterator;
840  }
841 
842  // number of variables per indicator variable per constraint
843  std::vector<std::vector<INDEX_TYPE> > numVariablesPerIndicatorVariablePerConstraint;
844  typedef typename LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::LinearConstraintType::IndicatorVariableType::LogicalOperatorType LogicalOperatorType;
845  std::vector<std::vector<LogicalOperatorType> > logicalOperatorPerIndicatorVariablePerConstraint;
846  for(size_t i = 0; i < numIndicatorariablesPerConstraint.size(); ++i) {
847  numVariablesPerIndicatorVariablePerConstraint.push_back(std::vector<INDEX_TYPE>());
848  logicalOperatorPerIndicatorVariablePerConstraint.push_back(std::vector<LogicalOperatorType>());
849  for(size_t j = 0; j < numIndicatorariablesPerConstraint[i]; ++j) {
850  logicalOperatorPerIndicatorVariablePerConstraint[i].push_back(static_cast<LogicalOperatorType>(*indexInIterator));
851  ++indexInIterator;
852  numVariablesPerIndicatorVariablePerConstraint[i].push_back(*indexInIterator);
853  ++indexInIterator;
854  }
855  }
856 
857  // variable state pairs
858  typedef typename LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::LinearConstraintType::IndicatorVariablesContainerType IndicatorVariablesContainerType;
859  std::vector<IndicatorVariablesContainerType> variableStatePairs;
860 
861  for(size_t i = 0; i < numVariablesPerIndicatorVariablePerConstraint.size(); ++i) {
862  variableStatePairs.push_back(IndicatorVariablesContainerType());
863  for(size_t j = 0; j < numVariablesPerIndicatorVariablePerConstraint[i].size(); ++j) {
864  variableStatePairs[i].push_back(typename LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::LinearConstraintType::IndicatorVariableType());
865  for(size_t k = 0; k < numVariablesPerIndicatorVariablePerConstraint[i][j]; ++k) {
866  const INDEX_TYPE variable = *indexInIterator;
867  ++indexInIterator;
868  const LABEL_TYPE label = *indexInIterator;
869  ++indexInIterator;
870  variableStatePairs[i][j].add(variable, label);
871  }
872 
873  variableStatePairs[i][j].setLogicalOperatorType(logicalOperatorPerIndicatorVariablePerConstraint[i][j]);
874  }
875  }
876 
877  // value input
878  // bound
879  for(LinearConstraintsIteratorType constraintsIter = constraints.begin(); constraintsIter != constraints.end(); ++constraintsIter) {
880  constraintsIter->setBound(*valueInIterator);
881  ++valueInIterator;
882  }
883 
884  // valid value
885  const VALUE_TYPE returnValid = *valueInIterator;
886  ++valueInIterator;
887 
888  // invalid value
889  const VALUE_TYPE returnInvalid = *valueInIterator;
890  ++valueInIterator;
891 
892  // coefficients
893  typedef typename LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::LinearConstraintType::CoefficientsContainerType CoefficientsContainerType;
894  std::vector<CoefficientsContainerType> coefficients;
895  for(size_t i = 0; i < numIndicatorariablesPerConstraint.size(); ++i) {
896  coefficients.push_back(CoefficientsContainerType());
897  for(size_t j = 0; j < numIndicatorariablesPerConstraint[i]; ++j) {
898  coefficients[i].push_back(*valueInIterator);
899  ++valueInIterator;
900  }
901  }
902 
903  // add variables and coefficients to constraints
904  for(size_t i = 0; i < constraints.size(); ++i) {
905  constraints[i].add(variableStatePairs[i].begin(), variableStatePairs[i].end(), coefficients[i].begin());
906  }
907 
908  dst = LinearConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>(shapeBegin, shapeEnd, constraints, returnValid, returnInvalid);
909 }
911 
912 } // namespace opengm
913 
914 #endif /* OPENGM_LINEAR_CONSTRAINT_FUNCTION_HXX_ */
#define OPENGM_FLOAT_TOL
LinearConstraintsIteratorType linearConstraintsEnd_impl() const
Implementation of LinearConstraintFunctionBase::linearConstraintsEnd.
ValueType returnValid_
Stores the return value of LinearConstraintFunction::operator() if no constraint is violated...
The OpenGM namespace.
Definition: config.hxx:43
IndicatorVariablesContainerType::const_iterator IndicatorVariablesIteratorType
Defines the const iterator type to iterate over the set of indicator variables.
Provides interface for liner constraint functions.
std::vector< std::vector< size_t > > indicatorVariableLookupTable_
Lookup table for fast access of the indicator variable IDs.
bool checkConstraints() const
Check linear constraints. Only used for assertion in debug mode.
IndicatorVariableType::IteratorType VariableLabelPairsIteratorType
Defines the const iterator type to iterate over the variable label pairs of an indicator variable...
LinearConstraintsContainerType constraints_
Stores the constraints of the linear constraint function.
LinearConstraintFunctionTraitsType::LinearConstraintType LinearConstraintType
Typedef of the LinearConstraint class which is used to represent linear constraints.
STL namespace.
std::vector< IndicatorVariableType > IndicatorVariablesContainerType
Defines the storage type for the set of indicator variables.
LinearConstraintFunctionTraitsType::ViolatedLinearConstraintsIteratorType ViolatedLinearConstraintsIteratorType
Defines the violated linear constraints iterator type which is used to iterate over the set of violat...
LinearConstraintFunctionTraitsType::IndexType IndexType
Typedef of the INDEX_TYPE template parameter type from the class LinearConstraintFunction.
MinMaxFunctor< ValueType > minMax() const
Get minimum and maximum at the same time.
LinearConstraintFunctionTraitsType::IndicatorVariablesIteratorType IndicatorVariablesIteratorType
Defines the indicator variables container iterator type which is used to iterate over the indicator v...
Defines the const iterator type to iterate over the subset of a sequence.
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
LinearConstraintFunctionTraitsType::LinearConstraintsContainerType LinearConstraintsContainerType
Defines the linear constraints container type which is used to store multiple linear constraints...
LinearConstraintFunctionTraitsType::ViolatedLinearConstraintsWeightsIteratorType ViolatedLinearConstraintsWeightsIteratorType
Defines the violated linear constraints weights iterator type which is used to iterate over the weigh...
IndicatorVariablesIteratorType indicatorVariablesOrderBegin_impl() const
Implementation of LinearConstraintFunctionBase::indicatorVariableOrderBegin.
std::vector< LabelType > shape_
Stores the shape of the linear constraint function.
Default implementation of a linear constraint function class.
LinearConstraintFunction()
LinearConstraintFunction constructor.
LinearConstraintFunctionTraitsType::ViolatedLinearConstraintsWeightsContainerType ViolatedLinearConstraintsWeightsContainerType
Defines the violated linear constraints weights container type which is used to store the weights of ...
void challenge_impl(ViolatedLinearConstraintsIteratorType &violatedConstraintsBegin, ViolatedLinearConstraintsIteratorType &violatedConstraintsEnd, ViolatedLinearConstraintsWeightsIteratorType &violatedConstraintsWeightsBegin, LABEL_ITERATOR labelingBegin, const ValueType tolerance=0.0) const
Implementation of LinearConstraintFunctionBase::challenge.
Base class for linear constraint functions.
std::vector< size_t > violatedConstraintsIds_
Stores the indices of the violated constraints which are detected by LinearConstraintFunction::challe...
IndicatorVariablesContainerType indicatorVariableList_
A list of all indicator variables present in the linear constraint function.
size_t shape(const size_t i) const
Number of labels of the indicated input variable.
LinearConstraintsIteratorType linearConstraintsBegin_impl() const
Implementation of LinearConstraintFunctionBase::linearConstraintsBegin.
void createIndicatorVariableLookupTable()
Helper function to create an indicator variable lookup table. The table is stored in LinearConstraint...
ViolatedLinearConstraintsWeightsContainerType violatedConstraintsWeights_
Stores the weights of the violated constraints which are detected by LinearConstraintFunction::challe...
LinearConstraintFunctionTraitsType::LinearConstraintsIteratorType LinearConstraintsIteratorType
Defines the linear constraints container iterator type which is used to iterate over the set of linea...
IndicatorVariablesIteratorType indicatorVariablesOrderEnd_impl() const
Implementation of LinearConstraintFunctionBase::indicatorVariableOrderEnd.
Traits class for linear constraint functions.
ValueType min() const
Minimum value of the linear constraint function.
const size_t FUNCTION_TYPE_ID_OFFSET
User-defined function have ids smaller than FUNCTION_TYPE_ID_OFFSET.
Provides implementation for class LinearConstraint.
ValueType operator()(STATES_ITERATOR_TYPE statesBegin) const
Function evaluation.
LinearConstraintFunctionTraits< LinearConstraintFunctionType > LinearConstraintFunctionTraitsType
Typedef of the LinearConstraintFunctionTraits class with appropriate template parameter.
LinearConstraintFunctionTraitsType::ValueType ValueType
Typedef of the VALUE_TYPE template parameter type from the class LinearConstraintFunction.
size_t size() const
Number of parameters.
Provides implementation for class SubsequenceIterator.
LinearConstraintFunctionBase< LinearConstraintFunctionType > LinearConstraintFunctionBaseType
Typedef of the LinearConstraintFunctionBase class with appropriate template parameter.
LinearConstraintFunctionTraitsType::VariableLabelPairsIteratorType VariableLabelPairsIteratorType
Defines the variable label pairs iterator type which is used to iterate over the variable label pairs...
void fillIndicatorVariableList()
Helper function to fill LinearConstraintFunction::indicatorVariableList_ with all indicator variables...
LinearConstraintFunction< VALUE_TYPE, INDEX_TYPE, LABEL_TYPE > LinearConstraintFunctionType
Typedef of the LinearConstraintFunction class with appropriate template parameter.
ValueType returnInvalid_
Stores the return value of LinearConstraintFunction::operator() if at least one constraint is violate...
LinearConstraintFunctionTraitsType::IndicatorVariablesContainerType IndicatorVariablesContainerType
Defines the indicator variables container type which is used to store the indicator variables used by...
Define a linear constraint for a set of indicatorVariables.
LinearConstraintFunctionTraitsType::LabelType LabelType
Typedef of the LABEL_TYPE template parameter type from the class LinearConstraintFunction.
void challengeRelaxed_impl(ViolatedLinearConstraintsIteratorType &violatedConstraintsBegin, ViolatedLinearConstraintsIteratorType &violatedConstraintsEnd, ViolatedLinearConstraintsWeightsIteratorType &violatedConstraintsWeightsBegin, LABEL_ITERATOR labelingBegin, const ValueType tolerance=0.0) const
Implementation of LinearConstraintFunctionBase::challengeRelaxed.
size_t size_
Stores the size of the linear constraint function.
SubsequenceIterator< LinearConstraintsIteratorType, typename std::vector< size_t >::const_iterator > ViolatedLinearConstraintsIteratorType
~LinearConstraintFunction()
LinearConstraintFunction destructor.
ValueType max() const
Maximum value of the linear constraint function.
size_t dimension() const
Number of input variables.