RcsPySim
A robot control and simulation library
ObservationModel.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (c) 2020, Fabio Muratore, Honda Research Institute Europe GmbH, and
3  Technical University of Darmstadt.
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8  1. Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  2. Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  3. Neither the name of Fabio Muratore, Honda Research Institute Europe GmbH,
14  or Technical University of Darmstadt, nor the names of its contributors may
15  be used to endorse or promote products derived from this software without
16  specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  DISCLAIMED. IN NO EVENT SHALL FABIO MURATORE, HONDA RESEARCH INSTITUTE EUROPE GMBH,
22  OR TECHNICAL UNIVERSITY OF DARMSTADT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  POSSIBILITY OF SUCH DAMAGE.
29 *******************************************************************************/
30 
31 #include "ObservationModel.h"
32 
33 #include <Rcs_VecNd.h>
34 #include <Rcs_macros.h>
35 
36 #include <limits>
37 #include <sstream>
38 #include <typeinfo>
39 
40 namespace Rcs
41 {
42 
44 
45 MatNd* ObservationModel::computeObservation(const MatNd* currentAction, double dt) const
46 {
47  MatNd* result = MatNd_create(getDim(), 1);
48  computeObservation(result, currentAction, dt);
49  return result;
50 }
51 
52 void ObservationModel::computeObservation(MatNd* observation, const MatNd* currentAction, double dt) const
53 {
54  // First state, then velocity
55  computeObservation(observation->ele, observation->ele + getStateDim(), currentAction, dt);
56 }
57 
59 {
60  // Do nothing
61 }
62 
64 {
65  // velocity dim == state dim
66  return getStateDim();
67 }
68 
69 // Set all min-values to -inf and all max-values to +inf by default
70 void ObservationModel::getLimits(double* minState, double* maxState, double* maxVelocity) const
71 {
72  unsigned int sd = getStateDim();
73  VecNd_setElementsTo(minState, -std::numeric_limits<double>::infinity(), sd);
74  VecNd_setElementsTo(maxState, std::numeric_limits<double>::infinity(), sd);
75  VecNd_setElementsTo(maxVelocity, std::numeric_limits<double>::infinity(), getVelocityDim());
76 }
77 
78 std::vector<std::string> ObservationModel::getStateNames() const
79 {
80  // Generate default names from class name and numbers
81  const char* className = typeid(*this).name();
82 
83  std::vector<std::string> out;
84  for (size_t i = 0; i < getStateDim(); ++i) {
85  std::ostringstream os;
86  os << className << "_" << i;
87  out.push_back(os.str());
88  }
89  return out;
90 }
91 
92 std::vector<std::string> ObservationModel::getVelocityNames() const
93 {
94  // Fast track for no velocities case
95  if (getVelocityDim() == 0) {
96  return {};
97  }
98  RCHECK_MSG(getVelocityDim() == getStateDim(),
99  "Must override getVelocityNames if velocity dim is not 0 or state dim.");
100 
101  // Append 'd' to each state name
102  std::vector<std::string> out;
103  for (auto& stateName : getStateNames()) {
104  out.push_back(stateName + "d");
105  }
106  return out;
107 }
108 
109 
110 unsigned int ObservationModel::getDim() const
111 {
112  // Observe state and velocity
113  return getStateDim() + getVelocityDim();
114 }
115 
116 void ObservationModel::getMinMax(double* min, double* max) const
117 {
118  unsigned int sd = getStateDim();
119  // Get the min and max velocity value pointer
120  double* minVel = min + sd;
121  double* maxVel = max + sd;
122 
123  // Obtain limits
124  getLimits(min, max, maxVel);
125  // Derive min velocity from max velocity
126  VecNd_constMul(minVel, maxVel, -1, getVelocityDim());
127 }
128 
129 std::vector<std::string> ObservationModel::getNames() const
130 {
131  // concat state and velocity names
132  auto res = getStateNames();
133  auto vn = getVelocityNames();
134  std::move(vn.begin(), vn.end(), std::inserter(res, res.end()));
135  return res;
136 }
137 
138 std::vector<ObservationModel*> ObservationModel::getNested() const
139 {
140  return {};
141 }
142 
143 } /* namespace Rcs */
virtual ~ObservationModel()
virtual std::vector< std::string > getStateNames() const
virtual std::vector< ObservationModel * > getNested() const
virtual std::vector< std::string > getVelocityNames() const
virtual void getMinMax(double *min, double *max) const final
MatNd * computeObservation(const MatNd *currentAction, double dt) const
virtual unsigned int getVelocityDim() const
virtual unsigned int getStateDim() const =0
virtual void getLimits(double *minState, double *maxState, double *maxVelocity) const
virtual std::vector< std::string > getNames() const final
virtual unsigned int getDim() const final