1 #ifndef OPENGM_NUM_LABELS_LIMITATION_FUNCTION_HXX_
2 #define OPENGM_NUM_LABELS_LIMITATION_FUNCTION_HXX_
17 template<
class VALUE_TYPE,
class INDEX_TYPE =
size_t,
class LABEL_TYPE =
size_t>
24 typedef typename LinearConstraintFunctionTraitsType::ValueType
ValueType;
25 typedef typename LinearConstraintFunctionTraitsType::IndexType
IndexType;
26 typedef typename LinearConstraintFunctionTraitsType::LabelType
LabelType;
39 template <
class SHAPE_ITERATOR_TYPE>
40 NumLabelsLimitationFunction(SHAPE_ITERATOR_TYPE shapeBegin, SHAPE_ITERATOR_TYPE shapeEnd,
const LabelType maxNumUsedLabels,
const ValueType returnValid = 0.0,
const ValueType returnInvalid = 1.0);
41 NumLabelsLimitationFunction(
const IndexType numVariables,
const LabelType numLabels,
const LabelType maxNumUsedLabels,
const ValueType returnValid = 0.0,
const ValueType returnInvalid = 1.0);
45 template<
class Iterator>
46 ValueType
operator()(Iterator statesBegin)
const;
47 size_t shape(
const size_t i)
const;
52 ValueType
min()
const;
53 ValueType
max()
const;
54 MinMaxFunctor<ValueType>
minMax()
const;
76 template <
class LABEL_ITERATOR>
77 void challenge_impl(ViolatedLinearConstraintsIteratorType& violatedConstraintsBegin, ViolatedLinearConstraintsIteratorType& violatedConstraintsEnd, ViolatedLinearConstraintsWeightsIteratorType& violatedConstraintsWeightsBegin, LABEL_ITERATOR labelingBegin,
const ValueType tolerance = 0.0)
const;
78 template <
class LABEL_ITERATOR>
79 void challengeRelaxed_impl(ViolatedLinearConstraintsIteratorType& violatedConstraintsBegin, ViolatedLinearConstraintsIteratorType& violatedConstraintsEnd, ViolatedLinearConstraintsWeightsIteratorType& violatedConstraintsWeightsBegin, LABEL_ITERATOR labelingBegin,
const ValueType tolerance = 0.0)
const;
90 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
109 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
118 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
119 class FunctionSerialization<NumLabelsLimitationFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE> > {
121 typedef typename NumLabelsLimitationFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::ValueType ValueType;
123 static size_t indexSequenceSize(
const NumLabelsLimitationFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>&);
124 static size_t valueSequenceSize(
const NumLabelsLimitationFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>&);
125 template<
class INDEX_OUTPUT_ITERATOR,
class VALUE_OUTPUT_ITERATOR>
126 static void serialize(
const NumLabelsLimitationFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>&, INDEX_OUTPUT_ITERATOR, VALUE_OUTPUT_ITERATOR);
127 template<
class INDEX_INPUT_ITERATOR,
class VALUE_INPUT_ITERATOR>
128 static void deserialize( INDEX_INPUT_ITERATOR, VALUE_INPUT_ITERATOR, NumLabelsLimitationFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>&);
443 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
445 : shape_(), numVariables_(), useSameNumLabels_(), maxNumLabels_(),
446 maxNumUsedLabels_(), size_(), returnValid_(), returnInvalid_(),
447 constraints_(), violatedConstraintsIds_(), violatedConstraintsWeights_(),
448 indicatorVariableList_() {
452 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
453 template <
class SHAPE_ITERATOR_TYPE>
455 : shape_(shapeBegin, shapeEnd), numVariables_(shape_.size()),
456 useSameNumLabels_(numVariables_ > 0 ?
std::equal(shape_.begin() + 1, shape_.end(), shape_.begin()) : true),
457 maxNumLabels_(numVariables_ > 0 ? *
std::max_element(shape_.begin(), shape_.end()) : 0),
458 maxNumUsedLabels_(maxNumUsedLabels),
459 size_(
std::accumulate(shapeBegin, shapeEnd, 1,
std::multiplies<typename
std::iterator_traits<SHAPE_ITERATOR_TYPE>::value_type>())),
460 returnValid_(returnValid), returnInvalid_(returnInvalid),
461 constraints_(1), violatedConstraintsIds_(1), violatedConstraintsWeights_(1),
462 indicatorVariableList_(maxNumLabels_) {
470 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
472 : shape_(), numVariables_(numVariables), useSameNumLabels_(true),
473 maxNumLabels_(numLabels), maxNumUsedLabels_(maxNumUsedLabels),
475 returnValid_(returnValid), returnInvalid_(returnInvalid),
476 constraints_(1), violatedConstraintsIds_(1), violatedConstraintsWeights_(1),
477 indicatorVariableList_(maxNumLabels_) {
485 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
490 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
491 template<
class Iterator>
493 std::vector<bool> labelIsUsed(maxNumLabels_,
false);
495 const Iterator statesEnd = statesBegin + numVariables_;
496 while(statesBegin != statesEnd) {
497 const LabelType currentLabel = *statesBegin;
499 if(!labelIsUsed[currentLabel]) {
500 labelIsUsed[currentLabel] =
true;
502 if(maxNumLabelsFound > maxNumUsedLabels_) {
503 return returnInvalid_;
511 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
514 return (useSameNumLabels_ ? maxNumLabels_ : shape_[i]);
517 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
519 return numVariables_;
522 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
527 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
529 return returnValid_ < returnInvalid_ ? returnValid_ : returnInvalid_;
532 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
534 return returnValid_ > returnInvalid_ ? returnValid_ : returnInvalid_;
537 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
539 if(returnValid_ < returnInvalid_) {
540 return MinMaxFunctor<VALUE_TYPE>(returnValid_, returnInvalid_);
543 return MinMaxFunctor<VALUE_TYPE>(returnInvalid_, returnValid_);
547 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
549 return constraints_.begin();
552 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
554 return constraints_.end();
557 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
559 return indicatorVariableList_.begin();
562 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
564 return indicatorVariableList_.end();
567 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
568 template <
class LABEL_ITERATOR>
570 std::vector<bool> labelIsUsed(maxNumLabels_,
false);
572 const LABEL_ITERATOR labelingEnd = labelingBegin + numVariables_;
573 while(labelingBegin != labelingEnd) {
574 const LabelType currentLabel = *labelingBegin;
576 if(!labelIsUsed[currentLabel]) {
577 labelIsUsed[currentLabel] =
true;
579 if(maxNumLabelsFound == static_cast<ValueType>(maxNumLabels_)) {
587 const ValueType weight = maxNumLabelsFound -
static_cast<ValueType>(maxNumUsedLabels_);
588 if(weight <= tolerance) {
591 violatedConstraintsWeightsBegin = violatedConstraintsWeights_.begin();
593 violatedConstraintsIds_[0] = 0;
594 violatedConstraintsWeights_[0] = weight;
597 violatedConstraintsWeightsBegin = violatedConstraintsWeights_.begin();
602 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
603 template <
class LABEL_ITERATOR>
605 double weight = -
static_cast<double>(maxNumUsedLabels_);
606 for(
LabelType i = 0; i < maxNumLabels_; ++i) {
607 weight += labelingBegin[i];
612 if(weight > tolerance) {
613 violatedConstraintsIds_[0] = 0;
614 violatedConstraintsWeights_[0] = weight;
616 violatedConstraintsWeightsBegin = violatedConstraintsWeights_.begin();
622 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
624 for(
LabelType i = 0; i < maxNumLabels_; ++i) {
625 typename LinearConstraintType::IndicatorVariableType indicatorVariable;
626 indicatorVariable.setLogicalOperatorType(LinearConstraintType::IndicatorVariableType::Or);
627 for(
IndexType j = 0; j < numVariables_; ++j) {
628 if(useSameNumLabels_) {
629 indicatorVariable.add(j, i);
630 }
else if(shape_[j] > i) {
631 indicatorVariable.add(j, i);
634 indicatorVariableList_[i] = indicatorVariable;
638 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
640 constraints_[0].setBound(maxNumUsedLabels_);
641 constraints_[0].setConstraintOperator(LinearConstraintType::LinearConstraintOperatorType::LessEqual);
642 for(
LabelType i = 0; i < maxNumLabels_; ++i) {
643 constraints_[0].add(indicatorVariableList_[i], 1.0);
648 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
650 const size_t sameNumLabelsSize = 1;
651 const size_t numVariablesSize = 1;
653 const size_t maxNumUsedLabelsSize = 1;
655 const size_t totalIndexSize = sameNumLabelsSize + numVariablesSize + shapeSize + maxNumUsedLabelsSize;
656 return totalIndexSize;
659 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
660 inline size_t FunctionSerialization<NumLabelsLimitationFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE> >::valueSequenceSize(
const NumLabelsLimitationFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>& src) {
661 const size_t returnSize = 2;
663 const size_t totalValueSize = returnSize;
664 return totalValueSize;
667 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
668 template<
class INDEX_OUTPUT_ITERATOR,
class VALUE_OUTPUT_ITERATOR>
669 inline void FunctionSerialization<NumLabelsLimitationFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE> >::serialize(
const NumLabelsLimitationFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>& src, INDEX_OUTPUT_ITERATOR indexOutIterator, VALUE_OUTPUT_ITERATOR valueOutIterator) {
672 *indexOutIterator =
static_cast<typename INDEX_OUTPUT_ITERATOR::value_type
>(src.useSameNumLabels_);
674 *indexOutIterator = src.numVariables_;
676 if(src.useSameNumLabels_) {
677 *indexOutIterator = src.maxNumLabels_;
680 for(
size_t i = 0; i < src.shape_.size(); ++i) {
681 *indexOutIterator = src.shape_[i];
687 *indexOutIterator = src.maxNumUsedLabels_;
691 *valueOutIterator = src.returnValid_;
693 *valueOutIterator = src.returnInvalid_;
696 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
697 template<
class INDEX_INPUT_ITERATOR,
class VALUE_INPUT_ITERATOR>
698 inline void FunctionSerialization<NumLabelsLimitationFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE> >::deserialize(INDEX_INPUT_ITERATOR indexInIterator, VALUE_INPUT_ITERATOR valueInIterator, NumLabelsLimitationFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>& dst) {
699 typedef VALUE_TYPE ValueType;
700 typedef INDEX_TYPE IndexType;
705 const bool useSameNumLabels = *indexInIterator;
707 const IndexType numVariables = *indexInIterator;
710 std::vector<LabelType> shape(indexInIterator, indexInIterator + (useSameNumLabels ? 1 : numVariables));
711 indexInIterator += (useSameNumLabels ? 1 : numVariables);
714 const LabelType maxNumUsedLabels = *indexInIterator;
718 ValueType returnValid = *valueInIterator;
722 ValueType returnInvalid = *valueInIterator;
724 if(useSameNumLabels) {
725 dst = NumLabelsLimitationFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>(numVariables, shape[0], maxNumUsedLabels, returnValid, returnInvalid);
727 dst = NumLabelsLimitationFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>(shape.begin(), shape.end(), maxNumUsedLabels, returnValid, returnInvalid);
LinearConstraintFunctionTraits< LinearConstraintFunctionType > LinearConstraintFunctionTraitsType
Typedef of the LinearConstraintFunctionTraits class with appropriate template parameter.
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 function.
LinearConstraintType::VariableLabelPairsIteratorType VariableLabelPairsIteratorType
size_t shape(const size_t i) const
Number of labels of the indicated input variable.
NumLabelsLimitationFunction< VALUE_TYPE, INDEX_TYPE, LABEL_TYPE > LinearConstraintFunctionType
Typedef of the NumLabelsLimitationFunction class with appropriate template parameter.
IndicatorVariablesContainerType::const_iterator IndicatorVariablesIteratorType
Defines the const iterator type to iterate over the set of indicator variables.
Provides implementation for the power function of unsigned integer values.
Provides interface for liner constraint functions.
IndicatorVariableType::IteratorType VariableLabelPairsIteratorType
Defines the const iterator type to iterate over the variable label pairs of an indicator variable...
LinearConstraintFunctionTraitsType::IndicatorVariablesIteratorType IndicatorVariablesIteratorType
Defines the indicator variables container iterator type which is used to iterate over the indicator v...
void fillIndicatorVariableList()
Helper function to fill NumLabelsLimitationFunction::indicatorVariableList_ with all indicator variab...
std::vector< IndicatorVariableType > IndicatorVariablesContainerType
Defines the storage type for the set of indicator variables.
LinearConstraint< ValueType, IndexType, LabelType > LinearConstraintType
SubsequenceIterator< LinearConstraintsIteratorType, typename std::vector< size_t >::const_iterator > ViolatedLinearConstraintsIteratorType
size_t size() const
Number of parameters.
LinearConstraintType::IndicatorVariablesContainerType IndicatorVariablesContainerType
LinearConstraintFunctionTraitsType::ValueType ValueType
Typedef of the VALUE_TYPE template parameter type from the class LabelOrderFunction.
NumLabelsLimitationFunction()
NumLabelsLimitationFunction constructor.
size_t unsignedIntegerPow(size_t base, size_t exponent)
Unsigned integer power function.
Defines the const iterator type to iterate over the subset of a sequence.
#define OPENGM_ASSERT(expression)
void createConstraints()
Helper function to create all linear constraints which are implied by the function.
LabelType maxNumLabels_
The maximum number of labels of the variables.
LinearConstraintsContainerType::const_iterator LinearConstraintsIteratorType
LinearConstraintsIteratorType linearConstraintsEnd_impl() const
Implementation of LinearConstraintFunctionBase::linearConstraintsEnd.
LinearConstraintFunctionTraitsType::LinearConstraintType LinearConstraintType
Typedef of the LinearConstraint class which is used to represent linear constraints.
LinearConstraintFunctionTraitsType::ViolatedLinearConstraintsIteratorType ViolatedLinearConstraintsIteratorType
Defines the violated linear constraints iterator type which is used to iterate over the set of violat...
~NumLabelsLimitationFunction()
NumLabelsLimitationFunction destructor.
A linear constraint function class for limiting the number of used labels.
IndicatorVariablesContainerType indicatorVariableList_
A list of all indicator variables present in the function.
Base class for linear constraint functions.
LinearConstraintFunctionTraitsType::LabelType LabelType
Typedef of the LABEL_TYPE template parameter type from the class LabelOrderFunction.
LinearConstraintFunctionTraitsType::LinearConstraintsIteratorType LinearConstraintsIteratorType
Defines the linear constraints container iterator type which is used to iterate over the set of linea...
LabelType maxNumUsedLabels_
The maximum number of different labels which are allowed in the assignment of the function...
IndicatorVariablesIteratorType indicatorVariablesOrderBegin_impl() const
Implementation of LinearConstraintFunctionBase::indicatorVariablesOrderBegin.
LinearConstraintFunctionTraitsType::VariableLabelPairsIteratorType VariableLabelPairsIteratorType
Defines the variable label pairs iterator type which is used to iterate over the variable label pairs...
ViolatedLinearConstraintsWeightsContainerType::const_iterator ViolatedLinearConstraintsWeightsIteratorType
size_t dimension() const
Number of input variables.
LinearConstraintsContainerType constraints_
Stores the linear constraints of the function.
LinearConstraintFunctionTraitsType::IndicatorVariablesContainerType IndicatorVariablesContainerType
Defines the indicator variables container type which is used to store the indicator variables used by...
std::vector< LinearConstraintType > LinearConstraintsContainerType
Traits class for linear constraint functions.
const size_t FUNCTION_TYPE_ID_OFFSET
User-defined function have ids smaller than FUNCTION_TYPE_ID_OFFSET.
MinMaxFunctor< ValueType > minMax() const
Get minimum and maximum at the same time.
LinearConstraintFunctionTraitsType::LinearConstraintsContainerType LinearConstraintsContainerType
Defines the linear constraints container type which is used to store multiple linear constraints...
std::vector< double > ViolatedLinearConstraintsWeightsContainerType
size_t numVariables_
The number of variables of the function.
Provides implementation for class LinearConstraint.
ViolatedLinearConstraintsWeightsContainerType violatedConstraintsWeights_
Stores the weights of the violated constraints which are detected by NumLabelsLimitationFunction::cha...
LinearConstraintFunctionTraitsType::IndexType IndexType
Typedef of the INDEX_TYPE template parameter type from the class LabelOrderFunction.
ValueType max() const
Maximum value of the function.
void challenge_impl(ViolatedLinearConstraintsIteratorType &violatedConstraintsBegin, ViolatedLinearConstraintsIteratorType &violatedConstraintsEnd, ViolatedLinearConstraintsWeightsIteratorType &violatedConstraintsWeightsBegin, LABEL_ITERATOR labelingBegin, const ValueType tolerance=0.0) const
Implementation of LinearConstraintFunctionBase::challenge.
LinearConstraintFunctionTraitsType::ViolatedLinearConstraintsWeightsIteratorType ViolatedLinearConstraintsWeightsIteratorType
Defines the violated linear constraints weights iterator type which is used to iterate over the weigh...
ValueType operator()(Iterator statesBegin) const
Function evaluation.
IndicatorVariablesIteratorType indicatorVariablesOrderEnd_impl() const
Implementation of LinearConstraintFunctionBase::indicatorVariablesOrderEnd.
Provides implementation for class SubsequenceIterator.
bool useSameNumLabels_
Indicator to tell that all variables have the same number of variables.
LinearConstraintType::IndicatorVariablesIteratorType IndicatorVariablesIteratorType
LinearConstraintFunctionBase< LinearConstraintFunctionType > LinearConstraintFunctionBaseType
Typedef of the LinearConstraintFunctionBase class with appropriate template parameter.
Define a linear constraint for a set of indicatorVariables.
std::vector< size_t > violatedConstraintsIds_
Stores the indices of the violated constraints which are detected by NumLabelsLimitationFunction::cha...
ValueType min() const
Minimum value of the function.
ValueType returnInvalid_
Stores the return value of NumLabelsLimitationFunction::operator() if the number of different labels ...
LinearConstraintFunctionTraitsType::ViolatedLinearConstraintsWeightsContainerType ViolatedLinearConstraintsWeightsContainerType
Defines the violated linear constraints weights container type which is used to store the weights of ...
std::vector< LabelType > shape_
The shape of the function (only used if variables have different number of labels).
ValueType returnValid_
Stores the return value of NumLabelsLimitationFunction::operator() if the number of different labels ...
LinearConstraintsIteratorType linearConstraintsBegin_impl() const
Implementation of LinearConstraintFunctionBase::linearConstraintsBegin.