Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnTableFactor.h
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnTableFactor.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <cstdlib>
16 #include <cassert>
17 #include <iostream>
18 #include <fstream>
19 #include <iomanip>
20 #include <vector>
21 #include <map>
22 #include <list>
23 
24 #include "Eigen/Core"
25 
26 #include "drwnBase.h"
27 #include "drwnIO.h"
28 #include "drwnVarUniverse.h"
29 #include "drwnVarAssignment.h"
30 
31 using namespace std;
32 using namespace Eigen;
33 
34 // drwnFactor --------------------------------------------------------------
39 
40 class drwnFactor : public drwnStdObjIface {
41  public:
42 #ifdef DRWN_FACTOR_DEBUG_STATISTICS
43  static unsigned _dbStatsRefCount;
44  static unsigned _dbStatsProductCount;
45  static unsigned _dbStatsDivideCount;
46  static unsigned _dbStatsAdditionCount;
47  static unsigned _dbStatsSubtractionCount;
48  static unsigned _dbStatsMarginalCount;
49  static unsigned _dbStatsMaxMinCount;
50  static unsigned _dbStatsReductionCount;
51  static unsigned _dbStatsNormalizeCount;
52  static unsigned _dbStatsNormalizeErrors;
53  static unsigned _dbStatsLargestFactorSize;
54  static unsigned _dbStatsCurrentMem;
55  static unsigned _dbStatsMaxMem;
56  static unsigned _dbStatsMaxTable;
57  static unsigned _dbStatsTotalMem;
58 #endif
59 
60  protected:
62  vector<int> _variables;
63  map<int, int> _varIndex;
64 
65  public:
67  drwnFactor(const drwnVarUniversePtr& ptr);
69  drwnFactor(const drwnFactor& psi);
70  virtual ~drwnFactor();
71 
72  // access functions
73  virtual drwnFactor *clone() const = 0;
74 
76  bool empty() const { return _variables.empty(); }
78  size_t size() const { return _variables.size(); }
80  virtual size_t memory() const { return 0; }
81 
83  const drwnVarUniversePtr& getUniverse() const { return _pUniverse; }
85  drwnVarUniverse getSubUniverse() const;
87  bool hasVariable(int var) const { return (_varIndex.find(var) != _varIndex.end()); }
89  int varId(int indx) const { return _variables[indx]; }
91  int varCard(int indx) const { return _pUniverse->varCardinality(_variables[indx]); }
92 
94  virtual void addVariable(int var);
96  void addVariable(const char *name);
98  void addVariables(const char *name, ...);
99  void addVariables(const vector<int>& c);
100  void addVariables(const drwnClique& c);
101  void addVariables(const drwnFactor& psi);
102 
104  const vector<int>& getOrderedVars() const { return _variables; }
106  drwnClique getClique() const { return drwnClique(_variables.begin(), _variables.end()); }
107 
108  // i/o
109  bool save(drwnXMLNode& xml) const;
110  bool load(drwnXMLNode& xml);
111 
113  virtual double getValueOf(const drwnFullAssignment& y) const = 0;
116  double getValueOf(const drwnPartialAssignment& y) const {
117  return getValueOf((drwnFullAssignment)y);
118  }
120  virtual void setValueOf(const drwnFullAssignment& y, double val) = 0;
123  void setValueOf(const drwnPartialAssignment& y, double val) {
124  setValueOf(drwnFullAssignment(y), val);
125  }
126 
127  protected:
128 #ifdef DRWN_FACTOR_DEBUG_STATISTICS
129  static void updateMemoryStats(int nSize, int previousSize = 0);
130 #endif
131 };
132 
133 // drwnTableFactor --------------------------------------------------------
134 
136 
143 
144 class drwnTableFactor : public drwnFactor {
145  protected:
146  friend class drwnTableFactorStorage;
147 
148  std::vector<int> _stride;
149 
150  int _nSize;
151  double *_data;
153 
154  public:
155  drwnTableFactor(const drwnVarUniversePtr& ptr, drwnTableFactorStorage *storage = NULL);
156  drwnTableFactor(const drwnTableFactor& psi);
157  virtual ~drwnTableFactor();
158 
159  // access functions
160  const char *type() const { return "drwnTableFactor"; }
161  drwnTableFactor *clone() const { return new drwnTableFactor(*this); }
162 
163  size_t memory() const { return _nSize * sizeof(double); }
164  size_t entries() const { return _nSize; }
165  bool isShared() const { return (_storage != NULL); }
166 
167  // add variables
169  void addVariable(int var);
170 
174  int indexOf(int var, int val, int indx = 0) const;
176  int indexOf(const drwnFullAssignment& assignment) const;
179  int indexOf(const drwnPartialAssignment& assignment) const;
181  int valueOf(int var, int indx) const;
184  void assignmentOf(int indx, drwnPartialAssignment& assignment) const;
187  void assignmentOf(int indx, drwnFullAssignment& assignment) const;
188 
189  int indexOfMin() const;
190  int indexOfMax() const;
191  pair<int, int> indexOfMinAndMax() const;
192 
193  // inline modifications
194  virtual drwnTableFactor& initialize();
195  drwnTableFactor& fill(double alpha);
196  drwnTableFactor& copy(const double *data);
197  drwnTableFactor& scale(double alpha);
198  drwnTableFactor& offset(double alpha);
199  virtual drwnTableFactor& normalize();
200 
201  // i/o
202  bool save(drwnXMLNode& xml) const;
203  bool load(drwnXMLNode& xml);
204 
205  double getValueOf(const drwnFullAssignment& y) const {
206  return _data[indexOf(y)];
207  }
208 
209  void setValueOf(const drwnFullAssignment& y, double val) {
210  _data[indexOf(y)] = val;
211  }
212 
213  // epsilon-tolerant data comparison
214  bool dataCompare(const drwnTableFactor& psi) const;
215  bool dataCompareAndCopy(const drwnTableFactor& psi);
216 
217  // operators
218  inline double& operator[](unsigned index) { return _data[index]; }
219  inline const double& operator[](unsigned index) const { return _data[index]; }
220 
221  drwnTableFactor& operator=(const drwnTableFactor& psi);
222 };
223 
224 // drwnTableFactorStorage class -------------------------------------------
231 
233  friend class drwnTableFactor;
234 
235  protected:
236  int _dataSize;
237  double *_data;
238 
239  set<drwnTableFactor *> _tables;
240 
241  public:
242  drwnTableFactorStorage(int nSize = 0);
244 
246  inline int capacity() const { return _dataSize; }
248  void reserve(int nSize);
249 
251  inline void zero(int nSize = -1) { fill(0.0, nSize); }
253  void fill(double v, int nSize = -1);
255  void copy(const double *p, int nSize = -1);
257  void copy(const drwnTableFactorStorage *p, int nSize = -1);
258 
259  // data access
260  inline double& operator[](unsigned index) { return _data[index]; }
261  inline const double& operator[](unsigned index) const { return _data[index]; }
262 
263  protected:
266  void registerFactor(drwnTableFactor *factor);
268  void unregisterFactor(drwnTableFactor *factor);
269 };
const char * type() const
returns object type as a string (e.g., Foo::type() { return "Foo"; })
Definition: drwnTableFactor.h:160
int varId(int indx) const
returns variable at given index
Definition: drwnTableFactor.h:89
void setValueOf(const drwnPartialAssignment &y, double val)
Sets the value of the factor for a given partial assignment. The variables in the scope of this facto...
Definition: drwnTableFactor.h:123
virtual void addVariable(int var)
add variable by id
Definition: drwnTableFactor.cpp:98
bool empty() const
true if the factor contains no variables
Definition: drwnTableFactor.h:76
void fill(double v, int nSize=-1)
set nSize (or all) entries to the value v
Definition: drwnTableFactor.cpp:613
int varCard(int indx) const
return cardinality of variable at given index
Definition: drwnTableFactor.h:91
drwnTableFactorStorage * _storage
pointer to shared storage (updates _data member)
Definition: drwnTableFactor.h:152
drwnClique getClique() const
returns the set of variables over which this factor is defined
Definition: drwnTableFactor.h:106
void zero(int nSize=-1)
set nSize (or all) entries to zero
Definition: drwnTableFactor.h:251
size_t memory() const
number of bytes comsumed by the factor data (excludes variables lists, etc)
Definition: drwnTableFactor.h:163
size_t size() const
number of variables in this factor
Definition: drwnTableFactor.h:78
void copy(const double *p, int nSize=-1)
copy nSize (or all) entries from array p
Definition: drwnTableFactor.cpp:628
Data structure for definining the random variables (name and cardinality) for a given problem instanc...
Definition: drwnVarUniverse.h:29
const vector< int > & getOrderedVars() const
returns the ordered set of variables over which this factor is defined
Definition: drwnTableFactor.h:104
void setValueOf(const drwnFullAssignment &y, double val)
Sets the value of the factor for a given (full) assignment.
Definition: drwnTableFactor.h:209
int _nSize
total size of factor (or 1 for singular factors)
Definition: drwnTableFactor.h:150
int capacity() const
return the total capacity of the storage
Definition: drwnTableFactor.h:246
Factor which stores the value of each assignment explicitly in table form.
Definition: drwnTableFactor.h:144
std::set< int > drwnClique
variable clique
Definition: drwnGraphUtils.h:37
std::vector< int > drwnFullAssignment
defines a complete assignment to all variables in the universe
Definition: drwnVarAssignment.h:36
int _dataSize
amount of memory allocated
Definition: drwnTableFactor.h:236
drwnVarUniversePtr _pUniverse
all variables in the universe
Definition: drwnTableFactor.h:61
defines an assignment to a subset of the variables
Definition: drwnVarAssignment.h:41
map< int, int > _varIndex
index of variable in factor (by variable id)
Definition: drwnTableFactor.h:63
Shared memory for table factors.
Definition: drwnTableFactor.h:232
Generic interface for a factor. Currently only inherited by drwnTableFactor.
Definition: drwnTableFactor.h:40
double * _data
data is stored as (a0, b0, ...), (a1, b0, ...), ...
Definition: drwnTableFactor.h:151
Data structures and utilities for encoding assignments to variables.
vector< int > _variables
list of variables in factor (by index in factor)
Definition: drwnTableFactor.h:62
bool hasVariable(int var) const
returns true if the variable is included in this factor
Definition: drwnTableFactor.h:87
double getValueOf(const drwnPartialAssignment &y) const
Returns the value of the factor for a given partial assignment. The variables in the scope of this fa...
Definition: drwnTableFactor.h:116
set< drwnTableFactor * > _tables
tables using this storage
Definition: drwnTableFactor.h:239
double * _data
memory allocation
Definition: drwnTableFactor.h:237
drwnTableFactor * clone() const
returns a copy of the class usually implemented as virtual Foo* clone() { return new Foo(*this); } ...
Definition: drwnTableFactor.h:161
double getValueOf(const drwnFullAssignment &y) const
Returns the value of the factor for a given (full) assignment.
Definition: drwnTableFactor.h:205
standard Darwin object interface (cloneable and writeable)
Definition: drwnInterfaces.h:72
virtual size_t memory() const
number of bytes comsumed by the factor data (excludes variables lists, etc)
Definition: drwnTableFactor.h:80
void initialize(drwnNNGraph &graph, const DistanceMetric &M)
randomly initialize edges (matched) for all active images (keeps existing matches unless an improveme...
Definition: drwnNNGraphMoves.h:159
const drwnVarUniversePtr & getUniverse() const
returns the variable universe
Definition: drwnTableFactor.h:83
std::vector< int > _stride
stride of variable in table (by index)
Definition: drwnTableFactor.h:148