-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.cpp
167 lines (139 loc) · 3.88 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
//==================================================================
// File : main.cpp
// Author : Rebecca Sagalyn
// Date : Aug 25, 2013
// Description : Driver for tsp.h
//==================================================================
#include <iostream>
#include <climits>
#include "tsp.h"
#include "usage.h"
#include "twoOpt.h"
#include "MyThread.h" // thread wrapper class
// The length was annoying me.
#define CPS CLOCKS_PER_SEC
#define NUM_THREADS 1
int main(int argc, char** argv) {
// Check that user entered filename on command line
if (argc < 2)
{
usage();
exit(-1);
}
// Read file names from input
string f, o;
f = o = argv[1];
o.append(".tour");
// Create new tsp object
TSP tsp(f, o);
int n = tsp.get_size();
// Start timing
clock_t t = clock();
clock_t t2;
// Read cities from file
if (DEBUG)
cout << "Reading cities" << endl;
tsp.readCities();
if (DEBUG)
cout << "Time to read cities: "
<< ((float) (clock() - t)) / CLOCKS_PER_SEC << " s\n";
cout << "number of cities: " << tsp.n << endl;
// Fill N x N matrix with distances between nodes
if (DEBUG)
cout << "\nFilling matrix" << endl;
t2 = clock();
tsp.fillMatrix_threads();
if (DEBUG)
cout << "Time to fill matrix: " << ((float) (clock() - t2)) / CPS
<< " s\n";
// Find a MST T in graph G
if (DEBUG)
cout << "\nFinding mst" << endl;
t2 = clock();
tsp.findMST_old();
if (DEBUG)
cout << "Time to find mst: " << ((float) (clock() - t2)) / CPS
<< " s\n";
// Find a minimum weighted matching M for odd vertices in T
if (DEBUG)
cout << "\nFinding perfect matching" << endl;
t2 = clock();
tsp.perfect_matching();
if (DEBUG)
cout << "Time to find matching: " << ((float) (clock() - t2)) / CPS
<< " s\n\n";
// Find the node that leads to the best path
clock_t start, end;
start = clock();
// Create array of thread objects
MyThread threads[NUM_THREADS];
int best = INT_MAX;
int bestIndex;
int stop_here = NUM_THREADS;
// Amount to increment starting node by each time
int increment = 1; // by 1 if n < 1040
if (n >= 600 && n < 1040)
increment = 3;
else if (n >= 1040 && n < 1800)
increment = 8;
else if (n >= 1800 && n < 3205)
increment = 25; // ~ 220s @ 3200
else if (n >= 3205 && n < 4005)
increment = 50; // ~ 230s @ 4000
else if (n >= 4005 && n < 5005)
increment = 120; // ~ 200 @ 5000
else if (n >= 5005 && n < 6500)
increment = 250; // ~ 220s @ 6447
else if (n >= 6500)
increment = 500;
int remaining = n;
// Start at node zero
int node = 0;
// Count to get thread ids
int count = 0;
while (remaining >= increment) {
// Keep track iteration when last node will be reached
if (remaining < (NUM_THREADS * increment)) {
// each iteration advances NUM_THREADS * increment nodes
stop_here = remaining / increment;
}
for (long t = 0; t < stop_here; t++) {
//cout << "Thread " << count << " starting at node " << node << endl;
threads[t].start_node = node;
threads[t].my_id = count;
threads[t].mytsp = &tsp;
threads[t].start();
node += increment;
count++;
}
// Wait for all the threads
for (long t = 0; t < stop_here; t++) {
threads[t].join();
}
remaining -= (stop_here * increment);
}
cout << "count: " << count << endl;
// Loop through each index used and find shortest path
for (long t = 0; t < count; t++) {
if (tsp.path_vals[t][1] < best) {
bestIndex = tsp.path_vals[t][0];
best = tsp.path_vals[t][1];
}
}
end = clock();
cout << "\nbest: " << best << " @ index " << bestIndex << endl;
cout << "time: " << ((float) (end - start)) / CPS << "s\n";
// Store best path
tsp.create_tour(bestIndex);
tsp.make_shorter();
tsp.make_shorter();
tsp.make_shorter();
tsp.make_shorter();
tsp.make_shorter();
cout << "\nFinal length: " << tsp.pathLength << endl;
// Print to file
tsp.printResult();
if (DEBUG)
cout << "\nTotal time: " << ((float) (clock() - t)) / CPS << "s\n";
return 0;
}