1 #ifndef OPENGM_LP_FUNCTIONTRANSFER_HXX_
2 #define OPENGM_LP_FUNCTIONTRANSFER_HXX_
14 template<
class VALUE_TYPE,
class INDEX_TYPE =
size_t,
class LABEL_TYPE =
size_t>
28 template<
class FUNCTION_TYPE>
30 template<
class FUNCTION_TYPE>
32 template<
class FUNCTION_TYPE>
34 template<
class FUNCTION_TYPE>
36 template<
class FUNCTION_TYPE>
37 static void getIndicatorVariables(
const FUNCTION_TYPE&
function, IndicatorVariablesContainerType& variables);
38 template<
class FUNCTION_TYPE>
39 static void getLinearConstraints(
const FUNCTION_TYPE&
function, LinearConstraintsContainerType& constraints);
44 template<
class FUNCTION_TYPE>
45 void operator()(
const FUNCTION_TYPE&
function);
49 template<
class FUNCTION_TYPE>
50 void operator()(
const FUNCTION_TYPE&
function);
53 IndicatorVariablesContainerType*
order_;
54 template<
class FUNCTION_TYPE>
55 void operator()(
const FUNCTION_TYPE&
function);
59 template<
class FUNCTION_TYPE>
60 void operator()(
const FUNCTION_TYPE&
function);
64 template<
class FUNCTION_TYPE>
65 void operator()(
const FUNCTION_TYPE&
function);
69 template<
class FUNCTION_TYPE>
70 void operator()(
const FUNCTION_TYPE&
function);
74 template<
class FUNCTION_TYPE,
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
94 static void getIndicatorVariables(
const FunctionType&
function, IndicatorVariablesContainerType& variables);
95 static void getLinearConstraints(
const FunctionType&
function, LinearConstraintsContainerType& constraints);
98 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
118 static void getIndicatorVariables(
const FunctionType&
function, IndicatorVariablesContainerType& variables);
119 static void getLinearConstraints(
const FunctionType&
function, LinearConstraintsContainerType& constraints);
122 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
142 static void getIndicatorVariables(
const FunctionType&
function, IndicatorVariablesContainerType& variables);
143 static void getLinearConstraints(
const FunctionType&
function, LinearConstraintsContainerType& constraints);
588 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
589 template<
class FUNCTION_TYPE>
594 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
595 template<
class FUNCTION_TYPE>
600 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
601 template<
class FUNCTION_TYPE>
606 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
607 template<
class FUNCTION_TYPE>
612 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
613 template<
class FUNCTION_TYPE>
618 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
619 template<
class FUNCTION_TYPE>
624 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
625 template<
class FUNCTION_TYPE>
630 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
631 template<
class FUNCTION_TYPE>
636 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
637 template<
class FUNCTION_TYPE>
642 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
643 template<
class FUNCTION_TYPE>
648 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
649 template<
class FUNCTION_TYPE>
654 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
655 template<
class FUNCTION_TYPE>
660 template<
class FUNCTION_TYPE,
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
666 template<
class FUNCTION_TYPE,
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
673 template<
class FUNCTION_TYPE,
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
679 template<
class FUNCTION_TYPE,
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
685 template<
class FUNCTION_TYPE,
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
691 template<
class FUNCTION_TYPE,
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
698 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
704 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
705 inline typename LPFunctionTransfer_impl<SumConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>
::IndexType LPFunctionTransfer_impl<SumConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>
::numSlackVariables(
const FunctionType&
function) {
710 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
717 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
720 coefficients.resize(1);
721 coefficients[0] =
function.lambda_;
724 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
728 for(
IndexType i = 0; i <
function.numVariables_; ++i) {
729 for(
LabelType j = 0; j < (
function.useSameNumLabels_ ?
function.maxNumLabels_ :
function.shape_[i]); ++j) {
731 variables.push_back(indicatorVariable);
735 variables.push_back(slackVariable);
738 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
741 constraints.resize(2);
744 constraint.
setBound(
function.bound_);
745 for(
IndexType i = 0; i <
function.numVariables_; ++i) {
746 for(
LabelType j = 0; j < (
function.useSameNumLabels_ ?
function.maxNumLabels_ :
function.shape_[i]); ++j) {
748 constraint.
add(indicatorVariable,
function.coefficients_[(
function.shareCoefficients_ ? j :
function.coefficientsOffsets_[i] + j)]);
752 constraint.
add(slackVariable, -1.0);
753 constraints[0] = constraint;
757 constraint2.
setBound(-
function.bound_);
758 for(
IndexType i = 0; i <
function.numVariables_; ++i) {
759 for(
LabelType j = 0; j < (
function.useSameNumLabels_ ?
function.maxNumLabels_ :
function.shape_[i]); ++j) {
761 constraint2.
add(indicatorVariable, -
function.coefficients_[(
function.shareCoefficients_ ? j :
function.coefficientsOffsets_[i] + j)]);
764 constraint2.
add(slackVariable, -1.0);
765 constraints[1] = constraint2;
768 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
774 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
775 inline typename LPFunctionTransfer_impl<LabelCostFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>
::IndexType LPFunctionTransfer_impl<LabelCostFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>
::numSlackVariables(
const FunctionType&
function) {
778 if(
function.useSingleCost_) {
782 for(
LabelType i = 0; i <
function.maxNumLabels_; ++i) {
784 if(
function.costs_[i] != 0.0) {
788 return numLabelCosts;
792 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
796 order.resize(numLabelCosts);
797 for(
IndexType i = 0; i < numLabelCosts; ++i) {
802 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
806 coefficients.resize(numLabelCosts);
807 if(
function.useSingleCost_) {
809 coefficients[0] =
function.singleCost_;
812 for(
LabelType i = 0; i <
function.maxNumLabels_; ++i) {
814 if(
function.costs_[i] != 0.0) {
815 coefficients[currentCostIndex] =
function.costs_[i];
822 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
826 variables.resize(numLabelCosts * 2);
827 if(
function.useSingleCost_) {
831 if(
function.useSameNumLabels_) {
832 for(
IndexType i = 0; i <
function.numVariables_; ++i) {
833 indicatorVar.add(i,
function.singleLabel_);
836 for(
IndexType i = 0; i <
function.numVariables_; ++i) {
837 if(
function.shape_[i] >
function.singleLabel_) {
838 indicatorVar.add(i,
function.singleLabel_);
842 variables[0] = indicatorVar;
847 for(
LabelType currentLabel = 0; currentLabel <
function.maxNumLabels_; ++currentLabel) {
848 if(
function.costs_[currentLabel] != 0) {
851 if(
function.useSameNumLabels_) {
852 for(
IndexType i = 0; i <
function.numVariables_; ++i) {
853 indicatorVar.
add(i, currentLabel);
856 for(
IndexType i = 0; i <
function.numVariables_; ++i) {
857 if(
function.shape_[i] > currentLabel) {
858 indicatorVar.
add(i, currentLabel);
862 variables[currentNonZeroLabel * 2] = indicatorVar;
864 variables[(currentNonZeroLabel * 2) + 1] =
IndicatorVariableType(
function.numVariables_ + currentNonZeroLabel, 0);
865 ++currentNonZeroLabel;
871 template<
class VALUE_TYPE,
class INDEX_TYPE,
class LABEL_TYPE>
878 constraints.resize(variables.size() / 2);
879 for(
size_t i = 0; i < constraints.size(); ++i) {
883 constraint.
add(variables[i * 2], 1.0);
884 constraint.
add(variables[(i * 2) + 1], -1.0);
885 constraints[i] = constraint;
void operator()(const FUNCTION_TYPE &function)
The operator which implements the access to the function of a graphical model factor.
Functor to call LPFunctionTransfer::getLinearConstraints() for a factor of the graphical model...
LABEL_TYPE LabelType
Typedef of the LABEL_TYPE template parameter type from the class LPFunctionTransfer_impl.
static void getSlackVariablesObjectiveCoefficients(const FunctionType &function, SlackVariablesObjectiveCoefficientsContainerType &coefficients)
This function will tell the coefficients of the slack variables for the objective function of the lin...
LABEL_TYPE LabelType
Typedef of the LABEL_TYPE template parameter type from the class LPFunctionTransfer.
LPFunctionTransferType::SlackVariablesObjectiveCoefficientsContainerType SlackVariablesObjectiveCoefficientsContainerType
IndicatorVariablesContainerType * order_
Pointer to the storage for the return value of the LPFunctionTransfer::getSlackVariablesOrder() metho...
static IndexType numSlackVariables(const FUNCTION_TYPE &function)
This function will tell the number of required slack variables for the function transfer.
static void getLinearConstraints(const FunctionType &function, LinearConstraintsContainerType &constraints)
This function will create the necessary linear constraints to add the function to the linear program...
Provides implementation of a label cost function.
static void getSlackVariablesOrder(const FunctionType &function, IndicatorVariablesContainerType &order)
This function will tell the order of the slack variables for the LP function transfer.
static void getSlackVariablesOrder(const FUNCTION_TYPE &function, IndicatorVariablesContainerType &order)
This function will tell the order of the slack variables which are required for the function transfer...
LPFunctionTransferType::LinearConstraintsContainerType LinearConstraintsContainerType
static void getSlackVariablesObjectiveCoefficients(const FUNCTION_TYPE &function, SlackVariablesObjectiveCoefficientsContainerType &coefficients)
This function will tell the coefficients of the slack variables for the objective function of the lin...
Defines the linear constraint operator type to be . Hence the left hand side of the constraint will b...
LPFunctionTransferType::IndicatorVariableType IndicatorVariableType
LPFunctionTransferType::IndicatorVariablesContainerType IndicatorVariablesContainerType
Defines the indicator variables container type which is used to store multiple indicator variables...
static void getIndicatorVariables(const FUNCTION_TYPE &function, IndicatorVariablesContainerType &variables)
This function will tell all the indicator variables which are used in the linear constraints which ar...
LPFunctionTransferType::LinearConstraintType LinearConstraintType
Typedef of the LinearConstraint class with appropriate template parameter.
LPFunctionTransfer< ValueType, IndexType, LabelType > LPFunctionTransferType
Typedef of the LPFunctionTransfer class with appropriate template parameter.
void operator()(const FUNCTION_TYPE &function)
The operator which implements the access to the function of a graphical model factor.
LinearConstraintType::IndicatorVariableType IndicatorVariableType
Defines the indicator variable type which is used within linear constraints.
static void getLinearConstraints(const FUNCTION_TYPE &function, LinearConstraintsContainerType &constraints)
This function will create the necessary linear constraints which are used to represent the function w...
Provides implementation of a sum constraint function.
LPFunctionTransferType::IndicatorVariableType IndicatorVariableType
Defines the indicator variable type which is used within linear constraints.
Functor to call LPFunctionTransfer::numSlackVariables() for a graphical model factor.
#define OPENGM_ASSERT(expression)
LPFunctionTransferType::LinearConstraintType LinearConstraintType
A label cost function class penalizing the usage of labels.
std::vector< ValueType > SlackVariablesObjectiveCoefficientsContainerType
Defines the container type which is used to store the coefficients of the slack variables for the obj...
LPFunctionTransferType::IndicatorVariableType IndicatorVariableType
void operator()(const FUNCTION_TYPE &function)
The operator which implements the access to the function of a graphical model factor.
LinearConstraint< ValueType, IndexType, LabelType > LinearConstraintType
Typedef of the LinearConstraint class with appropriate template parameter.
LinearConstraintsContainerType * constraints_
Pointer to the storage for the return value of the LPFunctionTransfer::getLinearConstraints() method...
IndexType numSlackVariables_
Storage for the return value of the LPFunctionTransfer::numSlackVariables() method.
LPFunctionTransferType::LinearConstraintsContainerType LinearConstraintsContainerType
FUNCTION_TYPE FunctionType
Typedef of the FUNCTION_TYPE template parameter type from the class LPFunctionTransfer_impl.
LPFunctionTransferType::IndicatorVariablesContainerType IndicatorVariablesContainerType
void setLogicalOperatorType(const LogicalOperatorType logicalOperatorType)
Set the logical operator type of the indicator variable.
LPFunctionTransferType::IndicatorVariablesContainerType IndicatorVariablesContainerType
SumConstraintFunction< VALUE_TYPE, INDEX_TYPE, LABEL_TYPE > FunctionType
LPFunctionTransfer< ValueType, IndexType, LabelType > LPFunctionTransferType
Default implementation for class opengm::LPFunctionTransfer.
SlackVariablesObjectiveCoefficientsContainerType * coefficients_
Pointer to the storage for the return value of the LPFunctionTransfer::getSlackVariablesObjectiveCoef...
Provides transformations for some function types when they are used in inference algorithms which use...
static IndexType numSlackVariables(const FunctionType &function)
This function will tell the number of required slack variables for the LP function transfer...
Functor to call LPFunctionTransfer::getSlackVariablesOrder() for a factor of the graphical model...
INDEX_TYPE IndexType
Typedef of the INDEX_TYPE template parameter type from the class LPFunctionTransfer.
void add(const IndicatorVariableType &indicatorVariable, const ValueType coefficient)
Add a single indicator variable and the corresponding coefficient to the linear constraint.
std::vector< IndicatorVariableType > IndicatorVariablesContainerType
Defines the indicator variables container type which is used to store multiple indicator variables...
IndicatorVariablesContainerType * variables_
Pointer to the storage for the return value of the LPFunctionTransfer::getIndicatorVariables() method...
VALUE_TYPE ValueType
Typedef of the VALUE_TYPE template parameter type from the class LPFunctionTransfer.
void operator()(const FUNCTION_TYPE &function)
The operator which implements the access to the function of a graphical model factor.
LPFunctionTransferType::LinearConstraintsContainerType LinearConstraintsContainerType
Defines the linear constraints container type which is used to store multiple linear constraints...
void add(const IndexType variable, const LabelType label)
Add a variable label pair to the indicator variable.
void operator()(const FUNCTION_TYPE &function)
The operator which implements the access to the function of a factor of the graphical model...
Combine a group of variables to a new variable.
LPFunctionTransferType::SlackVariablesObjectiveCoefficientsContainerType SlackVariablesObjectiveCoefficientsContainerType
Provides implementation for class LinearConstraint.
bool isTransferable_
Storage for the return value of the LPFunctionTransfer::isTransferable() method.
Functor to call LPFunctionTransfer::getSlackVariablesObjectiveCoefficients() for a factor of the grap...
Functor to call LPFunctionTransfer::isTransferable() for a factor of the graphical model...
std::vector< LinearConstraintType > LinearConstraintsContainerType
Defines the linear constraints container type which is used to store multiple linear constraints...
The indicator variable will be interpreted as 1 if at least one of the variables associated by the in...
A sum constraint function class penalizing the violation of a given linear equality constraint...
VALUE_TYPE ValueType
Typedef of the VALUE_TYPE template parameter type from the class LPFunctionTransfer_impl.
LabelCostFunction< VALUE_TYPE, INDEX_TYPE, LABEL_TYPE > FunctionType
INDEX_TYPE IndexType
Typedef of the INDEX_TYPE template parameter type from the class LPFunctionTransfer_impl.
Functor to call LPFunctionTransfer::getIndicatorVariables() for a factor of the graphical model...
LPFunctionTransferType::SlackVariablesObjectiveCoefficientsContainerType SlackVariablesObjectiveCoefficientsContainerType
Defines the container type which is used to store the coefficients of the slack variables for the obj...
void setConstraintOperator(const LinearConstraintOperatorValueType constraintOperator)
Set the constraint operator for the linear constraint.
Defines the linear constraint operator type to be . Hence the left hand side of the constraint will b...
Define a linear constraint for a set of indicatorVariables.
LPFunctionTransferType::LinearConstraintType LinearConstraintType
static bool isTransferable()
This function will tell if the function type provided via the template parameter can be transfered in...
LPFunctionTransfer< ValueType, IndexType, LabelType > LPFunctionTransferType
void setBound(const BoundType bound)
Set the bound of the linear constraint.
void operator()(const FUNCTION_TYPE &function)
The operator which implements the access to the function of a graphical model factor.
static void getIndicatorVariables(const FunctionType &function, IndicatorVariablesContainerType &variables)
This function will tell the used indicator variables for the linear constraints.
static bool isTransferable()
This function will tell if the function type provided via the template parameter can be transfered in...