/usr/include/opengm/inference/hqpbo.hxx is in libopengm-dev 2.3.6+20160905-1build2.
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 | #pragma once
#ifndef OPENGM_HQPBO_HXX
#define OPENGM_HQPBO_HXX
#include "opengm/graphicalmodel/graphicalmodel_factor.hxx"
#include "opengm/graphicalmodel/graphicalmodel.hxx"
#include "opengm/operations/adder.hxx"
#include "opengm/operations/minimizer.hxx"
#include "opengm/inference/inference.hxx"
#include "opengm/inference/visitors/visitors.hxx"
#include "opengm/inference/external/qpbo.hxx"
#include "opengm/inference/fix-fusion/fusion-move.hpp"
namespace opengm {
/// HQPBO Algorithm\n\n
///
///
/// \ingroup inference
template<class GM, class ACC>
class HQPBO : public Inference<GM, opengm::Minimizer>
{
public:
typedef GM GraphicalModelType;
typedef ACC AccumulationType;
OPENGM_GM_TYPE_TYPEDEFS;
typedef visitors::VerboseVisitor<HQPBO<GM,ACC> > VerboseVisitorType;
typedef visitors::TimingVisitor<HQPBO<GM,ACC> > TimingVisitorType;
typedef visitors::EmptyVisitor<HQPBO<GM,ACC> > EmptyVisitorType;
template<class _GM>
struct RebindGm{
typedef HQPBO<_GM, ACC> type;
};
template<class _GM,class _ACC>
struct RebindGmAndAcc{
typedef HQPBO<_GM, _ACC > type;
};
struct Parameter {
Parameter(){
}
template<class P>
Parameter(const P & p){
}
};
HQPBO(const GraphicalModelType&, Parameter = Parameter());
std::string name() const;
const GraphicalModelType& graphicalModel() const;
InferenceTermination infer();
template<class VISITOR>
InferenceTermination infer(VISITOR &);
InferenceTermination arg(std::vector<LabelType>&, const size_t& = 1) const;
void setStartingPoint(typename std::vector<LabelType>::const_iterator begin );
private:
const GraphicalModelType& gm_;
ValueType constV_;
HigherOrderEnergy<ValueType, 10> hoe_;
std::vector<LabelType> conf_;
ValueType bound_;
};
template<class GM, class ACC>
inline void
HQPBO<GM,ACC>::setStartingPoint
(
typename std::vector<typename HQPBO<GM,ACC>::LabelType>::const_iterator begin
) {
for (size_t i=0; i<gm_.numberOfVariables(); ++i)
conf_[i] = *(begin+i);
}
template<class GM,class ACC>
HQPBO<GM,ACC>::HQPBO
(
const GM & gm,
typename HQPBO<GM,ACC>::Parameter
)
: gm_(gm), constV_(0.0), conf_(std::vector<LabelType>(gm.numberOfVariables(),0))
{
hoe_.AddVars(gm_.numberOfVariables());
for (IndexType f = 0; f < gm_.numberOfFactors(); ++f)
{
IndexType size = gm_[f].numberOfVariables();
const LabelType l0 = 0;
const LabelType l1 = 1;
if (size == 0)
{
constV_ += gm_[f](&l0);
continue;
}
else if (size == 1)
{
IndexType var = gm_[f].variableIndex(0);
const ValueType e0 = gm_[f](&l0);
const ValueType e1 = gm_[f](&l1);
hoe_.AddUnaryTerm(var, e1 - e0);
}
else
{
unsigned int numAssignments = 1 << size;
AutoDeleteArray<ValueType> coeffs_array(new ValueType[numAssignments]);
ValueType * coeffs = coeffs_array.get();
for (unsigned int subset = 1; subset < numAssignments; ++subset)
{
coeffs[subset] = 0;
}
// For each boolean assignment, get the clique energy at the
// corresponding labeling
AutoDeleteArray<LabelType> cliqueLabels_array(new LabelType[size]);
LabelType * cliqueLabels = cliqueLabels_array.get();
for (unsigned int assignment = 0; assignment < numAssignments; ++assignment)
{
for (unsigned int i = 0; i < size; ++i)
{
if (assignment & (1 << i))
{
cliqueLabels[i] = l1;
}
else
{
cliqueLabels[i] = l0;
}
}
ValueType energy = gm_[f](cliqueLabels);
for (unsigned int subset = 1; subset < numAssignments; ++subset)
{
if (assignment & ~subset)
{
continue;
}
else
{
int parity = 0;
for (unsigned int b = 0; b < size; ++b)
{
parity ^= (((assignment ^ subset) & (1 << b)) != 0);
}
coeffs[subset] += parity ? -energy : energy;
}
}
}
typename HigherOrderEnergy<ValueType, 10>::VarId vars[10];
for (unsigned int subset = 1; subset < numAssignments; ++subset)
{
int degree = 0;
for (unsigned int b = 0; b < size; ++b)
{
if (subset & (1 << b))
{
vars[degree++] = gm_[f].variableIndex(b);
}
}
std::sort(vars, vars + degree);
hoe_.AddTerm(coeffs[subset], degree, vars);
}
}
}
}
template<class GM,class ACC>
inline std::string
HQPBO<GM,ACC>::name() const
{
return "HQPBO";
}
template<class GM,class ACC>
inline const typename HQPBO<GM,ACC>::GraphicalModelType&
HQPBO<GM,ACC>::graphicalModel() const
{
return gm_;
}
template<class GM,class ACC>
inline InferenceTermination
HQPBO<GM,ACC>::infer() {
EmptyVisitorType v;
return infer(v);
}
template<class GM,class ACC>
template<class VISITOR>
inline InferenceTermination
HQPBO<GM,ACC>::infer(VISITOR & visitor)
{
visitor.begin(*this);
kolmogorov::qpbo::QPBO<ValueType> qr(gm_.numberOfVariables(), 0);
hoe_.ToQuadratic(qr);
qr.Solve();
IndexType numberOfChangedVariables = 0;
for (IndexType i = 0; i < gm_.numberOfVariables(); ++i)
{
int label = qr.GetLabel(i);
if (label == 0 )
{
conf_[i] = 0;
}
else if (label == 1)
{
conf_[i] = 1;
}
else
{
//conf_[i] = 0;
}
}
bound_ = constV_ + 0.5 * qr.ComputeTwiceLowerBound();
visitor.end(*this);
return NORMAL;
}
template<class GM,class ACC>
inline InferenceTermination
HQPBO<GM,ACC>::arg
(
std::vector<LabelType>& arg,
const size_t& n
) const
{
if(n > 1) {
return UNKNOWN;
}
else {
arg.resize(gm_.numberOfVariables());
for (IndexType i = 0; i < gm_.numberOfVariables(); ++i)
arg[i] =conf_[i];
return NORMAL;
}
}
} // namespace opengm
#endif // #ifndef OPENGM_HQPBO_HXX
|