6 #include <boost/polygon/voronoi.hpp>
23 namespace graph_color {
38 std::vector<std::pair<int32_t, int32_t>>
nodes_;
44 std::vector<std::pair<uint32_t, uint32_t>>
edges_;
70 uint32_t
dist(
const std::pair<int32_t, int32_t> &p1,
71 const std::pair<int32_t, int32_t> &p2)
const {
72 return static_cast<uint32_t
>(std::round(
73 std::sqrt((p1.first - p2.first) * (p1.first - p2.first) +
74 (p1.second - p2.second) * (p1.second - p2.second))));
88 uint32_t
min_dist(uint32_t x, uint32_t y)
const {
90 for (
const auto &[px, py] :
nodes_) {
99 std::pair<int32_t, int32_t> &
operator[](uint32_t index) {
137 const std::vector<std::pair<uint32_t, uint32_t>>
get_edges()
const {
145 void draw(
const std::string &filename);
151 std::cout <<
"Color " << i <<
" : " <<
color_hist_[i] << std::endl;
153 std::cout <<
"Failure : " <<
failure_ << std::endl;
167 std::pair<qbpp::Model, qbpp::Vector<qbpp::Vector<qbpp::Var>>>
helper_func(
201 for (uint32_t i = 0; i < n; i++) {
202 uint32_t counter = 0;
203 uint32_t max_dist = 1;
205 while (counter < 10) {
218 if (
dist >= max_dist) {
228 for (
size_t i = 0; i <
nodes_.size(); i++) {
229 for (
size_t j = i + 1; j <
nodes_.size(); j++) {
230 if (
dist(i, j) < proximity) {
238 typedef boost::polygon::point_data<int> Point;
239 std::vector<Point> points;
240 for (
const auto &[x, y] :
nodes_) {
241 points.push_back(Point(x, y));
243 boost::polygon::voronoi_diagram<double> vd;
244 boost::polygon::construct_voronoi(points.begin(), points.end(), &vd);
245 for (
const auto &cell : vd.cells()) {
246 size_t source_index = cell.source_index();
247 const auto *edge = cell.incident_edge();
249 size_t twin_source_index = edge->twin()->cell()->source_index();
250 if (source_index < twin_source_index)
251 edges_.push_back({
static_cast<uint32_t
>(source_index),
252 static_cast<uint32_t
>(twin_source_index)});
254 }
while (edge != cell.incident_edge());
265 if (c >=
static_cast<decltype(c)
>(
color_hist_.size())) {
274 const std::vector<std::string> color_palette = {
309 std::ostringstream dot_stream;
310 dot_stream <<
"graph G {\n"
311 <<
"node [shape=circle, fixedsize=true, width=3, fontsize=100, "
313 <<
"edge [penwidth=8];\n";
315 for (
size_t i = 0; i <
nodes_.size(); ++i) {
316 dot_stream << i <<
" [label=\"" << i <<
"\", pos=\"" <<
nodes_[i].first
317 <<
"," <<
nodes_[i].second <<
"!\"" <<
", fillcolor=\"";
318 if (
static_cast<size_t>(
color_[i] + 1) < color_palette.size()) {
319 dot_stream << color_palette[static_cast<size_t>(
color_[i]) + 1];
321 dot_stream << color_palette[0];
323 dot_stream <<
"\", style=filled];\n";
326 for (
const auto &[node1, node2] :
edges_) {
329 dot_stream << node1 <<
" -- " << node2
330 <<
" [style=dashed,penwidth=16,color=\"red\"];\n";
332 dot_stream << node1 <<
" -- " << node2 <<
";\n";
338 std::string command =
339 "neato -T" + filename.substr(filename.rfind(
'.') + 1) +
" -o " + filename;
341 std::unique_ptr<FILE, qbpp::misc::PcloseDeleter> pipe(
342 popen(command.c_str(),
"w"));
345 throw std::runtime_error(
"popen() failed!");
348 std::string dot_content = dot_stream.str();
349 fwrite(dot_content.c_str(),
sizeof(
char), dot_content.size(), pipe.get());
356 inline std::pair<qbpp::Model, qbpp::Vector<qbpp::Vector<qbpp::Var>>>
358 uint32_t color_count) {
363 for (
auto [i, j] : graph_map.
get_edges()) {
var_val_t get(vindex_t index) const
Class to store a graph with node coloring and related information.
uint32_t get_grid_size() const
Gets the size of the grid.
uint32_t min_dist(uint32_t x, uint32_t y) const
Compute the minimum distance between a new node and all other nodes.
void gen_proximity_edges(uint32_t proximity)
Create an edges between nodes that are close to each other.
qbpp::Vector< int32_t > color_
List of colors of the nodes.
void gen_delaunay_edges()
Create edges between nodes using the Delaunay triangulation.
void draw(const std::string &filename)
Draw the graph in a file.
std::vector< std::pair< uint32_t, uint32_t > > edges_
List of edges with the distance between the nodes.
const uint32_t grid_size_
Size of the grid. grid_size x grid_size coordinates are used for the map.
const std::vector< std::pair< uint32_t, uint32_t > > get_edges() const
uint32_t dist(size_t i, size_t j) const
Compute the Euclidean distance between two nodes.
uint32_t dist(const std::pair< int32_t, int32_t > &p1, const std::pair< int32_t, int32_t > &p2) const
Compute the Euclidean distance between two nodes.
GraphColorMap(uint32_t grid_size=100)
Constructor to Create an empty map.
std::vector< uint32_t > color_hist_
Histogram of the colors.
uint32_t failure_
Number of failure nodes.
void gen_random_map(uint32_t n, bool is_circle=false)
Generate a random map with n nodes.
void print()
Displays the histogram of the colors.
std::vector< std::pair< int32_t, int32_t > > nodes_
List of nodes with coordinate (x, y)
void set_color_histogram(const GraphColorQuadModel &model, const qbpp::Sol &sol)
Set the color histogram of the nodes.
std::pair< int32_t, int32_t > & operator[](uint32_t index)
Get the position of the node at index i.
void add_node(uint32_t x, uint32_t y)
Add a node to the map.
uint32_t node_count() const
Gets the number of nodes in the map.
Class to store the QUBO expression with variables for the Graph Coloring Problem.
std::pair< qbpp::Model, qbpp::Vector< qbpp::Vector< qbpp::Var > > > helper_func(const GraphColorMap &map, uint32_t color_count)
Helper function to generate the QUBO expression for the GraphColorMap object and the number of colors...
uint32_t node_count() const
Returns the number of nodes in the TSP.
GraphColorQuadModel(const GraphColorMap &map, uint32_t color_count)
Generate a QUBO expression for the Graph Coloring Problem.
const qbpp::Vector< qbpp::Vector< qbpp::Var > > & get_x() const
Returns the reference of the variables for the Graph Coloring Problem.
const qbpp::Vector< qbpp::Vector< qbpp::Var > > x_
Variables for the Graph Coloring Problem.
GraphColorQuadModel(std::pair< qbpp::QuadModel, qbpp::Vector< qbpp::Vector< qbpp::Var >>> pair)
Delegated constructor for the GraphColorQuadModel.
Generates a QUBO Expression for the Graph Coloring Problem using QUBO++ library.
Expr vector_sum(const T &items[[maybe_unused]])
Var var(const std::string &var_str)
Expr sum(const Vector< T > &items)
Expr simplify_as_binary(const Expr &expr)
int32_t onehot_to_int(const Vector< var_val_t > &vec)
QUBO++, a C++ library for generating expressions for binary and spin variables.
A miscellaneous library used for sample programs of the QUBO++ library.