KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bbb_trajectory_prior.h
Go to the documentation of this file.
1 /* =========================================================================== *
2  |
3  | Copyright (c) 1994-2011 by Kobus Barnard (author)
4  |
5  | Personal and educational use of this code is granted, provided that this
6  | header is kept intact, and that the authorship is not misrepresented, that
7  | its use is acknowledged in publications, and relevant papers are cited.
8  |
9  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
10  |
11  | Please note that the code in this file has not necessarily been adequately
12  | tested. Naturally, there is no guarantee of performance, support, or fitness
13  | for any particular task. Nonetheless, I am interested in hearing about
14  | problems that you encounter.
15  |
16  | Author: Ernesto Brau
17  * =========================================================================== */
18 
19 /* $Id$ */
20 
21 #ifndef B3_TRAJECTORY_PRIOR_H
22 #define B3_TRAJECTORY_PRIOR_H
23 
24 #include <bbb_cpp/bbb_trajectory.h>
26 #include <l_cpp/l_exception.h>
27 #include <gp_cpp/gp_predictive.h>
28 #include <gp_cpp/gp_mean.h>
29 #include <gp_cpp/gp_covariance.h>
30 #include <gp_cpp/gp_pdf.h>
31 #include <m_cpp/m_vector.h>
32 #include <vector>
33 
34 namespace kjb {
35 namespace bbb {
36 
43 {
44 private:
45  typedef gp::Predictive_nl<gp::Zero, gp::Sqex> Prior;
46 
47 public:
53  Trajectory_prior(size_t dim, const Activity_library& lib) :
54  dim_(dim),
55  start_(0),
56  end_(0),
57  lib_(lib),
58  priors_dirty_(true)
59  {
60  IFT(dim != 0, Illegal_argument,
61  "Cannot create Trajectory prior: dimension cannot be 0.");
62  }
63 
65  void set_name(const std::string& name)
66  {
67  name_ = name;
68  priors_dirty_ = true;
69  }
70 
72  void set_start(size_t start) { start_ = start; priors_dirty_ = true; }
73 
75  void set_end(size_t end) { end_ = end; priors_dirty_ = true; }
76 
78  void set_endpoint_means(const Vector& mu_s, const Vector& mu_e)
79  {
80  IFT(mu_s.get_length() == dim_ && mu_e.get_length() == dim_,
81  Illegal_argument, "Cannot set traj prior means; bad dimension.");
82 
83  mu_s_ = mu_s;
84  mu_e_ = mu_e;
85 
86  priors_dirty_ = true;
87  }
88 
90  size_t dimension() const { return dim_; }
91 
93  const std::string& name() const { return name_; }
94 
96  size_t start() const { return start_; }
97 
99  size_t end() const { return end_; }
100 
102  const Vector& start_mean() const { return mu_s_; }
103 
105  const Vector& end_mean() const { return mu_e_; }
106 
108  const Activity_library& library() const { return lib_; }
109 
111  double operator()(const Trajectory& traj) const
112  {
113  IFT(traj.dimensions() == dim_, Illegal_argument,
114  "Cannot compute trajectory prior; wrong trajectory dimension.");
115 
116  IFT(traj.size() == end_ - start_ + 1, Illegal_argument,
117  "Cannot compute trajectory prior; wrong trajectory size.");
118 
119  update_priors();
120  double p = 0.0;
121  for(size_t d = 0; d < dim_; d++)
122  {
123  p += log_pdf(priors_[d], traj.dim(d));
124  }
125 
126  return p;
127  }
128 
129 private:
131  void update_priors() const;
132 
133  // sample() needs to access update_priors()
134  friend Trajectory sample(const Trajectory_prior& prior);
135 
136  size_t dim_;
137  std::string name_;
138  size_t start_;
139  size_t end_;
140  Vector mu_s_;
141  Vector mu_e_;
142  const Activity_library& lib_;
143  mutable std::vector<Prior> priors_;
144  mutable bool priors_dirty_;
145 };
146 
148 inline
150 {
151  prior.update_priors();
152 
153  const size_t D = prior.dimension();
154  std::vector<Trajectory::vec_t> trajs(D);
155  for(size_t d = 0; d < D; d++)
156  {
157  trajs[d] = sample(prior.priors_[d]);
158  }
159 
160  Trajectory trajectory;
161  trajectory.set_dimensions(prior.start(), trajs.begin(), trajs.end());
162 
163  return trajectory;
164 }
165 
166 }} // namespace kjb::bbb
167 
168 #endif /*B3_TRAJECTORY_PRIOR_H */
169 
Trajectory_prior(size_t dim, const Activity_library &lib)
Crate a trajectory prior.
Definition: bbb_trajectory_prior.h:53
Activity_sequence sample(const As_prior &prior)
Sample an activity sequence from the given prior.
Definition: bbb_activity_sequence_prior.cpp:90
size_t start() const
Get the start time of the prior.
Definition: bbb_trajectory_prior.h:96
const Vector & end_mean() const
Get the end mean.
Definition: bbb_trajectory_prior.h:105
Definition: bbb_trajectory_prior.h:42
size_t size() const
Gets the size of this person.
Definition: bbb_trajectory.h:118
int get_length() const
Return the length of the vector.
Definition: m_vector.h:501
void set_end(size_t end)
Set the end time.
Definition: bbb_trajectory_prior.h:75
size_t end() const
Get the end time of the prior.
Definition: bbb_trajectory_prior.h:99
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
const std::string & name() const
Get the activity name of the prior.
Definition: bbb_trajectory_prior.h:93
void set_endpoint_means(const Vector &mu_s, const Vector &mu_e)
Set the endpoint means.
Definition: bbb_trajectory_prior.h:78
#define IFT(a, ex, msg)
Definition: l_exception.h:101
const vec_t & dim(size_t d) const
Gets the trajectory of this person.
Definition: bbb_trajectory.h:93
Definition: bbb_trajectory.h:41
size_t dimension() const
Get the dimensionality of the trajectories of this prior.
Definition: bbb_trajectory_prior.h:90
double log_pdf(const MV_gaussian_distribution &P, const Vector &x)
Computes the log PDF a multivariate normal distribution at x.
Definition: prob_pdf.cpp:64
const Vector & start_mean() const
Get the start mean.
Definition: bbb_trajectory_prior.h:102
friend Trajectory sample(const Trajectory_prior &prior)
Draw a sample (a Trajectory) from a trajectory prior.
Definition: bbb_trajectory_prior.h:149
void set_start(size_t start)
Set the start time.
Definition: bbb_trajectory_prior.h:72
void set_name(const std::string &name)
Set the activity name.
Definition: bbb_trajectory_prior.h:65
const Activity_library & library() const
Get activity library.
Definition: bbb_trajectory_prior.h:108
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
void set_dimensions(size_t st, VecIter first, VecIter last)
Set dimensions of this trajectory.
Definition: bbb_trajectory.h:160
double operator()(const Trajectory &traj) const
Evaluate this prior on a trajectory.
Definition: bbb_trajectory_prior.h:111
D
Definition: APPgetLargeConnectedEdges.m:106
Support for error handling exception classes in libKJB.
size_t dimensions() const
Gets the dimensionality of this person's trajectory.
Definition: bbb_trajectory.h:105
Definition: bbb_activity_library.h:38
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...