-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColoredGraph.h
116 lines (100 loc) · 4.34 KB
/
ColoredGraph.h
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
#ifndef GRAPHCOLORING_COLOREDGRAPH_H
#define GRAPHCOLORING_COLOREDGRAPH_H
#include "Node.h"
#include <boost/graph/graphviz.hpp>
#include "boost/graph/sequential_vertex_coloring.hpp"
#include "Graph.h"
class ColoredGraph{
public:
std::vector<ColoredNode> colored_node_list;
unsigned long fitness;
ColoredGraph(int size){
colored_node_list.resize(size);
}
ColoredGraph& operator=(const ColoredGraph& g)= default;
ColoredGraph(const ColoredGraph &g): fitness(g.fitness), colored_node_list(g.colored_node_list) {}
~ColoredGraph(){}
void colorGraph(std::vector<Node> &node_list, const std::vector<int> &color_list) {
for(int i = 0; i < colored_node_list.size();i++){
colored_node_list[i].node_color = node_list[i].generateValidColor(colored_node_list, color_list);
}
fitness = getNumColorsUsed();
}
void colorGraphEmpty() {
for(auto &node : colored_node_list){
node.node_color = empty_color;
}
fitness = 0;
}
void colorGraphGreedy(std::vector<Node> &node_list, const std::vector<int> &color_list) {
for(int i = 0; i < colored_node_list.size();i++){
for(auto &color : color_list)
if(node_list[i].hasValidColor(colored_node_list, color)) {
colored_node_list[i].node_color = color;
break;
}
}
fitness = getNumColorsUsed();
}
void colorGraph(std::vector<Node> &node_list, const ColoredGraph& father, const ColoredGraph& mother,const std::vector<int> &color_list){
std::uniform_int_distribution<std::mt19937::result_type> dist2(0,1);
for(int i = 0; i < father.colored_node_list.size();i++){
auto &child_node = colored_node_list[i];
auto &father_node = father.colored_node_list[i];
auto &mother_node = mother.colored_node_list[i];
auto rnd = dist2(rng);
if(rnd == 0 && node_list[i].hasValidColor(colored_node_list, father_node.node_color))
child_node.node_color = father_node.node_color;
else if(node_list[i].hasValidColor(colored_node_list, mother_node.node_color))
child_node.node_color = mother_node.node_color;
else
child_node.node_color = node_list[i].generateValidColor( colored_node_list, color_list);
}
fitness = getNumColorsUsed();
}
void crossOver(std::vector<Node> &node_list,const ColoredGraph& father, const ColoredGraph& mother, const std::vector<int> &color_list){
colorGraph(node_list, father, mother, color_list);
}
void mutate(std::vector<Node> &node_list, int prob, const std::vector<int> &color_list) {
std::uniform_int_distribution<std::mt19937::result_type> dist(0,prob);
for(int i = 0; i < colored_node_list.size();i++){
if(dist(rng) == 0)
colored_node_list[i].node_color = node_list[i].generateValidColor(colored_node_list, color_list);
}
fitness = getNumColorsUsed();
}
void exportToDot(std::vector<Node> &node_list, const std::string& filename) const {
Graph::MyGraph graph;
for(const auto &n : colored_node_list){
auto vertex_id = add_vertex(graph);
std::stringstream stream;
stream << std::hex << n.node_color;
graph[vertex_id].color = "#" + stream.str();
}
for(int i = 0; i < node_list.size();i++){
Node n = node_list[i];
for(int j = 0; j < n.adjacentNodeList.size(); j++){
if( i < n.adjacentNodeList[j])
boost::add_edge(i,n.adjacentNodeList[j], graph);
}
}
boost::dynamic_properties dp;
dp.property("fillcolor", get(&vertex_info::color, graph));
dp.property("style", get(&vertex_info::style, graph));
dp.property("node_id", get(boost::vertex_index, graph));
std::ofstream output_file(filename);
write_graphviz_dp(output_file, graph, dp);
output_file.close();
}
unsigned long getNumColorsUsed() const{
std::set<int> colors;
for(const ColoredNode &n : colored_node_list){
colors.insert(n.node_color);
}
return colors.size();
}
unsigned long getFitness() const{
return fitness;
}
};
#endif //GRAPHCOLORING_COLOREDGRAPH_H