benchmark_task_varying_initial_state.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
26 
29 
31 #include <corbo-core/console.h>
32 #include <corbo-core/signals.h>
33 #include <corbo-core/time.h>
34 #include <corbo-core/time_series.h>
35 
36 #include <memory>
37 #include <string>
38 #include <thread>
39 
40 namespace corbo {
41 
43 
45  const std::string& ns) const
46 {
47 }
48 
49 void BenchmarkTaskVaryingInitialState::performTask(Environment& environment, SignalTargetInterface* signal_target, std::string* msg,
50  const std::string& ns)
51 {
52  if (!_main_task && _x01_n < 1) return;
53 
54  Eigen::VectorXd x0 = _x0_default;
55 
56  if (x0.size() != environment.getController()->getStateDimension())
57  {
58  if (msg) *msg += "BenchmarkTaskVaryingInitialState: dimension mismatch between x0_default and controller.\n";
59  }
60 
61  if (x0.size() < 2)
62  {
63  _x02_n = 1;
64  }
65 
66  double x01_dir = _x01_end - _x01_start;
67  double x01_step = _x01_n < 2 ? x01_dir : x01_dir / ((double)_x01_n - 1);
68  double x02_dir = _x02_end - _x02_start;
69  double x02_step = _x02_n < 2 ? x02_dir : x02_dir / ((double)_x02_n - 1);
70 
71  Duration sleeper(_wait_time);
72 
73  int idx = 0;
74  for (int i = 0; i < _x01_n; ++i)
75  {
76  x0[0] = _x01_start + (double)i * x01_step;
77 
78  for (int j = 0; j < _x02_n; ++j)
79  {
80  if (x0.size() > 1) x0[1] = _x02_start + (double)j * x02_step;
81 
82  PRINT_INFO("=============================");
83  PRINT_INFO("run: " << idx + 1 << "/" << _x01_n * _x02_n);
84  PRINT_INFO("=============================");
85 
86  std::string ns_bench = ns + "bench_" + std::to_string(idx) + "/";
87 
88  environment.reset();
89  environment.getPlantPtr()->setState(x0);
90 
91  _main_task->reset();
92  _main_task->performTask(environment, signal_target, msg, ns_bench);
93 
94  sleeper.sleep();
95 
96  ++idx;
97  }
98  }
99 }
100 
101 bool BenchmarkTaskVaryingInitialState::verify(const Environment& environment, std::string* msg) const
102 {
103  bool ret_val = true;
104 
105  if (!_main_task)
106  {
107  if (msg) *msg += "BenchmarkTaskVaryingInitialState(): no main task specified.\n";
108  return false;
109  }
110 
111  if (!_main_task->verify(environment, msg)) return false;
112 
113  int dim_x = environment.getController()->getStateDimension();
114 
115  if (_x01_n <= 0)
116  {
117  *msg += "_x01_n > 0 required.\n";
118  ret_val = false;
119  }
120 
121  if (_x02_n <= 0 && dim_x < 2)
122  {
123  *msg += "_x01_n > 0 required if state dimension is > 1.\n";
124  ret_val = false;
125  }
126 
127  if (_x0_default.size() != dim_x)
128  {
129  *msg += "Dimension of x0_default does not match state dimension.\n";
130  ret_val = false;
131  }
132 
133  return ret_val;
134 }
135 
137 {
138  if (_main_task) _main_task->reset();
139 }
140 
141 #ifdef MESSAGE_SUPPORT
142 void BenchmarkTaskVaryingInitialState::toMessage(corbo::messages::BenchmarkTaskVaryingInitialState& message) const
143 {
144  // if (_main_task) _main_task->toMessage(*message.mutable_main_task());
145  //
146  message.set_x01_start(_x01_start);
147  message.set_x01_end(_x01_end);
148  message.set_x01_n(_x01_n);
149 
150  message.set_x02_start(_x02_start);
151  message.set_x02_end(_x02_end);
152  message.set_x02_n(_x02_n);
153 
154  ClosedLoopControlTask::Ptr closed_loop_task = std::dynamic_pointer_cast<ClosedLoopControlTask>(_main_task);
155  if (closed_loop_task)
156  closed_loop_task->toMessage(*message.mutable_closed_loop_control_task());
157  else
158  {
159  OpenLoopControlTask::Ptr open_loop_task = std::dynamic_pointer_cast<OpenLoopControlTask>(_main_task);
160  if (open_loop_task) open_loop_task->toMessage(*message.mutable_open_loop_control_task());
161  }
162 
163  // x0 default
164  if (_x0_default.size() > 0)
165  {
166  message.mutable_x0_default()->Resize(_x0_default.size(), false);
167  Eigen::Map<Eigen::VectorXd>(message.mutable_x0_default()->mutable_data(), _x0_default.size()) = _x0_default;
168  }
169 
170  message.set_wait_time(_wait_time);
171 }
172 void BenchmarkTaskVaryingInitialState::fromMessage(const corbo::messages::BenchmarkTaskVaryingInitialState& message, std::stringstream* issues)
173 {
174  switch (message.main_task_case())
175  {
176  case messages::BenchmarkTaskVaryingInitialState::kClosedLoopControlTask:
177  {
178  ClosedLoopControlTask::Ptr closed_loop_task = std::make_shared<ClosedLoopControlTask>();
179  closed_loop_task->fromMessage(message.closed_loop_control_task(), issues);
180  _main_task = closed_loop_task;
181  break;
182  }
183  case messages::BenchmarkTaskVaryingInitialState::kOpenLoopControlTask:
184  {
185  OpenLoopControlTask::Ptr open_loop_task = std::make_shared<OpenLoopControlTask>();
186  open_loop_task->fromMessage(message.open_loop_control_task(), issues);
187  _main_task = open_loop_task;
188  break;
189  }
190  default:
191  {
192  _main_task.reset();
193  if (issues) *issues << "BenchmarkTaskVaryingInitialState: selected task not implemented.\n";
194  }
195  };
196 
197  _x01_start = message.x01_start();
198  _x01_end = message.x01_end();
199  _x01_n = message.x01_n();
200 
201  _x02_start = message.x02_start();
202  _x02_end = message.x02_end();
203  _x02_n = message.x02_n();
204 
205  _x0_default = Eigen::Map<const Eigen::VectorXd>(message.x0_default().data(), message.x0_default_size());
206 
207  _wait_time = message.wait_time();
208 }
209 #endif
210 
211 } // namespace corbo
Standard environment for control tasks.
Definition: environment.h:49
Perform open-loop control task.
std::shared_ptr< OpenLoopControlTask > Ptr
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
void getAvailableSignals(const Environment &environment, SignalTargetInterface &signal_target, const std::string &ns="") const override
Retrieve available signals from the task.
Interface class for signal targets.
void performTask(Environment &environment, SignalTargetInterface *signal_target=nullptr, std::string *msg=nullptr, const std::string &ns="") override
Perform task.
void reset()
Reset environment.
void sleep()
Sleep (current thread) for the specified duration.
Definition: time.h:178
std::shared_ptr< ClosedLoopControlTask > Ptr
PlantInterface::Ptr getPlantPtr()
Write access to the underlying plant.
Definition: environment.h:77
bool verify(const Environment &environment, std::string *msg=nullptr) const override
Check if the environment and other settings satisfy all requirements for the given task...
const ControllerInterface::Ptr & getController() const
Read access to the underlying controller.
Definition: environment.h:67
Representation of time durations.
Definition: time.h:106
Perform closed-loop control task.
#define PRINT_INFO(msg)
Print msg-stream.
Definition: console.h:117


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:06:45