QUBO++ Library with QUBO Solver APIs
Author: Koji Nakano, License: Non-commercial research and evaluation purposes without any guarantees.
partition_exhaustive.cpp
Go to the documentation of this file.
1 
6 #include <boost/program_options.hpp>
7 
8 #include "qbpp.hpp"
10 #include "qbpp_misc.hpp"
11 
12 namespace po = boost::program_options;
13 
18 int main(int argc, char **argv) {
19  // clang-format off
20  po::options_description desc(
21  "Solving the random partitioning problem using QUBO++ Exhaustive Solver.");
22  desc.add_options()("help,h", "produce help message.")
23  ("size,s", po::value<size_t>()->default_value(10), "Set the size of the input set.")
24  ("random,r", po::value<int>(), "Set the base seed for the random number generator for deterministic behavior.")
25  ("max,m", po::value<qbpp::energy_t>()->default_value(1000), "Set the maximum value of the input set.");
26  // clang-format on
27 
28  po::variables_map vm;
29  try {
30  po::store(po::parse_command_line(argc, argv, desc), vm);
31  } catch (const std::exception &e) {
32  std::cout << "Wrong arguments. Please use -h/--help option to see the "
33  "usage.\n";
34  return 1;
35  }
36  po::notify(vm);
37 
38  if (vm.count("help")) {
39  std::cout << desc << std::endl;
40  return 0;
41  }
42 
43  // Set the random seed for deterministic behavior if seed is provided.
44  if (vm.count("random")) {
45  qbpp::misc::RandomGenerator::set_seed(vm["random"].as<uint32_t>());
46  }
47 
49  size_t size = vm["size"].as<size_t>();
51  qbpp::energy_t max_val = vm["max"].as<qbpp::energy_t>();
52 
55 
57  for (size_t i = 0; i < size; i++) {
59  }
60 
62  std::cout << qbpp::str(w, "w") << std::endl;
63 
64  auto x = qbpp::var("x", w.size());
65  auto f = qbpp::sqr(2 * qbpp::sum(w * x) - qbpp::sum(w)).simplify_as_binary();
66 
67  // Generates a QUBO++ easy solver object from the QUBO model
69 
70  // Enables the default callback
71  solver.enable_default_callback();
72 
73  // Executes the QUBO++ easy solver
74  auto sol = solver.search();
75 
76  // Prints the QUBO solution
77  std::cout << "Solution = " << sol << std::endl;
78 
79  // Prints the sum of the input set and the sum of the elements in the set
80  std::cout << "sum0 = " << qbpp::eval(qbpp::sum(w * (1 - x)), sol) << " :";
81  for (size_t i = 0; i < size; i++) {
82  if (sol.get(x[i]) == 0) {
83  std::cout << " " << w[i];
84  }
85  }
86  std::cout << std::endl;
87  std::cout << "sum1 = " << qbpp::eval(qbpp::sum(w * x), sol) << " :";
88  for (size_t i = 0; i < size; i++) {
89  if (sol.get(x[i]) == 1) {
90  std::cout << " " << w[i];
91  }
92  }
93  std::cout << std::endl;
94 }
void emplace_back(T &&t)
Definition: qbpp.hpp:377
static void set_seed(uint32_t seed=1)
Definition: qbpp_misc.hpp:41
static uint64_t gen()
Definition: qbpp_misc.hpp:45
Var var(const std::string &var_str)
Definition: qbpp.hpp:2172
std::string str(Var var)
Definition: qbpp.hpp:1715
Expr sum(const Vector< T > &items)
Definition: qbpp.hpp:3209
ENERGY_TYPE energy_t
Definition: qbpp.hpp:130
energy_t eval(const Expr &expr, const Sol &sol)
Definition: qbpp.hpp:2109
Vector< Expr > sqr(const Vector< T > &arg)
Definition: qbpp.hpp:3547
int main(int argc, char **argv)
Solves the Partitioning problem using the QUBO++ Exhaustive Solver.
QUBO++, a C++ library for generating expressions for binary and spin variables.
Exhaustive QUBO Solver for solving QUBO problems.
A miscellaneous library used for sample programs of the QUBO++ library.