RcsPySim
A robot control and simulation library
AMNormalized.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 "AMNormalized.h"
32 
33 #include <Rcs_VecNd.h>
34 #include <Rcs_macros.h>
35 
36 namespace Rcs
37 {
38 
40  ActionModel(wrapped->getGraph()), wrapped(wrapped)
41 {
42  // Compute scale and shift from inner model bounds
43  const MatNd* iModMin = wrapped->getSpace()->getMin();
44  const MatNd* iModMax = wrapped->getSpace()->getMax();
45 
46  // shift is selected so that the median of min and max is 0
47  // shift = min + (max - min)/2
48  shift = MatNd_clone(iModMax);
49  MatNd_subSelf(shift, iModMin);
50  MatNd_constMulSelf(shift, 0.5);
51  MatNd_addSelf(shift, iModMin);
52 
53  // scale = (max - min)/2
54  scale = MatNd_clone(iModMax);
55  MatNd_subSelf(scale, iModMin);
56  MatNd_constMulSelf(scale, 0.5);
57 }
58 
60 {
61  delete wrapped;
62 }
63 
64 unsigned int AMNormalized::getDim() const
65 {
66  return wrapped->getDim();
67 }
68 
69 void AMNormalized::getMinMax(double* min, double* max) const
70 {
71  VecNd_setElementsTo(min, -1, getDim());
72  VecNd_setElementsTo(max, 1, getDim());
73 }
74 
75 std::vector<std::string> AMNormalized::getNames() const
76 {
77  return wrapped->getNames();
78 }
79 
81  MatNd* q_des, MatNd* q_dot_des, MatNd* T_des,
82  const MatNd* action, double dt)
83 {
84  // use temp storage for denormalized action, so that action remains unchanged.
85  MatNd* denormalizedAction = NULL;
86  MatNd_create2(denormalizedAction, action->m, action->n);
87  // denormalize: denAction = action * scale + shift
88  MatNd_eleMul(denormalizedAction, action, scale);
89  MatNd_addSelf(denormalizedAction, shift);
90 
91  MatNd_maxSelf(denormalizedAction, wrapped->getSpace()->getMin());
92  MatNd_minSelf(denormalizedAction, wrapped->getSpace()->getMax());
93 
94  //MatNd_printTranspose(denormalizedAction);
95 
96  // call wrapper
97  wrapped->computeCommand(q_des, q_dot_des, T_des, denormalizedAction, dt);
98  // destroy temp storage
99  MatNd_destroy(denormalizedAction);
100 }
101 
103 {
104  wrapped->reset();
105 }
106 
107 void AMNormalized::getStableAction(MatNd* action) const
108 {
109  // compute wrapped stable action
110  wrapped->getStableAction(action);
111  // and normalize it
112  for (unsigned int i = 0; i < getDim(); ++i) {
113  action->ele[i] = (action->ele[i] - shift->ele[i])/scale->ele[i];
114  }
115 }
116 
118 {
119  return wrapped;
120 }
121 
122 ActionModel* AMNormalized::clone(RcsGraph* newGraph) const
123 {
124  return new AMNormalized(wrapped->clone(newGraph));
125 }
126 
127 } /* namespace Rcs */
128 
virtual void computeCommand(MatNd *q_des, MatNd *q_dot_des, MatNd *T_des, const MatNd *action, double dt)
virtual std::vector< std::string > getNames() const
ActionModel * wrapped
Definition: AMNormalized.h:47
const MatNd * getMin() const
Definition: BoxSpace.h:129
virtual unsigned int getDim() const
virtual ~AMNormalized()
const BoxSpace * getSpace() const
virtual void getStableAction(MatNd *action) const
virtual void computeCommand(MatNd *q_des, MatNd *q_dot_des, MatNd *T_des, const MatNd *action, double dt)=0
const MatNd * getMax() const
Definition: BoxSpace.h:137
virtual void reset()
virtual void getMinMax(double *min, double *max) const
virtual void reset()
Definition: ActionModel.cpp:49
virtual ActionModel * getWrappedActionModel() const
ActionModel * clone() const
Definition: ActionModel.h:72
virtual std::vector< std::string > getNames() const
virtual unsigned int getDim() const =0
virtual void getStableAction(MatNd *action) const =0
AMNormalized(ActionModel *wrapped)