RcsPySim
A robot control and simulation library
TorchPolicy.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 "TorchPolicy.h"
32 
33 #include <torch/all.h>
34 
35 namespace Rcs
36 {
37 
38 TorchPolicy::TorchPolicy(const char* filename)
39 {
40  torch::set_default_dtype(torch::scalarTypeToTypeMeta(torch::kDouble));
41 
42  // Load policy from file
43  module = torch::jit::load(filename);
44 
45  // Make sure the module has the right data type
46  module.to(torch::kDouble);
47 }
48 
50 {
51  // Nothing to do here
52 }
53 
55 {
56  // Call a reset method if it exists
57  auto resetMethod = module.find_method("reset");
58  if (resetMethod.has_value()) {
59  torch::jit::Stack stack;
60  resetMethod->run(stack);
61  }
62 }
63 
64 void TorchPolicy::computeAction(MatNd* action, const MatNd* observation)
65 {
66  // Convert input to torch
67  torch::Tensor obs_to = torch::from_blob(observation->ele, {observation->m}, torch::dtype(torch::kDouble));
68 
69  // Forward input through the module
70  torch::Tensor act_to = module.forward({obs_to}).toTensor();
71 
72  // Convert output back to MatNd
73  torch::Tensor act_out = torch::from_blob(action->ele, {action->m}, torch::dtype(torch::kDouble));
74  act_out.copy_(act_to);
75 }
76 
78 
79 } /* namespace Rcs */
virtual void reset()
Definition: TorchPolicy.cpp:54
static ControlPolicyRegistration< TorchPolicy > RegTorchPolicy("torch")
torch::jit::script::Module module
Definition: TorchPolicy.h:56
TorchPolicy(const char *filename)
Definition: TorchPolicy.cpp:38
virtual void computeAction(MatNd *action, const MatNd *observation)
Definition: TorchPolicy.cpp:64
virtual ~TorchPolicy()
Definition: TorchPolicy.cpp:49