discreteconditionalpdf.cpp
Go to the documentation of this file.
1 // $Id$
2 // Copyright (C) 2003 Klaas Gadeyne <first dot last at gmail dot com>
3 // 2008 Tinne De Laet <first dot last at mech dot kuleuven dot be>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 #include "discreteconditionalpdf.h"
20 #include "../wrappers/rng/rng.h"
21 #include <vector>
22 #include <iostream>
23 
24 namespace BFL
25 {
26  using namespace std;
27 
29  int num_conditional_arguments,
30  int cond_arg_dimensions[])
31  : ConditionalPdf<int,int>(1,num_conditional_arguments)
32  , _num_states(num_states)
33  , _probs(num_states)
34  , _valuelist(num_states+1)
35  {
36  _cond_arg_dims_p = new int[num_conditional_arguments];
37  int total_dim = 1;
38  for ( int arg = 0 ; arg < num_conditional_arguments; arg++)
39  {
40  _cond_arg_dims_p[arg] = cond_arg_dimensions[arg];
41  total_dim *= cond_arg_dimensions[arg];
42  }
43  _total_dimension = total_dim * num_states;
44 #ifdef __CONSTRUCTOR__
45  cout << "DCPdf Constructor: total dimension allocated: "
46  << _total_dimension << endl;
47 #endif // __CONSTRUCTOR__
48  _probability_p = new double[_total_dimension];
49  }
50 
52  {
53 #ifdef __DCPDFDEBUG__
54  cout << "DCPdf Destructor:" << endl;
55 #endif // __DCPDFDEBUG__
56  //delete _probability_p;
57  //delete _cond_arg_dims_p;
58  }
59 
60 
62  : ConditionalPdf<int,int>(pdf)
64  , _probs(pdf.NumStatesGet())
65  , _valuelist(pdf.NumStatesGet()+1)
66  {
68  int total_dim = 1;
69  for ( unsigned int arg = 0 ; arg < NumConditionalArgumentsGet(); arg++)
70  {
71  _cond_arg_dims_p[arg] = pdf._cond_arg_dims_p[arg];
72  total_dim *= _cond_arg_dims_p[arg];
73  }
74  total_dim *= _num_states;
75  _total_dimension = total_dim;
76  _probability_p = new double[total_dim];
77  for (int prob = 0 ; prob < total_dim ; prob++)
78  {
79  _probability_p[prob] = pdf._probability_p[prob];
80  }
81  }
82 
83  //Clone function
85  {
86  return new DiscreteConditionalPdf(*this);
87  }
88 
89  // Get the number of discrete states
91  {
92  return _num_states;
93  }
94 
95  // Calculate index (used by ProbabilityGet and ProbabilitySet)
96  int DiscreteConditionalPdf::IndexGet(const int& input,
97  const std::vector<int>& condargs) const
98  {
99  int index = 0;
100  int blocksize = 1;
101 
102  // The first hyperdimension is that of input itself
103  index += input * blocksize;
104  blocksize *= NumStatesGet();
105  // The other ons are those of the conditional args
106  for (unsigned int arg = 0 ; arg < NumConditionalArgumentsGet() ; arg++ )
107  {
108  index += condargs[arg] * blocksize;
109  blocksize *= (this->_cond_arg_dims_p)[arg];
110  }
111 #ifdef __INDEXDEBUG__
112  cout << "DCPdf::IndexGet -> Index = " << index << endl;
113 #endif // __INDEXDEBUG__
114  return index;
115  }
116 
117 
118 
120  {
121  unsigned int index = IndexGet(input, ConditionalArgumentsGet());
122  double prob = (this->_probability_p)[index];
123  return prob;
124  }
125 
126  // Typical for discrete Pdf's
127  void DiscreteConditionalPdf::ProbabilitySet(const double& prob,
128  const int& input,
129  const std::vector<int>& condargs) const
130  {
131  int index = this->IndexGet(input, condargs);
132  _probability_p[index] = prob;
133  }
134 
135  bool DiscreteConditionalPdf::SampleFrom(Sample<int>& one_sample, int method, void * args) const
136  {
137  // Get the elements of which to sample from
138  int startindex = IndexGet(0,ConditionalArgumentsGet());
139  double SumWeights = 0.0; double CumSum=0.0;
140  unsigned int index;
141 
142  for ( index = 0; index < NumStatesGet() ; index++ )
143  {
144  _probs[index] = _probability_p[startindex+index];
145  CumSum += _probs[index];
146 #ifdef __DCPDFDEBUG__
147 #define TABWIDTH 10
148  cout << setw(TABWIDTH) << _probs[index];
149 #endif // __DCPDFDEBUG__
150  }
151  SumWeights = CumSum;
152  _valuelist[0] = 0.0; CumSum = 0.0;
153  for ( index = 1; index <= NumStatesGet() ; index++ )
154  {
155  CumSum += _probs[index-1]/SumWeights;
156  _valuelist[index] = CumSum;
157  }
158  // Check if last element of valuelist is +- 1
159  assert ( (_valuelist[NumStatesGet()] >= 1.0 - NUMERIC_PRECISION) &&
160  (_valuelist[NumStatesGet()] <= 1.0 + NUMERIC_PRECISION) );
161 
163 
164  // Sample from univariate uniform rng between 0 and 1;
165  double unif_sample; unif_sample = runif();
166  // Compare where we should be: THIS CAN BE MADE FASTER: TODO
167  index = 0;
168  while ( unif_sample > _valuelist[index] )
169  {
170  assert(index <= NumStatesGet());
171  index++;
172  }
173  one_sample.ValueSet(index-1);
174  return true;
175  }
176 
177 
178  bool
180  int num_samples, int method,
181  void * args) const
182  {
183  list_samples.resize(num_samples); // will break real-timeness if list_samples.size()!=num_samples
184  return Pdf<int>::SampleFrom(list_samples, num_samples,method,args);
185  }
186 
187 
188 } // End namespace BFL
unsigned int _num_states
number of discrete states
unsigned int NumStatesGet() const
Get the number of discrete states.
int * _cond_arg_dims_p
"Possible discrete states" of all the conditional arguments
void ValueSet(const T &value)
Set the value of the Sample.
virtual ~DiscreteConditionalPdf()
Destructor.
int IndexGet(const int &input, const std::vector< int > &condargs) const
Get the correct index in the row of doubles (double * probability)
const std::vector< int > & ConditionalArgumentsGet() const
Get the whole list of conditional arguments.
DiscreteConditionalPdf(int num_states=1, int num_conditional_arguments=1, int cond_arg_dimensions[]=NULL)
Constructor.
double * _probability_p
Pointer to the probability values.
Abstract Class representing conditional Pdfs P(x | ...)
void ProbabilitySet(const double &prob, const int &input, const std::vector< int > &condargs) const
Set the probability (Typical for discrete Pdf&#39;s)
Abstract Class representing all FULLY Discrete Conditional PDF&#39;s.
std::vector< double > _valuelist
int _total_dimension
Total dimension of the likelihoodtable.
virtual bool SampleFrom(vector< Sample< T > > &list_samples, const unsigned int num_samples, int method=DEFAULT, void *args=NULL) const
Draw multiple samples from the Pdf (overloaded)
Probability ProbabilityGet(const int &input) const
Get the probability of a certain argument.
double runif()
virtual bool SampleFrom(Sample< int > &one_sample, int method, void *args) const
Draw 1 sample from the Pdf:
Class representing a probability (a double between 0 and 1)
Definition: bfl_constants.h:39
#define NUMERIC_PRECISION
unsigned int NumConditionalArgumentsGet() const
Get the Number of conditional arguments.
virtual DiscreteConditionalPdf * Clone() const
Clone function.


bfl
Author(s): Klaas Gadeyne, Wim Meeussen, Tinne Delaet and many others. See web page for a full contributor list. ROS package maintained by Wim Meeussen.
autogenerated on Mon Feb 28 2022 21:56:33