QUBO++ Library with QUBO Solver APIs
Author: Koji Nakano, License: Non-commercial research and evaluation purposes without any guarantees.
graph_color_abs2.cpp
Go to the documentation of this file.
1 
7 #include <boost/program_options.hpp>
8 
9 #include "qbpp.hpp"
10 #include "qbpp_abs2.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  // clang-format off
22  po::options_description desc(
23  "Solving the randomly generated Graph Coloring Problem using QUBO++ Easy Solver");
24  desc.add_options()("help,h", "produce help message")
25  ("nodes,n", po::value<uint32_t>()->default_value(100),"set the number of nodes")
26  ("proximity,p", po::value<uint32_t>()->default_value(20),"set the proximity of the nodes")
27  ("Circle,C","Set nodes are placed on a circle")
28  ("Delaunay,D","Use Delaunay triangulation to connect the nodes")
29  ("colors,c", po::value<uint32_t>()->default_value(4),"set the number of colors to use")
30  ("time,t", po::value<uint32_t>()->default_value(10), "set time limit in seconds")
31  ("problem_seed,s", po::value<uint32_t>(), "set the random seed for the TSP map")
32  ("output,o", po::value<std::string>(), "set the output file (png, svg, etc) to save the TSP solution");
33  // clang-format on
34 
35  po::variables_map vm;
36  try {
37  po::store(po::parse_command_line(argc, argv, desc), vm);
38  } catch (const std::exception &e) {
39  std::cout << "Wrong arguments. Please use -h/--help option to see the "
40  "usage.\n";
41  return 1;
42  }
43  po::notify(vm);
44 
45  if (vm.count("help")) {
46  std::cout << desc << std::endl;
47  return 0;
48  }
49 
51  uint32_t node_count = vm["nodes"].as<uint32_t>();
53  uint32_t time_limit = vm["time"].as<uint32_t>();
55  uint32_t color_count = vm["colors"].as<uint32_t>();
56 
57  // Set the random seed for deterministic behavior if seed is provided.
58  if (vm.count("problem_seed")) {
59  qbpp::misc::RandomGenerator::set_seed(vm["problem_seed"].as<uint32_t>());
60  } else {
62  }
63 
64  std::cout << "Generating random graph with " << node_count << " nodes"
65  << std::endl;
66  qbpp::graph_color::GraphColorMap graph_color_map;
67 
68  graph_color_map.gen_random_map(node_count, vm.count("Circle"));
69 
70  if (vm.count("Delaunay")) {
71  graph_color_map.gen_delaunay_edges();
72  } else {
73  graph_color_map.gen_proximity_edges(vm["proximity"].as<uint32_t>());
74  }
75  qbpp::graph_color::GraphColorQuadModel model(graph_color_map, color_count);
76 
77  std::cout << "Variables = " << model.var_count()
78  << " Linear Terms = " << model.term_count(1)
79  << " Quadratic Terms = " << model.term_count(2) << std::endl;
80 
81  if (vm.count("easy_solver_seed")) {
83  vm["easy_solver_seed"].as<uint32_t>());
84  } else {
86  }
87 
88  qbpp_abs2::Solver solver;
89  qbpp_abs2::Param param;
90  param.set_time_limit(time_limit);
91  param.set_target_energy(0);
92  auto sol = solver(model, param);
93 
94  graph_color_map.set_color_histogram(model, sol);
95 
96  graph_color_map.print();
97 
98  if (vm.count("output") > 0) {
99  std::cout << "Writing the solution to " << vm["output"].as<std::string>()
100  << std::endl;
101  graph_color_map.draw(vm["output"].as<std::string>());
102  }
103 }
vindex_t var_count() const
Definition: qbpp.hpp:1154
size_t term_count(vindex_t deg) const
Definition: qbpp.hpp:1258
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
A class for setting parameters for the ABS2 QUBO solver.
Definition: qbpp_abs2.hpp:104
A class for calling the ABS2 QUBO solver.
Definition: qbpp_abs2.hpp:53
int main(int argc, char **argv)
Main function to generate a random map and solve the Graph Coloring Problem using the ABS2 QUBO Solve...
void set_time_limit(uint32_t time_limit)
Set the time limit for ABS2 QUBO solver.
Definition: qbpp_abs2.hpp:132
void set_target_energy(qbpp::energy_t target_energy)
Set the target energy for ABS2 QUBO solver.
Definition: qbpp_abs2.hpp:120
QUBO++, a C++ library for generating expressions for binary and spin variables.
QUBO++ interface to call ABS2 GPU QUBO solver.
A miscellaneous library used for sample programs of the QUBO++ library.