-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
100 lines (83 loc) · 4.4 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
#include "Graph.h"
#include "DrawingUtils.h"
#include <iostream>
#include <chrono>
int main()
{
auto start_time = std::chrono::high_resolution_clock::now();
auto citiesCount = 1000;
std::unordered_map<int, City> cities = City::generateRandomGraphCities(citiesCount);
Graph graph(cities);
std::chrono::duration<double> durationGen = std::chrono::high_resolution_clock::now() - start_time;
auto doubleTree = graph.doubleTreeTSP(0);
std::cout << "Double tree TSP weight = " << doubleTree.first.second << " duration = " << doubleTree.second << " seconds" << std::endl;
drawPathTSP(cities, doubleTree.first.first, "Double tree");
auto nearestNeighbors = graph.nearestNeighborTSP(0);
std::cout << "Nearest neighbor TSP weight = " << nearestNeighbors.first.second << " duration = " << nearestNeighbors.second << " seconds" << std::endl;
drawPathTSP(cities, nearestNeighbors.first.first, "Nearest neighbor");
auto randomInsertion = graph.randomInsertionTSP(0, 1);
std::cout << "Random insertion TSP weight = " << randomInsertion.first.second << " duration = " << randomInsertion.second << " seconds" << std::endl;
drawPathTSP(cities, randomInsertion.first.first, "Random insertion");
return 0;
}
int benchmarkTSP()
{
int citiesCount = 25000;
int attempts = 1;
double totalDurationGen = 0;
double totalDoubleTreeDuration = 0;
int totalDoubleTreeWeights = 0;
double totalNearestNeighborDuration = 0;
int totalNearestNeighborWeights = 0;
double totalRandomInsertionDuration = 0;
int totalRandomInsertionWeights = 0;
for (int i = 0; i < attempts; ++i)
{
auto start_time = std::chrono::high_resolution_clock::now();
std::unordered_map<int, City> cities = City::generateRandomGraphCities(citiesCount);
Graph graph(cities);
std::chrono::duration<double> durationGen = std::chrono::high_resolution_clock::now() - start_time;
totalDurationGen += durationGen.count();
auto doubleTree = graph.doubleTreeTSP(0);
totalDoubleTreeDuration += doubleTree.second;
totalDoubleTreeWeights += doubleTree.first.second;
auto nearestNeighbors = graph.nearestNeighborTSP(0);
totalNearestNeighborDuration += nearestNeighbors.second;
totalNearestNeighborWeights += nearestNeighbors.first.second;
auto randomInsertion = graph.randomInsertionTSP(0, 1);
totalRandomInsertionDuration += randomInsertion.second;
totalRandomInsertionWeights += randomInsertion.first.second;
}
std::cout << "Cities count: " << citiesCount << ", attempts: " << attempts << std::endl;
std::cout << "Average graph gen. duration: " << totalDurationGen / attempts << std::endl;
std::cout << "Average double tree algorithm duration = " << totalDoubleTreeDuration / attempts << ", weights = " << totalDoubleTreeWeights / attempts << std::endl;
std::cout << "Average nearest neighbor algorithm duration = " << totalNearestNeighborDuration / attempts << ", weights = " << totalNearestNeighborWeights / attempts << std::endl;
std::cout << "Average random insertion algorithm duration = " << totalRandomInsertionDuration / attempts << ", weights = " << totalRandomInsertionWeights / attempts << std::endl;
return 0;
}
int benchmarkDijkstra()
{
int nodeCount = 1000;
int kMin = 0;
int kMax = 50;
int attempts = 1000;
double totalDurationGen = 0;
double totalMinHeapDuration = 0;
double totalFibHeapDuration = 0;
for (int i = 0; i < attempts; ++i)
{
auto start_time = std::chrono::high_resolution_clock::now();
Graph gen(nodeCount, kMin, kMax);
std::chrono::duration<double> durationGen = std::chrono::high_resolution_clock::now() - start_time;
totalDurationGen += durationGen.count();
auto minHeap = gen.dijkstraMinHeap(0);
totalMinHeapDuration += minHeap.second;
auto fibHeap = gen.dijkstraFibHeap(0);
totalFibHeapDuration += fibHeap.second;
}
std::cout << "Node count: " << nodeCount << ", gen. boundaries: [" << kMin << ", " << kMax << "], attempts: " << attempts << std::endl;
std::cout << "Average graph gen. duration: " << totalDurationGen / attempts << std::endl;
std::cout << "Average minimum heap Dijkstra duration = " << totalMinHeapDuration / attempts << std::endl;
std::cout << "Average fibonacci heap Dijkstra duration = " << totalFibHeapDuration / attempts << std::endl;
return 0;
}