/usr/include/opengm/utilities/canonical_view.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 | //
// File: canonical_view.hxx
//
// This file is part of OpenGM.
//
// Copyright (C) 2015 Stefan Haller
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
#pragma once
#ifndef OPENGM_UTILITIES_CANONICAL_FACTORS_HXX
#define OPENGM_UTILITIES_CANONICAL_FACTORS_HXX
#include <map>
#include <vector>
#include <opengm/functions/accumulated_view.hxx>
#include <opengm/functions/constant.hxx>
#include <opengm/functions/explicit_function.hxx>
#include <opengm/graphicalmodel/graphicalmodel.hxx>
#include <opengm/utilities/metaprogramming.hxx>
namespace opengm {
namespace canonical_view {
/// \brief Controls cloning behavior of a CanonicalView.
///
/// To reduce the overhead of view functions, CanonicalView is extended to
/// allow cloning the calculated functions into ExplicitFunctions.
///
/// A template argument handles the different cases:
///
/// (1) CloneNever: Original behavior, no functions will be cloned. Original
/// functions get injected directly whenever possible.
///
/// (2) CloneViews: Only the view functions will be cloned using
/// ExplicitFunctions. Original functions get injected whenever
/// possible.
///
/// (3) CloneDeep: All functions get cloned.
enum CloneOption { CloneNever, CloneViews, CloneDeep };
} // namespace canonical_view
/// \cond HIDDEN_SYMBOLS
namespace canonical_view_internal {
// The type of the GraphicalModel is complicated, but can’t be typedef’d
// easilty. That’s why we use this handy type generator.
template<class GM>
struct Generator {
private:
typedef typename GM::ValueType ValType;
typedef typename GM::IndexType IndType;
typedef typename GM::LabelType LabType;
typedef typename GM::FunctionTypeList OldTypeList;
// We extend the type list of the old model and add some more
// functions.
typedef ConstantFunction<ValType, IndType, LabType> ConstFunType;
typedef AccumulatedViewFunction<GM> AccViewType;
typedef typename meta::TypeListGenerator<ConstFunType, AccViewType>::type NewTypeList;
public:
typedef GraphicalModel<
typename GM::ValueType,
typename GM::OperatorType,
typename meta::MergeTypeListsWithoutDups<OldTypeList, NewTypeList>::type,
typename GM::SpaceType
> type;
};
// Helper for perform the actual cloning (if requested by template
// parameter). The first GM parameter is expected be the CanonicalView.
template<class GM, canonical_view::CloneOption CLONE>
struct CloneHelper;
template<class GM>
struct CloneHelper<GM, canonical_view::CloneNever> {
template<class FUNC>
static const FUNC& handleInjected(const FUNC &func) { return func; }
template<class FUNC>
static const FUNC& handleView(const FUNC &func) { return func; }
};
template<class GM>
struct CloneHelper<GM, canonical_view::CloneViews> {
template<class FUNC>
static const FUNC& handleInjected(const FUNC &func) { return func; }
template<class FUNC>
static typename GM::ExplFuncType handleView(const FUNC &func)
{
typename GM::ExplFuncType result;
cloneAsExplicitFunction(func, result);
return result;
}
};
template<class GM>
struct CloneHelper<GM, canonical_view::CloneDeep> {
template<class FUNC>
static typename GM::ExplFuncType handleInjected(const FUNC &func)
{
typename GM::ExplFuncType result;
cloneAsExplicitFunction(func, result);
return result;
}
template<class FUNC>
static typename GM::ExplFuncType handleView(const FUNC &func)
{
typename GM::ExplFuncType result;
cloneAsExplicitFunction(func, result);
return result;
}
};
// This functor is run on the factor of the wrapped GraphicalModel. It
// will reinject the original function into the wrapper for the
// GraphicalModel.
template<class GM, canonical_view::CloneOption CLONE>
class FunctionInjectionFunctor {
public:
FunctionInjectionFunctor(GM &gm)
: gm_(&gm)
{
}
template<class FUNCTION>
void operator()(FUNCTION &func)
{
result_ = gm_->addFunction(CloneHelper<GM, CLONE>::handleInjected(func));
}
typename GM::FunctionIdentifier result() const
{
return result_;
}
private:
GM *gm_;
typename GM::FunctionIdentifier result_;
};
// This class injects a function of the wrapped GraphicalModel. Additional
// it uses a map to remember already injected functions. One original
// function is thus only inserted into the wrapper once.
template<class WRAPPER, class WRAPPED, canonical_view::CloneOption CLONE>
class FunctionInjector {
public:
FunctionInjector
(
WRAPPER &gm
)
: gm_(&gm)
{
}
typename WRAPPER::FunctionIdentifier
inject
(
const typename WRAPPED::FactorType &factor
)
{
typename WRAPPED::FunctionIdentifier id(factor.functionIndex(), factor.functionType());
typename MapType::const_iterator it = map_.find(id);
if (it != map_.end())
return it->second;
FunctionInjectionFunctor<WRAPPER, CLONE> injector(*gm_);
factor.callFunctor(injector);
typename WRAPPER::FunctionIdentifier result = injector.result();
map_[id] = result;
return result;
}
private:
typedef std::map<
typename WRAPPED::FunctionIdentifier,
typename WRAPPER::FunctionIdentifier
> MapType;
WRAPPER *gm_;
MapType map_;
};
} // namespace canonical_view_internal
/// \endcond HIDDEN_SYMBOLS
/// \brief Canonical view of an arbitrary GraphicalModel
///
/// This class wraps an arbitrary GraphicalModel and acts as a view on the
/// model. The original model is changed with respect to the following aspects:
///
/// - all variables are associated with exactly one unary factor (multiple
/// unary factors get squashed into one, a non-existent unary factor is
/// mapped to a zero-constant factor)
///
/// - there is at most one factor for a given set of variables (multiple
/// factors attached to the clique are squashed into one factor)
template<class GM, canonical_view::CloneOption CLONE = canonical_view::CloneNever>
class CanonicalView : public canonical_view_internal::Generator<GM>::type {
public:
typedef typename canonical_view_internal::Generator<GM>::type Parent;
typedef CanonicalView<GM, CLONE> MyType;
typedef typename Parent::IndexType IndexType;
typedef typename Parent::LabelType LabelType;
typedef typename Parent::ValueType ValueType;
typedef typename Parent::FunctionIdentifier FunctionIdentifier;
typedef ExplicitFunction<ValueType, IndexType, LabelType> ExplFuncType;
typedef ConstantFunction<ValueType, IndexType, LabelType> ConstFuncType;
typedef AccumulatedViewFunction<GM> ViewFuncType;
typedef canonical_view_internal::FunctionInjector<MyType, GM, CLONE> FunctionInjectorType;
typedef canonical_view_internal::CloneHelper<MyType, CLONE> CloneHelperType;
CanonicalView(const GM &gm)
: Parent(gm.space())
{
// FIXME: Use opengm::FastSequence, but operator< is missing. :-(
typedef std::vector<IndexType> Variables;
typedef std::vector<const typename GM::FactorType*> Factors;
typedef std::vector<Factors> UnaryFactors;
typedef std::map<Variables, Factors> FactorMap;
typedef std::map<FunctionIdentifier, typename GM::FunctionIdentifier> FunctionMap;
FunctionInjectorType injector(*this);
UnaryFactors unaryFactors(gm.numberOfVariables());
FactorMap otherFactors;
// Append all unary factors to the corresponding unary factor vector.
// All other factors are inserted into the factor map. The keys are the
// variable indices of the factor, so we group factors of the same
// variables together.
for (IndexType i = 0; i < gm.numberOfFactors(); ++i) {
const typename GM::FactorType &f = gm[i];
if (f.numberOfVariables() == 1) {
unaryFactors[f.variableIndex(0)].push_back(&f);
} else {
std::vector<IndexType> vars(f.variableIndicesBegin(), f.variableIndicesEnd());
otherFactors[vars].push_back(&f);
}
}
// Associate each variable with *exactly* one unary factor. (Create
// an empty factor if missing.)
for (IndexType i = 0; i < gm.numberOfVariables(); ++i) {
FunctionIdentifier fid;
IndexType vars[1] = { i };
// Note: Use of curly braces to create new scope.
switch (unaryFactors[i].size()) {
case 0: { // Create new “empty” factor.
LabelType shape[1] = { gm.numberOfLabels(i) };
ConstFuncType func(shape, shape+1, 0);
fid = this->addFunction(func);
break;
}
case 1: { // Reuse old function.
fid = injector.inject(*unaryFactors[i][0]);
break;
}
default: { // Create a new view function.
ViewFuncType func(unaryFactors[i].begin(), unaryFactors[i].end());
fid = this->addFunction(CloneHelperType::handleView(func));
break;
}
}
this->addFactor(fid, vars, vars+1);
}
// Accumulate all other factors (with order != 1).
for (typename FactorMap::const_iterator it = otherFactors.begin(); it != otherFactors.end(); ++it) {
const Variables &vars= it ->first;
const Factors &factors = it->second;
FunctionIdentifier fid;
OPENGM_ASSERT_OP(factors.size(), >, 0);
if (factors.size() == 1) {
// Reuse old function.
fid = injector.inject(*factors[0]);
} else {
// Create new view function.
ViewFuncType func(it->second.begin(), it->second.end());
fid = this->addFunction(CloneHelperType::handleView(func));
}
this->addFactor(fid, vars.begin(), vars.end());
}
}
};
} // namespace opengm
#endif
|