benchmark_task_increasing_n_open_loop.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 #include <corbo-core/console.h>
30 #include <corbo-core/signals.h>
31 #include <corbo-core/time.h>
32 #include <corbo-core/time_series.h>
35 
37 
38 #include <memory>
39 #include <string>
40 #include <thread>
41 
42 namespace corbo {
43 
45 
46 void BenchmarkTaskIncreasingHorizonOpenLoop::getAvailableSignals(const Environment& environment, SignalTargetInterface& signal_target,
47  const std::string& ns) const
48 {
49 }
50 
51 void BenchmarkTaskIncreasingHorizonOpenLoop::performTask(Environment& environment, SignalTargetInterface* signal_target, std::string* msg,
52  const std::string& ns)
53 {
54  if (!_open_loop_task || _n_end < _n_start || _repetitions < 1) return;
55 
57  PredictiveController::Ptr controller = std::dynamic_pointer_cast<PredictiveController>(environment.getControllerPtr());
58  if (controller)
59  {
60  ocp = std::dynamic_pointer_cast<StructuredOptimalControlProblem>(controller->getOptimalControlProblem());
61  }
62  else
63  {
64  PRINT_WARNING("This benchmark is designed for predictive controllers.");
65  }
66  if (!ocp)
67  {
69  "We currently support only StructuredOptimalControlProblems."); // TODO(roesmann): we just need an interface to setN() on the ocp!
70  }
71 
72  Duration sleeper(_wait_time);
73 
74  std::vector<double> _controller_step_times;
75  _controller_step_times.reserve(_repetitions);
76 
77  int n_step = std::max(1, _n_step);
78 
79  for (int n = _n_start; n <= _n_end && ok(); n = n + n_step)
80  {
81  PRINT_INFO("=============================");
82  PRINT_INFO("n = " << n);
83  PRINT_INFO("=============================");
84 
85  std::string ns_bench = ns + "bench_" + std::to_string(n) + "/";
86 
87  for (int i = 0; i < _repetitions && ok(); ++i)
88  {
89  PRINT_INFO_COND(i > 0 && i % 10 == 0, "Repetition no " << i + 1 << "/" << _repetitions);
90  environment.reset(); // reset should not deallocate memory of anything that is available via the APIs.
91  if (ocp)
92  {
93  DiscretizationGridInterface::Ptr grid = ocp->getDiscretizationGrid();
94  grid->setN(n, false);
95  if (_initial_tf >= 0)
96  grid->setInitialDt(_initial_tf / double(n - 1));
97  else
98  PRINT_WARNING_ONCE("initial_tf: is negative, using default value of the chosen grid.");
99 
100  // check if we have a shooting grid
101  ShootingGridBase::Ptr shooting_grid = std::dynamic_pointer_cast<ShootingGridBase>(grid);
102  if (shooting_grid)
103  {
104  shooting_grid->setNumControlsPerShootingInterval(
105  _shooting_num_u_per_interval); // TODO(roesmann): do we really need this here?! (it's based on an old version)
106  }
107  else
108  {
109  NonUniformShootingGridBase::Ptr nu_shooting_grid = std::dynamic_pointer_cast<NonUniformShootingGridBase>(grid);
110  if (nu_shooting_grid) nu_shooting_grid->setNumControlsPerShootingInterval(_shooting_num_u_per_interval);
111  }
112  }
113 
114  // only send all signals in the first reptition
115  _open_loop_task->performTask(environment, (i == 0 && _publish_task_signals) ? signal_target : nullptr, msg, ns_bench);
116  // get controller statistics
117  ControllerStatistics::Ptr ctrl_statistics = environment.getController()->getStatistics();
118  if (ctrl_statistics)
119  {
120  _controller_step_times.push_back(ctrl_statistics->step_time.toSec());
121  }
122  }
123  signal_target->sendIndexedValues(ns + "ctrl_step_times", n, _controller_step_times);
124  _controller_step_times.clear();
125  }
126 }
127 
128 bool BenchmarkTaskIncreasingHorizonOpenLoop::verify(const Environment& environment, std::string* msg) const
129 {
130  bool ret_val = true;
131 
132  if (!_open_loop_task)
133  {
134  if (msg) *msg += "BenchmarkTaskIncreasingHorizonOpenLoop(): no OpenLoopControlTask specified.\n";
135  return false;
136  }
137 
138  if (!_open_loop_task->verify(environment, msg)) return false;
139 
140  if (_repetitions < 1)
141  {
142  *msg += "Number of repetitions must be positive.\n";
143  ret_val = false;
144  }
145 
146  if (_n_start < 2 && _n_end < 2)
147  {
148  *msg += "n_start and n_end must be > 1.\n";
149  ret_val = false;
150  }
151 
152  if (_n_end < _n_start)
153  {
154  *msg += "n_end >= n_start ist not satisfied.\n";
155  ret_val = false;
156  }
157 
158  return ret_val;
159 }
160 
162 {
163  if (_open_loop_task) _open_loop_task->reset();
164 }
165 
166 #ifdef MESSAGE_SUPPORT
167 void BenchmarkTaskIncreasingHorizonOpenLoop::toMessage(corbo::messages::BenchmarkTaskIncreasingHorizonOpenLoop& message) const
168 {
169  if (_open_loop_task) _open_loop_task->toMessage(*message.mutable_open_loop_control_task());
170 
171  message.set_n_start(_n_start);
172  message.set_n_end(_n_end);
173  message.set_n_step(_n_step);
174  message.set_repetitions(_repetitions);
175  message.set_shooting_num_u_per_interval(_shooting_num_u_per_interval);
176  message.set_initial_tf(_initial_tf);
177  message.set_wait_time(_wait_time);
178  message.set_publish_task_signals(_publish_task_signals);
179 }
180 void BenchmarkTaskIncreasingHorizonOpenLoop::fromMessage(const corbo::messages::BenchmarkTaskIncreasingHorizonOpenLoop& message,
181  std::stringstream* issues)
182 {
183  // open-loop task
184  _open_loop_task = std::make_shared<OpenLoopControlTask>();
185  _open_loop_task->fromMessage(message.open_loop_control_task(), issues);
186 
187  _n_start = message.n_start();
188  _n_end = message.n_end();
189  _n_step = message.n_step();
190  _repetitions = message.repetitions();
191  _shooting_num_u_per_interval = message.shooting_num_u_per_interval();
192  _initial_tf = message.initial_tf();
193  _wait_time = message.wait_time();
194  _publish_task_signals = message.publish_task_signals();
195 }
196 #endif
197 
198 } // namespace corbo
PRINT_WARNING_ONCE
#define PRINT_WARNING_ONCE(msg)
Print msg-stream only once.
Definition: console.h:149
PRINT_WARNING
#define PRINT_WARNING(msg)
Print msg-stream.
Definition: console.h:145
predictive_controller.h
corbo::BenchmarkTaskIncreasingHorizonOpenLoop::reset
void reset() override
Reset task state.
Definition: benchmark_task_increasing_n_open_loop.cpp:183
corbo
Definition: communication/include/corbo-communication/utilities.h:37
console.h
non_uniform_shooting_grid_base.h
corbo::NonUniformShootingGridBase::Ptr
std::shared_ptr< NonUniformShootingGridBase > Ptr
Definition: non_uniform_shooting_grid_base.h:86
corbo::BenchmarkTaskIncreasingHorizonOpenLoop::performTask
void performTask(Environment &environment, SignalTargetInterface *signal_target=nullptr, std::string *msg=nullptr, const std::string &ns="") override
Perform task.
Definition: benchmark_task_increasing_n_open_loop.cpp:73
corbo::BenchmarkTaskIncreasingHorizonOpenLoop::_repetitions
int _repetitions
Definition: benchmark_task_increasing_n_open_loop.h:135
benchmark_task_increasing_n_open_loop.h
PRINT_INFO_COND
#define PRINT_INFO_COND(cond, msg)
Print msg-stream only if cond == true.
Definition: console.h:131
corbo::DiscretizationGridInterface::Ptr
std::shared_ptr< DiscretizationGridInterface > Ptr
Definition: discretization_grid_interface.h:107
corbo::Duration
Representation of time durations.
Definition: time.h:128
time.h
time_series.h
corbo::BenchmarkTaskIncreasingHorizonOpenLoop::_n_end
int _n_end
Definition: benchmark_task_increasing_n_open_loop.h:133
corbo::BenchmarkTaskIncreasingHorizonOpenLoop::_n_step
int _n_step
Definition: benchmark_task_increasing_n_open_loop.h:134
corbo::BenchmarkTaskIncreasingHorizonOpenLoop::verify
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.
Definition: benchmark_task_increasing_n_open_loop.cpp:150
signals.h
corbo::BenchmarkTaskIncreasingHorizonOpenLoop::_n_start
int _n_start
Definition: benchmark_task_increasing_n_open_loop.h:132
corbo::BenchmarkTaskIncreasingHorizonOpenLoop::BenchmarkTaskIncreasingHorizonOpenLoop
BenchmarkTaskIncreasingHorizonOpenLoop()
Default constructor.
Definition: benchmark_task_increasing_n_open_loop.cpp:66
structured_optimal_control_problem.h
corbo::BenchmarkTaskIncreasingHorizonOpenLoop::_initial_tf
double _initial_tf
Definition: benchmark_task_increasing_n_open_loop.h:136
utilities.h
corbo::BenchmarkTaskIncreasingHorizonOpenLoop::_open_loop_task
OpenLoopControlTask::Ptr _open_loop_task
Definition: benchmark_task_increasing_n_open_loop.h:130
corbo::ControllerStatistics::Ptr
std::shared_ptr< ControllerStatistics > Ptr
Definition: controllers/include/corbo-controllers/statistics.h:81
corbo::PredictiveController::Ptr
std::shared_ptr< PredictiveController > Ptr
Definition: predictive_controller.h:97
n
PlainMatrixType mat * n
Definition: eigenvalues.cpp:41
shooting_grid_base.h
corbo::BenchmarkTaskIncreasingHorizonOpenLoop::_shooting_num_u_per_interval
int _shooting_num_u_per_interval
Definition: benchmark_task_increasing_n_open_loop.h:138
corbo::ok
bool ok()
global method to check whether to proceed or cancel the current action
Definition: global.cpp:54
max
#define max(a, b)
Definition: datatypes.h:20
PRINT_INFO
#define PRINT_INFO(msg)
Print msg-stream.
Definition: console.h:117
corbo::BenchmarkTaskIncreasingHorizonOpenLoop::_publish_task_signals
bool _publish_task_signals
Definition: benchmark_task_increasing_n_open_loop.h:139
PRINT_ERROR
#define PRINT_ERROR(msg)
Print msg-stream as error msg.
Definition: console.h:173
corbo::BenchmarkTaskIncreasingHorizonOpenLoop::getAvailableSignals
void getAvailableSignals(const Environment &environment, SignalTargetInterface &signal_target, const std::string &ns="") const override
Retrieve available signals from the task.
Definition: benchmark_task_increasing_n_open_loop.cpp:68
corbo::BenchmarkTaskIncreasingHorizonOpenLoop::_wait_time
double _wait_time
Definition: benchmark_task_increasing_n_open_loop.h:137
corbo::ShootingGridBase::Ptr
std::shared_ptr< ShootingGridBase > Ptr
Definition: shooting_grid_base.h:86
corbo::StructuredOptimalControlProblem::Ptr
std::shared_ptr< StructuredOptimalControlProblem > Ptr
Definition: structured_optimal_control_problem.h:87


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Wed Mar 2 2022 00:05:37