1 #ifndef OPENGM_SUM_CONSTRAINT_FUNCTION_HXX_ 2 #define OPENGM_SUM_CONSTRAINT_FUNCTION_HXX_ 16 template<
class FUNCTION_TYPE,
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
17 class LPFunctionTransfer_impl;
19 template<
class VALUE_TYPE,
class INDEX_TYPE =
size_t,
class LABEL_TYPE =
size_t>
29 template <
class SHAPE_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
30 SumConstraintFunction(SHAPE_ITERATOR_TYPE shapeBegin, SHAPE_ITERATOR_TYPE shapeEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, COEFFICIENTS_ITERATOR_TYPE coefficientsEnd,
const bool shareCoefficients,
const ValueType lambda = 1.0,
const ValueType bound = 0.0);
31 template <
class COEFFICIENTS_ITERATOR_TYPE>
32 SumConstraintFunction(
const IndexType numVariables,
const LabelType numLabels, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, COEFFICIENTS_ITERATOR_TYPE coefficientsEnd,
const bool shareCoefficients,
const ValueType lambda = 1.0,
const ValueType bound = 0.0);
36 template<
class Iterator>
37 ValueType
operator()(Iterator statesBegin)
const;
38 size_t shape(
const size_t i)
const;
62 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
71 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
78 template<
class INDEX_OUTPUT_ITERATOR,
class VALUE_OUTPUT_ITERATOR>
80 template<
class INDEX_INPUT_ITERATOR,
class VALUE_INPUT_ITERATOR>
300 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
308 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
309 template <
class SHAPE_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
314 size_(
std::
accumulate(shapeBegin, shapeEnd, 1,
std::multiplies<typename
std::iterator_traits<SHAPE_ITERATOR_TYPE>::value_type>())),
324 size_t currentOffset = 0;
327 currentOffset +=
shape_[i];
332 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
333 template <
class COEFFICIENTS_ITERATOR_TYPE>
347 size_t currentOffset = 0;
355 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
360 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
361 template<
class Iterator>
376 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
386 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
391 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
397 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
399 const size_t sameNumLabelsSize = 1;
400 const size_t numVariablesSize = 1;
402 const size_t shareCoefficientsSize = 1;
403 const size_t coefficientsSize = 1;
405 const size_t totalIndexSize = sameNumLabelsSize + numVariablesSize
406 + shapeSize + shareCoefficientsSize + coefficientsSize;
407 return totalIndexSize;
410 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
413 const size_t lambdaSize = 1;
414 const size_t boundSize = 1;
416 const size_t totalValueSize = coefficientsSize + lambdaSize + boundSize;
417 return totalValueSize;
420 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
421 template<
class INDEX_OUTPUT_ITERATOR,
class VALUE_OUTPUT_ITERATOR>
425 *indexOutIterator =
static_cast<typename INDEX_OUTPUT_ITERATOR::value_type
>(src.
useSameNumLabels_);
433 for(
size_t i = 0; i < src.
shape_.size(); ++i) {
434 *indexOutIterator = src.
shape_[i];
454 *valueOutIterator = src.
lambda_;
458 *valueOutIterator = src.
bound_;
461 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
462 template<
class INDEX_INPUT_ITERATOR,
class VALUE_INPUT_ITERATOR>
469 const bool useSameNumLabels = *indexInIterator;
471 const IndexType numVariables = *indexInIterator;
473 INDEX_INPUT_ITERATOR shapeBegin = indexInIterator;
474 indexInIterator += (useSameNumLabels ? 1 : numVariables);
475 INDEX_INPUT_ITERATOR shapeEnd = indexInIterator;
477 const bool shareCoefficients = *indexInIterator;
480 const size_t numCoeffiecients = *indexInIterator;
484 VALUE_INPUT_ITERATOR coefficientsBegin = valueInIterator;
485 VALUE_INPUT_ITERATOR coefficientsEnd = valueInIterator + numCoeffiecients;
486 valueInIterator += numCoeffiecients;
489 const ValueType lambda = *valueInIterator;
493 const ValueType bound = *valueInIterator;
495 if(useSameNumLabels) {
Fallback implementation of member functions of OpenGM functions.
Provides implementation for the power function of unsigned integer values.
bool useSameNumLabels_
Tell if each variable of the function has the same number of labels.
size_t unsignedIntegerPow(size_t base, size_t exponent)
Unsigned integer power function.
INDEX_TYPE IndexType
Typedef of the INDEX_TYPE template parameter type from the class SumConstraintFunction.
#define OPENGM_ASSERT(expression)
LABEL_TYPE LabelType
Typedef of the LABEL_TYPE template parameter type from the class SumConstraintFunction.
~SumConstraintFunction()
SumConstraintFunction destructor.
size_t size() const
Number of parameters.
size_t size_
Stores the size of the function.
std::vector< ValueType > coefficients_
The coefficients of the equality constraint.
size_t dimension() const
Number of input variables.
Default implementation for class opengm::LPFunctionTransfer.
ReturnType accumulate() const
accumulate all values of the function
ValueType bound_
The bound for the equality constraint.
ValueType operator()(Iterator statesBegin) const
Function evaluation.
const size_t FUNCTION_TYPE_ID_OFFSET
User-defined function have ids smaller than FUNCTION_TYPE_ID_OFFSET.
VALUE_TYPE ValueType
Typedef of the VALUE_TYPE template parameter type from the class SumConstraintFunction.
SumConstraintFunction()
SumConstraintFunction constructor.
size_t numVariables_
The number of variables of the function.
bool shareCoefficients_
Tell if the labels of the variables share the same coefficients.
A sum constraint function class penalizing the violation of a given linear equality constraint...
std::vector< size_t > coefficientsOffsets_
The Offsets of the SumConstraintFunction::coefficients_ indices for each variable. Only valid if SumConstraintFunction::shareCoefficients_ is set to false.
LabelType maxNumLabels_
The maximum number of labels of the variables.
size_t shape(const size_t i) const
Number of labels of the indicated input variable.
ValueType lambda_
The scaling factor for the violation of the equality constraint.
std::vector< LabelType > shape_
The shape of the function. Only valid if SumConstraintFunction::useSameNumLabels_ is set to false...