forked from woodbri/vrpdptw
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Problem.cpp
155 lines (125 loc) · 3.9 KB
/
Problem.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
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
#include <limits>
#include <stdexcept>
#include <algorithm>
#include <math.h>
#include "Problem.h"
// NON class functions for sorting
bool sortByDist(Order a, Order b)
{
return a.dist > b.dist;
}
// Class functions
unsigned int Problem::getNodeCount() {
return (unsigned int) N.size();
}
unsigned int Problem::getOrderCount() {
return (unsigned int) O.size();
}
double Problem::distance(int n1, int n2) const {
double dx = N[n2].x - N[n1].x;
double dy = N[n2].y - N[n1].y;
return sqrt( dx*dx + dy*dy );
}
void Problem::dump() {
std::cout << "---- Problem -------------\n";
std::cout << "K: " << K << std::endl;
std::cout << "Q: " << Q << std::endl;
std::cout << "w1: " << w1 << std::endl;
std::cout << "w2: " << w2 << std::endl;
std::cout << "w3: " << w3 << std::endl;
std::cout << "extents: " << extents[0] << ", "
<< extents[1] << ", "
<< extents[2] << ", "
<< extents[3] << std::endl;
std::cout << "---- Orders --------------\n";
for (int i=0; i<O.size(); i++)
O[i].dump();
std::cout << "---- Nodes --------------\n";
for (int i=0; i<N.size(); i++)
N[i].dump();
std::cout << std::endl;
}
void Problem::loadProblem(char *infile)
{
std::ifstream in( infile );
std::string line;
// read header line
std::getline(in, line);
std::istringstream buffer( line );
buffer >> K;
buffer >> Q;
// initialize the extents
extents[0] = std::numeric_limits<double>::max();
extents[1] = std::numeric_limits<double>::max();
extents[2] = std::numeric_limits<double>::min();
extents[3] = std::numeric_limits<double>::min();
// read the nodes
while ( getline(in, line) ) {
Node node;
std::istringstream buffer( line );
buffer >> node.nid;
buffer >> node.x;
buffer >> node.y;
buffer >> node.demand;
buffer >> node.tw_open;
buffer >> node.tw_close;
buffer >> node.service;
buffer >> node.pid;
buffer >> node.did;
// compute the extents as we load the data for plotting
if (node.x < extents[0]) extents[0] = node.x;
if (node.y < extents[1]) extents[1] = node.y;
if (node.x > extents[2]) extents[2] = node.x;
if (node.y > extents[3]) extents[3] = node.y;
N.push_back(node);
if (node.nid == 0)
DepotClose = node.tw_close;
}
in.close();
// add a small buffer around the extents
extents[0] -= (extents[2] - extents[0]) * 0.02;
extents[2] += (extents[2] - extents[0]) * 0.02;
extents[1] -= (extents[3] - extents[1]) * 0.02;
extents[3] += (extents[3] - extents[1]) * 0.02;
// make orders from the nodes
makeOrders();
// sort the orders
sort(O.begin(), O.end(), sortByDist);
calcAvgTWLen();
}
void Problem::makeOrders ()
{
if (getNodeCount() == 0 || ((getNodeCount()-1)%2 != 0)) {
std::string errmsg = "Problem::makeOrders - Nodes have not be correctly loaded.";
throw std::runtime_error(errmsg);
}
O.reserve( (getNodeCount()-1)/2+1 );
int oid = 0;
// add the depot to the order list
Order order;
order.oid = oid++;
order.pid = 0;
order.did = 0;
order.dist = 0.0;
order.dist2 = 0.0;
O.push_back(order);
// for each pickup, get its delivery and create an order
for (int i=1; i<getNodeCount(); i++) {
if (N[i].pid == 0) {
Order order;
order.oid = oid++;
order.pid = i;
order.did = N[i].did;
order.dist = distance(0, i);
order.dist2 = distance(order.did, 0);
O.push_back(order);
}
}
}
void Problem::calcAvgTWLen() {
// get the average time window length
atwl = 0;
for (int i=0; i<N.size(); i++)
atwl += (N[i].tw_close - N[i].tw_open);
atwl /= N.size();
};