Uniform.cpp
Go to the documentation of this file.
1 /************************************************************************
2  * Copyright (C) 2012 Eindhoven University of Technology (TU/e). *
3  * All rights reserved. *
4  ************************************************************************
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above *
10  * copyright notice, this list of conditions and the following *
11  * disclaimer. *
12  * *
13  * 2. Redistributions in binary form must reproduce the above *
14  * copyright notice, this list of conditions and the following *
15  * disclaimer in the documentation and/or other materials *
16  * provided with the distribution. *
17  * *
18  * THIS SOFTWARE IS PROVIDED BY TU/e "AS IS" AND ANY EXPRESS OR *
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
21  * ARE DISCLAIMED. IN NO EVENT SHALL TU/e OR CONTRIBUTORS BE LIABLE *
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT *
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; *
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE *
28  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
29  * DAMAGE. *
30  * *
31  * The views and conclusions contained in the software and *
32  * documentation are those of the authors and should not be *
33  * interpreted as representing official policies, either expressed or *
34  * implied, of TU/e. *
35  ************************************************************************/
36 
37 #include "problib/pdfs/Uniform.h"
38 #include "problib/conversions.h"
39 
40 using namespace pbl;
41 
42 Uniform::Uniform(int dim) : PDF(dim, PDF::UNIFORM), uniform_probability_(0), size_is_set_(false) {
43 }
44 
45 Uniform::Uniform(int dim, double density) : PDF(dim, PDF::UNIFORM), uniform_probability_(density), size_is_set_(false) {
46 }
47 
50 }
51 
52 Uniform::Uniform(const Uniform& orig) : PDF(orig), mean_(orig.mean_), size_(orig.size_),
54 }
55 
57 }
58 
60  if (this != &other) {
61  mean_ = other.mean_;
62  size_ = other.size_;
63  size_is_set_ = other.size_is_set_;
65  dimensions_ = other.dimensions_;
66  }
67  return *this;
68 }
69 
71  return new Uniform(*this);
72 }
73 
74 double Uniform::getLikelihood(const PDF& pdf) const {
75  //assert_msg(false, "Uniform PDF: getLikelihood(PDF) is currently not implemented");
76 
77  assert(dimensions() == pdf.dimensions());
78 
79  if (size_is_set_) {
80 
81  arma::vec my_min = mean_ - size_ / 2;
82  arma::vec my_max = mean_ + size_ / 2;
83 
84  if (pdf.type() == PDF::UNIFORM) {
85  const Uniform* U = PDFtoUniform(pdf);
86 
87  arma::vec other_min = U->mean_ - U->size_ / 2;
88  arma::vec other_max = U->mean_ + U->size_ / 2;
89 
90  double overlapping_volume = 1;
91  for(int i = 0; i < dimensions(); ++i) {
92  double diff = std::min(my_max(i), other_max(i)) - std::max(my_min(i), other_min(i));
93  if (diff <= 0) {
94  return 0;
95  }
96  overlapping_volume *= diff;
97  }
98 
99  return overlapping_volume * uniform_probability_ * U->uniform_probability_;
100  } else if (pdf.type() == PDF::HYBRID) {
101  return pdf.getLikelihood(*this);
102  } else {
103  arma::vec other_mean;
104  if (!pdf.getExpectedValue(other_mean)) {
105  std::cout << pdf.toString() << std::endl;
106  assert_msg(false, "Uniform likelihood calculation: cannot determine expected value of pdf.");
107  return 0;
108  }
109 
110  for(int i = 0; i < dimensions(); ++i) {
111  if (other_mean(i) < my_min(i) || other_mean(i) > my_max(i)) {
112  return 0;
113  }
114  }
115 
116  return uniform_probability_;
117  }
118  }
119 
120  return uniform_probability_;
121 }
122 
123 void Uniform::setDensity(const double& density) {
124  uniform_probability_ = density;
125  size_is_set_ = false;
126 }
127 
128 double Uniform::getDensity(const arma::vec& vec) const {
129  return uniform_probability_;
130 }
131 
132 double Uniform::getMaxDensity() const {
133  return uniform_probability_;
134 }
135 
137  mean_ = mean;
138 }
139 
141  size_ = size;
143 }
144 
146  double volume = 1;
147  for(unsigned int i = 0; i < size_.n_elem; ++i) {
148  volume *= size_(i);
149  }
150  uniform_probability_ = 1.0 / volume;
151  size_is_set_ = true;
152 }
153 
154 std::string Uniform::toString(const std::string& indent) const {
155  std::stringstream s;
156  s << "U(" << uniform_probability_;
157 
158  if (size_is_set_) {
159  s << ", mean = " << mean_ << ", size = " << size_;
160  }
161 
162  s << ")";
163 
164  return s.str();
165 }
int dimensions() const
Definition: PDF.cpp:52
pbl::Vector mean_
Definition: Uniform.h:148
double uniform_probability_
Definition: Uniform.h:152
Uniform(int dim)
Constructs a uniform distribution with known dimensionality but unknown size and density.
Definition: Uniform.cpp:42
PDFType type() const
Definition: PDF.cpp:56
pbl::Vector size_
Definition: Uniform.h:150
arma_warn_unused eT min() const
int dimensions_
Definition: PDF.h:87
void setMean(const pbl::Vector mean)
Set the mean of the volume representing this uniform distribution.
Definition: Uniform.cpp:136
Uniform * clone() const
Creates a clone of the object. The clone method is cheap since it only copies a pointer. A deep clone will only be created if the original object is modified.
Definition: Uniform.cpp:70
uword size() const
#define assert_msg(_Expression, _Msg)
Definition: globals.h:53
virtual bool getExpectedValue(std::string &v) const
Definition: PDF.cpp:60
arma::vec Vector
Definition: datatypes.h:44
XmlRpcServer s
Uniform & operator=(const Uniform &other)
Assignment operator. The operation is cheap since it only copies a pointer. A deep clone will only be...
Definition: Uniform.cpp:59
arma_inline const Op< T1, op_mean > mean(const Base< typename T1::elem_type, T1 > &X, const uword dim=0)
double getMaxDensity() const
Returns the maximum density of this distribution, which always equals 1 / volume. ...
Definition: Uniform.cpp:132
std::string toString(const std::string &indent="") const
Represents the uniform distribution as a string for easier console output.
Definition: Uniform.cpp:154
const Uniform * PDFtoUniform(const PDF &pdf)
Casts a PDF object to a Uniform distribution if the PDF represents a Uniform distribution.
virtual ~Uniform()
Destructor.
Definition: Uniform.cpp:56
void setDensity(const double &density)
Sets the density of the uniform distribution.
Definition: Uniform.cpp:123
void setSize(const pbl::Vector size)
Set the size of the volume representing this uniform distribution.
Definition: Uniform.cpp:140
arma_warn_unused eT max() const
double getDensity(const arma::vec &vec) const
Calculates the density of the distribution at point v. Will always return the same density if v is in...
Definition: Uniform.cpp:128
This class represents a hyper-cube shaped uniform distribution.
Definition: Uniform.h:51
Definition: PDF.h:47
Vec< double > vec
Col< double > vec
virtual std::string toString(const std::string &indent="") const =0
double getLikelihood(const PDF &pdf) const
Definition: Uniform.cpp:74
void calculateUniformDensity()
Definition: Uniform.cpp:145
bool size_is_set_
Definition: Uniform.h:154
virtual double getLikelihood(const PDF &pdf) const =0


problib
Author(s): Sjoerd van den Dries, Jos Elfring
autogenerated on Fri Apr 16 2021 02:32:19