QUBO++ Library with QUBO Solver APIs
Author: Koji Nakano, License: Non-commercial research and evaluation purposes without any guarantees.
tsp_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_misc.hpp"
12 #include "qbpp_tsp.hpp"
13 
14 namespace po = boost::program_options;
15 
20 
21  public:
26 
29  void callback(const std::string &event) override {
30  if (event == "init") {
31  // Enable the callback for every new best solution.
32  set("new");
33  } else if (event == "new") {
34  auto sol = get_sol();
35  qbpp::tsp::TSPSol tsp_sol(tsp_quad_model, sol);
36  std::cout << "TTS = " << std::fixed << std::setprecision(3)
37  << sol.get_tts() << "s ";
38  tsp_sol.print();
39  }
40  }
41 };
42 
49 int main(int argc, char **argv) {
50  // clang-format off
51  po::options_description desc(
52  "Solving the randomly generated TSP using QUBO++ Easy Solver");
53  desc.add_options()("help,h", "produce help message")
54  ("nodes,n", po::value<uint32_t>()->default_value(10),"set the number of nodes in the TSP map")
55  ("time,t", po::value<uint32_t>()->default_value(10), "set time limit in seconds")
56  ("tsp_seed,s", po::value<uint32_t>(), "set the random seed for the TSP map")
57  ("output,o", po::value<std::string>(), "set the output file (png, svg, etc) to save the TSP solution")
58  ("fix,f", "fix node 0 as the starting node");
59  // clang-format on
60 
61  po::variables_map vm;
62  try {
63  po::store(po::parse_command_line(argc, argv, desc), vm);
64  } catch (const std::exception &e) {
65  std::cout << "Wrong arguments. Please use -h/--help option to see the "
66  "usage.\n";
67  return 1;
68  }
69  po::notify(vm);
70 
71  if (vm.count("help")) {
72  std::cout << desc << std::endl;
73  return 0;
74  }
75 
77  uint32_t nodes = vm["nodes"].as<uint32_t>();
79  uint32_t time_limit = vm["time"].as<uint32_t>();
81  bool fix_first = vm.count("fix");
82 
83  // Set the random seed for deterministic behavior if seed is provided.
84  if (vm.count("tsp_seed")) {
85  qbpp::misc::RandomGenerator::set_seed(vm["tsp_seed"].as<uint32_t>());
86  } else {
88  }
89 
90  std::cout << "Generating random TSP Map with " << nodes << " nodes"
91  << std::endl;
92  qbpp::tsp::TSPMap tsp_map;
93  tsp_map.gen_random_map(nodes);
94 
95  std::cout << "Generating a TSP QUBO expression" << std::endl;
96  qbpp::tsp::TSPQuadModel tsp_quad_model(tsp_map, fix_first);
97 
98  std::cout << "Variables = " << tsp_quad_model.var_count()
99  << " Linear Terms = " << tsp_quad_model.term_count(1)
100  << " Quadratic Terms = " << tsp_quad_model.term_count(2)
101  << std::endl;
102 
103  // Initialize ABS2 solver
104  qbpp_abs2::Solver abs2_solver;
105  // Create a model for ABS2 solver from model
106  qbpp_abs2::QuadModel abs2_model(tsp_quad_model);
107  // Create a callback function for ABS2 solver.
108  ABS2Callback abs2_callback(tsp_quad_model);
109  // Create a parameters object to store solver parameters.
110  qbpp_abs2::Param abs2_param;
111  // Set a time limit.
112  abs2_param.set_time_limit(time_limit);
113  // Set the callback function for ABS2 solver.
114  abs2_param.set(abs2_callback);
115 
116  std::cout << "Solving the TSP" << std::endl;
117  auto sol = abs2_solver(abs2_model, abs2_param);
118 
119  qbpp::tsp::TSPSol tsp_sol(tsp_quad_model, sol);
120  tsp_sol.print();
121 
122  if (vm.count("output")) {
124  for (uint32_t i = 0; i < nodes; ++i) graph.add_node(tsp_map[i]);
125 
126  for (uint32_t i = 0; i < nodes; ++i)
127  graph.add_edge(tsp_sol[i], tsp_sol[(i + 1) % nodes]);
128 
129  graph.draw(vm["output"].as<std::string>());
130  }
131 }
Class to define ABS2 callback function for factorization.
Definition: tsp_abs2.cpp:17
void callback(const std::string &event) override
Callback function for ABS2 solver.
Definition: tsp_abs2.cpp:29
ABS2Callback(const qbpp::tsp::TSPQuadModel &tsp_quad_model)
Construct a new ABS2 callback object.
Definition: tsp_abs2.cpp:24
const qbpp::tsp::TSPQuadModel & tsp_quad_model
The TSP expression.
Definition: tsp_abs2.cpp:19
void set(const std::string &operation)
Set the operation to the ABS2 Callback.
Callback()
Constructor for creating a callback object.
void set(const std::string &key, const std::string &val)
Set "val" to "key".
vindex_t var_count() const
Definition: qbpp.hpp:1154
size_t term_count(vindex_t deg) const
Definition: qbpp.hpp:1258
static void set_seed(uint32_t seed=1)
Definition: qbpp_misc.hpp:41
Class to draw a simple undirected graph.
Definition: qbpp_tsp.hpp:206
void add_edge(unsigned int node1, unsigned int node2)
Add an edge to the graph.
Definition: qbpp_tsp.hpp:235
void draw(std::string filename)
Draw the graph in a file.
Definition: qbpp_tsp.hpp:248
void add_node(int x, int y, const std::string &label="")
Add a node to the graph.
Definition: qbpp_tsp.hpp:221
Class to generates a random map for the TSP with n nodes.
Definition: qbpp_tsp.hpp:30
void gen_random_map(uint32_t n)
Generate a random map with n nodes.
Definition: qbpp_tsp.hpp:284
Class to store the QUBO expression for the Traveling Salesman Problem (TSP).
Definition: qbpp_tsp.hpp:103
Class to store a Tour of the TSP.
Definition: qbpp_tsp.hpp:143
void print() const
Print the tour.
Definition: qbpp_tsp.hpp:177
A class for defining the ABS2 callback function.
Definition: qbpp_abs2.hpp:204
A class for setting parameters for the ABS2 QUBO solver.
Definition: qbpp_abs2.hpp:104
A class for storing both the qbpp::QuadModel and abs2::Model.
Definition: qbpp_abs2.hpp:76
A class for calling the ABS2 QUBO solver.
Definition: qbpp_abs2.hpp:53
void set_time_limit(uint32_t time_limit)
Set the time limit for ABS2 QUBO solver.
Definition: qbpp_abs2.hpp:132
Sol get_sol() const
Get the solution from the ABS2 solver.
Definition: qbpp_abs2.hpp:222
Namespace to use ABS2 QUBO solver from QUBO++ library.
Definition: qbpp_abs2.hpp:16
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.
Generates a QUBO Expression for the Traveling Salesman Problem (TSP) using QUBO++ library.
int main(int argc, char **argv)
Main function to generate a random map and solve the Traveling Salesman Problem (TSP) using the ABS2 ...
Definition: tsp_abs2.cpp:49