Randomiser.hh
Go to the documentation of this file.
1 
11 #ifndef _RANDOMISER_HH_
12 #define _RANDOMISER_HH_
13 
14 // Random distributions
15 #include <random>
16 // Required fields workaround
17 #include <limits>
18 
19 // Open YAML config files
20 #include "yaml-cpp/yaml.h"
21 
22 // Gazebo
23 #include <gazebo/gazebo_client.hh>
24 
25 // Custom messages
26 #include "dr_request.pb.h"
27 // Domain randomization plugin interface
28 #include "DRInterface.hh"
29 
30 // Debug streams
31 #include "debug.hh"
32 
36 typedef gap::msgs::ModelCmd ModelCmdMsg;
37 
38 // Random samplers
39 
42 {
44  public: virtual ~IRandomSampler() {};
45 
49  public: virtual double sample(std::mt19937 & gen);
50 };
51 
54 {
56  public: std::normal_distribution<double> dist;
57 
59  private: double mean;
61  private: double std;
62 
66  public: GaussianSampler(
67  double mean,
68  double std);
69 
71  public: ~GaussianSampler() override {};
72 
76  public: double sample(std::mt19937 & gen) override;
77 };
78 
81 {
83  public: std::uniform_real_distribution<double> dist;
84 
86  private: double a;
88  private: double b;
90  private: bool log_uniform {false};
91 
96  public: UniformSampler(
97  double a,
98  double b,
99  bool log_uniform=false);
100 
102  public: ~UniformSampler() override {};
103 
107  public: double sample(std::mt19937 & gen) override;
108 };
109 
110 // Random properties
111 
114 {
118  public: bool additive;
119 
121  public: static const char CFG_TARGET_OBJ[];
122 
126  public: RandomProperty(
127  IRandomSampler *sampler_,
128  bool additive_);
129 
131  public: virtual ~RandomProperty();
132 
138  public: virtual void fillMsg(DRRequest & msg,
139  DRInterface & api,
140  std::mt19937 & gen,
141  std::string & target);
142 };
143 
146 {
148  public: std::vector<std::string> models;
149 
155  public: ModelScale(
156  IRandomSampler *sampler_,
157  bool additive_,
158  std::vector<std::string> & models_);
159 
161  public: ~ModelScale() override {};
162 
168  public: void fillMsg(DRRequest & msg,
169  DRInterface & api,
170  std::mt19937 & gen,
171  std::string & target) override;
172 };
173 
175 class LinkMass : public RandomProperty
176 {
178  public: std::vector<std::string> models;
180  public: std::vector<std::string> links;
182  public: std::vector<double> masses;
183 
191  public: LinkMass(
192  IRandomSampler *sampler_,
193  bool additive_,
194  std::vector<std::string> & models_,
195  std::vector<std::string> & links_,
196  std::vector<double> & masses_);
197 
199  public: ~LinkMass() override {};
200 
206  public: void fillMsg(DRRequest & msg,
207  DRInterface & api,
208  std::mt19937 & gen,
209  std::string & target) override;
210 };
211 
214 {
216  public: std::vector<std::string> models;
218  public: std::vector<std::string> links;
220  public: std::vector<double> mu1;
222  public: std::vector<double> mu2;
224  public: std::vector<double> kp;
226  public: std::vector<double> kd;
227 
238  public: FrictionCoefficient(
239  IRandomSampler *sampler_,
240  bool additive_,
241  std::vector<std::string> & models_,
242  std::vector<std::string> & links_,
243  std::vector<double> & mu1_,
244  std::vector<double> & mu2_,
245  std::vector<double> & kp_,
246  std::vector<double> & kd_);
247 
249  public: ~FrictionCoefficient() override {};
250 
256  public: void fillMsg(DRRequest & msg,
257  DRInterface & api,
258  std::mt19937 & gen,
259  std::string & target) override;
260 };
261 
264 {
266  public: std::vector<std::string> models;
268  public: std::vector<std::string> joints;
270  public: std::vector<double> damping;
271 
279  public: JointDampingCoefficient(
280  IRandomSampler *sampler_,
281  bool additive_,
282  std::vector<std::string> & models_,
283  std::vector<std::string> & joints_,
284  std::vector<double> & damping_);
285 
287  public: ~JointDampingCoefficient() override {};
288 
294  public: void fillMsg(DRRequest & msg,
295  DRInterface & api,
296  std::mt19937 & gen,
297  std::string & target) override;
298 };
299 
301 class PGain : public RandomProperty
302 {
304  public: std::vector<std::string> models;
306  public: std::vector<std::string> joints;
308  public: std::vector<int> types;
310  public: std::vector<double> p_gains;
311 
320  public: PGain(
321  IRandomSampler *sampler_,
322  bool additive_,
323  std::vector<std::string> & models_,
324  std::vector<std::string> & joints_,
325  std::vector<int> & types_,
326  std::vector<double> & p_gains_);
327 
329  public: ~PGain() override {};
330 
336  public: void fillMsg(DRRequest & msg,
337  DRInterface & api,
338  std::mt19937 & gen,
339  std::string & target) override;
340 };
341 
344 {
346  public: std::vector<std::string> models;
348  public: std::vector<std::string> joints;
350  public: std::vector<double> lower;
352  public: std::vector<double> upper;
353 
362  public: JointLimit(
363  IRandomSampler *sampler_,
364  bool additive_,
365  std::vector<std::string> & models_,
366  std::vector<std::string> & joints_,
367  std::vector<double> & lower_,
368  std::vector<double> & upper_);
369 
371  public: ~JointLimit() override {};
372 
378  public: void fillMsg(DRRequest & msg,
379  DRInterface & api,
380  std::mt19937 & gen,
381  std::string & target) override;
382 };
383 
385 class Gravity : public RandomProperty
386 {
388  public: std::vector<double> gravity;
389 
395  public: Gravity(
396  IRandomSampler *sampler_,
397  bool additive_,
398  std::vector<double> & gravity_);
399 
401  public: ~Gravity() override {};
402 
408  public: void fillMsg(DRRequest & msg,
409  DRInterface & api,
410  std::mt19937 & gen,
411  std::string & target) override;
412 };
413 
414 
415 // Randomiser class
416 
419 {
420  // Private attributes
421 
423  private: DRInterface api;
425  private: std::vector<RandomProperty*> properties;
427  private: std::mt19937 m_mt;
429  private: std::string target {"TARGET_OBJECT"};
430 
432  public: Randomiser(const std::string & config);
433 
435  public: ~Randomiser();
436 
439  public: void randomise(bool blocking=true);
440 
443  public: void setTargetName(const std::string & target);
444 
445  // Public constants for yml config file
446  // TODO - move to file?
447 
449  public: static const char CFG_PROPERTIES[];
451  public: static const char CFG_MODEL_SCALE[];
453  public: static const char CFG_LINK_MASS[];
455  public: static const char CFG_FRICTION[];
457  public: static const char CFG_JOINT_DAMPING[];
459  public: static const char CFG_P_GAIN[];
461  public: static const char CFG_JOINT_LIMIT[];
463  public: static const char CFG_GRAVITY[];
464 
466  public: static const char CFG_DIST[];
468  public: static const char CFG_UNIFORM[];
470  public: static const char CFG_LOG_UNIFORM[];
472  public: static const char CFG_UNIFORM_A[];
474  public: static const char CFG_UNIFORM_B[];
476  public: static const char CFG_GAUSSIAN[];
478  public: static const char CFG_GAUSSIAN_MEAN[];
480  public: static const char CFG_GAUSSIAN_STD[];
481 
483  public: static const char CFG_ADDITIVE[];
485  public: static const char CFG_VECTOR[];
487  public: static const char CFG_TARGET[];
488 
490  public: static const char CFG_MODEL[];
492  public: static const char CFG_LINK[];
494  public: static const char CFG_JOINT[];
496  public: static const char CFG_MASS[];
498  public: static const char CFG_DAMPING[];
500  public: static const char CFG_LOWER[];
502  public: static const char CFG_UPPER[];
504  public: static const char CFG_MU1[];
506  public: static const char CFG_MU2[];
508  public: static const char CFG_KP[];
510  public: static const char CFG_KD[];
512  public: static const char CFG_TYPE[];
514  public: static const char CFG_P[];
516  public: static const char CFG_TYPE_POS[];
517 };
518 
519 #endif
Gaussian distribution sampler.
Definition: Randomiser.hh:53
std::vector< double > lower
Respective list of initial joint lower limits.
Definition: Randomiser.hh:350
P controller gains random property.
Definition: Randomiser.hh:301
Link mass random property.
Definition: Randomiser.hh:175
Joint limits random property.
Definition: Randomiser.hh:343
std::vector< double > damping
Respective list of initial damping coefficients.
Definition: Randomiser.hh:270
std::vector< std::string > models
List of affected models.
Definition: Randomiser.hh:266
std::vector< std::string > models
List of affected models.
Definition: Randomiser.hh:346
std::vector< double > mu1
Respective list of initial mu1.
Definition: Randomiser.hh:220
std::vector< std::string > models
List of affected models.
Definition: Randomiser.hh:178
std::vector< double > masses
Respective list of initial link masses.
Definition: Randomiser.hh:182
virtual ~IRandomSampler()
Destructor.
Definition: Randomiser.hh:44
Abstract class for random property.
Definition: Randomiser.hh:113
~Gravity() override
Destructor.
Definition: Randomiser.hh:401
~FrictionCoefficient() override
Destructor.
Definition: Randomiser.hh:249
Uniform/Log-uniform distribution sampler.
Definition: Randomiser.hh:80
gap::msgs::ModelCmd ModelCmdMsg
Declaration for model command message type.
Definition: Randomiser.hh:36
std::vector< std::string > joints
List of affected joints.
Definition: Randomiser.hh:348
virtual double sample(std::mt19937 &gen)
Gets random sample.
Definition: Randomiser.cc:541
Abstract PRNG sampler class.
Definition: Randomiser.hh:41
std::vector< std::string > joints
List of affected joints.
Definition: Randomiser.hh:268
~JointDampingCoefficient() override
Destructor.
Definition: Randomiser.hh:287
Joint limits random property.
Definition: Randomiser.hh:385
double mean
Gaussian mean.
Definition: Randomiser.hh:59
std::vector< double > gravity
Initial gravity vector.
Definition: Randomiser.hh:388
~LinkMass() override
Destructor.
Definition: Randomiser.hh:199
Friction coefficients random property.
Definition: Randomiser.hh:213
DRInterface api
DRInterface API.
Definition: Randomiser.hh:423
std::vector< int > types
Respective list of controller types.
Definition: Randomiser.hh:308
IRandomSampler * sampler
Random distribution sampler.
Definition: Randomiser.hh:116
std::mt19937 m_mt
Mersenne Twister pseudorandom number generator.
Definition: Randomiser.hh:427
double b
Uniform higher limit.
Definition: Randomiser.hh:88
~ModelScale() override
Destructor.
Definition: Randomiser.hh:161
std::vector< std::string > models
List of affected models.
Definition: Randomiser.hh:304
std::vector< double > kp
Respective list of initial kp.
Definition: Randomiser.hh:224
double std
Gaussian standard deviation.
Definition: Randomiser.hh:61
std::vector< std::string > joints
List of affected joints.
Definition: Randomiser.hh:306
~GaussianSampler() override
Destructor.
Definition: Randomiser.hh:71
std::vector< double > kd
Respective list of initial kd.
Definition: Randomiser.hh:226
std::vector< double > mu2
Respective list of initial mu2.
Definition: Randomiser.hh:222
std::vector< std::string > models
List of affected models.
Definition: Randomiser.hh:148
std::normal_distribution< double > dist
Gaussian distribution.
Definition: Randomiser.hh:56
~UniformSampler() override
Destructor.
Definition: Randomiser.hh:102
std::uniform_real_distribution< double > dist
Uniform distribution.
Definition: Randomiser.hh:83
~JointLimit() override
Destructor.
Definition: Randomiser.hh:371
Randomiser representation class.
Definition: Randomiser.hh:418
Joint damping coefficients random property.
Definition: Randomiser.hh:263
gap::msgs::DRRequest DRRequest
Declaration for request message type.
Definition: Randomiser.hh:34
std::vector< RandomProperty * > properties
Randomised properties.
Definition: Randomiser.hh:425
std::vector< std::string > models
List of affected models.
Definition: Randomiser.hh:216
double a
Uniform lower limit.
Definition: Randomiser.hh:86
bool additive
Whether term is additive or a scaling factor.
Definition: Randomiser.hh:118
Model scale random property.
Definition: Randomiser.hh:145
std::vector< double > upper
Respective list of initial joint upper limits.
Definition: Randomiser.hh:352
~PGain() override
Destructor.
Definition: Randomiser.hh:329
std::vector< std::string > links
List of affected links.
Definition: Randomiser.hh:218
std::vector< double > p_gains
Respective list of initial P controller gains.
Definition: Randomiser.hh:310
std::vector< std::string > links
List of affected links.
Definition: Randomiser.hh:180