- 3.0.2 optimal control module.
NLOCBackendMP.hpp
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 
7 #pragma once
8 
9 #include <iostream>
10 #include <memory>
11 #include <thread>
12 #include <mutex>
13 #include <condition_variable>
14 
15 #include "NLOCBackendBase.hpp"
17 
18 namespace ct {
19 namespace optcon {
20 
21 
25 template <size_t STATE_DIM,
26  size_t CONTROL_DIM,
27  size_t P_DIM,
28  size_t V_DIM,
29  typename SCALAR = double,
30  bool CONTINUOUS = true>
31 class NLOCBackendMP final : public NLOCBackendBase<STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS>
32 {
33 public:
34  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
35 
37  typedef typename Base::OptConProblem_t OptConProblem_t;
38 
39  NLOCBackendMP(const OptConProblem_t& optConProblem, const NLOptConSettings& settings);
40 
41 
42  NLOCBackendMP(const OptConProblem_t& optConProblem,
43  const std::string& settingsFile,
44  bool verbose = true,
45  const std::string& ns = "alg");
46 
48  virtual ~NLOCBackendMP();
49 
50 protected:
51  virtual void computeLQApproximation(size_t firstIndex, size_t lastIndex) override;
52 
53  virtual void rolloutShots(size_t firstIndex, size_t lastIndex) override;
54 
55  SCALAR performLineSearch() override;
56 
57 private:
58  enum WORKER_STATE
59  {
60  IDLE,
61  LINE_SEARCH,
62  ROLLOUT_SHOTS,
63  COMPUTE_LQ_PROBLEM,
64  SHUTDOWN
65  };
66 
67  void startupRoutine();
68 
69  void shutdownRoutine();
70 
72 
75  void launchWorkerThreads();
76 
78 
81  void threadWork(size_t threadId);
82 
84 
87  void lineSearchWorker(size_t threadId);
88 
90 
95  void computeLQProblemWorker(size_t threadId);
96 
98  void rolloutShotWorker(size_t threadId);
99 
104  size_t generateUniqueProcessID(const size_t& iterateNo, const int workerState, const size_t resetCount);
105 
107  void printString(const std::string& text);
108 
109  std::vector<std::thread> workerThreads_;
110  std::atomic_bool workersActive_;
111  std::atomic_int workerTask_;
112 
113  std::mutex workerWakeUpMutex_;
114  std::condition_variable workerWakeUpCondition_;
115 
116  std::mutex kCompletedMutex_;
117  std::condition_variable kCompletedCondition_;
118 
119  std::mutex kCompletedMutexCost_;
120  std::condition_variable kCompletedConditionCost_;
121 
122  std::mutex lineSearchResultMutex_;
123  std::mutex alphaBestFoundMutex_;
124  std::condition_variable alphaBestFoundCondition_;
125 
126  std::atomic_size_t alphaTaken_;
127  size_t alphaMax_;
128  size_t alphaExpBest_;
129  size_t alphaExpMax_;
130  std::atomic_bool alphaBestFound_;
131  std::vector<size_t> alphaProcessed_;
132 
133  std::atomic_size_t kTaken_;
134  std::atomic_size_t kCompleted_;
135 
136  size_t KMax_;
137  size_t KMin_;
138 
139  SCALAR lowestCostPrevious_;
140 };
141 
142 
143 } // namespace optcon
144 } // namespace ct
virtual void computeLQApproximation(size_t firstIndex, size_t lastIndex) override
build LQ approximation around trajectory (linearize dynamics and general constraints, quadratize cost, etc)
Definition: NLOCBackendMP-impl.hpp:212
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef NLOCBackendBase< STATE_DIM, CONTROL_DIM, P_DIM, V_DIM, SCALAR, CONTINUOUS > Base
Definition: NLOCBackendMP.hpp:36
virtual ~NLOCBackendMP()
destructor
Definition: NLOCBackendMP-impl.hpp:34
Settings for the NLOptCon algorithm.
Definition: NLOptConSettings.hpp:198
CppAD::AD< CppAD::cg::CG< double > > SCALAR
SCALAR performLineSearch() override
performLineSearch: execute the line search, possibly with different threading schemes ...
Definition: NLOCBackendMP-impl.hpp:409
Base::OptConProblem_t OptConProblem_t
Definition: NLOCBackendMP.hpp:37
Definition: NLOCBackendMP.hpp:31
C++ implementation of GNMS.
Definition: NLOCBackendBase.hpp:48
NLOCBackendMP(const OptConProblem_t &optConProblem, const NLOptConSettings &settings)
Definition: NLOCBackendMP-impl.hpp:13
const bool verbose
Definition: ConstraintComparison.h:18
virtual void rolloutShots(size_t firstIndex, size_t lastIndex) override
integrates the specified shots and computes the corresponding defects
Definition: NLOCBackendMP-impl.hpp:312