RcsPySim
A robot control and simulation library
OMCollisionCostPrediction.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 
32 #include "OMCollisionCost.h"
33 
34 #include <Rcs_collisionModel.h>
35 #include <Rcs_typedef.h>
36 #include <Rcs_VecNd.h>
37 #include <Rcs_macros.h>
38 
39 #include <stdexcept>
40 
41 namespace Rcs
42 {
43 
45  RcsGraph* graph, RcsCollisionMdl* collisionMdl,
46  const ActionModel* actionModel, size_t horizon,
47  double maxCollCost) : realGraph(graph), horizon(horizon),
48  maxCollCost(maxCollCost)
49 {
50  // Copy graph and action model for prediction
51  predictGraph = RcsGraph_clone(graph);
52  predictActionModel = actionModel->clone(predictGraph);
53 
54  // Copy collision model for predict graph.
55  this->collisionMdl = RcsCollisionModel_clone(collisionMdl, predictGraph);
56 }
57 
58 
60 {
61  // Destroy cloned collision model
62  RcsCollisionModel_destroy(collisionMdl);
63 
64  delete predictActionModel;
65  RcsGraph_destroy(predictGraph);
66 }
67 
69 {
70  return 1;
71 }
72 
74 {
75  return 0;
76 }
77 
79  double* state, double* velocity, const MatNd* currentAction,
80  double dt) const
81 {
82  // NOTE: prediction doesn't update q_dot, since we don't simulate it and it's not relevant for the collision model.
83 
84  // Reset prediction state
85  RcsGraph_setState(predictGraph, realGraph->q, realGraph->q_dot);
87 
88  // Compute cost for initial step
89  RcsCollisionModel_compute(collisionMdl);
90  double predCostSum = RcsCollisionMdl_cost(collisionMdl);
91 
92  // This OM can only predict the future collision costs if the current action was given
93  if (currentAction != NULL) {
94  // Execute prediction
95  for (size_t i = 0; i <= horizon; ++i) {
96  // Compute action from action model
97  // Passing NULL to q_dot_des and T_des is not strictly allowed, but since we can't support them, this
98  // will at least cause an error with unsupported action models.
99  predictActionModel->computeCommand(predictGraph->q, predictGraph->q_dot, NULL, currentAction, dt);
100 
101  // Update graph using forward kinematics (no physics simulation)
102  RcsGraph_setState(predictGraph, NULL, predictGraph->q_dot);
103 
104  // Sum up step cost
105  RcsCollisionModel_compute(collisionMdl);
106  predCostSum += RcsCollisionMdl_cost(collisionMdl);
107  }
108  // Compute average
109  predCostSum /= (horizon + 1.);
110  }
111  // The state is the average collision cost
112  state[0] = predCostSum;
113 }
114 
115 std::vector<std::string> OMCollisionCostPrediction::getStateNames() const
116 {
117  return {"PredCollCost_h" + std::to_string(horizon)};
118 }
119 
120 void OMCollisionCostPrediction::getLimits(double* minState, double* maxState, double* maxVelocity) const
121 {
122 // ObservationModel::getLimits(minState, maxState, maxVelocity);
123  VecNd_setZero(minState, getStateDim()); // minimum cost is 0
124  VecNd_setElementsTo(maxState, maxCollCost, getStateDim()); // maximum cost (theoretically infinite)
125 }
126 
127 } /* namespace Rcs */
RcsGraph * realGraph
Graph to observe (not owned)
ActionModel * predictActionModel
Action model copy for prediction.
virtual unsigned int getVelocityDim() const
std::vector< std::string > getStateNames() const override
virtual unsigned int getStateDim() const
virtual void getLimits(double *minState, double *maxState, double *maxVelocity) const
virtual void computeCommand(MatNd *q_des, MatNd *q_dot_des, MatNd *T_des, const MatNd *action, double dt)=0
RcsGraph * predictGraph
Graph copy for prediction.
size_t horizon
Time horizon for the predicted collision costs (horizon = 1 mean the current step plus one step ahead...
OMCollisionCostPrediction(RcsGraph *graph, RcsCollisionMdl *collisionMdl, const ActionModel *actionModel, size_t horizon=10, double maxCollCost=1e4)
virtual void computeObservation(double *state, double *velocity, const MatNd *currentAction, double dt) const
virtual void reset()
Definition: ActionModel.cpp:49
ActionModel * clone() const
Definition: ActionModel.h:72
RcsCollisionMdl * collisionMdl
Rcs collision model.