-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsamplesparser.cpp
More file actions
191 lines (184 loc) · 5.75 KB
/
samplesparser.cpp
File metadata and controls
191 lines (184 loc) · 5.75 KB
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
#include "samplesparser.h"
#include <regex>
#include <iostream>
#include <fstream>
SamplesParser::SamplesParser(std::vector<std::string> const & filesPath) :
mDistances(new std::unordered_map<Pair, double, PairHash>())
{
parseFiles(filesPath);
}
SamplesParser::SamplesParser()
{
}
std::shared_ptr<std::unordered_map<Pair, double, PairHash> > SamplesParser::getDistances() const
{
return mDistances;
}
std::vector<Customer> SamplesParser::getCustomers() const
{
return mCustomers;
}
std::vector<Truck> SamplesParser::getTrucks() const
{
return mTrucks;
}
void SamplesParser::parseFiles(const std::vector<std::string> &filesPath)
{
//std::vector<unsigned int> weights;
std::unordered_map<unsigned int, std::pair<double, double>> coordsShift;
std::size_t maxId = 1;
std::size_t zoneShift = 0;
for (const std::string& path: filesPath)
{
std::ifstream stream(path);
if (!stream)
{
}
std::string str;
for (std::size_t i = 0; i < 3; ++i) // read first meaningless lines
{
std::getline(stream, str);
}
std::getline(stream, str);
std::regex dimRegex("DIMENSION(\\s*):(\\s*)(\\d+)(\\s+)");
std::smatch dimMatch;
if (!std::regex_match(str, dimMatch, dimRegex))
{
#if WITH_LOG
std::cout << str << std::endl;
#endif
}
unsigned int dimension = std::stoi(dimMatch[3].str());
std::getline(stream, str); //metrics info, we choose always euclid
std::getline(stream, str);
std::regex capacityRegex("CAPACITY(\\s*):(\\s*)(\\d+)(\\s+)");
std::smatch capacityMatch;
if (!std::regex_match(str, capacityMatch, capacityRegex))
{
#if WITH_LOG
std::cout << str << std::endl;
#endif
}
unsigned int capacity = std::stoi(capacityMatch[3].str());
std::getline(stream, str);
std::regex vehiclesRegex("VEHICLES(\\s*):(\\s*)(\\d+)(\\s+)");
std::smatch vehiclesMatch;
if (!std::regex_match(str, vehiclesMatch, vehiclesRegex))
{
#if WITH_LOG
std::cout << str << std::endl;
#endif
}
unsigned int vehicles = std::stoi(vehiclesMatch[3].str());
for (unsigned int i = 0; i < vehicles; ++i)
{
mTrucks.push_back(Truck(mTrucks.size(), capacity, 0, 1 << zoneShift));
std::cout << "zone is " << (1 << zoneShift) << std::endl;
}
std::getline(stream, str); //NODE_COORD_SECTION
std::unordered_map<unsigned int, std::pair<double, double>> coords;
std::regex coordsRegex("(\\s*)(\\d+)(\\s+)((-)?\\d+(.\\d+)*)(\\s+)((-)?\\d+(.\\d+)*)(\\s*)");
for (unsigned int i = 0; i < dimension; ++i)
{
std::getline(stream, str);
std::smatch coordsMatch;
if (!std::regex_match(str, coordsMatch, coordsRegex))
{
#if WITH_LOG
std::cout << str << std::endl;
#endif
}
unsigned int custId = std::stoi(coordsMatch[2]);
double xCoord = std::stod(coordsMatch[4]);
double yCoord = std::stod(coordsMatch[8]);
coords.insert(std::make_pair(custId,
std::make_pair(xCoord, yCoord)));
#if WITH_LOG
std::cout << "id is " << custId << " x " << xCoord << " y " << yCoord << std::endl;
#endif
}
std::getline(stream, str); //DEMAND_SECTION
std::vector<std::pair<unsigned int, unsigned int>> weights;
std::regex weightsRegex("(\\s*)(\\d+)(\\s+)(\\d+)(\\s+)");
for (unsigned int i = 0; i < dimension; ++i)
{
std::getline(stream, str);
std::smatch weightsMatch;
if (!std::regex_match(str, weightsMatch, weightsRegex))
{
#if WITH_LOG
std::cout << str << std::endl;
#endif
}
double weight = std::stod(weightsMatch[4]);
weights.push_back(std::make_pair(std::stoi(weightsMatch[2]), weight));
// if (std::abs(weight) > std::numeric_limits<double>::epsilon())
// {
// mCustomers.push_back(Customer(mCustomers.size() + 1, Package(weight, 0)));
// }
}
std::getline(stream, str); //DEPOT_SECTION
std::getline(stream, str);
std::regex depotReg("(\\s*)(\\d+)(\\s+)");
std::smatch depotMatch;
if (!std::regex_match(str, depotMatch, depotReg))
{
#if WITH_LOG
std::cout << str << std::endl;
#endif
}
unsigned int depotId = std::stoi(depotMatch[2]);
if (depotId != 1)
{
std::cout << "RULE IS INCORECT! DEPOT NOT FIRST!!!" <<std::endl;
}
std::pair<double, double> depotCoord = coords.at(depotId);
std::size_t curMaxId = maxId;
for (std::pair<unsigned int, unsigned int> weight : weights)
{
if (weight.first == depotId)
{
continue;
}
#ifdef WITH_LOG
if (!mCustomers.empty() && maxId + weight.first <= mCustomers.back().id)
{
std::cout << "bad id creation!!!" << std::endl;
}
#endif
mCustomers.push_back(Customer(maxId + weight.first, Package(weight.second, 0), Customer::DeliveryFromDepot,
0, 1 << zoneShift));
curMaxId = std::max(maxId + weight.first, curMaxId);
}
for (auto coordPair : coords)
{
std::pair<double, double> curCoord = coordPair.second;
if (coordPair.first != depotId)
{
//TODO::NOT THE SAME ORDER!!!!!
coordsShift.insert(std::make_pair(coordPair.first + maxId,
std::make_pair(curCoord.first - depotCoord.first,
curCoord.second - depotCoord.second)));
}
}
maxId = curMaxId + 1;
//++zoneShift;
}
for (auto firstPair : coordsShift)
{
for (auto secondPair : coordsShift)
{
double first = firstPair.second.first - secondPair.second.first;
double second = firstPair.second.second - secondPair.second.second;
double distance = std::sqrt(first * first + second * second);
//std::cout << distance << std::endl;
mDistances->insert(std::make_pair(Pair(firstPair.first, secondPair.first), distance));
}
double first = firstPair.second.first;
double second = firstPair.second.second;
double distance = std::sqrt(first * first + second * second);
mDistances->insert(std::make_pair(Pair(firstPair.first, 0), distance));
mDistances->insert(std::make_pair(Pair(0, firstPair.first), distance));
}
mDistances->insert(std::make_pair(Pair(0, 0), 0));
}