Skip to content

Commit 1cd8b58

Browse files
committed
Add ability to print out found colourings
1 parent d1a7bcf commit 1cd8b58

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

include/colouring/graph_colouring.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,17 @@ namespace graph_colouring {
269269
* @param k the (maximum) number of colors
270270
* @param populationSize the number of maintained colourings
271271
* @param maxItr the maximum number of iterations
272-
* @param threadCount the number of used threads
272+
* @param threadCount the number of used worker threads
273+
* @param outputStream if not null, it will be used to report recently found colourings
273274
* @return the best colourings for each passed colouring category
274275
*/
275276
std::vector<ColouringResult> perform(const std::vector<std::shared_ptr<ColouringStrategy>> &strategies,
276277
const graph_access &G,
277278
ColorCount k,
278279
size_t populationSize,
279280
size_t maxItr,
280-
size_t threadCount = std::thread::hardware_concurrency());
281+
size_t threadCount = std::thread::hardware_concurrency(),
282+
std::ostream *outputStream = nullptr);
281283
};
282284

283285

include/colouring/hca.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace graph_colouring {
1818
* for k-i coloring could be obtained in the previous runs
1919
* @param logStream if specified, the algorithm will print out the results of each
2020
* iteration into the output stream
21+
* @param outputStream if not null, it will be used to report recently found colourings
2122
* @return the best found colouring
2223
*/
2324
Colouring hybridColouringAlgorithm(const graph_access &G,
@@ -27,6 +28,7 @@ namespace graph_colouring {
2728
size_t L,
2829
size_t A,
2930
double alpha,
30-
size_t threadCount = std::thread::hardware_concurrency());
31+
size_t threadCount = std::thread::hardware_concurrency(),
32+
std::ostream *outputStream = nullptr);
3133

3234
}

src/colouring/graph_colouring.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ namespace graph_colouring {
3030
struct MasterPackage {
3131
/**< The next k to be searched */
3232
ColorCount next_k;
33+
/**< The strategy which reported this found k */
34+
size_t reportingStrategy;
35+
/**< The actual colouring */
3336
};
3437

3538
/**
@@ -198,7 +201,7 @@ namespace graph_colouring {
198201
}
199202
if (strategy.isSolution(G, target_k, result) && last_reported_k > target_k) {
200203
last_reported_k = colorCount(result);
201-
masterQueue.push({last_reported_k});
204+
masterQueue.push({last_reported_k, wp.strategyId});
202205
}
203206
}
204207
std::this_thread::yield();
@@ -211,7 +214,8 @@ namespace graph_colouring {
211214
const ColorCount k,
212215
const size_t populationSize,
213216
const size_t maxItr,
214-
const size_t threadCount) {
217+
const size_t threadCount,
218+
std::ostream *outputStream) {
215219

216220
assert(!strategies.empty());
217221
assert(populationSize > 0);
@@ -220,6 +224,7 @@ namespace graph_colouring {
220224

221225
if (4 * threadCount > strategies.size() * populationSize) {
222226
std::cerr << "WARNING: Make sure that populationSize is bigger than 4*categoryCount*threadCount\n";
227+
assert(0);
223228
}
224229

225230
std::vector<Colouring> P(strategies.size() * populationSize);
@@ -264,9 +269,14 @@ namespace graph_colouring {
264269
}
265270
}
266271

267-
MasterPackage mp = {0};
272+
MasterPackage mp = {0, 0};
268273
while (!hasFinished(context)) {
269274
while (masterQueue.pop(mp)) {
275+
if (outputStream != nullptr) {
276+
auto &ss = *outputStream;
277+
ss << "Found colouring k = " << mp.next_k
278+
<< " from colouring strategy " << mp.reportingStrategy << "\n";
279+
}
270280
if (target_k >= mp.next_k) {
271281
target_k = mp.next_k - 1;
272282
for (size_t strategyId = 0; strategyId < strategies.size(); strategyId++) {

src/colouring/hca.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ namespace graph_colouring {
1414
const size_t L,
1515
const size_t A,
1616
const double alpha,
17-
const size_t threadCount) {
17+
const size_t threadCount,
18+
std::ostream *outputStream) {
19+
1820
std::vector<InitOperator> hcaInitOps = {[](const graph_access &graph,
1921
const ColorCount colors) {
2022
return graph_colouring::initByGreedySaturation(graph, colors);
@@ -39,6 +41,7 @@ namespace graph_colouring {
3941
k,
4042
population_size,
4143
maxItr,
42-
threadCount)[0].s;
44+
threadCount,
45+
outputStream)[0].s;
4346
}
4447
}

0 commit comments

Comments
 (0)