solver  1.0
ILPModel.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 <algorithm>
19 #include <iostream>
20 #include <iterator>
21 #include "SolverConfig.h"
22 #include "Shared/Exceptions.h"
23 #include "ILPModel/ILPModel.h"
24 
25 using namespace std;
26 
28  uint64_t numberOfVariables = A.numberOfColumns(), numberOfConstraints = A.numberOfRows();
29  if (numberOfVariables == 0 || numberOfConstraints == 0)
30  throw SolverException(caller(), "Empty matrix 'A' of the ILP formulation!");
31 
32  uint32_t cSize = max(c.size(), gurobiC.size());
33  if (numberOfVariables != x.size() || numberOfVariables != cSize)
34  throw SolverException(caller(), "Invalid horizontal size of 'A', 'c', 'x'!");
35 
36  if (numberOfConstraints != ops.size() || numberOfConstraints != b.size())
37  throw SolverException(caller(), "Invalid vertical size of 'A', 'ops', 'b'!");
38 
39  if (!varDesc.empty() && varDesc.size() != numberOfVariables)
40  throw SolverException(caller(), "Incomplete description of variables!");
41 
42  if (!conDesc.empty() && conDesc.size() != numberOfConstraints)
43  throw SolverException(caller(), "Incomplete description of constraints!");
44 }
45 
47  try {
48  checkFormulation();
49  double densityOfMatrixA = A.densityOfMatrix();
50  uint64_t numberOfBinaryVariables = 0, numberOfFloatVariables = 0, numberOfIntegerVariables = 0;
51  for (uint64_t v = 0; v < numberOfVariables(); ++v) {
52  switch (x[v].type) {
53  case FLT:
54  ++numberOfFloatVariables;
55  break;
56  case BIN:
57  ++numberOfBinaryVariables;
58  break;
59  case INT:
60  ++numberOfIntegerVariables;
61  break;
62  default:
63  throw SolverException(caller(), "Unknown type of variable!");
64  }
65  }
66 
67  cout<<"Number of variables: "<<numberOfVariables()<<endl;
68  cout<<"Number of float variables: "<<numberOfFloatVariables<<endl;
69  cout<<"Number of binary variables: "<<numberOfBinaryVariables<<endl;
70  cout<<"Number of integer variables: "<<numberOfIntegerVariables<<endl;
71  cout<<"Number of constraints: "<<numberOfConstraints()<<endl;
72  cout<<"Density of matrix A: "<<densityOfMatrixA*100.0<<" %"<<endl;
73 
74  } catch (...) {
75  throw_with_nested(InvalidArgument(caller(), "Invalid dimensions of ILP formulation!"));
76  }
77 }
78 
void printStatistics() const
It prints various statistics like density of A matrix and number of constraints to the standard outpu...
Definition: ILPModel.cpp:46
STL namespace.
A general exception of the program.
Definition: Exceptions.h:58
The file defines extended exceptions for the better error handling in the program.
Exception is thrown if a method is given invalid parameters or a user provides invalid program argume...
Definition: Exceptions.h:120
General model for Integer Linear Programming problem.
void checkFormulation() const
It checks that sizes of vectors and the matrix are valid.
Definition: ILPModel.cpp:27