RcsPySim
A robot control and simulation library
ControlPolicy.h
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 #ifndef _CONTROLPOLICY_H_
32 #define _CONTROLPOLICY_H_
33 
34 #include <Rcs_MatNd.h>
35 
36 #include <vector>
37 #include <string>
38 
39 namespace Rcs
40 {
41 
42 class PropertySource;
43 
44 /**
45  * Base class for a control policy that computes the actions from a given observation vector.
46  */
48 {
49 public:
50  // static registry
51 
52  //! Policy factory function. Should read a policy from a file.
53  typedef ControlPolicy* (* ControlPolicyCreateFunction)(const char*);
54 
55  /*! Register a control policy type.
56  * @param[in] name policy type name
57  * @param[in] creator factory function
58  */
59  static void registerType(const char* name, ControlPolicyCreateFunction creator);
60 
61  /*! Load a saved policy.
62  * @param[in] name policy type name
63  * @param[in] dataFile file to load
64  * @return loaded policy
65  */
66  static ControlPolicy* create(const char* name, const char* dataFile);
67 
68  /*! Load a saved policy defined by the given configuration.
69  * @param[in] config property config, containing type and file entries
70  * @return loaded policy
71  */
72  static ControlPolicy* create(PropertySource* config);
73 
74  //! List available policy names.
75  static std::vector<std::string> getTypeNames();
76 
77  ControlPolicy();
78 
79  virtual ~ControlPolicy();
80 
81  /*! Reset internal state if any.
82  * The default implementation does nothing.
83  */
84  virtual void reset();
85 
86  /*!
87  * Compute the action according to the policy.
88  * @param[out] action matrix to store the action in
89  * @param[in] observation current observed state
90  */
91  virtual void computeAction(MatNd* action, const MatNd* observation) = 0;
92 };
93 
94 /**
95  * Create a static field of this type to register a control policy type.
96  */
97 template<class Policy>
99 {
100 public:
101  /**
102  * Register the template type under the given name.
103  * @param name experiment name
104  */
105  explicit ControlPolicyRegistration(const char* name)
106  {
108  }
109 
110  /**
111  * Creator function passed to ExperimentConfig::registerType.
112  */
113  static ControlPolicy* create(const char* dataFile)
114  {
115  return new Policy(dataFile);
116  }
117 
118 };
119 
120 } /* namespace Rcs */
121 
122 #endif /* _CONTROLPOLICY_H_ */
static std::vector< std::string > getTypeNames()
List available policy names.
static void registerType(const char *name, ControlPolicyCreateFunction creator)
virtual void reset()
ControlPolicyRegistration(const char *name)
virtual void computeAction(MatNd *action, const MatNd *observation)=0
ControlPolicy *(* ControlPolicyCreateFunction)(const char *)
Policy factory function. Should read a policy from a file.
Definition: ControlPolicy.h:53
static ControlPolicy * create(const char *dataFile)
static ControlPolicy * create(const char *name, const char *dataFile)