solver  1.0
ParallelHeuristicSolver.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_PARALLEL_SOLVER_H
19 #define HLIDAC_PES_PARALLEL_SOLVER_H
20 
27 #include <atomic>
28 #include <map>
29 #include <vector>
30 #include "Shared/Algorithms.h"
35 
47  public:
58  Solution solve();
59  private:
68  void controlThread();
80  void workerThread(uint32_t threadId, std::vector<std::vector<CircuitRecord>> initialCircuits, PrecalculatedCircuits precalculatedCircuits);
81 
103  std::vector<CircuitRecord> generateRandomAlternatives(const uint32_t& threadId, const Graph& g, const DistanceMatrix<double>& m);
112  HamiltonianCircuit<Location> getShortestCircuit(const uint32_t& threadId, const std::vector<StaticActivity*>& circuit,
113  const std::vector<Location*>& fixed, bool writeCircuit = true);
119  std::vector<std::vector<CircuitRecord>> generateShortestCircuits(PrecalculatedCircuits& precalculatedCircuits);
120 
128  static double penaltyFunction(const double& minCycle, const double& demandedCycleTime);
129 
136  uint32_t selectAlternative(const std::vector<CircuitRecord>& alternatives) const;
147  double calculateNewCycleTime(const uint32_t& threadId, const double& currentCycleTime, std::vector<StaticActivity*>& alternative,
148  std::vector<Location*>& fixed, Location *toFix, PrecalculatedCircuits& precalculatedCircuits);
156  void addRandomTuplesToKB(const uint32_t& threadId, const uint32_t& numOfTuples,
157  std::vector<std::vector<CircuitRecord>>& initialCircuits, PrecalculatedCircuits& precalculatedCircuits);
162  void generatePromisingTuples(const uint32_t& threadId);
163 
172  double changeRobotPaths(uint32_t threadId, const CircuitTuple& t, PartialSolution& ps, const std::vector<std::vector<Location*>>& fixed);
173 
179  void printProgressInfo(const double& currentRuntime);
185  void writePerformanceRecordToLogFile(const double& initializationTime, const double& finalRuntime);
186 
188  double mRuntime;
190  uint32_t mTabuListSize;
191 
193  std::atomic<bool> mFeasible;
195  std::atomic<bool> mStopCondition;
196 
205 
207  std::vector<std::vector<std::vector<double>>> mDist;
209  std::vector<std::vector<std::vector<int64_t>>> mPreds;
211  std::vector<std::vector<std::vector<Location*>>> mLocs;
212 
214  std::vector<SpatialCmpTuple> mSortedSptCmp;
215 };
216 
217 #endif
std::vector< SpatialCmpTuple > mSortedSptCmp
The vector of spatial compatibility pairs heuristically sorted in decreasing order of difficulty...
RoboticLine mLine
Robotic cell to be optimized.
The structure representing a solution found by an algorithm.
Definition: Solution.h:50
void addRandomTuplesToKB(const uint32_t &threadId, const uint32_t &numOfTuples, std::vector< std::vector< CircuitRecord >> &initialCircuits, PrecalculatedCircuits &precalculatedCircuits)
It generates and adds a requested number of tuples to the KnowledgeBase.
ParallelHeuristicSolver(const RoboticLine &l, const PrecalculatedMapping &m)
It initializes the heuristic, i.e. preallocates some arrays, calculates the length of the tabu list...
Instance of this class includes all the data structures and methods related to a robot.
Definition: RoboticLine.h:432
std::unordered_map< ShortestCircuit, HamiltonianCircuit< Location >> PrecalculatedCircuits
Mapping of ShortestCircuit to the related shortest closed path through locations. ...
std::vector< std::vector< CircuitRecord > > generateShortestCircuits(PrecalculatedCircuits &precalculatedCircuits)
It finds random alternatives for each robot and calculates their shortest closed paths through locati...
void workerThread(uint32_t threadId, std::vector< std::vector< CircuitRecord >> initialCircuits, PrecalculatedCircuits precalculatedCircuits)
The worker thread generates initial tuples, and optimizes them by local modifications proposed by sub...
double changeRobotPaths(uint32_t threadId, const CircuitTuple &t, PartialSolution &ps, const std::vector< std::vector< Location * >> &fixed)
It randomly modifies the robot paths to enable the heuristic to search otherwise unreachable parts of...
std::atomic< bool > mFeasible
Used to stop other OpenMP threads if the instance infeasibility is detected by a thread.
PrecalculatedMapping mMapping
Fast searching in the data structure of the robotic cell.
static double penaltyFunction(const double &minCycle, const double &demandedCycleTime)
The function expressing the risk of exceeding the robot cycle time depending on its lower estimation...
Declares LP solver which deals with partially fixed energy optimization problems created by the paral...
Solution solve()
It optimizes the robotic cell and returns the best found solution.
std::vector< CircuitRecord > generateRandomAlternatives(const uint32_t &threadId, const Graph &g, const DistanceMatrix< double > &m)
Generates some random alternatives, i.e. candidates for a feasible order, from a distance graph and d...
uint32_t selectAlternative(const std::vector< CircuitRecord > &alternatives) const
It randomly selects a robot alternative according to a distribution function that slightly prefers th...
void controlThread()
Generates various alternatives, launches worker threads, prints progress info, joins the workers...
std::atomic< bool > mStopCondition
The control thread sets this flag to true to stop all the worker threads.
Protected access to the shared data of the threads of the parallel heuristic.
Definition: KnowledgeBase.h:46
Various data structures used by the heuristic.
uint32_t mTabuListSize
The number of tabu list elements calculated in the constructor.
The optimization sub-heuristics and Linear Programming solver for partially fixed problems...
std::vector< std::vector< std::vector< Location * > > > mLocs
Preallocated 2-D array of locations for getShortestCircuit method, indexed by thread.
double mRuntime
The runtime of this heuristic, updated from time to time.
Defines the optimization part of the parallel heuristic, i.e. sub-heuristics and the evaluation of tu...
A partially fixed problem, i.e. tuple.
double calculateNewCycleTime(const uint32_t &threadId, const double &currentCycleTime, std::vector< StaticActivity * > &alternative, std::vector< Location * > &fixed, Location *toFix, PrecalculatedCircuits &precalculatedCircuits)
A highly optimized function calculates a lower estimation on the robot cycle time if toFix location i...
std::vector< std::vector< std::vector< double > > > mDist
Preallocated 2-D array of distances for getShortestCircuit method, indexed by thread.
Fixed locations, power saving modes, and movements.
Graph constructMinimalDurationGraph(Robot *r) const
Generates a distance graph for the given robot.
DistanceMatrix< double > createInitialDistanceMatrix(const Graph &g) const
It creates an initial matrix of distances based on edge lengths.
KnowledgeBase mKB
Various data of the heuristic, e.g. generated tuples, performance statistics, etc.
void generatePromisingTuples(const uint32_t &threadId)
It generates promising tuples and adds them to the KnowledgeBase.
A parallel heuristic for the energy optimization of robotic cells.
Universal algorithms like Floyd-Warshall, Golden Search, etc.
A graph in which random alternatives will be searched for.
Declares the class storing the best found solutions, tuples, and information about the heuristic perf...
void writePerformanceRecordToLogFile(const double &initializationTime, const double &finalRuntime)
It writes various statistics about the heuristic performance to a CSV file.
HamiltonianCircuit< Location > getShortestCircuit(const uint32_t &threadId, const std::vector< StaticActivity * > &circuit, const std::vector< Location * > &fixed, bool writeCircuit=true)
It calculates the shortest circuit through locations that visits each static activity in the same ord...
std::vector< std::vector< std::vector< int64_t > > > mPreds
Preallocated 2-D array of predecessors for getShortestCircuit method, indexed by thread.
void printProgressInfo(const double &currentRuntime)
Prints various information about the performance of the sub-heuristics, generation of tuples...
std::vector< std::vector< T >> DistanceMatrix
Definition of the matrix data type.
Definition: Algorithms.h:39
Location of the robot used either during work (welding) or waiting.
Definition: RoboticLine.h:192
The robotic cell corresponds to an instance of this class.
Definition: RoboticLine.h:563
The structure contains the maps for fast searching in the robotic cell.
HeuristicAlgorithms algo
Optimization sub-heuristics and the related Linear Programming solver for partially fixed problems...