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>
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>
125 template<
class INDEX_OUTPUT_ITERATOR,
class VALUE_OUTPUT_ITERATOR>
127 template<
class INDEX_INPUT_ITERATOR,
class VALUE_INPUT_ITERATOR>
443 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
452 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
453 template <
class SHAPE_ITERATOR_TYPE>
459 size_(
std::
accumulate(shapeBegin, shapeEnd, 1,
std::multiplies<typename
std::iterator_traits<SHAPE_ITERATOR_TYPE>::value_type>())),
470 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
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>
496 while(statesBegin != statesEnd) {
497 const LabelType currentLabel = *statesBegin;
499 if(!labelIsUsed[currentLabel]) {
500 labelIsUsed[currentLabel] =
true;
511 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
517 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
522 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
527 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
532 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
537 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
547 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
552 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
557 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
562 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
567 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
568 template <
class LABEL_ITERATOR>
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_)) {
588 if(weight <= tolerance) {
602 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
603 template <
class LABEL_ITERATOR>
607 weight += labelingBegin[i];
612 if(weight > tolerance) {
622 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
625 typename LinearConstraintType::IndicatorVariableType indicatorVariable;
626 indicatorVariable.setLogicalOperatorType(LinearConstraintType::IndicatorVariableType::Or);
629 indicatorVariable.add(j, i);
630 }
else if(
shape_[j] > i) {
631 indicatorVariable.add(j, i);
638 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
641 constraints_[0].setConstraintOperator(LinearConstraintType::LinearConstraintOperatorType::LessEqual);
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>
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>
672 *indexOutIterator =
static_cast<typename INDEX_OUTPUT_ITERATOR::value_type
>(src.
useSameNumLabels_);
680 for(
size_t i = 0; i < src.
shape_.size(); ++i) {
681 *indexOutIterator = src.
shape_[i];
696 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
697 template<
class INDEX_INPUT_ITERATOR,
class VALUE_INPUT_ITERATOR>
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) {
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
opengm::FunctionBase< NumLabelsLimitationFunction< VALUE_TYPE, INDEX_TYPE, LABEL_TYPE >, LinearConstraintFunctionTraits< NumLabelsLimitationFunction< VALUE_TYPE, INDEX_TYPE, LABEL_TYPE > >::ValueType, LinearConstraintFunctionTraits< NumLabelsLimitationFunction< VALUE_TYPE, INDEX_TYPE, LABEL_TYPE > >::IndexType, LinearConstraintFunctionTraits< NumLabelsLimitationFunction< VALUE_TYPE, INDEX_TYPE, LABEL_TYPE > >::LabelType >::accumulate ReturnType accumulate() const
accumulate all values of the function
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.