RcsPySim
A robot control and simulation library
OMCombined.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 "OMCombined.h"
32 
33 namespace Rcs
34 {
35 
37 {
38  for (auto part : parts) {
39  delete part;
40  }
41 }
42 
44 {
45  parts.push_back(part);
46 }
47 
48 unsigned int Rcs::OMCombined::getStateDim() const
49 {
50  // do it for all parts
51  unsigned int sumdim = 0;
52  for (auto part : parts) {
53  sumdim += part->getStateDim();
54  }
55  return sumdim;
56 }
57 
58 unsigned int OMCombined::getVelocityDim() const
59 {
60  // do it for all parts
61  unsigned int sumdim = 0;
62  for (auto part : parts) {
63  sumdim += part->getVelocityDim();
64  }
65  return sumdim;
66 }
67 
68 void Rcs::OMCombined::computeObservation(double* state, double* velocity, const MatNd* currentAction, double dt) const
69 {
70  // do it for all parts
71  for (auto part : parts) {
72  part->computeObservation(state, velocity, currentAction, dt);
73  state += part->getStateDim();
74  velocity += part->getVelocityDim();
75  }
76 }
77 
79  double* minState, double* maxState,
80  double* maxVelocity) const
81 {
82  // do it for all parts
83  for (auto part : parts) {
84  part->getLimits(minState, maxState, maxVelocity);
85  minState += part->getStateDim();
86  maxState += part->getStateDim();
87  maxVelocity += part->getVelocityDim();
88  }
89 }
90 
91 std::vector<std::string> Rcs::OMCombined::getStateNames() const
92 {
93  // reserve dim out vars
94  std::vector<std::string> out;
95  out.reserve(getStateDim());
96  // concatenate names from parts
97  for (auto part : parts) {
98  auto pnames = part->getStateNames();
99  // move the elements from pnames since it is a copy anyways.
100  std::move(pnames.begin(), pnames.end(), std::inserter(out, out.end()));
101  }
102  return out;
103 }
104 
105 std::vector<std::string> OMCombined::getVelocityNames() const
106 {
107  // reserve dim out vars
108  std::vector<std::string> out;
109  out.reserve(getVelocityDim());
110  // concatenate names from parts
111  for (auto part : parts) {
112  auto pnames = part->getVelocityNames();
113  // move the elements from pnames since it is a copy anyways.
114  std::move(pnames.begin(), pnames.end(), std::inserter(out, out.end()));
115  }
116  return out;
117 }
118 
120 {
121  // do it for all parts
122  for (auto part : parts) {
123  part->reset();
124  }
125 }
126 
127 std::vector<ObservationModel*> OMCombined::getNested() const
128 {
129  return parts;
130 }
131 
132 } /* namespace Rcs */
virtual std::vector< ObservationModel * > getNested() const
Definition: OMCombined.cpp:127
virtual void computeObservation(double *state, double *velocity, const MatNd *currentAction, double dt) const
Definition: OMCombined.cpp:68
virtual std::vector< std::string > getStateNames() const
Definition: OMCombined.cpp:91
std::vector< ObservationModel * > parts
Definition: OMCombined.h:77
virtual void reset()
Definition: OMCombined.cpp:119
void addPart(ObservationModel *part)
Definition: OMCombined.cpp:43
virtual void getLimits(double *minState, double *maxState, double *maxVelocity) const
Definition: OMCombined.cpp:78
virtual ~OMCombined()
Definition: OMCombined.cpp:36
virtual unsigned int getStateDim() const
Definition: OMCombined.cpp:48
virtual unsigned int getVelocityDim() const
Definition: OMCombined.cpp:58
virtual std::vector< std::string > getVelocityNames() const
Definition: OMCombined.cpp:105