1 #ifndef OPENGM_SUM_CONSTRAINT_FUNCTION_HXX_ 2 #define OPENGM_SUM_CONSTRAINT_FUNCTION_HXX_ 17 template<
class FUNCTION_TYPE,
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
18 class LPFunctionTransfer_impl;
20 template<
class VALUE_TYPE,
class INDEX_TYPE =
size_t,
class LABEL_TYPE =
size_t>
30 template <
class SHAPE_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
31 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);
32 template <
class COEFFICIENTS_ITERATOR_TYPE>
33 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);
37 template<
class Iterator>
38 ValueType
operator()(Iterator statesBegin)
const;
39 size_t shape(
const size_t i)
const;
63 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
72 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
79 template<
class INDEX_OUTPUT_ITERATOR,
class VALUE_OUTPUT_ITERATOR>
81 template<
class INDEX_INPUT_ITERATOR,
class VALUE_INPUT_ITERATOR>
301 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
309 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
310 template <
class SHAPE_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
315 size_(
std::
accumulate(shapeBegin, shapeEnd, 1,
std::multiplies<typename
std::iterator_traits<SHAPE_ITERATOR_TYPE>::value_type>())),
325 size_t currentOffset = 0;
328 currentOffset +=
shape_[i];
333 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
334 template <
class COEFFICIENTS_ITERATOR_TYPE>
348 size_t currentOffset = 0;
356 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
361 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
362 template<
class Iterator>
377 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
387 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
392 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
398 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
400 const size_t sameNumLabelsSize = 1;
401 const size_t numVariablesSize = 1;
403 const size_t shareCoefficientsSize = 1;
404 const size_t coefficientsSize = 1;
406 const size_t totalIndexSize = sameNumLabelsSize + numVariablesSize
407 + shapeSize + shareCoefficientsSize + coefficientsSize;
408 return totalIndexSize;
411 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
414 const size_t lambdaSize = 1;
415 const size_t boundSize = 1;
417 const size_t totalValueSize = coefficientsSize + lambdaSize + boundSize;
418 return totalValueSize;
421 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
422 template<
class INDEX_OUTPUT_ITERATOR,
class VALUE_OUTPUT_ITERATOR>
426 *indexOutIterator =
static_cast<typename INDEX_OUTPUT_ITERATOR::value_type
>(src.
useSameNumLabels_);
434 for(
size_t i = 0; i < src.
shape_.size(); ++i) {
435 *indexOutIterator = src.
shape_[i];
455 *valueOutIterator = src.
lambda_;
459 *valueOutIterator = src.
bound_;
462 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
463 template<
class INDEX_INPUT_ITERATOR,
class VALUE_INPUT_ITERATOR>
470 const bool useSameNumLabels = *indexInIterator;
472 const IndexType numVariables = *indexInIterator;
474 INDEX_INPUT_ITERATOR shapeBegin = indexInIterator;
475 indexInIterator += (useSameNumLabels ? 1 : numVariables);
476 INDEX_INPUT_ITERATOR shapeEnd = indexInIterator;
478 const bool shareCoefficients = *indexInIterator;
481 const size_t numCoeffiecients = *indexInIterator;
485 VALUE_INPUT_ITERATOR coefficientsBegin = valueInIterator;
486 VALUE_INPUT_ITERATOR coefficientsEnd = valueInIterator + numCoeffiecients;
487 valueInIterator += numCoeffiecients;
490 const ValueType lambda = *valueInIterator;
494 const ValueType bound = *valueInIterator;
496 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...