solver  1.0
VariableMappingILP.cpp
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 #include <cmath>
19 #include "SolverConfig.h"
20 #include "Shared/Utils.h"
22 
23 using namespace std;
24 
25 constexpr double BOOL_TOL = 0.001;
26 
28  try {
29  VariableMappingLP::addActivities(r->activities(), false);
32  } catch (...) {
33  throw_with_nested(SolverException(caller(), "Mapping keys are not unique!"));
34  }
35 }
36 
38  try {
39  for (Robot* r : l.robots()) // First LP mappings must be created to have right indices.
40  VariableMappingLP::addActivities(r->activities(), false);
41 
42  for (Robot* r : l.robots()) {
45  }
46 
48  } catch (...) {
49  throw_with_nested(SolverException(caller(), "Activity identifications are not unique!"));
50  }
51 }
52 
54  uint32_t varCount = VariableMappingLP::numberOfVariables();
55  varCount += x.size()+z.size()+y.size()+w.size();
56  for (const auto& p : c)
57  varCount += p.second.size();
58 
59  return varCount;
60 }
61 
63  uint32_t idx = numberOfVariables();
64  for (Activity* a : r->activities()) {
65  StaticActivity* sa = dynamic_cast<StaticActivity*>(a);
66  if (sa != nullptr) {
67  for (Location* l : sa->locations()) {
68  variables.emplace_back(BIN, 0.0, 1.0);
69  varDesc.emplace_back("x_{"+to_string(sa->aid())+"}#{"+to_string(l->point())+"}");
70  setValue(x, {sa->aid(), l->point()}, idx++, caller());
71  }
72 
73  for (RobotPowerMode* pm : sa->parent()->powerModes()) {
74  variables.emplace_back(BIN, 0.0, 1.0);
75  varDesc.emplace_back("z_{"+to_string(sa->aid())+"}#{"+to_string(pm->pid())+"}");
76  setValue(z, {sa->aid(), pm->pid()}, idx++, caller());
77  }
78  }
79 
80  DynamicActivity* da = dynamic_cast<DynamicActivity*>(a);
81  if (da != nullptr) {
82  for (Movement* mv : da->movements()) {
83  variables.emplace_back(BIN, 0.0, 1.0);
84  varDesc.emplace_back("y_{"+to_string(da->aid())+"}#{"+to_string(mv->mid())+"}");
85  setValue(y, {da->aid(), mv->mid()}, idx++, caller());
86  }
87  }
88  }
89 }
90 
92  uint32_t idx = numberOfVariables();
93  for (Activity* a : r->activities()) {
94  DynamicActivity* da = dynamic_cast<DynamicActivity*>(a);
95  if (da != nullptr && da->optional()) {
96  Variable var(BIN, 0.0, 1.0);
97  variables.push_back(var);
98  varDesc.emplace_back("w_{"+to_string(da->aid())+"}#{"+to_string(da->successor()->aid())+"}");
99  setValue(w, {da->aid(), da->successor()->aid()}, idx++, caller());
100  }
101  }
102 }
103 
105  uint32_t idx = numberOfVariables();
106  for (const pair<ActivityMode*, ActivityMode*>& col : l.collisions()) {
107  int32_t numberOfRobots = l.robots().size();
108  ActivityMode *am1 = col.first, *am2 = col.second;
109  assert(am1->baseParent() != nullptr && am2->baseParent() != nullptr && "Unexpected null pointers!");
110  const Activity *act1 = am1->baseParent(), *act2 = am2->baseParent();
111  for (int32_t r = -numberOfRobots; r <= numberOfRobots; ++r) {
112  uint32_t m1 = am1->id(), m2 = am2->id();
113  uint32_t a1 = act1->aid(), a2 = act2->aid();
114 
115  variables.emplace_back(BIN, 0.0, 1.0);
116  varDesc.emplace_back("c_{"+to_string(a1)+","+to_string(m1)+"}#{"+to_string(a2)+","+to_string(m2)+"}#{" +to_string(r)+"}");
117  c[{pack(a1,m1), pack(a2,m2)}].push_back(idx++);
118  }
119  }
120 }
121 
122 map<uint32_t, uint32_t> inverseMapping(const vector<double>& solution, const map2to1& mapping) {
123  map<uint32_t, uint32_t> m;
124  for (const auto& p : mapping) {
125  if (abs(solution[p.second]-1.0) < BOOL_TOL)
126  setValue(m, p.first.first, p.first.second, caller());
127  }
128 
129  return m;
130 }
131 
The instance of the class corresponds to a robot movement between two coordinates.
Definition: RoboticLine.h:133
The base class incorporating common properties of robot operations and movements. ...
Definition: RoboticLine.h:68
Instance of this class includes all the data structures and methods related to a robot.
Definition: RoboticLine.h:432
void addActivities(const std::vector< Activity * > &activities, bool mandatory, bool mapW=true)
It adds the mapping for extra activities.
virtual bool optional() const
It returns whether this dynamic activity is optional, i.e. may or may not be performed.
Definition: RoboticLine.h:277
std::vector< Variable > variables
Continuous variables of the problem with the well-specified domains.
uint32_t numberOfVariables() const
Total number of variables including both continuous and binary ones.
map4toN c
Binary variables used for collisions resolution.
void addActivityBinaryVariables(Robot *r)
It creates the mapping for x, z, and y variables.
Collection of movements between two static activities.
Definition: RoboticLine.h:261
void addActivityOrderBinaryVariables(Robot *r)
It constructs the mapping for w variables.
STL namespace.
A general exception of the program.
Definition: Exceptions.h:58
Mapping of continuous variables occurring in the energy optimization problem.
std::vector< std::string > varDesc
Description of the continuous variables.
Various auxiliary functions used across the program.
void addCollisionBinaryVariables(const RoboticLine &l)
Mapping of c variables is created.
uint64_t pack(const uint32_t &v1, const uint32_t &v2)
It packs two uint32_t numbers to uint64_t data type.
Definition: Utils.cpp:26
Collection of locations in which a robot operation (or waiting) can be performed. ...
Definition: RoboticLine.h:304
VariableMappingILP(Robot *r)
Constructs the mapping of all the variables related to robot r.
It represents the power saving mode of the robot.
Definition: RoboticLine.h:359
std::map< uint32_t, uint32_t > inverseMapping(const std::vector< double > &solution, const map2to1 &mapping)
It extracts the variables set to true and returns the ,,selected" indices.
Either a movement or location.
Definition: RoboticLine.h:54
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
Mapping of integer, to be more precise binary, variables of the energy optimization problem...
Structure containing information about the variable.
Definition: ILPModel.h:48
std::map< std::pair< uint32_t, uint32_t >, uint32_t > map2to1
Mapping of variables indexed by two numbers.