ComputingPowerRequestStrategy.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package RobotComponents
19  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl.txt
22  * GNU General Public License
23  */
24 
26 
27 namespace armarx
28 {
29  bool
31  {
32  return std::all_of(requestStrategies.begin(),
33  requestStrategies.end(),
34  [](ComputingPowerRequestStrategyBasePtr& s)
35  { return s->shouldAllocateComputingPower(); });
36  }
37 
38  void
40  {
41  if (skipping)
42  {
43  allocatedLastTime = Clock::now();
44  }
45  else
46  {
47  allocatedLastTime += std::chrono::seconds{timeDeltaInSeconds};
48  }
49  }
50 
51  void
53  {
54  if (skipping)
55  {
56  allocateNextTime = nodeCount + nodeCountDelta;
57  }
58  else
59  {
60  allocateNextTime += nodeCountDelta;
61  }
62  }
63 
64  void
65  cprs::TaskStatus::updateNodeCount(Ice::Long count, const Ice::Current&)
66  {
67  for (auto& strat : strategyPerTaskStatus)
68  {
69  strat.second->updateNodeCount(count);
70  }
71  }
72 
73  void
74  cprs::TaskStatus::updateTaskStatus(armarx::TaskStatus::Status newStatus, const Ice::Current&)
75  {
76  const auto it = strategyPerTaskStatus.find(newStatus);
77  current = (it == strategyPerTaskStatus.end()) ? nullptr : it->second.get();
78 
79  for (auto& strat : strategyPerTaskStatus)
80  {
81  strat.second->updateTaskStatus(newStatus);
82  }
83  }
84 
85  void
87  {
88  for (auto& strat : strategyPerTaskStatus)
89  {
90  strat.second->allocatedComputingPower();
91  }
92  }
93 
94  void
96  Ice::Long tries,
97  const Ice::Current&)
98  {
99  for (auto& strat : strategyPerTaskStatus)
100  {
101  strat.second->updateNodeCreations(nodesCreated, tries);
102  }
103  }
104 
105  void
107  Ice::Long tries,
108  const Ice::Current&)
109  {
110  for (std::size_t i = 0; i < static_cast<std::size_t>(tries); ++i)
111  {
112  backlog.at((currentBacklogIndex + i) % backlogSize) =
113  i < static_cast<std::size_t>(nodesCreated) ? 0 : 1;
114  }
115 
116  currentBacklogIndex = (currentBacklogIndex + tries) % backlogSize;
117  }
118 
119  void
121  {
122  const auto perc = std::accumulate(backlog.begin(), backlog.end(), 1.f) / backlog.size();
123  const auto usedTimeDelta = timeDeltaInSeconds / (1.f + sigma * perc);
124  ARMARX_CHECK_EXPRESSION(usedTimeDelta >= 0);
125 
126  if (skipping)
127  {
128  allocatedLastTime = Clock::now();
129  }
130  else
131  {
132  allocatedLastTime += std::chrono::seconds{static_cast<std::size_t>(usedTimeDelta)};
133  }
134  }
135 
136  void
138  {
139  for (auto& s : requestStrategies)
140  {
141  s->setCurrentStateAsInitialState();
142  }
143  }
144 
145  void
147  {
148  for (auto& s : requestStrategies)
149  {
150  s->updateNodeCount(count);
151  }
152  }
153 
154  void
155  cprs::CompoundedRequestStrategy::updateTaskStatus(armarx::TaskStatus::Status newStatus,
156  const Ice::Current&)
157  {
158  for (auto& s : requestStrategies)
159  {
160  s->updateTaskStatus(newStatus);
161  }
162  }
163 
164  void
166  {
167  for (auto& s : requestStrategies)
168  {
169  s->allocatedComputingPower();
170  }
171  }
172 
173  void
175  Ice::Long tries,
176  const Ice::Current&)
177  {
178  for (auto& s : requestStrategies)
179  {
180  s->updateNodeCreations(nodesCreated, tries);
181  }
182  }
183 
184 } // namespace armarx
ComputingPowerRequestStrategy.h
armarx::cprs::CompoundedRequestStrategy::updateTaskStatus
void updateTaskStatus(armarx::TaskStatus::Status newStatus, const Ice::Current &=Ice::emptyCurrent) override
Calls this function for all compounded strategies.
Definition: ComputingPowerRequestStrategy.cpp:155
armarx::cprs::And::shouldAllocateComputingPower
bool shouldAllocateComputingPower(const ::Ice::Current &=Ice::emptyCurrent) override
Returns true if all compounded strategies return true.
Definition: ComputingPowerRequestStrategy.cpp:30
armarx::cprs::TaskStatus::updateNodeCount
void updateNodeCount(Ice::Long count, const Ice::Current &=Ice::emptyCurrent) override
Passes this call to all sub strategies.
Definition: ComputingPowerRequestStrategy.cpp:65
armarx::cprs::TaskStatus::updateNodeCreations
void updateNodeCreations(Ice::Long nodesCreated, Ice::Long tries, const Ice::Current &=Ice::emptyCurrent) override
Passes this call to all sub strategies.
Definition: ComputingPowerRequestStrategy.cpp:95
armarx::cprs::CompoundedRequestStrategy::updateNodeCount
void updateNodeCount(Ice::Long count, const Ice::Current &=Ice::emptyCurrent) override
Calls this function for all compounded strategies.
Definition: ComputingPowerRequestStrategy.cpp:146
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:918
armarx::cprs::CompoundedRequestStrategy::setCurrentStateAsInitialState
void setCurrentStateAsInitialState(const ::Ice::Current &=Ice::emptyCurrent) override
Calls this function for all compounded strategies.
Definition: ComputingPowerRequestStrategy.cpp:137
armarx::cprs::NoNodeCreated::allocatedComputingPower
void allocatedComputingPower(const Ice::Current &=Ice::emptyCurrent) override
Definition: ComputingPowerRequestStrategy.cpp:120
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::cprs::TaskStatus::allocatedComputingPower
void allocatedComputingPower(const Ice::Current &=Ice::emptyCurrent) override
Passes this call to all sub strategies.
Definition: ComputingPowerRequestStrategy.cpp:86
armarx::cprs::ElapsedTime::allocatedComputingPower
void allocatedComputingPower(const Ice::Current &=Ice::emptyCurrent) override
Sets the next time to allocate depending on the flag skipping.
Definition: ComputingPowerRequestStrategy.cpp:39
armarx::cprs::CompoundedRequestStrategy::updateNodeCreations
void updateNodeCreations(Ice::Long nodesCreated, Ice::Long tries, const Ice::Current &=Ice::emptyCurrent) override
Passes this call to all sub strategies.
Definition: ComputingPowerRequestStrategy.cpp:174
armarx::cprs::TaskStatus::updateTaskStatus
void updateTaskStatus(armarx::TaskStatus::Status newStatus, const Ice::Current &=Ice::emptyCurrent) override
Switches the current strategy and passes this call to all sub strategies.
Definition: ComputingPowerRequestStrategy.cpp:74
armarx::cprs::CompoundedRequestStrategy::allocatedComputingPower
void allocatedComputingPower(const Ice::Current &=Ice::emptyCurrent) override
Calls this function for all compounded strategies.
Definition: ComputingPowerRequestStrategy.cpp:165
armarx::cprs::TotalNodeCount::allocatedComputingPower
void allocatedComputingPower(const Ice::Current &=Ice::emptyCurrent) override
Sets the next creation count to allocate depending on the flag skipping.
Definition: ComputingPowerRequestStrategy.cpp:52
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::cprs::NoNodeCreated::updateNodeCreations
void updateNodeCreations(Ice::Long nodesCreated, Ice::Long tries, const Ice::Current &=Ice::emptyCurrent) override
Updates the number of failed node creations.
Definition: ComputingPowerRequestStrategy.cpp:106
armarx::core::time::Clock::now
DateTime now() const
Current date/time of the clock.
Definition: Clock.cpp:22