Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d1a7bcf

Browse files
Introduce ColorCount typedef
1 parent 2f66446 commit d1a7bcf

File tree

12 files changed

+82
-79
lines changed

12 files changed

+82
-79
lines changed

‎include/colouring/crossover/gpx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ namespace graph_colouring {
1111
* @return a new colouring based on the two parents
1212
*/
1313
Colouring gpxCrossover(const Colouring &s1,
14-
const Colouring &s2);
14+
const Colouring &s2);
1515
}

‎include/colouring/graph_colouring.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ std::ostream &operator<<(std::ostream &strm, const std::vector<T> &set) {
4141

4242
namespace graph_colouring {
4343

44+
typedef Color ColorCount;
45+
4446
/**
4547
* Represents a colouring of a specific graph where
4648
* config[n] = c means that color c is associated to node with node id n
@@ -54,26 +56,26 @@ namespace graph_colouring {
5456
* The parameter k represents the (desired) number of colors for the configuration.
5557
*/
5658
typedef std::function<Colouring(const graph_access &G,
57-
const size_t k)> InitOperator;
59+
const ColorCount k)> InitOperator;
5860

5961
/**
6062
* Creates a new colouring based on two existing parent configurations s1 and s2.
6163
*/
62-
typedef std::function<Colouring(const graph_access &G,
63-
const Colouring &s1,
64-
const Colouring &s2)> CrossoverOperator;
64+
typedef std::function<Colouring(const Colouring &s1,
65+
const Colouring &s2,
66+
const graph_access &G)> CrossoverOperator;
6567

6668
/**
6769
* Optimizes / mutates the existign colouring s.
6870
*/
69-
typedef std::function<Colouring(const graph_access &G,
70-
const Colouring &s)> LSOperator;
71+
typedef std::function<Colouring(const Colouring &s,
72+
const graph_access &G)> LSOperator;
7173

7274
/**
7375
* @param s a graph colouring
7476
* @return the number of colurs used in the configuration \s
7577
*/
76-
size_t colorCount(const Colouring &s);
78+
ColorCount colorCount(const Colouring &s);
7779

7880
/**
7981
* @param G the target graph
@@ -166,7 +168,7 @@ namespace graph_colouring {
166168
* @return true if the given coloring is a valid solution.
167169
*/
168170
virtual bool isSolution(const graph_access &G,
169-
const size_t k,
171+
const ColorCount k,
170172
const Colouring &s) const {
171173
return colorCount(s) <= k &&
172174
numberOfConflictingEdges(G, s) == 0 &&
@@ -272,7 +274,7 @@ namespace graph_colouring {
272274
*/
273275
std::vector<ColouringResult> perform(const std::vector<std::shared_ptr<ColouringStrategy>> &strategies,
274276
const graph_access &G,
275-
size_t k,
277+
ColorCount k,
276278
size_t populationSize,
277279
size_t maxItr,
278280
size_t threadCount = std::thread::hardware_concurrency());

‎include/colouring/hca.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace graph_colouring {
2121
* @return the best found colouring
2222
*/
2323
Colouring hybridColouringAlgorithm(const graph_access &G,
24-
size_t k,
24+
ColorCount k,
2525
size_t populationSize,
2626
size_t maxItr,
2727
size_t L,

‎include/colouring/init/greedy_saturation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ namespace graph_colouring {
66
/**
77
* Naive implementation of the greedy saturation initialization operator.
88
* See Hybrid Evolutionary Algorithms for Graph Coloring, page 385
9-
* @param G the target graph
109
* @param k the number of used colors
10+
* @param G the target graph
1111
* @return a (possibly invalid) colouring with \p k colors
1212
*/
1313
Colouring initByGreedySaturation(const graph_access &G,
14-
size_t k);
14+
ColorCount k);
1515
}

‎include/colouring/ls/tabu_search.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ namespace graph_colouring {
66
/**
77
* Naive implementation of the tabu search operator.
88
* See Hybrid Evolutionary Algorithms for Graph Coloring, page 386
9-
* @param G the graph G
109
* @param s the (invalid) colouring s of graph \p G
10+
* @param G the graph G
1111
* @param L the maximum number of iterations
1212
* @param A tuning parameter for table list length
1313
* @param alpha tuning parameter for table list length
1414
* @return an enhanced colouring based of configuration \p s
1515
*/
16-
Colouring tabuSearchOperator(const graph_access &G,
17-
const Colouring &s,
18-
size_t L,
19-
size_t A,
20-
double alpha);
16+
Colouring tabuSearchOperator(const Colouring &s,
17+
const graph_access &G,
18+
size_t L,
19+
size_t A,
20+
double alpha);
2121
}

‎micro_benchs/colouring/hca_mb.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ void BM_hca(benchmark::State &state,
1414
graph_io::readGraphWeighted(G, graphFile);
1515

1616
auto threadCount = size_t(state.range(0));
17-
auto min_k = size_t(state.range(1));
18-
auto k = size_t(state.range(2));
17+
auto min_k = ColorCount(state.range(1));
18+
auto k = ColorCount(state.range(2));
1919
auto population_size = size_t(state.range(3));
2020
auto maxItr = size_t(state.range(4));
2121
auto L = size_t(state.range(5));

‎src/colouring/graph_colouring.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace graph_colouring {
1919
/**< The used colouring strategy */
2020
size_t strategyId;
2121
/**< The number of colors used for the colouring strategy */
22-
size_t target_k;
22+
ColorCount target_k;
2323
/**< The index of the first colouring */
2424
size_t colouring;
2525
};
@@ -29,7 +29,7 @@ namespace graph_colouring {
2929
*/
3030
struct MasterPackage {
3131
/**< The next k to be searched */
32-
size_t next_k;
32+
ColorCount next_k;
3333
};
3434

3535
/**
@@ -40,9 +40,9 @@ namespace graph_colouring {
4040
std::atomic<size_t> wpCount;
4141
};
4242

43-
size_t colorCount(const Colouring &s) {
43+
ColorCount colorCount(const Colouring &s) {
4444
std::vector<bool> usedColor(s.size());
45-
size_t color_count = 0;
45+
ColorCount color_count = 0;
4646
for (auto n : s) {
4747
if (n != UNCOLORED && !usedColor[n]) {
4848
usedColor[n] = true;
@@ -105,6 +105,15 @@ namespace graph_colouring {
105105
return nextTry;
106106
}
107107

108+
inline bool hasFinished(const std::vector<ColouringStrategyContext> &context) {
109+
for (auto &c : context) {
110+
if (c.wpCount > 0) {
111+
return false;
112+
}
113+
}
114+
return true;
115+
}
116+
108117
static void workerThread(const std::vector<std::shared_ptr<ColouringStrategy>> &strategies,
109118
const graph_access &G,
110119
const size_t populationSize,
@@ -115,7 +124,7 @@ namespace graph_colouring {
115124
boost::lockfree::queue<MasterPackage> &masterQueue,
116125
std::vector<Colouring> &population,
117126
std::vector<std::atomic<bool>> &lock,
118-
std::atomic<size_t> &target_k,
127+
std::atomic<ColorCount> &target_k,
119128
std::atomic<bool> &terminated) {
120129
typedef std::mt19937::result_type seed_type;
121130
typename std::chrono::system_clock seed_clock;
@@ -125,7 +134,7 @@ namespace graph_colouring {
125134
std::mt19937 generator(init_seed);
126135

127136
//Only used to avoid rapid reporting of already known colourings
128-
size_t last_reported_k = target_k + 1;
137+
ColorCount last_reported_k = target_k + 1;
129138

130139
WorkingPackage wp = {0, 0, 0, 0};
131140
while (!terminated) {
@@ -157,7 +166,7 @@ namespace graph_colouring {
157166
auto lsOp = strategies[wp.strategyId]->lsOperators[
158167
lsOprDist(generator)];
159168

160-
result = lsOp(G, crossoverOp(G, *parents[0], *parents[1]));
169+
result = lsOp(crossoverOp(*parents[0], *parents[1], G), G);
161170
*parents[weakerParent] = result;
162171

163172
lock[p1] = false;
@@ -175,7 +184,7 @@ namespace graph_colouring {
175184
auto initOpr = strategy.initOperators[initOprDist(generator)];
176185
auto lsOpr = strategy.lsOperators[lsOprDist(generator)];
177186

178-
result = lsOpr(G, initOpr(G, wp.target_k));
187+
result = lsOpr(initOpr(G, wp.target_k), G);
179188
population[wp.strategyId * populationSize + wp.colouring] = result;
180189

181190
lock[wp.strategyId * populationSize + wp.colouring] = false;
@@ -196,19 +205,10 @@ namespace graph_colouring {
196205
}
197206
}
198207

199-
bool hasFinished(const std::vector<ColouringStrategyContext> &context) {
200-
for (auto &c : context) {
201-
if (c.wpCount > 0) {
202-
return false;
203-
}
204-
}
205-
return true;
206-
}
207-
208208
std::vector<ColouringResult>
209209
ColouringAlgorithm::perform(const std::vector<std::shared_ptr<ColouringStrategy>> &strategies,
210210
const graph_access &G,
211-
const size_t k,
211+
const ColorCount k,
212212
const size_t populationSize,
213213
const size_t maxItr,
214214
const size_t threadCount) {
@@ -228,7 +228,7 @@ namespace graph_colouring {
228228
std::vector<ColouringStrategyContext> context(strategies.size());
229229

230230
//Represents the smallest number of colors used in a recently found colouring
231-
std::atomic<size_t> target_k(k);
231+
std::atomic<ColorCount> target_k(k);
232232
boost::lockfree::queue<WorkingPackage> workQueue(populationSize * strategies.size());
233233

234234
//It is possible that all threads report the same found k colouring

‎src/colouring/hca.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ namespace graph_colouring {
88

99
Colouring hybridColouringAlgorithm(
1010
const graph_access &G,
11-
const size_t k,
11+
const ColorCount k,
1212
const size_t population_size,
1313
const size_t maxItr,
1414
const size_t L,
1515
const size_t A,
1616
const double alpha,
1717
const size_t threadCount) {
1818
std::vector<InitOperator> hcaInitOps = {[](const graph_access &graph,
19-
const size_t colors) {
19+
const ColorCount colors) {
2020
return graph_colouring::initByGreedySaturation(graph, colors);
2121

2222
}};
23-
std::vector<CrossoverOperator> hcaCrossoverOps = {[](const graph_access &G_,
24-
const Colouring &s1,
25-
const Colouring &s2) {
23+
std::vector<CrossoverOperator> hcaCrossoverOps = {[](const Colouring &s1,
24+
const Colouring &s2,
25+
const graph_access &graph) {
2626
return graph_colouring::gpxCrossover(s1, s2);
2727
}};
28-
std::vector<LSOperator> hcaLSOps = {[L, A, alpha](const graph_access &graph,
29-
const Colouring &s) {
30-
return graph_colouring::tabuSearchOperator(graph, s, L, A, alpha);
28+
std::vector<LSOperator> hcaLSOps = {[L, A, alpha](const Colouring &s,
29+
const graph_access &graph) {
30+
return graph_colouring::tabuSearchOperator(s, graph, L, A, alpha);
3131
}};
3232

3333
auto invalidColoring = std::make_shared<InvalidColouringStrategy>(hcaInitOps,

‎src/colouring/init/greedy_saturation.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "colouring/init/greedy_saturation.h"
22

3+
using namespace graph_colouring;
4+
35
inline std::set<NodeID> toSet(const graph_access &G) {
46
std::set<NodeID> nodes;
57
for (NodeID n = 0; n < G.number_of_nodes(); n++) {
@@ -8,24 +10,24 @@ inline std::set<NodeID> toSet(const graph_access &G) {
810
return nodes;
911
}
1012

11-
static size_t numberOfAllowedClasses(const graph_access &G,
12-
const graph_colouring::Colouring &s,
13-
const NodeID nodeID,
14-
const size_t k) {
15-
size_t count = 0;
13+
static ColorCount numberOfAllowedClasses(const graph_access &G,
14+
const Colouring &s,
15+
const NodeID nodeID,
16+
const ColorCount k) {
17+
ColorCount count = 0;
1618
for (Color c = 0; c < k; c++) {
17-
count += static_cast<size_t>(graph_colouring::allowedInClass(G, s, c, nodeID));
19+
count += static_cast<ColorCount>(graph_colouring::allowedInClass(G, s, c, nodeID));
1820
}
1921
return count;
2022
}
2123

2224
static NodeID nextNodeWithMinAllowedClasses(const graph_access &G,
23-
const graph_colouring::Colouring &c,
25+
const Colouring &c,
2426
const std::set<NodeID> &nodes,
25-
const size_t k) {
27+
const ColorCount k) {
2628
assert(!nodes.empty());
2729
NodeID targetNode = *nodes.begin();
28-
size_t minNumberOfAllowedClasses = k;
30+
ColorCount minNumberOfAllowedClasses = k;
2931
for (auto n : nodes) {
3032
auto count = numberOfAllowedClasses(G, c, n, k);
3133
if (count < minNumberOfAllowedClasses) {
@@ -36,8 +38,7 @@ static NodeID nextNodeWithMinAllowedClasses(const graph_access &G,
3638
return targetNode;
3739
}
3840

39-
graph_colouring::Colouring graph_colouring::initByGreedySaturation(const graph_access &G,
40-
const size_t k) {
41+
Colouring graph_colouring::initByGreedySaturation(const graph_access &G, const ColorCount k) {
4142
Colouring s(G.number_of_nodes(), std::numeric_limits<NodeID>::max());
4243

4344
auto nodes = toSet(G);

‎src/colouring/ls/tabu_search.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
using namespace graph_colouring;
44

5-
Colouring graph_colouring::tabuSearchOperator(const graph_access &G,
6-
const Colouring &s,
7-
const size_t L,
8-
const size_t A,
9-
const double alpha) {
5+
Colouring graph_colouring::tabuSearchOperator(const Colouring &s,
6+
const graph_access &G,
7+
const size_t L,
8+
const size_t A,
9+
const double alpha) {
1010
Colouring s_mutated(s);
1111
std::uniform_int_distribution<size_t> distribution(0, A - 1);
1212
std::mt19937 generator;
@@ -15,7 +15,7 @@ Colouring graph_colouring::tabuSearchOperator(const graph_access &G,
1515
+ alpha * numberOfConflictingNodes(G, s_mutated));
1616

1717
//Number of classes
18-
const size_t k = colorCount(s_mutated);
18+
const ColorCount k = colorCount(s_mutated);
1919
//Number of nodes
2020
const size_t V = G.number_of_nodes();
2121

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /