2 #ifndef OPENGM_DUALDDECOMPOSITION_SUBGRADIENT_HXX 3 #define OPENGM_DUALDDECOMPOSITION_SUBGRADIENT_HXX 22 template<
class GM,
class INF,
class DUALBLOCK >
52 template<
class _GM,
class _ACC>
65 Parameter() : useAdaptiveStepsize_(false), useProjectedAdaptiveStepsize_(false){};
69 : subPara_(p.subPara_),
70 useAdaptiveStepsize_(p.useAdaptiveStepsize_),
71 useProjectedAdaptiveStepsize_(p.useProjectedAdaptiveStepsize_){
84 virtual std::string
name()
const {
return "DualDecompositionSubGradient";};
93 virtual void allocate();
95 void dualStep(
const size_t);
96 template <
class T_IndexType,
class T_LabelType>
97 void getPartialSubGradient(
const size_t,
const std::vector<T_IndexType>&, std::vector<T_LabelType>&)
const;
98 double euclideanProjectedSubGradientNorm();
101 std::vector<std::vector<LabelType> > subStates_;
103 Accumulation<ValueType,LabelType,AccumulationType> acUpperBound_;
104 Accumulation<ValueType,LabelType,AccumulationType> acNegLowerBound_;
107 std::vector<ValueType> values_;
110 std::vector<ValueType> mem_;
120 template<
class GM,
class INF,
class DUALBLOCK>
125 subStates_.resize(
subGm_.size());
126 for(
size_t i=0; i<
subGm_.size(); ++i)
127 subStates_[i].resize(
subGm_[i].numberOfVariables());
130 template<
class GM,
class INF,
class DUALBLOCK>
135 subStates_.resize(
subGm_.size());
136 for(
size_t i=0; i<
subGm_.size(); ++i)
137 subStates_[i].resize(
subGm_[i].numberOfVariables());
143 template <
class GM,
class INF,
class DUALBLOCK>
146 if(DDIsView<DualVariableType>::isView()){
154 for(
size_t i=0; i<(*it).duals_.size(); ++i){
157 if(DDIsView<DualVariableType>::isView())
162 template <
class GM,
class INF,
class DUALBLOCK>
169 template<
class GM,
class INF,
class DUALBLOCK>
174 return infer(visitor);
176 template<
class GM,
class INF,
class DUALBLOCK>
177 template<
class VISITOR>
181 std::cout.precision(15);
183 visitor.begin(*
this);
193 for(
size_t subModelId=0; subModelId<
subGm_.size(); ++subModelId){
196 inf.arg(subStates_[subModelId]);
202 std::vector<LabelType> temp;
203 std::vector<LabelType> temp2;
204 const std::vector<SubVariableListType>& subVariableLists = para_.
decomposition_.getVariableLists();
205 (*this).template getBounds<AccumulationType>(subStates_, subVariableLists, lowerBound_, upperBound_, temp);
206 acNegLowerBound_(-lowerBound_,temp2);
207 acUpperBound_(upperBound_, temp);
212 stepsize = para_.
stepsizeStride_ * fabs(acUpperBound_.value() - lowerBound_)
213 /(*this).subGradientNorm(1);
216 double subgradientNorm = euclideanProjectedSubGradientNorm();
217 stepsize = para_.
stepsizeStride_ * fabs(acUpperBound_.value() - lowerBound_)
218 /subgradientNorm/subgradientNorm;
221 double primalDualGap = fabs(acUpperBound_.value() + acNegLowerBound_.value());
222 double subgradientNorm = euclideanProjectedSubGradientNorm();
223 stepsize = para_.
getStepsize(iteration,primalDualGap,subgradientNorm);
232 std::vector<size_t> s;
233 for(
typename std::vector<DualBlockType>::const_iterator it =
dualBlocks_.begin(); it !=
dualBlocks_.end(); ++it){
234 const size_t numDuals = (*it).duals_.size();
235 typename SubFactorListType::const_iterator lit = (*((*it).subFactorList_)).begin();
236 s.resize((*lit).subIndices_.size());
237 for(
size_t i=0; i<numDuals; ++i){
238 getPartialSubGradient<size_t>((*lit).subModelId_, (*lit).subIndices_, s);
240 (*it).duals_[i](s.begin()) += stepsize;
241 for(
size_t j=0; j<numDuals; ++j){
242 (*it).duals_[j](s.begin()) -= stepsize/numDuals;
251 if(visitor(*
this)!= 0){
259 AccumulationType::iop(0.0001,-0.0001,o);
260 OPENGM_ASSERT(AccumulationType::bop(lowerBound_, upperBound_+o));
261 OPENGM_ASSERT(AccumulationType::bop(-acNegLowerBound_.value(), acUpperBound_.value()+o));
264 || fabs((acUpperBound_.value()+ acNegLowerBound_.value())/acUpperBound_.value()) <= para_.
minimalRelAccuracy_){
278 template<
class GM,
class INF,
class DUALBLOCK>
280 arg(std::vector<LabelType>& conf,
const size_t n)
const 286 acUpperBound_.state(conf);
291 template<
class GM,
class INF,
class DUALBLOCK>
294 return acUpperBound_.value();
297 template<
class GM,
class INF,
class DUALBLOCK>
300 return -acNegLowerBound_.value();
306 template <
class GM,
class INF,
class DUALBLOCK>
312 template <
class GM,
class INF,
class DUALBLOCK>
313 template <
class T_IndexType,
class T_LabelType>
316 const size_t subModelId,
317 const std::vector<T_IndexType>& subIndices,
318 std::vector<T_LabelType> & s
322 for(
size_t n=0; n<s.size(); ++n){
323 s[n] = subStates_[subModelId][subIndices[n]];
327 template <
class GM,
class INF,
class DUALBLOCK>
331 std::vector<LabelType> s;
333 typename std::vector<DUALBLOCK>::const_iterator it;
334 typename SubFactorListType::const_iterator lit;
336 const size_t numDuals = (*it).duals_.
size();
338 lit = (*((*it).subFactorList_)).begin();
339 s.resize((*lit).subIndices_.size());
340 for(
size_t i=0; i<numDuals; ++i){
341 getPartialSubGradient((*lit).subModelId_, (*lit).subIndices_, s);
345 for(
size_t i=0; i<M.
size(); ++i){
346 norm += M(i) * pow(1.0 - M(i)/numDuals,2);
347 norm += (numDuals-M(i)) * pow( M(i)/numDuals,2);
double minimalRelAccuracy_
the relative accuracy that has to be guaranteed to stop with an approximate solution (set 0 for optim...
virtual ValueType value() const
return the solution (value)
virtual ValueType bound() const
return a bound on the solution
virtual InferenceTermination arg(std::vector< LabelType > &, const size_t=1) const
InfType::Parameter subPara_
Parameter for Subproblems.
std::vector< DualBlockType > dualBlocks_
INF::AccumulationType AccumulationType
DecompositionType::SubVariable SubVariableType
DecompositionType::SubVariableListType SubVariableListType
DualDecompositionBase< GmType, DualBlockType > DDBaseType
size_t numDualsOvercomplete_
Platform-independent runtime measurements.
double minimalAbsAccuracy_
the absolut accuracy that has to be guaranteed to stop with an approximate solution (set 0 for optima...
#define OPENGM_ASSERT(expression)
visitors::TimingVisitor< DualDecompositionSubGradient< GM, INF, DUALBLOCK > > TimingVisitorType
bool useAdaptiveStepsize_
GraphicalModelDecomposition decomposition_
decomposition of the model (needs to fit to the model structure)
DualBlockType::SubFactorType SubFactorType
std::vector< SubGmType > subGm_
void init(DualDecompositionBaseParameter &)
DualDecompositionSubGradient< _GM, RebindedInf, DUALBLOCK > type
virtual const GmType & graphicalModel() const
virtual InferenceTermination infer()
DualDecompositionSubGradient(const GmType &)
DDBaseType::SubGmType SubGmType
DDBaseType::SubVariableListType SubVariableListType
INF::template RebindGm< _GM, _ACC >::type RebindedInf
Dual-Decomposition-Subgradient Inference based on dual decomposition using sub-gradient descent Refe...
virtual std::string name() const
void DualVarAssign(DUALVAR &dv, T *t)
GraphicalModelType::ValueType ValueType
Inference algorithm interface.
DualBlockType::SubFactorListType SubFactorListType
bool useProjectedAdaptiveStepsize_
double getStepsize(size_t iteration, double primalDualGap, double subgradientNorm)
DualDecompositionSubGradient< _GM, RebindedInf, DUALBLOCK > type
Runtime-flexible multi-dimensional array.
DualBlockType::DualVariableType DualVariableType
DualBlockType::DualVariableType DualVariableType
DDBaseType::SubVariableType SubVariableType
Minimization as a unary accumulation.
size_t maximalNumberOfIterations_
maximum number of dual iterations
INF::template RebindGm< _GM >::type RebindedInf
visitors::EmptyVisitor< DualDecompositionSubGradient< GM, INF, DUALBLOCK > > EmptyVisitorType
visitors::VerboseVisitor< DualDecompositionSubGradient< GM, INF, DUALBLOCK > > VerboseVisitorType
A framework for inference algorithms based on Lagrangian decomposition.
const size_t size() const
Get the number of data items.