QUBO++ Library with QUBO Solver APIs
Author: Koji Nakano, License: Non-commercial research and evaluation purposes without any guarantees.
nqueen_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_nqueen.hpp"
12 
13 namespace po = boost::program_options;
14 
22 int main(int argc, char *argv[]) {
23  // clang-format off
24  po::options_description desc(
25  "N-Queens Problem Solver using QUBO++ Easy Solver");
26  desc.add_options()("help,h", "produce help message")
27  ("dimension,d", po::value<int>()->default_value(8), "set dimension of the chessboard")
28  ("time_limit,t", po::value<int>()->default_value(10), "set time limit in seconds")
29  ("seed,s", po::value<int>(), "set random seed")
30  ("expand,e", "expand the one-hot formula for QUBO model generation")
31  ("fast,f", "fast mode for QUBO model generation")
32  ("parallel,p", "parallel mode for QUBO model generation (default)");
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 
50  int dimension = vm["dimension"].as<int>();
51  int time_limit = vm["time_limit"].as<int>();
52 
53  // Set the random seed for deterministic behavior if seed is provided.
54  if (vm.count("seed")) {
55  qbpp::misc::RandomGenerator::set_seed(vm["seed"].as<int>());
56  }
57 
59  if (vm.count("expand")) {
61  } else if (vm.count("fast")) {
63  } else if (vm.count("parallel")) {
65  } else {
67  }
68 
69  qbpp::nqueen::NQueenQuadModel nqueen_model(dimension, mode);
70 
71  std::cout << "Generating the QUBO model." << std::endl;
72 
73  std::cout << "Variables = " << nqueen_model.var_count()
74  << " Linear Terms = " << nqueen_model.term_count(1)
75  << " Quadratic Terms = " << nqueen_model.term_count(2) << std::endl;
76 
77  std::cout << "Generating an EasySolver object for the QUBO model."
78  << std::endl;
79  auto solver = qbpp::easy_solver::EasySolver(nqueen_model);
80  solver.set_time_limit(time_limit);
81  solver.set_target_energy(0);
82  solver.enable_default_callback();
83 
84  std::cout << "Executing the EasySolver to solve the QUBO model." << std::endl;
85  auto sol = solver.search();
86 
87  // Print the solution as a chessboard.
88  for (int i = 0; i < dimension; ++i) {
89  for (int j = 0; j < dimension; ++j)
90  std::cout << static_cast<int>(sol.get(nqueen_model.get_var(i, j)));
91  std::cout << std::endl;
92  }
93 
94  std::cout << "Dimension = " << dimension << " TTS = " << std::fixed
95  << std::setprecision(3) << std::setfill('0') << solver.get_tts()
96  << "s Energy = " << sol.get_energy() << std::endl;
97 }
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 generate a QUBO model for the N-Queens problem.
Definition: qbpp_nqueen.hpp:22
qbpp::Var get_var(int i, int j) const
Gets the variable at (i, j)
int main(int argc, char *argv[])
Solves the N-Queens problem using EasySolver in the QUBO++ library. library.
Definition: nqueen_easy.cpp:22
QUBO++, a C++ library for generating expressions for binary and spin variables.
Easy QUBO Solver for solving QUBO problems.
Generates QUBO expression for the N-Queens problem using the QUBO++ library.