-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulated_annealing.cpp
More file actions
189 lines (137 loc) · 3.27 KB
/
simulated_annealing.cpp
File metadata and controls
189 lines (137 loc) · 3.27 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
#include <iostream>
#include <random>
#include <chrono>
#include <utility>
#include <algorithm>
#include "simulated_annealing.h"
#include <time.h>
#include <cstdlib>
unsigned annealing::seed = std::chrono::system_clock::now().time_since_epoch().count();
void annealing::reset()
{
std::default_random_engine generator(seed);
seed++;
std::uniform_int_distribution<int> distribution(0, input->n - 1);
int numberOfWarehouses = input->k;
chosen.assign(input->n, false);
while(numberOfWarehouses)
{
int tempindex = distribution(generator);
if(chosen[tempindex] == false)
{
chosen[tempindex] = true;
numberOfWarehouses--;
}
}
}
double annealing::heuristic()
{
double sum = 0;
for( int j = 0; j < input->n; j++ )
{
if( chosen[j] == true )
sum += input->pointAsWarehouseQuality[j];
}
return sum;
}
annealing::annealing(graph * input_)
{
input = input_;
std::default_random_engine generator(seed);
seed++;
std::uniform_int_distribution<int> distribution(0, input->n - 1);
int numberOfWarehouses = input->k;
chosen.assign(input->n, false);
while(numberOfWarehouses)
{
int tempindex = distribution(generator);
if(chosen[tempindex] == false)
{
chosen[tempindex] = true;
numberOfWarehouses--;
}
}
}
std::pair<int,int> annealing::next()
{
std::default_random_engine generator(seed);
seed++;
std::uniform_int_distribution<int> distribution(0, input->n - 1);
int j;
do
{
j = distribution(generator);
} while ( chosen[j] != false );
int i;
do
{
i = distribution(generator);
} while (chosen[i] == false);
return std::make_pair (i,j); // makni i-ti cvor, dodaj j-ti cvor
}
double annealing::quality()
{
std::vector<int> temp;
// pronalazak najblizeg skladista za svako ne skladiste
for (int i = 0; i < input->n; ++i)
{
temp.push_back(-1);
if (chosen[i] == false) //samo gledaj redove od ne-skaldišta
{
for (int j = 0; j < input->n; ++j)
{
if(chosen[j] == true) //nemoj gledat udaljenost izmedu dva ne-skladista
if (temp[i] == -1 || input->matrix[i][j] < input->matrix[i][temp[i]])
temp[i] = j;
}
}
}
// pronalazak naudaljenijeg najblizeg skladista
int temporary = -1;
for (int i = 0; i < input->n; ++i)
{
if (temp[i] != -1)
if( temporary == -1 || input->matrix[i][temp[i]] > input->matrix[temporary][temp[temporary]])
temporary = i;
}
worst.first = temp[temporary];
worst.second = temporary;
return input->matrix[worst.first][worst.second];
}
void annealing::swap (int i, int j) // prvi makne, drugi doda
{
if(chosen[i] == true)
{
chosen[i] = false;
chosen[j] = true;
}
else
{
chosen[i] = true;
chosen[j] = false;
}
}
void annealing::SA(int temperature, double alpha)
{
std::default_random_engine generator(seed);
std::uniform_real_distribution<double> distribution(0, 1);
double q1,q2;
while (temperature > 1)
{
q1 = heuristic();
std::pair<int, int> temp = next();
swap(temp.first, temp.second);
q2 = heuristic();
if (q1 < q2)
if( distribution(generator) >= exp( -1*(q2-q1)/temperature ) )
swap(temp.second, temp.first);
temperature *= alpha;
}
return; // ako return quality() onda vaki put napravi taj alg koji je spor pa bolje da se on poziva ako je fakat nuzno
}
annealing::annealing()
{
}
annealing::~annealing()
{
}