Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnVarUniverse.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: drwnVarUniverse.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 <vector>
18 #include <string>
19 
20 #include "drwnBase.h"
21 #include "drwnGraphUtils.h"
22 
23 using namespace std;
24 
25 // drwnVarUniverse ---------------------------------------------------------
28 
30  protected:
33  vector<int> _varCards;
34  vector<string> _varNames;
35 
36  public:
38  drwnVarUniverse(int numVars, int varCards = 2);
39  drwnVarUniverse(const vector<int>& varCards);
40  ~drwnVarUniverse();
41 
42  // access functions
43  virtual const char *type() const { return "drwnVarUniverse"; }
44  virtual drwnVarUniverse *clone() const { return new drwnVarUniverse(*this); }
45 
47  int numVariables() const { return _numVariables; }
49  bool hasUniformCardinality() const { return _varCards.empty(); }
51  int varCardinality(int v) const { return _varCards.empty() ? _uniformCards : _varCards[v]; }
53  int maxCardinality() const { return _varCards.empty() ? _uniformCards : *max_element(_varCards.begin(), _varCards.end()); }
54  int maxCardinality(const drwnClique& c) const;
56  double logStateSpace() const;
58  double logStateSpace(const drwnClique& c) const;
60  string varName(int v) const { return _varNames.empty() ? (string("X") + toString(v)) : _varNames[v]; }
62  int findVariable(const char* name) const;
63 
65  int addVariable(const char *name = NULL);
67  int addVariable(int varCard, const char *name = NULL);
68 
70  drwnVarUniverse slice(const vector<int>& vars) const;
72  drwnVarUniverse slice(const drwnClique& vars) const;
73 
74  // i/o
75  void clear();
76  bool save(drwnXMLNode& xml) const;
77  bool load(drwnXMLNode& xml);
78 
79  void print() const;
80 };
81 
82 // drwnVarUniversePtr ------------------------------------------------------
85 
87 
int _uniformCards
cardinality of variables if all the same
Definition: drwnVarUniverse.h:32
vector< string > _varNames
optional string name for each variable
Definition: drwnVarUniverse.h:34
vector< int > _varCards
cardinality of variables if some are different
Definition: drwnVarUniverse.h:33
bool hasUniformCardinality() const
returns true if all variables have the same cardinality
Definition: drwnVarUniverse.h:49
int maxCardinality() const
returns the greatest cardinality in the universe or in clique c
Definition: drwnVarUniverse.h:53
int varCardinality(int v) const
returns the cardinality of variable v (between 0 and _numVariables - 1)
Definition: drwnVarUniverse.h:51
virtual const char * type() const
returns object type as a string (e.g., Foo::type() { return "Foo"; })
Definition: drwnVarUniverse.h:43
Data structure for definining the random variables (name and cardinality) for a given problem instanc...
Definition: drwnVarUniverse.h:29
int _numVariables
number of variables in the model (universe)
Definition: drwnVarUniverse.h:31
std::set< int > drwnClique
variable clique
Definition: drwnGraphUtils.h:37
virtual drwnVarUniverse * clone() const
returns a copy of the class usually implemented as virtual Foo* clone() { return new Foo(*this); } ...
Definition: drwnVarUniverse.h:44
std::string toString(const T &v)
Templated function to make conversion from simple data types like int and double to strings easy for ...
Definition: drwnStrUtils.h:134
string varName(int v) const
returns the name of variable v (between 0 and _numVariables - 1)
Definition: drwnVarUniverse.h:60
standard Darwin object interface (cloneable and writeable)
Definition: drwnInterfaces.h:72
Generic graph utilities.
int numVariables() const
returns the number of variables in the model
Definition: drwnVarUniverse.h:47