-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscpxx.hpp
238 lines (219 loc) · 7.41 KB
/
scpxx.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
MIT License
Copyright (c) 2019 Ronaldd Pinho
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef _SCPXX_SINGLE_HEADER_HPP_
#define _SCPXX_SINGLE_HEADER_HPP_
#include <stdexcept>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
namespace scpxx {
/**
* Exception for throw when not is possible open a file
*/
class FileNotOpenException : public std::runtime_error {
public:
FileNotOpenException(const std::string filename)
: std::runtime_error("Impossible open the file " + filename),
name(filename) {}
protected:
std::string name;
};
class Matrix : protected std::vector< std::vector<bool> > {
public:
using std::vector<std::vector<bool>>::operator[];
using std::vector<std::vector<bool>>::iterator;
using std::vector<std::vector<bool>>::begin;
using std::vector<std::vector<bool>>::end;
/*
* Default Constructor
* Gets the number of rows and number of columns
*/
Matrix (unsigned int _nrows = 0, unsigned int _ncolumns = 0)
: std::vector<std::vector<bool>>(_nrows, std::vector<bool>(_ncolumns,0)),
numRows(_nrows), numColumns(_ncolumns), vecOfCosts(std::vector<int>(_ncolumns))
{}
/*
* Destructor
*/
~Matrix() {}
/*
* Returns the number of rows of the Matrix */
unsigned int num_rows() { return numRows; }
/*
* Returns the number of columns of the Matrix */
unsigned int num_columns() { return numColumns; }
/*
* Set costs of all columns
* Need a vector<int> with NCOL positions */
void costs(std::vector<int> _costs) {
if (_costs.size() != numColumns)
throw std::logic_error("Number of costs should be equals to number of columns");
vecOfCosts = _costs;
}
/*
* Returns a reference to the vector of columns costs */
const std::vector<int>& costs() {
return vecOfCosts;
}
/*
* Density of the set covering problem instance:
* Number of Ones on the Matrix */
float density() {
iterator row = this->begin();
int sum = 0;
for (; row != this->end(); ++row) {
sum = std::accumulate(row->begin(), row->end(), sum);
}
return (float) float(sum) / float(num_rows() * num_columns());
}
/*
* Add a row in the matrix defining the columns that covers this row */
void add_row(const std::vector<int> _columnsCovering) {
if (_columnsCovering.size() > numColumns)
throw std::length_error("Number of columns is very larger");
std::vector<bool> aux_row(numColumns);
std::vector<int>::const_iterator it = _columnsCovering.begin();
for (; it != _columnsCovering.end(); ++it)
aux_row[*it -1] = 1;
push_back(aux_row);
}
/*
* Defines the coverage of a existing row in the matrix passing the columns
* that covers this row */
void set_row(unsigned int _index, std::vector<int> _columnsCovering) {
if (_columnsCovering.size() > numColumns)
throw std::length_error("Number of columns is very larger");
if (_index >= numColumns)
throw std::length_error("Index out of bounds");
std::vector<int>::iterator it = _columnsCovering.begin();
for (; it != _columnsCovering.end(); ++it)
at(_index)[*it -1] = 1;
}
private:
unsigned int numRows;
unsigned int numColumns;
std::vector<int> vecOfCosts;
};
/**
* Class that represents a instance file. Inherit from ifstream.
* Gets a filename string and can read all values
*/
template <typename T>
class InstanceFile : public std::ifstream {
public:
/*
* Default Constructor
* inherit from "input file stream"
*/
explicit InstanceFile(std::string _filename) : std::ifstream(_filename) {}
/*
* Destructor
*/
~InstanceFile() {}
/*
* Gets a string with numbers and split through a separator _sep and
* returns into a vector<int>
*/
std::vector<T> split(std::string _str, char _sep) {
using std::vector;
using std::string;
vector<string> strs;
vector<T> values;
int begin = 0;
for (int i=0 ; i < _str.size() ; i++) {
if (_str[i] == _sep) {
strs.push_back( _str.substr(begin, i-begin) );
begin = i+1;
}
}
strs.push_back(_str.substr(begin, _str.size()));
for (int i=0; i < strs.size(); i++) {
if ((strs[i] != "") and (strs[i] != " ")) {
values.push_back( std::stoi(strs[i]) );
}
}
return values;
};
/*
* Copy all integer numbers from instance file to a vector
*/
void bufferize() {
if (!this->is_open())
throw std::runtime_error("File not open");
std::stringstream buffer;
std::string line;
while (std::getline(*this, line)) {
buffer << line;
}
vecOfvalues = split(buffer.str(), ' ');
buffer.clear();
}
/*
* Returns a reference to the vector of integers with all values
*/
std::vector<T>& values() {
return vecOfvalues;
}
protected:
std::vector<T> vecOfvalues;
}; // end of InstanceFile class
/**
* Class for Set Covering Problem instance files from OR-Libary format
* Gets a string filename of a file scp**.txt
*/
class SCPFile : public InstanceFile<int> {
public:
/*
* Default constructor
*/
explicit SCPFile(std::string filename) : InstanceFile<int>(filename) {}
/* Destructor */
~SCPFile() {}
Matrix generate_matrix() {
int nRows = this->vecOfvalues[0];
int nColumns = this->vecOfvalues[1];
std::vector<int> costs_vector;
std::vector<int> columnsCover;
int index = 2;
for (; index < nColumns+2; index++)
costs_vector.push_back(this->vecOfvalues[index]);
index = nColumns+2;
Matrix m(nRows, nColumns);
int rowIndex = 0;
while (rowIndex < nRows) {
int endList = index + vecOfvalues[index];
for (int i = index+1; i <= endList; i++) {
columnsCover.push_back( vecOfvalues[i] );
index = i;
}
//std::cout << rowIndex << std::endl;
m.set_row(rowIndex, columnsCover);
columnsCover.clear();
rowIndex++;
index++;
}
m.costs(costs_vector);
costs_vector.clear();
return m;
}
};
} // end of namespace scpxx
#endif // _SCPXX_SINGLE_HEADER_HPP_