solver  1.0
DataStructures.h
Go to the documentation of this file.
1 /*
2  This file is part of the EnergyOptimizatorOfRoboticCells program.
3 
4  EnergyOptimizatorOfRoboticCells is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  EnergyOptimizatorOfRoboticCells is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with EnergyOptimizatorOfRoboticCells. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef HLIDAC_PES_DATASTRUCTURES_H
19 #define HLIDAC_PES_DATASTRUCTURES_H
20 
27 #include <vector>
28 #include <unordered_map>
29 #include "RoboticLine.h"
30 #include "Shared/Utils.h"
31 #include "Shared/Exceptions.h"
32 
33 /* PROCESS PHASES OF THE PARALLEL SOLVER */
34 
36 enum Algo {
37  LP_INITIAL_PROBLEM = 0, POWER_MODE_HEURISTIC = 1, LOCATION_CHANGE_HEURISTIC = 2, PATH_CHANGE = 4
38 };
39 
40 /* GRAPH REPRESENTATION FOR DISTANCE MATRIX COMPUTATION */
41 
46 struct Edge {
52  uint32_t i;
54  uint32_t j;
59  double length;
60 };
61 
65 struct Graph {
67  std::vector<Edge> edges;
69  uint32_t startIdent;
71  uint32_t endIdent;
73  std::vector<StaticActivity*> idToActivity;
75  std::vector<std::vector<Edge>> idToSuccessors;
76 };
77 
78 /* HAMILTONIAN CIRCUIT REPRESENTATION */
79 
84 template <class T>
87  double minLength;
89  std::vector<T*> circuit;
90 };
91 
97  ShortestCircuit() = default;
98  ShortestCircuit(const std::vector<StaticActivity*> arg1, const std::vector<Location*>& arg2) : circuit(arg1), fixedLocations(arg2) { }
99  bool operator==(const ShortestCircuit& sc) const {
100  return circuit == sc.circuit && fixedLocations == sc.fixedLocations;
101  }
102 
104  std::vector<StaticActivity*> circuit;
106  std::vector<Location*> fixedLocations;
107 };
108 
114  CircuitRecord(const ShortestCircuit& arg1, const HamiltonianCircuit<Location>& arg2) : sc(arg1), hc(arg2) { }
116  bool operator<(const CircuitRecord& cr) const {
117  return hc.minLength < cr.hc.minLength;
118  }
119 
124 };
125 
126 /* GENERATED TUPLE - POTENTIALLY FEASIBLE SOLUTION */
127 
133 struct CircuitTuple {
135  std::vector<CircuitRecord> tuple;
136 };
137 
138 /* REPRESENTATION OF LP SOLUTION */
139 
150  double totalEnergy;
152  std::vector<std::vector<double>> startLocs;
154  std::vector<std::vector<double>> durLocs;
156  std::vector<std::vector<double>> startMvs;
158  std::vector<std::vector<double>> durMvs;
159 
161  std::map<Activity*, std::pair<double, double>> actToStartAndDur;
163  std::map<ActivityMode*, std::pair<double, double>> modeToStartAndDur;
164 };
165 
166 /* COLLISION RESOLUTION */
167 
174  CollisionResolution() : a1(nullptr), a2(nullptr), multiplier(0), intersection(0.0) { }
180  int32_t multiplier;
182  double intersection;
183 };
184 
185 /* POWER SAVING MODE SELECTION */
186 
192  ModeSwitchInfo() = default;
193  ModeSwitchInfo(uint32_t r, uint32_t l, Location* relLoc, RobotPowerMode* f, RobotPowerMode *t, double ee)
194  : robotIdx(r), locationIdx(l), loc(relLoc), from(f), to(t), estimatedEnergy(ee) { }
195 
197  bool operator<(const ModeSwitchInfo& msi) const {
198  return estimatedEnergy < msi.estimatedEnergy;
199  }
200 
202  uint32_t robotIdx;
204  uint32_t locationIdx;
213 };
214 
220  ActivityModeInfo(ActivityMode* m, double s, double d) : mode(m), curStartTime(s), curDuration(d) { }
221  ActivityModeInfo(ActivityMode* m, RobotPowerMode* pm, double e, double s, double d, double minDur, double maxDur)
222  : mode(m), pwrm(pm), energy(e), curStartTime(s), curDuration(d), minDuration(minDur), maxDuration(maxDur) { }
223 
225  bool operator<(const ActivityModeInfo& ami) const {
226  return curStartTime < ami.curStartTime;
227  }
228 
234  double energy;
236  double curStartTime;
238  double curDuration;
240  double minDuration;
242  double maxDuration;
243 };
244 
245 /* TABU LIST */
246 
254 class TabuList {
255  public:
257  TabuList(const uint32_t& tabuListSize) : mListIdx(0u), mTabu(tabuListSize) { }
258 
265  bool isTabu(Location *l, RobotPowerMode *from, RobotPowerMode *to) const {
266  return std::find(mTabu.cbegin(), mTabu.cend(), Element(l, from, to)) != mTabu.cend();
267  }
268 
275  assert(!isTabu(l, from, to) && "Adding already forbidden combination!");
276  if (mListIdx < mTabu.size()) {
277  mTabu[mListIdx++] = {l, from, to};
278  mListIdx = (mListIdx % mTabu.size());
279  } else {
280  throw SolverException(caller(), "Cannot add an element to an empty tabu list!");
281  }
282  }
283 
288  void diversify() {
289  uint32_t startIdx = mListIdx;
290  int32_t toErase = (int32_t) ceil(0.3*mTabu.size());
291  do {
292  if (!mTabu[startIdx].isEmpty()) {
293  mTabu[startIdx] = Element();
294  --toErase;
295  }
296 
297  startIdx = ((startIdx+1) % mTabu.size());
298 
299  } while (startIdx != mListIdx && toErase > 0);
300  }
301  private:
305  struct Element {
306  Element() : loc(nullptr), m1(nullptr), m2(nullptr) { }
307  Element(Location *l, RobotPowerMode *rm1, RobotPowerMode *rm2) : loc(l), m1(rm1), m2(rm2) { }
308 
310  bool isEmpty() const {
311  return loc == nullptr && m1 == nullptr && m2 == nullptr;
312  }
314  bool operator==(const Element& e) const {
315  return loc == e.loc && m1 == e.m1 && m2 == e.m2;
316  }
317 
324  };
325 
327  uint32_t mListIdx;
329  std::vector<Element> mTabu;
330 };
331 
332 /* MOVING AVERAGE */
333 
339 template <class T>
341  public:
346  MovingAverage(uint32_t length) : mLength(length) {
347  if (mLength <= 0)
348  throw InvalidArgument(caller(), "A positive length is expected!");
349  }
350 
352  double average() const {
353  if (!mData.empty())
354  return std::accumulate(mData.cbegin(), mData.cend(), T())/mData.size();
355  else
356  return T();
357  }
359  void addValue(const T& val) {
360  if (mData.size() < mLength) {
361  mData.push_back(val);
362  } else {
363  mData.erase(mData.begin());
364  mData.push_back(val);
365  }
366  }
368  bool filled() const {
369  return mData.size() == mLength;
370  }
372  void clear() {
373  mData.clear();
374  }
375  private:
377  uint32_t mLength;
379  std::vector<T> mData;
380 };
381 
382 /* USEFUL SHORTCUTS FOR COMPLEX DATA TYPES */
383 
389 namespace std {
391  template <>
392  struct hash<ShortestCircuit> {
393  uintptr_t operator() (const ShortestCircuit& sc) const {
394  uintptr_t hash1 = hashOW(sc.circuit);
395  uintptr_t hash2 = hashEW(sc.fixedLocations);
396  return hash1^hash2;
397  }
398  };
399 }
400 
402 
404 using PrecalculatedCircuits = std::unordered_map<ShortestCircuit, HamiltonianCircuit<Location>>;
405 
406 #endif
407 
std::vector< T * > circuit
Closed path through locations or static activities, each static activity is visited just once...
bool isTabu(Location *l, RobotPowerMode *from, RobotPowerMode *to) const
It checks whether the modification of a partial solution is allowed, i.e. the move/modification is no...
std::vector< std::vector< double > > durLocs
Durations assigned to static activities' locations.
void addTabu(Location *l, RobotPowerMode *from, RobotPowerMode *to)
It adds a performed modification into the tabu list.
void diversify()
The base class incorporating common properties of robot operations and movements. ...
Definition: RoboticLine.h:68
uint32_t mListIdx
Current index to the tabu list, i.e. a current write position.
An element of the tabu list.
std::vector< std::vector< double > > startMvs
Start times assigned to dynamic activities' movements.
CollisionResolution()
Empty collision constructed – a1 and a2 are null pointers.
std::vector< StaticActivity * > idToActivity
Node index to static activity, note that dynamic activities are already incorporated in the lengths o...
std::unordered_map< ShortestCircuit, HamiltonianCircuit< Location >> PrecalculatedCircuits
Mapping of ShortestCircuit to the related shortest closed path through locations. ...
StaticActivity * t
Edge enters to activity t.
Closed path through locations or static activities including an order of operations, i.e. alternative.
RobotPowerMode * to
The candidate power saving mode to switch to.
Shortest closed path through locations.
RobotPowerMode * pwrm
Power saving mode of the robot if it applies, i.e. mode can be cast to Location.
Hamiltonian circuit through static activities and the fixed locations.
double energy
Energy required by the activity for the current duration.
STL namespace.
Activity * a2
Second activity involved in the collision.
double estimatedEnergy
Estimated energy consumption after switching to the candidate power saving mode.
A general exception of the program.
Definition: Exceptions.h:58
std::vector< CircuitRecord > tuple
Tuple, i.e. a partially fixed problem.
The structure stores how to resolve one collision between robots.
bool filled() const
Returns whether the average is calculated from the full length.
ShortestCircuit sc
Selected alternative and fixed locations.
A graph edge in the distance graph.
std::vector< Edge > edges
Graph edges.
double minDuration
Minimal possible duration of the activity for the given mode, robot, and power saving mode (if mode ~...
double curStartTime
Current start time of the activity.
bool operator==(const Element &e) const
The element is equal to the another one if all the data members are equal.
A short-term memory, containing a list of forbidden moves, that mitigates the risk of cycling...
ActivityMode * mode
Selected mode of an activity, i.e. movement or location.
Various auxiliary functions used across the program.
uint64_t hashOW(const C &v)
It calculates a hash of the container, the order of elements influences (Order Wise) the hash value...
std::vector< std::vector< double > > startLocs
Start times assigned to static activities' locations.
uintptr_t hashEW(const C &v)
It calculates a hash of the container, the order of elements does not influence (Element Wise) the ha...
int32_t multiplier
Multiplier addressing the time shift in the number of cycles.
double curDuration
Current duration of the activity.
The file defines extended exceptions for the better error handling in the program.
double maxDuration
Maximal possible duration of the activity limited by e.g. the robot, performed operation, etc.
double totalEnergy
Energy consumption of a robotic cell for this timing.
A partially fixed problem, i.e. tuple.
Exception is thrown if a method is given invalid parameters or a user provides invalid program argume...
Definition: Exceptions.h:120
Collection of locations in which a robot operation (or waiting) can be performed. ...
Definition: RoboticLine.h:304
Structure encapsulates the time and energy properties of an activity with its selected mode (a moveme...
void clear()
Remove all the values from which the running average is calculated.
TabuList(const uint32_t &tabuListSize)
Constructs a tabu list with the fixed size.
bool operator<(const CircuitRecord &cr) const
It enables to sort the circuits according to their lengths (shorter is better).
It represents the power saving mode of the robot.
Definition: RoboticLine.h:359
bool operator<(const ModeSwitchInfo &msi) const
Sorting the switching alternatives according to the estimated energy consumption (less is better)...
void addValue(const T &val)
Add a new value to the moving average.
Location * loc
Location where a power saving mode was changed.
MovingAverage(uint32_t length)
It constructs an instance of the moving average.
std::vector< Element > mTabu
Tabu list containing the forbidden modifications.
bool operator<(const ActivityModeInfo &ami) const
Sort the activities according to start time, left to right direction in the Gantt.
double minLength
The lower bound on the robot cycle time for this circuit (neglecting global constraints).
A graph in which random alternatives will be searched for.
Algo
Defining constants for different states of the heuristic.
std::vector< std::vector< Edge > > idToSuccessors
Node index to leaving edges that enters to its (node) successors.
uint32_t endIdent
An index of the end node.
double intersection
Duration of the collision between activities a1 and a2.
uint32_t locationIdx
Index of the related location of the robot.
bool isEmpty() const
Whether a tabu list element is empty, i.e. it does not contain a modification.
HamiltonianCircuit< Location > hc
Shortest closed path through locations, fixed locations are included.
RobotPowerMode * from
The original power saving mode.
Either a movement or location.
Definition: RoboticLine.h:54
uint32_t startIdent
An index of the start node.
StaticActivity * f
Edge leaves the activity f.
std::vector< T > mData
The vector of values from which the running average is calculated.
Records a potential energy impact if a power saving mode of a robot is switched to another one...
uint32_t j
An index assigned to the activity t, used for an access to the distance matrix.
Location of the robot used either during work (welding) or waiting.
Definition: RoboticLine.h:192
std::map< Activity *, std::pair< double, double > > actToStartAndDur
Activity to the assigned start time and duration.
Location * loc
A pointer to the considered location.
The file contains various classes devoted to abstract representation of the robotic cell...
double length
std::map< ActivityMode *, std::pair< double, double > > modeToStartAndDur
Selected location/movement to the assigned start time and duration.
std::vector< std::vector< double > > durMvs
Durations assigned to dynamic activities' movements.
std::vector< StaticActivity * > circuit
Selected alternative, i.e. order of operations.
std::vector< Location * > fixedLocations
Locations required to be fixed to enforce the spatial compatibility between robots.
The class encapsulating the calculation of the moving average.
Obtained timing for a partial problem.
uint32_t robotIdx
Index of the robot.
Activity * a1
First activity involved in the collision.
uint32_t i
An index assigned to the activity f, used for an access to the distance matrix.
uint32_t mLength
The maximal number of values used for the calculation of the moving average.
double average() const
Current value of the running average.
RobotPowerMode * m2
The power saving mode which replaced the original one.
RobotPowerMode * m1
Original power saving mode that was replaced by another one.