Rumba C++ SDK
Optimizer.h
Go to the documentation of this file.
1 /*
2 
3  *
4  ***
5  *****
6  ********************* Mercenaries Engineering SARL
7  ***************** Copyright (C) 2025
8  *************
9  ********* http://www.mercenaries-engineering.com
10  ***********
11  **** ****
12  ** **
13 
14 */
15 #pragma once
16 
17 #include "Plug.h"
18 #include <ImathVec.h>
19 
20 namespace maquina
21 {
22 
23 class EvaluationEngine;
24 class NodeDelegate;
25 
26 /* Optimize a graph values.
27  *
28  * Optimize multiple output graph values by modifying multiple graph inputs.
29  *
30  * A single optimizer::solve() call can be performed at the same time.
31  */
33 {
34 public:
35  enum class LogLevel
36  {
37  silent,
38  summary,
39  details
40  };
41 
42  Optimizer(LogLevel log_level = LogLevel::silent);
43 
44  enum Axis
45  {
46  x_axis = 1,
47  y_axis = 2,
48  z_axis = 4
49  };
50 
51  void add_param_float(Plug& plug, double default_value, double initial_step);
52  void add_param_rotation_axis(int axes, std::shared_ptr<NodeDelegate> nd_);
53  void add_param_rotation(std::shared_ptr<NodeDelegate> nd_);
54  void add_param_translation(std::shared_ptr<NodeDelegate> nd_, double initial_step);
55 
56  void add_output_rotation(const Plug& plug, const Imath::Quatd& goal);
57  void add_output_translation(const Plug& plug, const Imath::V3d& goal);
58 
59  double solve(double error_threshold = 0.00001, int max_iterations = 1000);
60 
61  static const double rotation_initial_step;
62 
63 private:
64 
65  struct Parameter
66  {
67  enum class Mode { float_, rotation_axes, rotation, translation };
68 
69  Parameter(std::shared_ptr<NodeDelegate> nd, std::optional<Plug> plug, Mode mode, int axes, const Imath::V3d& initial_value,
70  double _initial_step);
71 
72  int _dim() const;
73  std::shared_ptr<NodeDelegate> _nd;
74  std::optional<Plug> _plug;
75  Imath::Eulerd::Order _rotate_order;
76  Mode _mode;
77  int _axes;
78  Imath::V3d _initial_value;
79  double _initial_step;
80  };
81 
82  struct Objective
83  {
84  enum class Mode { rotation, translation };
85  Objective(const Plug& _plug, const Imath::Quatd& goal_rotation, const Imath::V3d& goal_translation, Mode mode);
86 
87  Plug _plug;
88  Imath::Quatd _goal_rotation;
89  Imath::V3d _goal_translation;
90  Mode _mode;
91  };
92 
93 private:
94 
95  static double _f(const std::vector<double> &x, std::vector<double> &grad, void* _data);
96 
97  int _count = 0;
98  std::vector<double> _x;
99  std::vector<Parameter> _parameters;
100  std::vector<Objective> _objectives;
101  std::shared_ptr<maquina::EvaluationEngine> _ee;
102  maquina::Plug _optimizer_inputs;
103  LogLevel _log_level;
104 };
105 
106 }
Order
Definition: ImathEuler.h:151
Definition: ImathFrame.h:42
The x component of a vector value.
Axis
Definition: Optimizer.h:44
This version of the SDK is unstable, i-e, it may change with no warning.
Definition: AddCurveAction.h:20
static const double rotation_initial_step
Definition: Optimizer.h:61
Definition: Optimizer.h:32
MAQUINA_EXPORT Value default_value(const maquina::StringView &type_name)
Return the default value of a type.
Definition: ImathQuat.h:71
#define MAQUINA_EXPORT
Definition: Export.h:31
A node plug.
Definition: Plug.h:59
LogLevel
Definition: Optimizer.h:35