-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.cpp
107 lines (87 loc) · 2.12 KB
/
instance.cpp
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
# include <iostream>
# include <fstream>
# include <sstream>
# include <vector>
# include "iomanip"
# include "headers.h"
Instance::Instance(int n, int l, int u, int b, int o, vector<vector<int>> distances) {
this->n = n;
this->l = l;
this->u = u;
this->b = b;
this->o = o;
this->distances = distances;
}
int Instance::getN() {
return n;
}
int Instance::getL() {
return l;
}
int Instance::getU() {
return u;
}
int Instance::getB() {
return b;
}
int Instance::getO() {
return o;
}
vector<vector<int>> Instance::getDistances() {
return distances;
}
void Instance::print() {
cout << "n: " << n << endl;
cout << endl;
cout << "l: " << l << endl;
cout << "u: " << u << endl;
cout << "b: " << b << endl;
cout << "o: " << o << endl;
cout << endl;
int i = 0;
for (vector<int> row : distances) {
cout << setw(2) << i++ + 1<< ": ";
for (int distance : row) {
cout << setw(6) << distance << " ";
}
cout << endl;
}
cout << endl;
}
// Function to read a .txt instance file
// Returns an Instance with instance file data
Instance readTxtInstance(string filename) {
ifstream file(filename);
string line;
int n, l, u, b, o;
vector<vector<int>> distances;
if (file.is_open()) {
getline(file, line);
n = stoi(line.substr(2));
getline(file, line);
getline(file, line);
l = stoi(line.substr(2));
getline(file, line);
u = stoi(line.substr(2));
getline(file, line);
b = stoi(line.substr(2));
getline(file, line);
o = stoi(line.substr(2));
getline(file, line);
for (int i = 0; i < n; i++) {
getline(file, line);
stringstream ss(line);
vector<int> row;
for (int j = 0; j < n; j++) {
int distance;
ss >> distance;
row.push_back(distance);
}
distances.push_back(row);
}
}
else {
cout << "Unable to open file" << endl;
}
return Instance(n, l, u, b, o, distances);
}