QUBO++ Library with QUBO Solver APIs
Author: Koji Nakano, License: Non-commercial research and evaluation purposes without any guarantees.
graph_color_easy.cpp
Go to the documentation of this file.
1 
7 #include <boost/program_options.hpp>
8 
9 #include "qbpp.hpp"
10 #include "qbpp_easy_solver.hpp"
11 #include "qbpp_graph_color.hpp"
12 #include "qbpp_misc.hpp"
13 
14 namespace po = boost::program_options;
15 
20 int main(int argc, char **argv) {
21  po::options_description desc(
22  "Solve a randomly generated Graph Coloring Problem using the QUBO++ Easy "
23  "Solver.");
24  // clang-format off
25  desc.add_options()
26  ("help,h", "Display this help message.")
27  ("nodes,n", po::value<uint32_t>()->default_value(100), "Specify the number of nodes.")
28  ("proximity,p", po::value<uint32_t>()->default_value(20), "Specify the proximity between nodes.")
29  ("Circle,C", "Arrange nodes in a circular layout.")
30  ("Delaunay,D", "Connect nodes using Delaunay triangulation.")
31  ("colors,c", po::value<uint32_t>()->default_value(4), "Specify the number of colors to use.")
32  ("time,t", po::value<uint32_t>()->default_value(10), "Set the time limit in seconds.")
33  ("seed,s", po::value<uint32_t>(), "Set the initial random seed to reproduce the generated map.")
34  ("output,o", po::value<std::string>(), "Specify the output file (e.g., png, svg) to save the solution.");
35  // clang-format on
36 
37  po::variables_map vm;
38  try {
39  po::store(po::parse_command_line(argc, argv, desc), vm);
40  } catch (const std::exception &e) {
41  std::cout << "Wrong arguments. Please use -h/--help option to see the "
42  "usage.\n";
43  return 1;
44  }
45  po::notify(vm);
46 
47  if (vm.count("help")) {
48  std::cout << desc << std::endl;
49  return 0;
50  }
51 
53  uint32_t node_count = vm["nodes"].as<uint32_t>();
55  uint32_t time_limit = vm["time"].as<uint32_t>();
57  uint32_t color_count = vm["colors"].as<uint32_t>();
58 
59  // Set the random seed for deterministic behavior if seed is provided.
60  if (vm.count("problem_seed")) {
61  qbpp::misc::RandomGenerator::set_seed(vm["problem_seed"].as<uint32_t>());
62  } else {
64  }
65 
66  std::cout << "Generating random graph with " << node_count << " nodes"
67  << std::endl;
68  qbpp::graph_color::GraphColorMap graph_color_map;
69 
70  graph_color_map.gen_random_map(node_count, vm.count("Circle"));
71 
72  if (vm.count("Delaunay")) {
73  graph_color_map.gen_delaunay_edges();
74  } else {
75  graph_color_map.gen_proximity_edges(vm["proximity"].as<uint32_t>());
76  }
77  qbpp::graph_color::GraphColorQuadModel model(graph_color_map, color_count);
78 
79  std::cout << "Variables = " << model.var_count()
80  << " Linear Terms = " << model.term_count(1)
81  << " Quadratic Terms = " << model.term_count(2) << std::endl;
82 
83  if (vm.count("easy_solver_seed")) {
85  vm["easy_solver_seed"].as<uint32_t>());
86  } else {
88  }
89 
90  qbpp::easy_solver::EasySolver solver(model);
91 
92  solver.set_time_limit(time_limit);
93  solver.set_target_energy(0);
94  solver.enable_default_callback();
95 
96  auto sol = solver.search();
97 
98  graph_color_map.set_color_histogram(model, sol);
99  graph_color_map.print();
100 
101  if (vm.count("output") > 0) {
102  std::cout << "Writing the solution to " << vm["output"].as<std::string>()
103  << std::endl;
104  graph_color_map.draw(vm["output"].as<std::string>());
105  }
106 }
vindex_t var_count() const
Definition: qbpp.hpp:1154
size_t term_count(vindex_t deg) const
Definition: qbpp.hpp:1258
void set_target_energy(energy_t energy)
void set_time_limit(uint32_t limit)
Class to store a graph with node coloring and related information.
void gen_proximity_edges(uint32_t proximity)
Create an edges between nodes that are close to each other.
void gen_delaunay_edges()
Create edges between nodes using the Delaunay triangulation.
void draw(const std::string &filename)
Draw the graph in a file.
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.
void set_color_histogram(const GraphColorQuadModel &model, const qbpp::Sol &sol)
Set the color histogram of the nodes.
Class to store the QUBO expression with variables for the Graph Coloring Problem.
static void set_seed(uint32_t seed=1)
Definition: qbpp_misc.hpp:41
int main(int argc, char **argv)
Main function to generate a random map and solve the Graph Coloring Problem using the Easy Solver.
QUBO++, a C++ library for generating expressions for binary and spin variables.
Easy QUBO Solver for solving QUBO problems.
A miscellaneous library used for sample programs of the QUBO++ library.