-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAcoFF.cpp
More file actions
227 lines (189 loc) · 6.75 KB
/
AcoFF.cpp
File metadata and controls
227 lines (189 loc) · 6.75 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <random>
#include <limits>
#include <cstddef>
#include <atomic>
#include <thread>
#include <ff/parallel_for.hpp>
#include <ff/farm.hpp>
#include "Parameters.cpp"
#include "Environment.cpp"
#include "Ant.cpp"
using namespace ff;
template <typename T, typename D>
struct Emitter: ff_node_t< Ant<T> > {
ff_loadbalancer * const loadBalancer;
const Parameters<T> & params;
const Environment<T, D> & env;
std::vector< Ant<T> > & ants;
Emitter(ff_loadbalancer * const loadBalancer,
const Parameters<T> & params,
const Environment<T, D> & env,
std::vector< Ant<T> > & ants) :
loadBalancer(loadBalancer),
params(params),
env (env),
ants (ants)
{}
Ant<T> * svc(Ant<T> * s) {
if (s == nullptr) {
for (uint32_t i = 0; i < env.nAnts; ++i) {
Ant<T> * ant = &ants[i];
ff_node::ff_send_out(ant);
}
loadBalancer->broadcast_task(EOS);
}
return (Ant<T> *)GO_OUT;
}
};
template <typename T, typename D>
struct Worker: ff_node_t< Ant<T> > {
const Parameters<T> & params;
Environment<T, D> & env;
Worker(const Parameters<T> & params, Environment<T, D> & env) :
params(params),
env (env)
{}
inline T atomic_addf(std::atomic<T> * f, T value) {
T old = f->load(std::memory_order_consume);
T desired = old + value;
while (!f->compare_exchange_weak(old, desired,
std::memory_order_release,
std::memory_order_consume))
{
desired = old + value;
}
return desired;
}
Ant<T> * svc( Ant<T> * ant ) {
if ((void *)ant == EOS) {
return (Ant<T> * )EOS;
}
ant->constructTour(env.fitness, env.edges);
const T tau = params.q / ant->getTourLength();
auto bTabu = ant->getTabu().begin();
const auto constbTabu = bTabu;
while ( bTabu != ant->getTabu().end() - 1) {
const uint32_t from = *(bTabu++);
const uint32_t to = *(bTabu);
atomic_addf( &env.delta[from * env.nCities + to], tau );
atomic_addf( &env.delta[to * env.nCities + from], tau );
}
const uint32_t from = *(bTabu);
const uint32_t to = *(constbTabu);
atomic_addf( &env.delta[from * env.nCities + to], tau );
atomic_addf( &env.delta[to * env.nCities + from], tau );
return ant;
}
~Worker() {}
};
template <typename T, typename D>
class AcoFF {
private:
const Parameters<T> & params;
Environment<T, D> & env;
const uint32_t mapWorkers;
const uint32_t farmWorkers;
std::vector< Ant<T> > ants;
ParallelForReduce<T> pfr;
ParallelForReduce< Ant<T> * > pfrAnts;
void initEta(std::vector<T> & eta,
const std::vector<T> & edges,
const uint32_t nCities)
{
const uint32_t elems = nCities * nCities;
pfr.parallel_for(0L, elems, [&](const long i) {
const T edgeVal = edges[i];
eta[i] = (edgeVal == 0.0 ? 0.0 : 1.0 / edgeVal);
});
pfr.threadPause();
}
void calcFitness(std::vector<T> & fitness,
const std::vector<T> & pheromone,
const std::vector<T> & eta,
const uint32_t nCities,
const T alpha,
const T beta)
{
const uint32_t elems = nCities * nCities;
pfr.parallel_for(0L, elems, [&](const long i) {
fitness[i] = pow(pheromone[i], alpha) * pow(eta[i], beta);
});
pfr.threadPause();
}
void updateBestTour(std::vector<uint32_t> & bestTour,
T & bestTourLength)
{
Ant<T> maxAnt(0);
Ant<T> * bestAnt = &ants[0];
pfrAnts.parallel_reduce(bestAnt,
&maxAnt,
0L, env.nAnts - 1,
[&](const long i, Ant<T> * minAnt) {
if (ants[i + 1] < *minAnt) minAnt = &ants[i + 1];
},
[](Ant<T> * minAnt, Ant<T> * ant) {
if (*ant < *minAnt) minAnt = ant;
});
pfrAnts.threadPause();
std::copy ( bestAnt->getTabu().begin(), bestAnt->getTabu().end(), bestTour.begin() );
bestTourLength = bestAnt->getTourLength();
}
void resetDelta(std::vector<D> & delta,
const uint32_t nCities)
{
const uint32_t elems = nCities * nCities;
pfr.parallel_for(0L, elems, [&](const long i) {
delta[i] = 0.0;
});
pfr.threadPause();
}
void updatePheromone(std::vector<T> & pheromone,
const std::vector<D> & delta,
const uint32_t nCities,
const T rho) {
const uint32_t elems = nCities * nCities;
pfr.parallel_for(0L, elems, [&](const long i) {
pheromone[i] = pheromone[i] * rho + delta[i];
});
pfr.threadPause();
}
public:
AcoFF(const Parameters<T> & params,
Environment<T, D> & env,
const uint32_t mapWorkers,
const uint32_t farmWorkers) :
params (params),
env (env),
mapWorkers (mapWorkers),
farmWorkers(farmWorkers),
ants (env.nAnts, Ant<T>(env.nCities)),
pfr (mapWorkers),
pfrAnts (mapWorkers)
{
initEta(env.eta, env.edges, env.nCities);
}
void solve() {
ff_Farm<> farmTour( [&]() {
std::vector< std::unique_ptr< ff_node > > workers;
for(uint32_t i = 0; i < farmWorkers; ++i)
workers.push_back( make_unique< Worker<T, D> >(params, env) );
return workers;
} ());
Emitter<T, D> E(farmTour.getlb(), params, env, ants);
farmTour.add_emitter(E);
farmTour.remove_collector();
farmTour.wrap_around();
// farmTour.set_scheduling_ondemand();
uint32_t epoch = 0;
do {
calcFitness (env.fitness, env.pheromone, env.eta, env.nCities, params.alpha, params.beta);
// Calc Tour
if (farmTour.run_then_freeze() < 0) { error("Running farm\n"); exit(EXIT_RUN_FARM); }
if (farmTour.wait_freezing() < 0) { error("Wait freezing farm\n"); exit(EXIT_WAIT_FREEZING_FARM); }
updateBestTour (env.bestTour, env.bestTourLength);
resetDelta (env.delta, env.nCities);
updatePheromone(env.pheromone, env.delta, env.nCities, params.rho);
} while ( ++epoch < params.maxEpoch );
}
~AcoFF(){}
};