-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProbabilistic.cpp
152 lines (121 loc) · 3.89 KB
/
Probabilistic.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
#include "pch.h"
#include "Graph.h"
#include <queue>
#include <iostream>
#include <iomanip>
#include <random>
#include <chrono>
// SalesmanTrackProbabilistic ==================================================
struct Cell
{
double mDijkstraDistance;
list<CEdge*> mSection;
};
using Matrix = vector<vector<Cell>>;
CTrack SalesmanTrackProbabilistic(CGraph& graph, CVisits& visits)
{
if (visits.m_Vertices.empty())
{
return { &graph };
}
int accumulator = 0;
Matrix matrix(visits.m_Vertices.size() - 1);
for (auto iOrigin = visits.m_Vertices.begin(); iOrigin != next(visits.m_Vertices.begin(), visits.m_Vertices.size() - 1); ++iOrigin)
{
DijkstraQueue(graph, *iOrigin);
(*iOrigin)->m_indexMatrix = accumulator;
for (const CVertex* iDestination : visits.m_Vertices)
{
Cell cell;
cell.mDijkstraDistance = iDestination->m_DijkstraDistance;
if (*iOrigin != iDestination)
{
while (iDestination != *iOrigin)
{
cell.mSection.push_front(iDestination->m_pDijkstraPrevious);
iDestination = iDestination->m_pDijkstraPrevious->m_pOrigin;
}
}
matrix[accumulator].push_back(cell);
}
accumulator++;
}
visits.m_Vertices.back()->m_indexMatrix = accumulator;
deque<int> range;
for (const CVertex* iVertex : visits.m_Vertices)
{
range.push_back(iVertex->m_indexMatrix);
}
range.pop_front();
range.pop_back();
deque rangeCopy(range);
double firstLength = 0;
vector<int> firstSolution;
firstSolution.push_back(0);
while (!rangeCopy.empty())
{
accumulator = rand() % rangeCopy.size();
int newVertex = rangeCopy[accumulator];
rangeCopy.erase(next(rangeCopy.begin(), accumulator));
firstLength += matrix[firstSolution.back()][newVertex].mDijkstraDistance;
firstSolution.push_back(newVertex);
}
firstLength += matrix[firstSolution.back()][visits.m_Vertices.back()->m_indexMatrix].mDijkstraDistance;
firstSolution.push_back(visits.m_Vertices.back()->m_indexMatrix);
vector<int> gradientSolution;
double gradientLength = std::numeric_limits<double>::max();
static constexpr int gradientIteration = 10000;
for (int i = 0; i < gradientIteration * visits.m_Vertices.size(); i++)
{
rangeCopy = range;
bool stopGradient = false;
int newVertex;
double currentLength = 0;
vector<int> currentSolution;
currentSolution.push_back(0);
while (!rangeCopy.empty() && currentLength < gradientLength && !stopGradient)
{
if (currentLength > gradientLength)
{
stopGradient = true;
break;
}
const int gradientIndex = rand() % rangeCopy.size();
newVertex = rangeCopy[gradientIndex];
rangeCopy.erase(next(rangeCopy.begin(), gradientIndex));
currentLength += matrix[currentSolution.back()][newVertex].mDijkstraDistance;
currentSolution.push_back(newVertex);
}
if (stopGradient)
{
continue;
}
currentLength += matrix[currentSolution.back()][visits.m_Vertices.back()->m_indexMatrix].mDijkstraDistance;
currentSolution.push_back(visits.m_Vertices.back()->m_indexMatrix);
if (currentLength < gradientLength)
{
gradientLength = currentLength;
gradientSolution = currentSolution;
}
}
if (gradientLength < firstLength)
{
auto solution = CTrack(&graph, matrix[gradientSolution[0]][gradientSolution[1]].mSection);
for (int element = 1; element < gradientSolution.size() - 1; element++)
{
for (CEdge* iEdge : matrix[gradientSolution[element]][gradientSolution[element + 1]].mSection)
{
solution.m_Edges.push_back(iEdge);
}
}
return solution;
}
auto solution = CTrack(&graph, matrix[firstSolution[0]][firstSolution[1]].mSection);
for (int element = 1; element < firstSolution.size() - 1; element++) {
for (CEdge* iEdge : matrix[firstSolution[element]][firstSolution[element + 1]].mSection)
{
solution.m_Edges.push_back(iEdge);
}
}
return solution;
}