2 #ifndef OPENGM_GRAPHICALMODEL_FACTOR_HXX
3 #define OPENGM_GRAPHICALMODEL_FACTOR_HXX
32 template<
class FACTOR,
class FUNCTOR>
41 typedef typename FACTOR::VariablesIteratorType
ViIterator;
43 template<
class FUNCTION>
45 functor_(factor_.variableIndicesBegin(),factor_.variableIndicesEnd(),
function);
58 class FUNCTION_TYPE_LIST,
60 >
class GraphicalModel;
62 template<
class GRAPHICAL_MODEL>
class Factor;
66 void save(
const GM&,
const std::string&,
const std::string&);
68 void load(GM_& gm,
const std::string&,
const std::string&);
71 namespace functionwrapper {
73 template<
size_t IX,
size_t DX,
bool END>
74 struct FactorInvariant;
75 template<
size_t IX,
size_t DX>
76 struct FactorInvariant<IX, DX, false> {
77 template<
class GM,
class FACTOR>
78 void static op(
const GM &,
const FACTOR &);
80 template<
size_t IX,
size_t DX>
81 struct FactorInvariant<IX, DX, true> {
82 template<
class GM,
class FACTOR>
83 void static op(
const GM &,
const FACTOR &);
88 namespace detail_graphical_model {
90 struct FunctionWrapper;
91 template<
size_t DX,
class VALUE_TYPE>
92 struct FunctionValueWrapper;
93 template<
size_t IX,
size_t DX,
bool end>
94 struct FunctionWrapperExecutor;
95 template<
size_t IX,
size_t DX,
bool end,
class VALUE_TYPE>
96 struct FunctionValueWrapperExecutor;
102 template<
class GRAPHICAL_MODEL>
112 typedef typename GraphicalModelType::ValueType
ValueType;
113 typedef typename GraphicalModelType::LabelType
LabelType;
114 typedef typename GraphicalModelType::IndexType
IndexType;
117 typedef FactorShapeAccessor<Factor<GRAPHICAL_MODEL> > ShapeAccessorType;
129 Factor(GraphicalModelPointerType);
131 Factor(GraphicalModelPointerType,
const IndexType,
const UInt8Type,
const IndexType,
const IndexType);
138 IndexType
size()
const;
141 IndexType
shape(
const IndexType)
const;
145 template<
size_t FUNCTION_TYPE_INDEX>
146 const typename meta::TypeAtTypeList<FunctionTypeList, FUNCTION_TYPE_INDEX>::type&
function()
const;
149 template<
class ITERATOR>
152 template<
class FUNCTOR>
155 template<
class FUNCTOR>
158 template<
class ITERATOR>
160 template<
class ITERATOR>
162 template<
int FunctionType,
class ITERATOR>
166 template<
class ITERATOR>
176 template<
int PROPERTY>
178 template<
int PROPERTY>
181 template<
class FUNCTOR>
183 template<
class FUNCTOR>
185 template<
class FUNCTOR>
187 template<
class FUNCTOR>
190 ValueType
sum()
const;
192 ValueType
min()
const;
193 ValueType
max()
const;
196 void testInvariant()
const;
198 const VisContainerType & variableIndexSequence()
const;
199 template<
size_t FUNCTION_TYPE_INDEX>
200 typename meta::TypeAtTypeList<FunctionTypeList, FUNCTION_TYPE_INDEX>::type&
function();
202 GraphicalModelPointerType gm_;
203 IndexType functionIndex_;
208 VisContainerType vis_;
209 template<
typename,
typename,
typename,
typename>
212 friend struct opengm::detail_graphical_model::FunctionWrapper;
213 template<
size_t,
size_t,
bool>
214 friend struct opengm::detail_graphical_model::FunctionWrapperExecutor;
217 template<
typename GM_>
219 template<
typename GM_>
221 template<
typename,
typename,
typename >
225 template<
class,
class,
class,
class>
226 friend class opengm::functionwrapper::binary::OperationWrapperSelector;
227 template<
class ,
class,
class>
228 friend class opengm::functionwrapper::unary::OperationWrapperSelector;
230 template<
class,
class,
class,
class,
class,
class,
class>
231 friend class opengm::functionwrapper::binary::OperationWrapper;
233 template <
class,
size_t>
234 friend class opengm::meta::GetFunction;
235 template<
class,
class,
class>
236 friend class opengm::functionwrapper::AccumulateSomeWrapper;
238 template<
class,
class,
class,
size_t,
size_t,
bool >
239 friend class opengm::functionwrapper::executor::AccumulateSomeExecutor;
241 template<
class,
class,
class,
size_t,
size_t,
bool>
242 friend class opengm::functionwrapper::executor::binary::InplaceOperationExecutor;
244 template<
class A,
class B,
class OP,
size_t IX,
size_t DX,
bool>
245 friend class opengm::functionwrapper::executor::unary::OperationExecutor;
249 template<
class T,
class I,
class L>
268 template<
class VARIABLE_INDEX_ITERATOR,
class SHAPE_ITERATOR>
269 IndependentFactor(VARIABLE_INDEX_ITERATOR, VARIABLE_INDEX_ITERATOR, SHAPE_ITERATOR, SHAPE_ITERATOR);
270 template<
class VARIABLE_INDEX_ITERATOR,
class SHAPE_ITERATOR>
271 IndependentFactor(VARIABLE_INDEX_ITERATOR, VARIABLE_INDEX_ITERATOR, SHAPE_ITERATOR, SHAPE_ITERATOR,
const ValueType);
272 template<
class GRAPHICAL_MODEL,
class VARIABLE_INDEX_ITERATOR>
275 template<
class GRAPHICAL_MODEL>
278 template<
class GRAPHICAL_MODEL>
280 template<
class VARIABLE_INDEX_ITERATOR,
class SHAPE_ITERATOR>
281 void assign(VARIABLE_INDEX_ITERATOR, VARIABLE_INDEX_ITERATOR, SHAPE_ITERATOR, SHAPE_ITERATOR);
282 template<
class VARIABLE_INDEX_ITERATOR,
class SHAPE_ITERATOR>
283 void assign(VARIABLE_INDEX_ITERATOR, VARIABLE_INDEX_ITERATOR, SHAPE_ITERATOR, SHAPE_ITERATOR,
const ValueType);
284 template<
class GRAPHICAL_MODEL,
class VARIABLE_INDEX_ITERATOR>
285 void assign(
const GRAPHICAL_MODEL&, VARIABLE_INDEX_ITERATOR, VARIABLE_INDEX_ITERATOR);
286 template<
class GRAPHICAL_MODEL,
class VARIABLE_INDEX_ITERATOR>
287 void assign(
const GRAPHICAL_MODEL&, VARIABLE_INDEX_ITERATOR, VARIABLE_INDEX_ITERATOR,
const ValueType);
288 void assign(
const ValueType);
295 template<
size_t FUNCTION_TYPE_INDEX>
296 const FunctionType&
function()
const;
299 IndexType
shape(
const size_t dimIndex)
const;
302 template<
class ITERATOR>
304 template<
class ITERATOR>
307 T
operator()(
const IndexType,
const IndexType)
const;
308 T
operator()(
const IndexType,
const IndexType,
const IndexType)
const;
309 T
operator()(
const IndexType,
const IndexType,
const IndexType,
const IndexType)
const;
311 template<
class INDEX_ITERATOR,
class STATE_ITERATOR>
312 void fixVariables(INDEX_ITERATOR, INDEX_ITERATOR, STATE_ITERATOR);
313 template<
class ITERATOR>
316 T&
operator()(
const IndexType,
const IndexType);
317 T&
operator()(
const IndexType,
const IndexType,
const IndexType);
318 T&
operator()(
const IndexType,
const IndexType,
const IndexType,
const IndexType);
319 template<
class UNARY_OPERATOR_TYPE>
321 template<
class BINARY_OPERATOR_TYPE>
322 void operateBinary(
const T value, BINARY_OPERATOR_TYPE binaryOperator);
323 template<
class GRAPHICAL_MODEL,
class BINARY_OPERATOR_TYPE>
325 template<
class BINARY_OPERATOR_TYPE>
327 template<
class BINARY_OPERATOR_TYPE>
331 template<
class ACCUMULATOR>
332 void accumulate(ValueType&, std::vector<LabelType>&)
const;
333 template<
class ACCUMULATOR>
335 template<
class ACCUMULATOR,
class VariablesIterator>
337 template<
class ACCUMULATOR,
class VariablesIterator>
338 void accumulate(VariablesIterator, VariablesIterator) ;
339 const FunctionType&
function()
const;
342 {
return function_.
isPotts(); }
362 template<
size_t FUNCTION_TYPE_INDEX>
363 FunctionType&
function();
366 std::vector<IndexType> variableIndices_;
367 FunctionType function_;
371 template<
typename,
typename,
typename,
typename>
374 template<
class,
class,
class,
class>
375 friend class opengm::functionwrapper::binary::OperationWrapperSelector;
376 template<
class ,
class,
class>
377 friend class opengm::functionwrapper::unary::OperationWrapperSelector;
378 template<
class,
class,
class,
class,
class,
class,
class>
379 friend class opengm::functionwrapper::binary::OperationWrapper;
380 template <
class,
size_t>
381 friend class opengm::meta::GetFunction;
382 template<
class,
class,
class>
383 friend class opengm::functionwrapper::AccumulateSomeWrapper;
384 template<
class,
class,
class,
size_t,
size_t,
bool>
385 friend class opengm::functionwrapper::executor::AccumulateSomeExecutor;
386 template<
class,
class,
class,
size_t,
size_t,
bool>
387 friend class opengm::functionwrapper::executor::binary::InplaceOperationExecutor;
388 template<
class A,
class B,
class OP,
size_t IX,
size_t DX,
bool>
389 friend class opengm::functionwrapper::executor::unary::OperationExecutor;
390 template<
class ACC,
class A,
class ViAccIterator>
391 friend void accumulate(A &, ViAccIterator, ViAccIterator );
395 template<
class GRAPHICAL_MODEL>
403 template<
class GRAPHICAL_MODEL>
413 functionIndex_(functionIndex),
414 functionTypeId_(functionTypeId),
415 vis_(gm->factorsVis_, indexInVisVector,order)
430 template<
class GRAPHICAL_MODEL>
438 vis_(gm_->factorsVis_)
441 template<
class GRAPHICAL_MODEL>
447 functionIndex_(src.functionIndex_),
448 functionTypeId_(src.functionTypeId_),
452 template<
class GRAPHICAL_MODEL>
460 functionTypeId_ = src.functionTypeId_;
461 functionIndex_ = src.functionIndex_;
468 template<
class GRAPHICAL_MODEL>
472 return ShapeIteratorType(ShapeAccessorType(
this), 0);
475 template<
class GRAPHICAL_MODEL>
479 return ShapeIteratorType(ShapeAccessorType(
this), vis_.size());
484 template<
class GRAPHICAL_MODEL>
493 template<
class GRAPHICAL_MODEL>
494 inline typename Factor<GRAPHICAL_MODEL>::IndexType
499 return gm_->numberOfLabels(vis_[j]);
502 template<
class GRAPHICAL_MODEL>
510 template<
class GRAPHICAL_MODEL>
520 template<
class GRAPHICAL_MODEL>
527 return gm_->numberOfLabels(vis_[j]);
532 template<
class GRAPHICAL_MODEL>
533 template<
class ITERATOR>
539 return opengm::detail_graphical_model::FunctionWrapper<
541 >::getValue (this->gm_, begin, functionIndex_, functionTypeId_);
548 template<
class GRAPHICAL_MODEL>
549 template<
class FUNCTOR>
555 return opengm::detail_graphical_model::FunctionWrapper<
557 >::callFunctor(this->gm_, functionIndex_, functionTypeId_,functor);
563 template<
class GRAPHICAL_MODEL>
564 template<
class FUNCTOR>
572 return opengm::detail_graphical_model::FunctionWrapper<
574 >::callFunctor(this->gm_, functionIndex_, functionTypeId_,viFunctor);
580 template<
class GRAPHICAL_MODEL>
581 template<
class ITERATOR>
587 opengm::detail_graphical_model::FunctionWrapper<
589 >::getValues (this->gm_, begin, functionIndex_, functionTypeId_);
592 template<
class GRAPHICAL_MODEL>
593 template<
class ITERATOR>
599 opengm::detail_graphical_model::FunctionWrapper<
601 >::getValuesSwitchedOrder (this->gm_, begin, functionIndex_, functionTypeId_);
606 template<
class GRAPHICAL_MODEL>
607 template<
int FunctionType,
class ITERATOR>
609 Factor<GRAPHICAL_MODEL>::operator()
613 return gm_->
template functions<FunctionType>()[functionIndex_].
operator()(begin);
627 template<
class GRAPHICAL_MODEL>
628 template<
int PROPERTY>
632 return opengm::detail_graphical_model::FunctionWrapper<
634 >:: template binaryProperty<GRAPHICAL_MODEL,PROPERTY> (this->gm_, functionIndex_, functionTypeId_);
649 template<
class GRAPHICAL_MODEL>
650 template<
int PROPERTY>
651 inline typename GRAPHICAL_MODEL::ValueType
654 return opengm::detail_graphical_model::FunctionWrapper<
656 >:: template valueProperty<GRAPHICAL_MODEL,PROPERTY> (this->gm_, functionIndex_, functionTypeId_);
668 template<
class GRAPHICAL_MODEL>
669 template<
class FUNCTOR>
675 opengm::detail_graphical_model::FunctionWrapper<
677 >:: template forAllValuesInAnyOrder<GRAPHICAL_MODEL,FUNCTOR> (this->gm_,functor, functionIndex_, functionTypeId_);
690 template<
class GRAPHICAL_MODEL>
691 template<
class FUNCTOR>
697 opengm::detail_graphical_model::FunctionWrapper<
699 >:: template forAtLeastAllUniqueValues<GRAPHICAL_MODEL,FUNCTOR> (this->gm_,functor, functionIndex_, functionTypeId_);
711 template<
class GRAPHICAL_MODEL>
712 template<
class FUNCTOR>
718 opengm::detail_graphical_model::FunctionWrapper<
720 >:: template forAllValuesInOrder<GRAPHICAL_MODEL,FUNCTOR> (this->gm_,functor, functionIndex_, functionTypeId_);
723 template<
class GRAPHICAL_MODEL>
724 template<
class FUNCTOR>
730 opengm::detail_graphical_model::FunctionWrapper<
732 >:: template forAllValuesInSwitchedOrder<GRAPHICAL_MODEL,FUNCTOR> (this->gm_,functor, functionIndex_, functionTypeId_);
735 template<
class GRAPHICAL_MODEL>
739 return opengm::detail_graphical_model::FunctionWrapper<
741 >::isPotts (this->gm_, functionIndex_, functionTypeId_);
744 template<
class GRAPHICAL_MODEL>
748 return opengm::detail_graphical_model::FunctionWrapper<
750 >::isGeneralizedPotts (this->gm_, functionIndex_, functionTypeId_);
753 template<
class GRAPHICAL_MODEL>
757 return opengm::detail_graphical_model::FunctionWrapper<
759 >::isSubmodular (this->gm_, functionIndex_, functionTypeId_);
762 template<
class GRAPHICAL_MODEL>
766 if(this->numberOfVariables()==2) {
767 return opengm::detail_graphical_model::FunctionWrapper<
769 >::isSquaredDifference(this->gm_, functionIndex_, functionTypeId_);
776 template<
class GRAPHICAL_MODEL>
780 if(this->numberOfVariables()==2) {
781 return opengm::detail_graphical_model::FunctionWrapper<
783 >::isTruncatedSquaredDifference(this->gm_, functionIndex_, functionTypeId_);
790 template<
class GRAPHICAL_MODEL>
794 if(this->numberOfVariables() == 2) {
795 return opengm::detail_graphical_model::FunctionWrapper<
797 >::isAbsoluteDifference(this->gm_, functionIndex_, functionTypeId_);
804 template<
class GRAPHICAL_MODEL>
808 if(this->numberOfVariables()==2) {
809 return opengm::detail_graphical_model::FunctionWrapper<
811 >::isTruncatedAbsoluteDifference (this->gm_, functionIndex_, functionTypeId_);
818 template<
class GRAPHICAL_MODEL>
822 return opengm::detail_graphical_model::FunctionWrapper<
824 >::isLinearConstraint (this->gm_, functionIndex_, functionTypeId_);
827 template<
class GRAPHICAL_MODEL>
830 return opengm::detail_graphical_model::FunctionWrapper<
832 >::sum (this->gm_, functionIndex_, functionTypeId_);
835 template<
class GRAPHICAL_MODEL>
838 return opengm::detail_graphical_model::FunctionWrapper<
840 >::product (this->gm_, functionIndex_, functionTypeId_);
843 template<
class GRAPHICAL_MODEL>
846 return opengm::detail_graphical_model::FunctionWrapper<
848 >::min (this->gm_, functionIndex_, functionTypeId_);
851 template<
class GRAPHICAL_MODEL>
854 return opengm::detail_graphical_model::FunctionWrapper<
856 >::max (this->gm_, functionIndex_, functionTypeId_);
859 template<
class GRAPHICAL_MODEL>
860 template<
class ITERATOR>
865 for(IndexType j = 0; j < numberOfVariables(); ++j) {
866 *out = this->variableIndex(j);
871 template<
class GRAPHICAL_MODEL>
872 template<
size_t FUNCTION_TYPE_INDEX>
873 inline typename meta::TypeAtTypeList< typename Factor<GRAPHICAL_MODEL>::FunctionTypeList, FUNCTION_TYPE_INDEX>::type&
875 typedef typename meta::SmallerNumber<FUNCTION_TYPE_INDEX, Factor<GRAPHICAL_MODEL>::NrOfFunctionTypes>::type MetaBoolAssertType;
877 return meta::FieldAccess::template byIndex<FUNCTION_TYPE_INDEX>(gm_->functionDataField_).
878 functionData_.functions_[functionIndex_];
881 template<
class GRAPHICAL_MODEL>
882 template<
size_t FUNCTION_TYPE_INDEX>
883 inline const typename meta::TypeAtTypeList< typename Factor<GRAPHICAL_MODEL>::FunctionTypeList, FUNCTION_TYPE_INDEX>::type&
885 typedef typename meta::SmallerNumber<FUNCTION_TYPE_INDEX, Factor<GRAPHICAL_MODEL>::NrOfFunctionTypes>::type MetaBoolAssertType;
887 return meta::FieldAccess::template byIndex<FUNCTION_TYPE_INDEX>(gm_->functionDataField_).
888 functionData_.functions_[functionIndex_];
891 template<
class GRAPHICAL_MODEL>
897 template<
class GRAPHICAL_MODEL>
903 template<
class GRAPHICAL_MODEL>
907 if(vis_.size() != 0) {
908 size_t val = this->shape(0);
909 for(
size_t i = 1; i<this->numberOfVariables(); ++i) {
910 val *= this->shape(i);
917 template<
class GRAPHICAL_MODEL>
919 opengm::functionwrapper::executor::FactorInvariant
923 meta::EqualNumber<Factor<GRAPHICAL_MODEL>::NrOfFunctionTypes, 0>::value
927 template<
class GRAPHICAL_MODEL>
930 return static_cast<UInt8Type> (functionTypeId_);
933 template<
class GRAPHICAL_MODEL>
936 return functionIndex_;
939 template<
class T,
class I,
class L>
941 : variableIndices_(),
946 template<
class T,
class I,
class L>
949 const ValueType constant
951 : variableIndices_(),
960 template<
class T,
class I,
class L>
961 template<
class VARIABLE_INDEX_ITERATOR,
class SHAPE_ITERATOR>
964 VARIABLE_INDEX_ITERATOR beginVi,
965 VARIABLE_INDEX_ITERATOR endVi,
966 SHAPE_ITERATOR beginShape,
967 SHAPE_ITERATOR endShape
969 : variableIndices_(beginVi, endVi),
970 function_(beginShape, endShape, 1)
972 OPENGM_ASSERT(std::distance(beginVi, endVi) == std::distance(beginShape, endShape));
976 template<
class T,
class I,
class L>
977 template<
class VARIABLE_INDEX_ITERATOR,
class SHAPE_ITERATOR>
980 VARIABLE_INDEX_ITERATOR beginVi,
981 VARIABLE_INDEX_ITERATOR endVi,
982 SHAPE_ITERATOR beginShape,
983 SHAPE_ITERATOR endShape,
984 const ValueType constant
986 : variableIndices_(beginVi, endVi),
987 function_(beginShape, endShape, constant)
989 OPENGM_ASSERT(std::distance(beginVi, endVi) == std::distance(beginShape, endShape));
993 template<
class T,
class I,
class L>
994 template<
class VARIABLE_INDEX_ITERATOR,
class SHAPE_ITERATOR>
997 VARIABLE_INDEX_ITERATOR beginVi,
998 VARIABLE_INDEX_ITERATOR endVi,
999 SHAPE_ITERATOR beginShape,
1000 SHAPE_ITERATOR endShape
1002 OPENGM_ASSERT(std::distance(beginVi, endVi) == std::distance(beginShape, endShape));
1005 function_.resize(beginShape, endShape, 1);
1006 variableIndices_.assign(beginVi, endVi);
1009 template<
class T,
class I,
class L>
1010 template<
class VARIABLE_INDEX_ITERATOR,
class SHAPE_ITERATOR>
1013 VARIABLE_INDEX_ITERATOR beginVi,
1014 VARIABLE_INDEX_ITERATOR endVi,
1015 SHAPE_ITERATOR beginShape,
1016 SHAPE_ITERATOR endShape,
1017 const ValueType constant
1019 OPENGM_ASSERT(std::distance(beginVi, endVi) == std::distance(beginShape, endShape));
1022 function_.resize(beginShape, endShape, constant);
1023 variableIndices_.assign(beginVi, endVi);
1027 template<
class T,
class I,
class L>
1028 template<
size_t FUNCTION_TYPE_INDEX>
1035 template<
class T,
class I,
class L>
1036 inline const typename IndependentFactor<T, I, L>::FunctionType&
1042 template<
class T,
class I,
class L>
1043 template<
size_t FUNCTION_TYPE_INDEX>
1050 template<
class T,
class I,
class L>
1054 const ValueType constant
1059 variableIndices_.clear();
1062 template<
class T,
class I,
class L>
1063 template<
class GRAPHICAL_MODEL,
class VARIABLE_INDEX_ITERATOR>
1066 const GRAPHICAL_MODEL& gm,
1067 VARIABLE_INDEX_ITERATOR begin,
1068 VARIABLE_INDEX_ITERATOR end
1072 this->variableIndices_.assign(begin, end);
1073 std::vector<size_t> factorShape(variableIndices_.size());
1074 for(
size_t i = 0; i < factorShape.size(); ++i) {
1075 factorShape[i] = gm.numberOfLabels(variableIndices_[i]);
1077 this->function_.assign();
1078 this->function_.resize(factorShape.begin(), factorShape.end());
1081 template<
class T,
class I,
class L>
1082 template<
class GRAPHICAL_MODEL,
class VARIABLE_INDEX_ITERATOR>
1085 const GRAPHICAL_MODEL& gm,
1086 VARIABLE_INDEX_ITERATOR begin,
1087 VARIABLE_INDEX_ITERATOR end,
1088 const ValueType value
1091 this->variableIndices_.assign(begin, end);
1092 std::vector<size_t> factorShape(variableIndices_.size());
1093 for(
size_t i = 0; i < factorShape.size(); ++i) {
1094 factorShape[i] =
static_cast<size_t> (gm.numberOfLabels(this->variableIndices_[i]));
1098 this->function_.assign();
1099 this->function_.resize(factorShape.begin(), factorShape.end(), value);
1102 template<
class T,
class I,
class L>
1103 template<
class GRAPHICAL_MODEL,
class VARIABLE_INDEX_ITERATOR>
1106 const GRAPHICAL_MODEL& gm,
1107 VARIABLE_INDEX_ITERATOR begin,
1108 VARIABLE_INDEX_ITERATOR end,
1109 const ValueType value
1111 : variableIndices_(begin, end)
1114 std::vector<size_t> shape(variableIndices_.size());
1115 for(
size_t i = 0; i < shape.size(); ++i) {
1116 shape[i] = gm.numberOfLabels(variableIndices_[i]);
1118 this->function_.assign();
1119 this->function_.resize(shape.begin(), shape.end(), value);
1122 template<
class T,
class I,
class L>
1127 : variableIndices_(src.variableIndices_)
1129 if(src.variableIndices_.size() == 0) {
1130 FunctionType tmp(src.function_(0));
1134 function_ = src.function_;
1138 template<
class T,
class I,
class L>
1139 template<
class GRAPHICAL_MODEL>
1151 ShapeWalker< typename Factor<GRAPHICAL_MODEL>::ShapeIteratorType> walker(src.
shapeBegin(), dimension);
1153 for(
size_t scalarIndex = 0; scalarIndex < function_.size(); ++scalarIndex) {
1154 function_(coordinate.begin()) = src(coordinate.begin());
1160 size_t indexToScalar[]={0};
1168 template<
class T,
class I,
class L>
1176 function_ = src.function_;
1177 variableIndices_ = src.variableIndices_;
1182 template<
class T,
class I,
class L>
1183 template<
class GRAPHICAL_MODEL>
1185 IndependentFactor<T, I, L>::operator=
1199 ShapeWalker< typename Factor<GRAPHICAL_MODEL>::ShapeIteratorType> walker(src.
shapeBegin(), dimension);
1201 for(
size_t scalarIndex = 0; scalarIndex < function_.size(); ++scalarIndex) {
1202 function_(scalarIndex) = src(coordinate.begin());
1207 size_t indexToScalar[]={0};
1213 template<
class T,
class I,
class L>
1217 return variableIndices_.size();
1221 template<
class T,
class I,
class L>
1225 const IndexType index
1229 return function_.shape(index);
1233 template<
class T,
class I,
class L>
1237 return function_.size();
1241 template<
class T,
class I,
class L>
1247 if(variableIndices_.size() == 0) {
1251 return function_.shape(index);
1254 template<
class T,
class I,
class L>
1258 return function_.shapeBegin();
1261 template<
class T,
class I,
class L>
1265 return function_.shapeEnd();
1268 template<
class T,
class I,
class L>
1272 return variableIndices_.begin();
1275 template<
class T,
class I,
class L>
1279 return variableIndices_.end();
1284 template<
class T,
class I,
class L>
1291 return variableIndices_[index];
1294 template<
class T,
class I,
class L>
1297 if(variableIndices_.size() == 0) {
1298 function_(0) =
static_cast<ValueType
> (0);
1302 std::vector<size_t> states;
1303 opengm::accumulate<Minimizer>(*
this, v, states);
1310 template<
class T,
class I,
class L>
1311 template<
class ITERATOR>
1313 IndependentFactor<T, I, L>::operator()
1317 return function_(begin);
1320 template<
class T,
class I,
class L>
1321 inline std::vector<I>&
1324 return this->variableIndices_;
1327 template<
class T,
class I,
class L>
1328 inline const std::vector<I>&
1331 return this->variableIndices_;
1335 template<
class T,
class I,
class L>
1337 IndependentFactor<T, I, L>::operator()
1342 return function_(x0);
1346 template<
class T,
class I,
class L>
1348 IndependentFactor<T, I, L>::operator()
1355 return function_(x0, x1);
1359 template<
class T,
class I,
class L>
1361 IndependentFactor<T, I, L>::operator()
1369 return function_(x0, x1, x2);
1373 template<
class T,
class I,
class L>
1375 IndependentFactor<T, I, L>::operator()
1384 return function_(x0, x1, x2, x3);
1387 template<
class T,
class I,
class L>
1388 template<
class UNARY_OPERATOR_TYPE>
1392 UNARY_OPERATOR_TYPE unaryOperator
1394 if(this->variableIndices_.size() != 0) {
1395 for(
size_t i = 0; i < function_.size(); ++i) {
1396 function_(i) =
static_cast<T
> (unaryOperator(function_(i)));
1400 function_(0) =
static_cast<T
> (unaryOperator(function_(0)));
1404 template<
class T,
class I,
class L>
1405 template<
class BINARY_OPERATOR_TYPE>
1410 BINARY_OPERATOR_TYPE binaryOperator
1412 if(this->variableIndices_.size() != 0) {
1413 for(
size_t i = 0; i < function_.size(); ++i) {
1414 function_(i) =
static_cast<T
> (binaryOperator(function_(i), value));
1418 function_(0) =
static_cast<T
> (binaryOperator(function_(0), value));
1422 template<
class T,
class I,
class L>
1423 template<
class GRAPHICAL_MODEL,
class BINARY_OPERATOR_TYPE>
1428 BINARY_OPERATOR_TYPE binaryOperator
1430 opengm::operateBinary(*
this, srcB, binaryOperator);
1433 template<
class T,
class I,
class L>
1434 template<
class BINARY_OPERATOR_TYPE>
1439 BINARY_OPERATOR_TYPE binaryOperator
1441 opengm::operateBinary(*
this, srcB, binaryOperator);
1444 template<
class T,
class I,
class L>
1445 template<
class BINARY_OPERATOR_TYPE>
1451 BINARY_OPERATOR_TYPE binaryOperator
1453 opengm::operateBinary(srcA, srcB, *
this, binaryOperator);
1456 template<
class T,
class I,
class L>
1457 template<
class ACCUMULATOR>
1462 std::vector<LabelType>& resultState
1464 opengm::accumulate<ACCUMULATOR> (*
this, result, resultState);
1467 template<
class T,
class I,
class L>
1468 template<
class ACCUMULATOR>
1474 opengm::accumulate<ACCUMULATOR> (*
this, result);
1477 template<
class T,
class I,
class L>
1478 template<
class ACCUMULATOR,
class VariablesIterator>
1482 VariablesIterator begin,
1483 VariablesIterator end,
1486 opengm::accumulate<ACCUMULATOR> (*
this, begin, end, dstFactor);
1489 template<
class T,
class I,
class L>
1490 template<
class ACCUMULATOR,
class VariablesIterator>
1493 VariablesIterator begin,
1494 VariablesIterator end
1496 opengm::accumulate<ACCUMULATOR> (*
this, begin, end);
1501 template<
class T,
class I,
class L>
1502 template<
class ITERATOR>
1507 return function_(begin);
1511 template<
class T,
class I,
class L>
1516 return function_(x0);
1520 template<
class T,
class I,
class L>
1527 return function_(x0, x1);
1531 template<
class T,
class I,
class L>
1538 return function_(x0, x1, x2);
1542 template<
class T,
class I,
class L>
1550 return function_(x0, x1, x2, x3);
1553 template<
class T,
class I,
class L>
1554 template<
class ITERATOR>
1560 for(
size_t j=0; j<variableIndices_.size(); ++j) {
1561 *out = variableIndices_[j];
1570 template<
class T,
class I,
class L>
1571 template<
class INDEX_ITERATOR,
class STATE_ITERATOR>
1575 INDEX_ITERATOR beginIndex,
1576 INDEX_ITERATOR endIndex,
1577 STATE_ITERATOR beginLabels
1579 if(this->variableIndices_.size() != 0) {
1587 while(beginIndex != endIndex) {
1589 if(*beginIndex>this->variableIndices_.back()) {
1592 for(
size_t i = counter; i<this->variableIndices_.size(); ++i) {
1593 if(*beginIndex<this->variableIndices_[i])
break;
1594 else if(*beginIndex == this->variableIndices_[i]) {
1604 for(
size_t i = 0; i<this->variableIndices_.size(); ++i) {
1606 for(
size_t j = 0; j < variablesToFix.
size(); ++j) {
1607 if(variablesToFix[j] == this->variableIndices_[i]) {
1612 if(found ==
false) {
1613 variablesNotToFix.
push_back(this->variableIndices_[i]);
1614 newShape.
push_back(this->numberOfLabels(i));
1617 if(variablesToFix.
size() != 0) {
1618 FunctionType& factorFunction = this->function_;
1619 std::vector<LabelType> fullCoordinate(this->numberOfVariables());
1620 if(variablesToFix.
size() == this->variableIndices_.size()) {
1621 FunctionType tmp(factorFunction(newStates.
begin()));
1622 factorFunction = tmp;
1623 this->variableIndices_.clear();
1631 (shapeBegin(), factorFunction.
dimension(), positionOfVariablesToFix, newStates);
1632 FunctionType tmp(newShape.
begin(), newShape.
end());
1633 const size_t subSize = subWalker.subSize();
1634 subWalker.resetCoordinate();
1635 for(
size_t i = 0; i < subSize; ++i) {
1636 tmp(i) = factorFunction(subWalker.coordinateTuple().begin());
1639 factorFunction = tmp;
1640 this->variableIndices_.
assign(variablesNotToFix.
begin(), variablesNotToFix.
end());
1649 #define OPENGM_INDEPENDENT_FACTOR_OPERATION_GENERATION(BINARY_OPERATOR_SYMBOL, BINARY_INPLACE_OPERATOR_SYMBOL, BINARY_FUNCTOR_NAME) \
1650 template<class T, class I, class L> \
1651 inline IndependentFactor<T, I, L> & \
1652 operator BINARY_INPLACE_OPERATOR_SYMBOL \
1654 IndependentFactor<T, I, L>& op1, \
1657 op1.operateBinary(op2, BINARY_FUNCTOR_NAME<T>()); \
1660 template<class GRAPHICAL_MODEL> \
1661 inline IndependentFactor<typename GRAPHICAL_MODEL::ValueType, typename GRAPHICAL_MODEL::IndexType, typename GRAPHICAL_MODEL::LabelType > & \
1662 operator BINARY_INPLACE_OPERATOR_SYMBOL \
1664 IndependentFactor<typename GRAPHICAL_MODEL::ValueType, typename GRAPHICAL_MODEL::IndexType, typename GRAPHICAL_MODEL::LabelType >& op1, \
1665 const Factor<GRAPHICAL_MODEL>& op2 \
1667 op1.operateBinary(op2, BINARY_FUNCTOR_NAME<typename GRAPHICAL_MODEL::ValueType> ()); \
1670 template<class T, class I, class L> \
1671 inline IndependentFactor<T, I, L> & \
1672 operator BINARY_INPLACE_OPERATOR_SYMBOL \
1674 IndependentFactor<T, I, L>& op1, \
1675 const IndependentFactor<T, I, L>& op2 \
1677 op1.operateBinary(op2, BINARY_FUNCTOR_NAME<T> ()); \
1680 template<class T, class I, class L> \
1681 inline IndependentFactor<T, I, L> \
1682 operator BINARY_OPERATOR_SYMBOL \
1684 const IndependentFactor<T, I, L>& op1, \
1685 const IndependentFactor<T, I, L>& op2 \
1687 IndependentFactor<T, I, L> tmp; \
1688 opengm::operateBinary(op1, op2, tmp, BINARY_FUNCTOR_NAME <T> ()); \
1691 template<class T, class I, class L> \
1692 inline IndependentFactor<T, I, L> \
1693 operator BINARY_OPERATOR_SYMBOL \
1696 const IndependentFactor<T, I, L>& op2 \
1698 IndependentFactor<T, I, L> tmp; \
1699 opengm::operateBinary(op1, op2, tmp, BINARY_FUNCTOR_NAME <T> ()); \
1702 template<class T, class I, class L> \
1703 inline IndependentFactor<T, I, L> \
1704 operator BINARY_OPERATOR_SYMBOL \
1706 const IndependentFactor<T, I, L>& op1, \
1709 IndependentFactor<T, I, L> tmp; \
1710 opengm::operateBinary(op1, op2, tmp, BINARY_FUNCTOR_NAME <T> ()); \
1713 template<class GRAPHICAL_MODEL> \
1714 inline IndependentFactor<typename GRAPHICAL_MODEL::ValueType, typename GRAPHICAL_MODEL::IndexType, typename GRAPHICAL_MODEL::LabelType > \
1715 operator BINARY_OPERATOR_SYMBOL \
1717 const Factor<GRAPHICAL_MODEL> & op1, \
1718 const IndependentFactor<typename GRAPHICAL_MODEL::ValueType, typename GRAPHICAL_MODEL::IndexType, typename GRAPHICAL_MODEL::LabelType >& op2 \
1720 IndependentFactor<typename GRAPHICAL_MODEL::ValueType, typename GRAPHICAL_MODEL::IndexType, typename GRAPHICAL_MODEL::LabelType > tmp; \
1721 opengm::operateBinary(op1, op2, tmp, BINARY_FUNCTOR_NAME <typename GRAPHICAL_MODEL::ValueType> ()); \
1724 template<class GRAPHICAL_MODEL> \
1725 inline IndependentFactor<typename GRAPHICAL_MODEL::ValueType, typename GRAPHICAL_MODEL::IndexType, typename GRAPHICAL_MODEL::LabelType > \
1726 operator BINARY_OPERATOR_SYMBOL \
1728 const IndependentFactor<typename GRAPHICAL_MODEL::ValueType, typename GRAPHICAL_MODEL::IndexType, typename GRAPHICAL_MODEL::LabelType >& op1, \
1729 const Factor<GRAPHICAL_MODEL> & op2 \
1731 IndependentFactor<typename GRAPHICAL_MODEL::ValueType, typename GRAPHICAL_MODEL::IndexType, typename GRAPHICAL_MODEL::LabelType > tmp; \
1732 opengm::operateBinary(op1, op2, tmp, BINARY_FUNCTOR_NAME <typename GRAPHICAL_MODEL::ValueType> ()); \
1735 template<class GRAPHICAL_MODEL> \
1736 inline IndependentFactor<typename GRAPHICAL_MODEL::ValueType, typename GRAPHICAL_MODEL::IndexType, typename GRAPHICAL_MODEL::LabelType > \
1737 operator BINARY_OPERATOR_SYMBOL \
1739 const Factor<GRAPHICAL_MODEL>& op1, \
1740 const Factor<GRAPHICAL_MODEL>& op2 \
1742 IndependentFactor<typename GRAPHICAL_MODEL::ValueType, typename GRAPHICAL_MODEL::IndexType, typename GRAPHICAL_MODEL::LabelType > tmp; \
1743 opengm::operateBinary(op1, op2, tmp, BINARY_FUNCTOR_NAME <typename GRAPHICAL_MODEL::ValueType> ()); \
1746 template<class GRAPHICAL_MODEL> \
1747 inline IndependentFactor<typename GRAPHICAL_MODEL::ValueType, typename GRAPHICAL_MODEL::IndexType, typename GRAPHICAL_MODEL::LabelType > \
1748 operator BINARY_OPERATOR_SYMBOL \
1750 const Factor<GRAPHICAL_MODEL>& op1, \
1751 const typename GRAPHICAL_MODEL::ValueType & op2 \
1753 IndependentFactor<typename GRAPHICAL_MODEL::ValueType, typename GRAPHICAL_MODEL::IndexType, typename GRAPHICAL_MODEL::LabelType > tmp; \
1754 opengm::operateBinary(op1, op2, tmp, BINARY_FUNCTOR_NAME <typename GRAPHICAL_MODEL::ValueType> ()); \
1757 template<class GRAPHICAL_MODEL> \
1758 inline IndependentFactor<typename GRAPHICAL_MODEL::ValueType, typename GRAPHICAL_MODEL::IndexType, typename GRAPHICAL_MODEL::LabelType > \
1759 operator BINARY_OPERATOR_SYMBOL \
1761 const typename GRAPHICAL_MODEL::ValueType & op1, \
1762 const Factor<GRAPHICAL_MODEL>& op2 \
1764 IndependentFactor<typename GRAPHICAL_MODEL::ValueType, typename GRAPHICAL_MODEL::IndexType, typename GRAPHICAL_MODEL::LabelType > tmp; \
1765 opengm::operateBinary(op1, op2, tmp, BINARY_FUNCTOR_NAME <typename GRAPHICAL_MODEL::ValueType> ()); \
1778 namespace functionwrapper {
1780 namespace executor {
1782 template<
size_t IX,
size_t DX>
1783 template<
class GM,
class FACTOR>
1784 void FactorInvariant<IX, DX, false>::op
1789 typedef typename GM::IndexType IndexType;
1790 typedef typename GM::LabelType LabelType;
1791 if(factor.functionType() == IX) {
1792 const IndexType functionIndex =
static_cast<IndexType
>(factor.functionIndex());
1793 const size_t numVar =
static_cast<size_t>(factor.numberOfVariables());
1794 const size_t dimFunction =
static_cast<size_t>(meta::FieldAccess::template byIndex<IX> (gm.functionDataField_).functionData_.functions_[functionIndex].dimension());
1795 const IndexType numberOfFunctions =
static_cast<IndexType
>(meta::FieldAccess::template byIndex<IX> (gm.functionDataField_).functionData_.functions_.size());
1798 "function index must be smaller than numberOfFunctions for that given function type")
1800 "number of variable indices of the factor must match the functions dimension")
1801 for(
size_t i = 0; i < numVar; ++i) {
1802 const LabelType numberOfLabelsOfFunction = meta::FieldAccess::template byIndex<IX> (gm.functionDataField_).functionData_.functions_[functionIndex].shape(i);
1803 OPENGM_CHECK_OP(factor.numberOfLabels(i) , == , numberOfLabelsOfFunction,
1804 "number of labels of the variables in a factor must match the functions shape")
1810 meta::Increment<IX>::value,
1812 meta::EqualNumber< meta::Increment<IX>::value, DX>::value
1817 template<
size_t IX,
size_t DX>
1818 template<
class GM,
class FACTOR>
1819 void FactorInvariant<IX, DX, true>::op
1834 #endif // #ifndef OPENGM_GRAPHICALMODEL_FACTOR_HXX
void assign(const allocator_type &=allocator_type())
Clear Marray.
void accumulate(ValueType &, std::vector< LabelType > &) const
VariablesIteratorType variableIndicesEnd() const
size_t numberOfVariables() const
void forAtLeastAllUniqueValues(FUNCTOR &functor) const
call a functor for at least all unique values in no defined order
bool isTruncatedAbsoluteDifference() const
VariablesIteratorType variableIndicesBegin() const
Factor (with corresponding function and variable indices), independent of a GraphicalModel.
bool binaryProperty() const
compute a binary property of a factor
IndependentFactor & operator=(const IndependentFactor &)
bool isTruncatedSquaredDifference() const
void forAllValuesInSwitchedOrder(FUNCTOR &functor) const
IndexType dimension() const
detail_types::UInt8Type UInt8Type
SizeT.
UInt8Type functionType() const
bool isSquaredDifference() const
bool isLinearConstraint() const
bool isSquaredDifference()
ShapeIteratorType shapeEnd() const
void variableIndices(ITERATOR out) const
GraphicalModelType::LabelType LabelType
const FunctionType & function() const
void push_back(const T &)
append a value
bool isTruncatedAbsoluteDifference() const
bool isTruncatedSquaredDifference()
bool isAbsoluteDifference()
void load(GM_ &gm, const std::string &, const std::string &)
ShapeIteratorType shapeBegin() const
void copyValuesSwitchedOrder(ITERATOR iterator) const
GraphicalModelType::ValueType ValueType
Vector that stores values on the stack if size is smaller than MAX_STACK.
T const * end() const
end iterator
VectorType::const_iterator const_iterator
VariablesIteratorType variableIndicesBegin() const
GraphicalModelType::FunctionTypeList FunctionTypeList
#define OPENGM_ASSERT(expression)
GRAPHICAL_MODEL * GraphicalModelPointerType
ReturnType product() const
IndexType variableIndex(const size_t) const
return the index of the j-th variable
std::vector< IndexType >::const_iterator VariablesIteratorType
IndexType shape(const size_t dimIndex) const
return the extension of the value table of the of the function in a specific dimension ...
VariablesIteratorType variableIndicesEnd() const
void forAllValuesInOrder(FUNCTOR &functor) const
call a functor for all values in last coordinate major order
#define OPENGM_META_ASSERT(assertion, msg)
opengm compile time assertion
void assign(VARIABLE_INDEX_ITERATOR, VARIABLE_INDEX_ITERATOR, SHAPE_ITERATOR, SHAPE_ITERATOR)
opengm::AccessorIterator< ShapeAccessorType, true > ShapeIteratorType
Abstraction (wrapper class) of factors, independent of the function used to implement the factor...
IndexType numberOfVariables() const
GraphicalModelType::IndexType IndexType
const size_t dimension() const
Get the dimension.
void callFunctor(FUNCTOR &f) const
call a functor for the function of the factor the first and only argument passed to the functor is th...
ExplicitFunction< ValueType, IndexType, LabelType > FunctionType
const std::vector< IndexType > & variableIndexSequence() const
bool isAbsoluteDifference() const
ValueType product() const
bool isGeneralizedPotts() const
#define OPENGM_INDEPENDENT_FACTOR_OPERATION_GENERATION(BINARY_OPERATOR_SYMBOL, BINARY_INPLACE_OPERATOR_SYMBOL, BINARY_FUNCTOR_NAME)
ViFunctor(const FACTOR &factor, FUNCTOR &functor)
void operateBinary(const T value, BINARY_OPERATOR_TYPE binaryOperator)
bool isGeneralizedPotts()
VisContainerType::const_iterator VariablesIteratorType
bool isSubmodular() const
meta::TypeListGenerator< FunctionType >::type FunctionTypeList
bool isAbsoluteDifference() const
std::vector< IndexType > VisContainerType
void callViFunctor(FUNCTOR &f) const
call a functor for the function of the factor the first to arguments passed to the functor are a begi...
void variableIndices(ITERATOR) const
void fixVariables(INDEX_ITERATOR, INDEX_ITERATOR, STATE_ITERATOR)
assign specific labels to a specific subset of variables (reduces the order)
IndexType variableIndex(const IndexType) const
return the index of the j-th variable
ValueType operator()(ITERATOR) const
evaluate the factor for a sequence of labels
bool isTruncatedAbsoluteDifference()
#define OPENGM_CHECK_OP(A, OP, B, TXT)
FACTOR::VariablesIteratorType ViIterator
T const * begin() const
begin iterator
GRAPHICAL_MODEL GraphicalModelType
const size_t * ShapeIteratorType
bool isSubmodular() const
const meta::TypeAtTypeList< FunctionTypeList, FUNCTION_TYPE_INDEX >::type & function() const
IndexType functionIndex() const
void forAllValuesInAnyOrder(FUNCTOR &functor) const
call a functor for all values in no defined order
Factor & operator=(const Factor &)
void copyValues(ITERATOR iterator) const
copies the values of a factors into an iterator
bool isSquaredDifference() const
void operateUnary(UNARY_OPERATOR_TYPE unaryOperator)
void operator()(const FUNCTION &function)
bool isGeneralizedPotts() const
bool isTruncatedSquaredDifference() const
IndexType numberOfLabels(const IndexType) const
return the number of labels of a specific variable
ValueType valueProperty() const
VectorView< std::vector< IndexType >, IndexType > VisContainerType
ShapeIteratorType shapeEnd() const
ShapeIteratorType shapeBegin() const
size_t size() const
return the number of entries of the value table of the function
void save(const GM &, const std::string &, const std::string &)
save a graphical model to an HDF5 file
T operator()(ITERATOR) const
evaluate the function underlying the factor, given labels to be assigned the variables ...
IndexType numberOfLabels(const IndexType) const
return the number of labels of the j-th variable
IndexType shape(const IndexType) const
return the extension a value table encoding this factor would have in the dimension of the j-th varia...