/usr/include/opengm/inference/auxiliary/lp_functiontransfer.hxx is in libopengm-dev 2.3.6-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 | #ifndef OPENGM_LP_FUNCTIONTRANSFER_HXX_
#define OPENGM_LP_FUNCTIONTRANSFER_HXX_
#include <opengm/opengm.hxx>
#include <opengm/datastructures/linear_constraint.hxx>
#include <opengm/functions/soft_constraint_functions/sum_constraint_function.hxx>
#include <opengm/functions/soft_constraint_functions/label_cost_function.hxx>
namespace opengm {
/*********************
* class definition *
*********************/
template<class VALUE_TYPE, class INDEX_TYPE = size_t, class LABEL_TYPE = size_t>
class LPFunctionTransfer {
public:
// typedefs
typedef VALUE_TYPE ValueType;
typedef INDEX_TYPE IndexType;
typedef LABEL_TYPE LabelType;
typedef LinearConstraint<ValueType, IndexType, LabelType> LinearConstraintType;
typedef std::vector<LinearConstraintType> LinearConstraintsContainerType;
typedef typename LinearConstraintType::IndicatorVariableType IndicatorVariableType;
typedef std::vector<IndicatorVariableType> IndicatorVariablesContainerType;
typedef std::vector<ValueType> SlackVariablesObjectiveCoefficientsContainerType;
// transfer interface
template<class FUNCTION_TYPE>
static bool isTransferable();
template<class FUNCTION_TYPE>
static IndexType numSlackVariables(const FUNCTION_TYPE& function);
template<class FUNCTION_TYPE>
static void getSlackVariablesOrder(const FUNCTION_TYPE& function, IndicatorVariablesContainerType& order);
template<class FUNCTION_TYPE>
static void getSlackVariablesObjectiveCoefficients(const FUNCTION_TYPE& function, SlackVariablesObjectiveCoefficientsContainerType& coefficients);
template<class FUNCTION_TYPE>
static void getIndicatorVariables(const FUNCTION_TYPE& function, IndicatorVariablesContainerType& variables);
template<class FUNCTION_TYPE>
static void getLinearConstraints(const FUNCTION_TYPE& function, LinearConstraintsContainerType& constraints);
// functors
struct IsTransferableFunctor {
bool isTransferable_;
template<class FUNCTION_TYPE>
void operator()(const FUNCTION_TYPE& function);
};
struct NumSlackVariablesFunctor {
IndexType numSlackVariables_;
template<class FUNCTION_TYPE>
void operator()(const FUNCTION_TYPE& function);
};
struct GetSlackVariablesOrderFunctor {
IndicatorVariablesContainerType* order_;
template<class FUNCTION_TYPE>
void operator()(const FUNCTION_TYPE& function);
};
struct GetSlackVariablesObjectiveCoefficientsFunctor {
SlackVariablesObjectiveCoefficientsContainerType* coefficients_;
template<class FUNCTION_TYPE>
void operator()(const FUNCTION_TYPE& function);
};
struct GetIndicatorVariablesFunctor {
IndicatorVariablesContainerType* variables_;
template<class FUNCTION_TYPE>
void operator()(const FUNCTION_TYPE& function);
};
struct GetLinearConstraintsFunctor {
LinearConstraintsContainerType* constraints_;
template<class FUNCTION_TYPE>
void operator()(const FUNCTION_TYPE& function);
};
};
template<class FUNCTION_TYPE, class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
class LPFunctionTransfer_impl {
public:
// typedefs
typedef VALUE_TYPE ValueType;
typedef INDEX_TYPE IndexType;
typedef LABEL_TYPE LabelType;
typedef FUNCTION_TYPE FunctionType;
typedef LPFunctionTransfer<ValueType, IndexType, LabelType> LPFunctionTransferType;
typedef typename LPFunctionTransferType::LinearConstraintType LinearConstraintType;
typedef typename LPFunctionTransferType::LinearConstraintsContainerType LinearConstraintsContainerType;
typedef typename LPFunctionTransferType::IndicatorVariableType IndicatorVariableType;
typedef typename LPFunctionTransferType::IndicatorVariablesContainerType IndicatorVariablesContainerType;
typedef typename LPFunctionTransferType:: SlackVariablesObjectiveCoefficientsContainerType SlackVariablesObjectiveCoefficientsContainerType;
// transfer interface
static bool isTransferable();
static IndexType numSlackVariables(const FunctionType& function);
static void getSlackVariablesOrder(const FunctionType& function, IndicatorVariablesContainerType& order);
static void getSlackVariablesObjectiveCoefficients(const FunctionType& function, SlackVariablesObjectiveCoefficientsContainerType& coefficients);
static void getIndicatorVariables(const FunctionType& function, IndicatorVariablesContainerType& variables);
static void getLinearConstraints(const FunctionType& function, LinearConstraintsContainerType& constraints);
};
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
class LPFunctionTransfer_impl<SumConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE> {
public:
// typedefs
typedef VALUE_TYPE ValueType;
typedef INDEX_TYPE IndexType;
typedef LABEL_TYPE LabelType;
typedef SumConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE> FunctionType;
typedef LPFunctionTransfer<ValueType, IndexType, LabelType> LPFunctionTransferType;
typedef typename LPFunctionTransferType::LinearConstraintType LinearConstraintType;
typedef typename LPFunctionTransferType::LinearConstraintsContainerType LinearConstraintsContainerType;
typedef typename LPFunctionTransferType::IndicatorVariableType IndicatorVariableType;
typedef typename LPFunctionTransferType::IndicatorVariablesContainerType IndicatorVariablesContainerType;
typedef typename LPFunctionTransferType:: SlackVariablesObjectiveCoefficientsContainerType SlackVariablesObjectiveCoefficientsContainerType;
// transfer interface
static bool isTransferable();
static IndexType numSlackVariables(const FunctionType& function);
static void getSlackVariablesOrder(const FunctionType& function, IndicatorVariablesContainerType& order);
static void getSlackVariablesObjectiveCoefficients(const FunctionType& function, SlackVariablesObjectiveCoefficientsContainerType& coefficients);
static void getIndicatorVariables(const FunctionType& function, IndicatorVariablesContainerType& variables);
static void getLinearConstraints(const FunctionType& function, LinearConstraintsContainerType& constraints);
};
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
class LPFunctionTransfer_impl<LabelCostFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE> {
public:
// typedefs
typedef VALUE_TYPE ValueType;
typedef INDEX_TYPE IndexType;
typedef LABEL_TYPE LabelType;
typedef LabelCostFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE> FunctionType;
typedef LPFunctionTransfer<ValueType, IndexType, LabelType> LPFunctionTransferType;
typedef typename LPFunctionTransferType::LinearConstraintType LinearConstraintType;
typedef typename LPFunctionTransferType::LinearConstraintsContainerType LinearConstraintsContainerType;
typedef typename LPFunctionTransferType::IndicatorVariableType IndicatorVariableType;
typedef typename LPFunctionTransferType::IndicatorVariablesContainerType IndicatorVariablesContainerType;
typedef typename LPFunctionTransferType:: SlackVariablesObjectiveCoefficientsContainerType SlackVariablesObjectiveCoefficientsContainerType;
// transfer interface
static bool isTransferable();
static IndexType numSlackVariables(const FunctionType& function);
static void getSlackVariablesOrder(const FunctionType& function, IndicatorVariablesContainerType& order);
static void getSlackVariablesObjectiveCoefficients(const FunctionType& function, SlackVariablesObjectiveCoefficientsContainerType& coefficients);
static void getIndicatorVariables(const FunctionType& function, IndicatorVariablesContainerType& variables);
static void getLinearConstraints(const FunctionType& function, LinearConstraintsContainerType& constraints);
};
/***********************
* class documentation *
***********************/
/*! \class LPFunctionTransfer
* \brief Provides transformations for some function types when they are used
* in inference algorithms which use linear programming.
*
* In inference algorithms like opengm::LPCplex which rely on opengm::LPBase
* the inference task is reformulated as a linear program. Therefore all
* factors which are not linear constraint factors are added the same way to
* the LP no matter what function type is used for these factors. However some
* function types like opengm::LabelCostFunction can be added in a more
* efficient formulation to the LP. This class provides an interface to
* transform the function behind a factor so opengm::LPBase can benefit from
* the more efficient representation of the factor.
*
* \tparam VALUE_TYPE Value type.
* \tparam INDEX_TYPE Index type.
* \tparam LABEL_TYPE Label type.
*/
/*! \typedef LPFunctionTransfer::ValueType
* \brief Typedef of the VALUE_TYPE template parameter type from the class
* LPFunctionTransfer.
*/
/*! \typedef LPFunctionTransfer::IndexType
* \brief Typedef of the INDEX_TYPE template parameter type from the class
* LPFunctionTransfer.
*/
/*! \typedef LPFunctionTransfer::LabelType
* \brief Typedef of the LABEL_TYPE template parameter type from the class
* LPFunctionTransfer.
*/
/*! \typedef LPFunctionTransfer::LinearConstraintType
* \brief Typedef of the LinearConstraint class with appropriate template
* parameter.
*/
/*! \typedef LPFunctionTransfer::LinearConstraintsContainerType
* \brief Defines the linear constraints container type which is used to store
* multiple linear constraints.
*/
/*! \typedef LPFunctionTransfer::IndicatorVariableType
* \brief Defines the indicator variable type which is used within linear
* constraints.
*/
/*! \typedef LPFunctionTransfer::IndicatorVariablesContainerType
* \brief Defines the indicator variables container type which is used to store
* multiple indicator variables.
*/
/*! \typedef LPFunctionTransfer::SlackVariablesObjectiveCoefficientsContainerType
* \brief Defines the container type which is used to store the coefficients of
* the slack variables for the objective function.
*/
/*! \fn bool LPFunctionTransfer::isTransferable()
* \brief This function will tell if the function type provided via the
* template parameter can be transfered into a more efficient LP
* formulation.
*
* \note This function will call LPFunctionTransfer_impl::isTransferable()
* which by default will return false unless a partial template
* specialization of the class opengm::LPFunctionTransfer_impl is
* provided for the FUNCTION_TYPE type.
*
* \tparam FUNCTION_TYPE The type of the function.
*
* \return True if the function can be transfered, false otherwise.
*/
/*! \fn IndexType LPFunctionTransfer::numSlackVariables(const FUNCTION_TYPE& function)
* \brief This function will tell the number of required slack variables for
* the function transfer.
*
* \note This function will call LPFunctionTransfer_impl::numSlackVariables()
* which by default will throw a runtime error for a not supported
* function type unless a partial template specialization of the class
* opengm::LPFunctionTransfer_impl is provided for the FUNCTION_TYPE
* type.
*
* \tparam FUNCTION_TYPE The type of the function.
*
* \param[in] function The function which will be transfered to the LP.
*
* \return The number of required slack variables.
*/
/*! \fn void LPFunctionTransfer::getSlackVariablesOrder(const FUNCTION_TYPE& function, IndicatorVariablesContainerType& order)
* \brief This function will tell the order of the slack variables which are
* required for the function transfer.
*
* \note This function will call
* LPFunctionTransfer_impl::getSlackVariablesOrder() which by default
* will throw a runtime error for a not supported function type unless a
* partial template specialization of the class
* opengm::LPFunctionTransfer_impl is provided for the FUNCTION_TYPE
* type.
*
* \tparam FUNCTION_TYPE The type of the function.
*
* \param[in] function The function which will be transfered to the LP.
* \param[out] order This container will be filled with the indicator variables
* representing the slack variables.
*/
/*! \fn void LPFunctionTransfer::getSlackVariablesObjectiveCoefficients(const FUNCTION_TYPE& function, SlackVariablesObjectiveCoefficientsContainerType& coefficients)
* \brief This function will tell the coefficients of the slack variables for
* the objective function of the linear program.
*
* \note This function will call
* LPFunctionTransfer_impl::getSlackVariablesObjectiveCoefficients()
* which by default will throw a runtime error for a not supported
* function type unless a partial template specialization of the class
* opengm::LPFunctionTransfer_impl is provided for the FUNCTION_TYPE
* type.
*
* \tparam FUNCTION_TYPE The type of the function.
*
* \param[in] function The function which will be transfered to the LP.
* \param[out] coefficients This container will be filled with the coefficients
* of the slack variables for the objective function of the linear
* program.
*/
/*! \fn void LPFunctionTransfer::getIndicatorVariables(const FUNCTION_TYPE& function, IndicatorVariablesContainerType& variables)
* \brief This function will tell all the indicator variables which are used in
* the linear constraints which are used for the function transfer.
*
* \note This function will call
* LPFunctionTransfer_impl::getIndicatorVariables() which by default will
* throw a runtime error for a not supported function type unless a
* partial template specialization of the class
* opengm::LPFunctionTransfer_impl is provided for the FUNCTION_TYPE
* type.
*
* \tparam FUNCTION_TYPE The type of the function.
*
* \param[in] function The function which will be transfered to the LP.
* \param[out] variables This container will be filled with the indicator
* variables which are used in the linear constraints.
*
*/
/*! \fn void LPFunctionTransfer::getLinearConstraints(const FUNCTION_TYPE& function, LinearConstraintsContainerType& constraints)
* \brief This function will create the necessary linear constraints which are
* used to represent the function within the LP.
*
* \note This function will call
* LPFunctionTransfer_impl::getLinearConstraints() which by default will
* throw a runtime error for a not supported function type unless a
* partial template specialization of the class
* opengm::LPFunctionTransfer_impl is provided for the FUNCTION_TYPE
* type.
*
* \tparam FUNCTION_TYPE The type of the function.
*
* \param[in] function The function which will be transfered to the LP.
* \param[out] constraints This container will be filled with the linear
* constraints which are required to add the function
* to the LP. Slack variables which are required for
* the transfer will be represented by
* opengm::IndicatorVariables consisting of exactly one
* variable-label-pair where the variable id is set
* to a value greater or equal to the number of
* variables of the function and the label is set to 0.
*/
/*! \struct LPFunctionTransfer::IsTransferableFunctor
* \brief Functor to call LPFunctionTransfer::isTransferable() for a factor of
* the graphical model.
*/
/*! \var LPFunctionTransfer::IsTransferableFunctor::isTransferable_
* \brief Storage for the return value of the
* LPFunctionTransfer::isTransferable() method.
*/
/*! \fn void LPFunctionTransfer::IsTransferableFunctor::operator()(const FUNCTION_TYPE& function)
* \brief The operator which implements the access to the function of a factor
* of the graphical model.
*
* \tparam FUNCTION_TYPE The function type.
*
* \param[in] function The function.
*/
/*! \struct LPFunctionTransfer::NumSlackVariablesFunctor
* \brief Functor to call LPFunctionTransfer::numSlackVariables() for a
* graphical model factor.
*/
/*! \var LPFunctionTransfer::NumSlackVariablesFunctor::numSlackVariables_
* \brief Storage for the return value of the
* LPFunctionTransfer::numSlackVariables() method.
*/
/*! \fn void LPFunctionTransfer::NumSlackVariablesFunctor::operator()(const FUNCTION_TYPE& function)
* \brief The operator which implements the access to the function of a
* graphical model factor.
*
* \tparam FUNCTION_TYPE The function type.
*
* \param[in] function The function.
*/
/*! \struct LPFunctionTransfer::GetSlackVariablesOrderFunctor
* \brief Functor to call LPFunctionTransfer::getSlackVariablesOrder()
* for a factor of the graphical model.
*/
/*! \var LPFunctionTransfer::GetSlackVariablesOrderFunctor::order_
* \brief Pointer to the storage for the return value of the
* LPFunctionTransfer::getSlackVariablesOrder() method.
*/
/*! \fn void LPFunctionTransfer::GetSlackVariablesOrderFunctor::operator()(const FUNCTION_TYPE& function)
* \brief The operator which implements the access to the function of a
* graphical model factor.
*
* \tparam FUNCTION_TYPE The function type.
*
* \param[in] function The function.
*/
/*! \struct LPFunctionTransfer::GetSlackVariablesObjectiveCoefficientsFunctor
* \brief Functor to call
* LPFunctionTransfer::getSlackVariablesObjectiveCoefficients()
* for a factor of the graphical model.
*/
/*! \var LPFunctionTransfer::GetSlackVariablesObjectiveCoefficientsFunctor::coefficients_
* \brief Pointer to the storage for the return value of the
* LPFunctionTransfer::getSlackVariablesObjectiveCoefficients() method.
*/
/*! \fn void LPFunctionTransfer::GetSlackVariablesObjectiveCoefficientsFunctor::operator()(const FUNCTION_TYPE& function)
* \brief The operator which implements the access to the function of a
* graphical model factor.
*
* \tparam FUNCTION_TYPE The function type.
*
* \param[in] function The function.
*/
/*! \struct LPFunctionTransfer::GetIndicatorVariablesFunctor
* \brief Functor to call LPFunctionTransfer::getIndicatorVariables() for a
* factor of the graphical model.
*/
/*! \var LPFunctionTransfer::GetIndicatorVariablesFunctor::variables_
* \brief Pointer to the storage for the return value of the
* LPFunctionTransfer::getIndicatorVariables() method.
*/
/*! \fn void LPFunctionTransfer::GetIndicatorVariablesFunctor::operator()(const FUNCTION_TYPE& function)
* \brief The operator which implements the access to the function of a
* graphical model factor.
*
* \tparam FUNCTION_TYPE The function type.
*
* \param[in] function The function.
*/
/*! \struct LPFunctionTransfer::GetLinearConstraintsFunctor
* \brief Functor to call LPFunctionTransfer::getLinearConstraints() for a
* factor of the graphical model.
*/
/*! \var LPFunctionTransfer::GetLinearConstraintsFunctor::constraints_
* \brief Pointer to the storage for the return value of the
* LPFunctionTransfer::getLinearConstraints() method.
*/
/*! \fn void LPFunctionTransfer::GetLinearConstraintsFunctor::operator()(const FUNCTION_TYPE& function)
* \brief The operator which implements the access to the function of a
* graphical model factor.
*
* \tparam FUNCTION_TYPE The function type.
*
* \param[in] function The function.
*/
/*! \class LPFunctionTransfer_impl
* \brief Default implementation for class opengm::LPFunctionTransfer.
*
* \tparam FUNCTION_TYPE Function type,
* \tparam VALUE_TYPE Value type.
* \tparam INDEX_TYPE Index type.
* \tparam LABEL_TYPE Label type.
*
* \note This class has to be overwritten via partial template specialization
* to enable support for new function types.
*/
/*! \typedef LPFunctionTransfer_impl::ValueType
* \brief Typedef of the VALUE_TYPE template parameter type from the class
* LPFunctionTransfer_impl.
*/
/*! \typedef LPFunctionTransfer_impl::IndexType
* \brief Typedef of the INDEX_TYPE template parameter type from the class
* LPFunctionTransfer_impl.
*/
/*! \typedef LPFunctionTransfer_impl::LabelType
* \brief Typedef of the LABEL_TYPE template parameter type from the class
* LPFunctionTransfer_impl.
*/
/*! \typedef LPFunctionTransfer_impl::FunctionType
* \brief Typedef of the FUNCTION_TYPE template parameter type from the class
* LPFunctionTransfer_impl.
*/
/*! \typedef LPFunctionTransfer_impl::LPFunctionTransferType
* \brief Typedef of the LPFunctionTransfer class with appropriate template
* parameter.
*/
/*! \typedef LPFunctionTransfer_impl::LinearConstraintType
* \brief Typedef of the LinearConstraint class with appropriate template
* parameter.
*/
/*! \typedef LPFunctionTransfer_impl::LinearConstraintsContainerType
* \brief Defines the linear constraints container type which is used to store
* multiple linear constraints.
*/
/*! \typedef LPFunctionTransfer_impl::IndicatorVariableType
* \brief Defines the indicator variable type which is used within linear
* constraints.
*/
/*! \typedef LPFunctionTransfer_impl::IndicatorVariablesContainerType
* \brief Defines the indicator variables container type which is used to store
* multiple indicator variables.
*/
/*! \typedef LPFunctionTransfer_impl::SlackVariablesObjectiveCoefficientsContainerType
* \brief Defines the container type which is used to store the coefficients of
* the slack variables for the objective function.
*/
/*! \fn bool LPFunctionTransfer_impl::isTransferable()
* \brief This function will tell if the function type provided via the
* template parameter can be transfered into a more efficient LP
* formulation.
*
* \return This function will always return false as this is the default return
* value.
*/
/*! \fn IndexType LPFunctionTransfer_impl::numSlackVariables(const FunctionType& function)
* \brief This function will tell the number of required slack variables for
* the LP function transfer.
*
* \note This function will throw a runtime error of unsupported function type
* as numSlackVariables() must not be called for functions which are not
* supported. Supported functions are those for which a partial template
* specialization of the class LPFunctionTransfer_impl is provided.
*
* \param[in] function The function which will be transformed.
*
* \return Nothing as this function will throw a runtime error.
*/
/*! \fn void LPFunctionTransfer_impl::getSlackVariablesOrder(const FunctionType& function, IndicatorVariablesContainerType& order)
* \brief This function will tell the order of the slack variables for the LP
* function transfer.
*
* \note This function will throw a runtime error of unsupported function type
* as getSlackVariablesOrder() must not be called for functions which are
* not supported. Supported functions are those for which a partial
* template specialization of the class LPFunctionTransfer_impl is
* provided.
*
* \param[in] function The function which will be transformed.
* \param[out] order This container will be filled with the order of the slack
* variables.
*/
/*! \fn void LPFunctionTransfer_impl::getSlackVariablesObjectiveCoefficients(const FunctionType& function, SlackVariablesObjectiveCoefficientsContainerType& coefficients)
* \brief This function will tell the coefficients of the slack variables for
* the objective function of the linear program.
*
* \note This function will throw a runtime error of unsupported function type
* as getSlackVariablesObjectiveCoefficients() must not be called for
* functions which are not supported. Supported functions are those for
* which a partial template specialization of the class
* LPFunctionTransfer_impl is provided.
*
* \param[in] function The function which will be transformed.
* \param[out] coefficients This container will be filled with the coefficients
* of the slack variables for the objective function of the lLP.
*/
/*! \fn void LPFunctionTransfer_impl::getIndicatorVariables(const FunctionType& function, IndicatorVariablesContainerType& variables)
* \brief This function will tell the used indicator variables for the
* linear constraints.
*
* \note This function will throw a runtime error of unsupported function type
* as getIndicatorVariables() must not be called for functions which are
* not supported. Supported functions are those for which a partial
* template specialization of the class LPFunctionTransfer_impl is
* provided.
*
* \param[in] function The function which will be transformed.
* \param[out] variables This container will be filled with the indicator
* variables which are used in the linear constraints.
*/
/*! \fn void LPFunctionTransfer_impl::getLinearConstraints(const FunctionType& function, LinearConstraintsContainerType& constraints)
* \brief This function will create the necessary linear constraints to add the
* function to the linear program.
*
* \note This function will throw a runtime error of unsupported function type
* as getLinearConstraints() must not be called for functions which are
* not supported. Supported functions are those for which a partial
* template specialization of the class LPFunctionTransfer_impl is
* provided.
*
* \param[in] function The function which will be transformed.
* \param[out] constraints This container will be filled with the linear
* constraints which are required to add the function
* to the linear program. Slack variables which are
* required for the transformation will be represented
* by opengm::IndicatorVariables consisting of exactly
* one variable-label-pair where the variable id is set
* to a value greater or equal to the number of
* variables of the function and the label is set to 0.
*/
/******************
* implementation *
******************/
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
template<class FUNCTION_TYPE>
inline bool LPFunctionTransfer<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::isTransferable() {
return LPFunctionTransfer_impl<FUNCTION_TYPE, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::isTransferable();
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
template<class FUNCTION_TYPE>
inline typename LPFunctionTransfer<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::IndexType LPFunctionTransfer<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::numSlackVariables(const FUNCTION_TYPE& function) {
return LPFunctionTransfer_impl<FUNCTION_TYPE, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::numSlackVariables(function);
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
template<class FUNCTION_TYPE>
inline void LPFunctionTransfer<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getSlackVariablesOrder(const FUNCTION_TYPE& function, IndicatorVariablesContainerType& order) {
LPFunctionTransfer_impl<FUNCTION_TYPE, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getSlackVariablesOrder(function, order);
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
template<class FUNCTION_TYPE>
inline void LPFunctionTransfer<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getSlackVariablesObjectiveCoefficients(const FUNCTION_TYPE& function, SlackVariablesObjectiveCoefficientsContainerType& coefficients) {
LPFunctionTransfer_impl<FUNCTION_TYPE, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getSlackVariablesObjectiveCoefficients(function, coefficients);
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
template<class FUNCTION_TYPE>
inline void LPFunctionTransfer<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getIndicatorVariables(const FUNCTION_TYPE& function, IndicatorVariablesContainerType& variables) {
LPFunctionTransfer_impl<FUNCTION_TYPE, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getIndicatorVariables(function, variables);
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
template<class FUNCTION_TYPE>
inline void LPFunctionTransfer<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getLinearConstraints(const FUNCTION_TYPE& function, LinearConstraintsContainerType& constraints) {
LPFunctionTransfer_impl<FUNCTION_TYPE, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getLinearConstraints(function, constraints);
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
template<class FUNCTION_TYPE>
inline void LPFunctionTransfer<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::IsTransferableFunctor::operator()(const FUNCTION_TYPE& function) {
isTransferable_ = isTransferable<FUNCTION_TYPE>();
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
template<class FUNCTION_TYPE>
inline void LPFunctionTransfer<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::NumSlackVariablesFunctor::operator()(const FUNCTION_TYPE& function) {
numSlackVariables_ = numSlackVariables(function);
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
template<class FUNCTION_TYPE>
inline void LPFunctionTransfer<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::GetSlackVariablesOrderFunctor::operator()(const FUNCTION_TYPE& function) {
getSlackVariablesOrder(function, *order_);
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
template<class FUNCTION_TYPE>
inline void LPFunctionTransfer<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::GetSlackVariablesObjectiveCoefficientsFunctor::operator()(const FUNCTION_TYPE& function) {
getSlackVariablesObjectiveCoefficients(function, *coefficients_);
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
template<class FUNCTION_TYPE>
inline void LPFunctionTransfer<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::GetIndicatorVariablesFunctor::operator()(const FUNCTION_TYPE& function) {
getIndicatorVariables(function, *variables_);
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
template<class FUNCTION_TYPE>
inline void LPFunctionTransfer<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::GetLinearConstraintsFunctor::operator()(const FUNCTION_TYPE& function) {
getLinearConstraints(function, *constraints_);
}
template<class FUNCTION_TYPE, class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
inline bool LPFunctionTransfer_impl<FUNCTION_TYPE, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::isTransferable() {
// default implementation
return false;
}
template<class FUNCTION_TYPE, class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
inline typename LPFunctionTransfer_impl<FUNCTION_TYPE, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::IndexType LPFunctionTransfer_impl<FUNCTION_TYPE, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::numSlackVariables(const FunctionType& function) {
// default implementation
throw opengm::RuntimeError("Unsupported Function Type.");
return 0;
}
template<class FUNCTION_TYPE, class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
inline void LPFunctionTransfer_impl<FUNCTION_TYPE, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getSlackVariablesOrder(const FunctionType& function, IndicatorVariablesContainerType& order) {
// default implementation
throw opengm::RuntimeError("Unsupported Function Type.");
}
template<class FUNCTION_TYPE, class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
inline void LPFunctionTransfer_impl<FUNCTION_TYPE, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getSlackVariablesObjectiveCoefficients(const FunctionType& function, SlackVariablesObjectiveCoefficientsContainerType& coefficients) {
// default implementation
throw opengm::RuntimeError("Unsupported Function Type.");
}
template<class FUNCTION_TYPE, class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
inline void LPFunctionTransfer_impl<FUNCTION_TYPE, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getIndicatorVariables(const FunctionType& function, IndicatorVariablesContainerType& variables) {
// default implementation
throw opengm::RuntimeError("Unsupported Function Type.");
}
template<class FUNCTION_TYPE, class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
inline void LPFunctionTransfer_impl<FUNCTION_TYPE, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getLinearConstraints(const FunctionType& function, LinearConstraintsContainerType& constraints) {
// default implementation
throw opengm::RuntimeError("Unsupported Function Type.");
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
inline bool LPFunctionTransfer_impl<SumConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::isTransferable() {
// implementation for SumConstraintFunction
return true;
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
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) {
// implementation for SumConstraintFunction
return 1;
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
inline void LPFunctionTransfer_impl<SumConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getSlackVariablesOrder(const FunctionType& function, IndicatorVariablesContainerType& order) {
// implementation for SumConstraintFunction
order.resize(1);
order[0] = IndicatorVariableType(function.numVariables_, 0);
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
inline void LPFunctionTransfer_impl<SumConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getSlackVariablesObjectiveCoefficients(const FunctionType& function, SlackVariablesObjectiveCoefficientsContainerType& coefficients) {
// implementation for SumConstraintFunction
coefficients.resize(1);
coefficients[0] = function.lambda_;
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
inline void LPFunctionTransfer_impl<SumConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getIndicatorVariables(const FunctionType& function, IndicatorVariablesContainerType& variables) {
// implementation for SumConstraintFunction
variables.clear();
for(IndexType i = 0; i < function.numVariables_; ++i) {
for(LabelType j = 0; j < (function.useSameNumLabels_ ? function.maxNumLabels_ : function.shape_[i]); ++j) {
const IndicatorVariableType indicatorVariable(i, j);
variables.push_back(indicatorVariable);
}
}
const IndicatorVariableType slackVariable(function.numVariables_, 0);
variables.push_back(slackVariable);
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
inline void LPFunctionTransfer_impl<SumConstraintFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getLinearConstraints(const FunctionType& function, LinearConstraintsContainerType& constraints) {
// implementation for SumConstraintFunction
constraints.resize(2);
LinearConstraintType constraint;
constraint.setConstraintOperator(LinearConstraintType::LinearConstraintOperatorType::LessEqual);
constraint.setBound(function.bound_);
for(IndexType i = 0; i < function.numVariables_; ++i) {
for(LabelType j = 0; j < (function.useSameNumLabels_ ? function.maxNumLabels_ : function.shape_[i]); ++j) {
const IndicatorVariableType indicatorVariable(i, j);
constraint.add(indicatorVariable, function.coefficients_[(function.shareCoefficients_ ? j : function.coefficientsOffsets_[i] + j)]);
}
}
const IndicatorVariableType slackVariable(function.numVariables_, 0);
constraint.add(slackVariable, -1.0);
constraints[0] = constraint;
LinearConstraintType constraint2;
constraint2.setConstraintOperator(LinearConstraintType::LinearConstraintOperatorType::LessEqual);
constraint2.setBound(-function.bound_);
for(IndexType i = 0; i < function.numVariables_; ++i) {
for(LabelType j = 0; j < (function.useSameNumLabels_ ? function.maxNumLabels_ : function.shape_[i]); ++j) {
const IndicatorVariableType indicatorVariable(i, j);
constraint2.add(indicatorVariable, -function.coefficients_[(function.shareCoefficients_ ? j : function.coefficientsOffsets_[i] + j)]);
}
}
constraint2.add(slackVariable, -1.0);
constraints[1] = constraint2;
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
inline bool LPFunctionTransfer_impl<LabelCostFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::isTransferable() {
// implementation for LabelCostFunction
return true;
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
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) {
// implementation for LabelCostFunction
// one slack variable for each label cost which is not zero
if(function.useSingleCost_) {
return 1;
} else {
IndexType numLabelCosts = 0;
for(LabelType i = 0; i < function.maxNumLabels_; ++i) {
// sort out unused labels
if(function.costs_[i] != 0.0) {
++numLabelCosts;
}
}
return numLabelCosts;
}
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
inline void LPFunctionTransfer_impl<LabelCostFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getSlackVariablesOrder(const FunctionType& function, IndicatorVariablesContainerType& order) {
// implementation for LabelCostFunction
const IndexType numLabelCosts = numSlackVariables(function);
order.resize(numLabelCosts);
for(IndexType i = 0; i < numLabelCosts; ++i) {
order[i] = IndicatorVariableType(function.numVariables_ + i, 0);
}
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
inline void LPFunctionTransfer_impl<LabelCostFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getSlackVariablesObjectiveCoefficients(const FunctionType& function, SlackVariablesObjectiveCoefficientsContainerType& coefficients) {
// implementation for LabelCostFunction
const IndexType numLabelCosts = numSlackVariables(function);
coefficients.resize(numLabelCosts);
if(function.useSingleCost_) {
OPENGM_ASSERT(numLabelCosts == 1);
coefficients[0] = function.singleCost_;
} else {
LabelType currentCostIndex = 0;
for(LabelType i = 0; i < function.maxNumLabels_; ++i) {
// sort out unused label costs
if(function.costs_[i] != 0.0) {
coefficients[currentCostIndex] = function.costs_[i];
++currentCostIndex;
}
}
}
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
inline void LPFunctionTransfer_impl<LabelCostFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getIndicatorVariables(const FunctionType& function, IndicatorVariablesContainerType& variables) {
// implementation for LabelCostFunction
const IndexType numLabelCosts = numSlackVariables(function);
variables.resize(numLabelCosts * 2);
if(function.useSingleCost_) {
OPENGM_ASSERT(variables.size() == 2);
IndicatorVariableType indicatorVar;
indicatorVar.setLogicalOperatorType(IndicatorVariableType::Or);
if(function.useSameNumLabels_) {
for(IndexType i = 0; i < function.numVariables_; ++i) {
indicatorVar.add(i, function.singleLabel_);
}
} else {
for(IndexType i = 0; i < function.numVariables_; ++i) {
if(function.shape_[i] > function.singleLabel_) {
indicatorVar.add(i, function.singleLabel_);
}
}
}
variables[0] = indicatorVar;
variables[1] = IndicatorVariableType(function.numVariables_, 0);
} else {
LabelType currentNonZeroLabel = 0;
for(LabelType currentLabel = 0; currentLabel < function.maxNumLabels_; ++currentLabel) {
if(function.costs_[currentLabel] != 0) {
IndicatorVariableType indicatorVar;
indicatorVar.setLogicalOperatorType(IndicatorVariableType::Or);
if(function.useSameNumLabels_) {
for(IndexType i = 0; i < function.numVariables_; ++i) {
indicatorVar.add(i, currentLabel);
}
} else {
for(IndexType i = 0; i < function.numVariables_; ++i) {
if(function.shape_[i] > currentLabel) {
indicatorVar.add(i, currentLabel);
}
}
}
variables[currentNonZeroLabel * 2] = indicatorVar;
variables[(currentNonZeroLabel * 2) + 1] = IndicatorVariableType(function.numVariables_ + currentNonZeroLabel, 0);
++currentNonZeroLabel;
}
}
}
}
template<class VALUE_TYPE, class INDEX_TYPE, class LABEL_TYPE>
inline void LPFunctionTransfer_impl<LabelCostFunction<VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>, VALUE_TYPE, INDEX_TYPE, LABEL_TYPE>::getLinearConstraints(const FunctionType& function, LinearConstraintsContainerType& constraints) {
// implementation for LabelCostFunction
IndicatorVariablesContainerType variables;
getIndicatorVariables(function, variables);
OPENGM_ASSERT(variables.size() % 2 == 0);
constraints.resize(variables.size() / 2);
for(size_t i = 0; i < constraints.size(); ++i) {
LinearConstraintType constraint;
constraint.setConstraintOperator(LinearConstraintType::LinearConstraintOperatorType::Equal);
constraint.setBound(0.0);
constraint.add(variables[i * 2], 1.0); // Or indicator variable
constraint.add(variables[(i * 2) + 1], -1.0); // slack variable
constraints[i] = constraint;
}
}
} // namespace opengm
#endif /* OPENGM_LP_FUNCTIONTRANSFER_HXX_ */
|