QUBO++ Library with QUBO Solver APIs
Author: Koji Nakano, License: Non-commercial research and evaluation purposes without any guarantees.
partition_easy.cpp
Go to the documentation of this file.
1 
6 #include <boost/program_options.hpp>
7 
8 #include "qbpp.hpp"
9 #include "qbpp_easy_solver.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++ Easy 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  ("time,t", po::value<uint32_t>()->default_value(10), "Set the time limit in seconds.");
27  // clang-format on
28 
29  po::variables_map vm;
30  try {
31  po::store(po::parse_command_line(argc, argv, desc), vm);
32  } catch (const std::exception &e) {
33  std::cout << "Wrong arguments. Please use -h/--help option to see the "
34  "usage.\n";
35  return 1;
36  }
37  po::notify(vm);
38 
39  if (vm.count("help")) {
40  std::cout << desc << std::endl;
41  return 0;
42  }
43 
44  // Set the random seed for deterministic behavior if seed is provided.
45  if (vm.count("random")) {
46  qbpp::misc::RandomGenerator::set_seed(vm["random"].as<uint32_t>());
47  }
48 
50  size_t size = vm["size"].as<size_t>();
52  qbpp::energy_t max_val = vm["max"].as<qbpp::energy_t>();
53 
55  uint32_t time_limit = vm["time"].as<uint32_t>();
56 
59 
61  for (size_t i = 0; i < size; i++) {
63  }
64 
66  std::cout << qbpp::str(w, "w") << std::endl;
67 
68  auto x = qbpp::var("x", w.size());
69  auto f =
70  qbpp::sqr(qbpp::sum(w * (1 - x)) - qbpp::sum(w * x)).simplify_as_binary();
71 
72  // Generates a QUBO++ easy solver object from the QUBO model
73  auto solver = qbpp::easy_solver::EasySolver(f);
74 
75  // Sets the time limit
76  solver.set_time_limit(time_limit);
77 
78  // Sets the target energy to 1
79  solver.set_target_energy(1);
80 
81  // Enables the default callback
82  solver.enable_default_callback();
83 
84  // Executes the QUBO++ easy solver
85  auto sol = solver.search();
86 
87  // Prints the QUBO solution
88  std::cout << "Solution = " << sol << std::endl;
89 
90  // Prints the sum of the input set and the sum of the elements in the set
91  std::cout << "sum0 = " << qbpp::eval(qbpp::sum(w * (1 - x)), sol) << " :";
92  for (size_t i = 0; i < size; i++) {
93  if (sol.get(x[i]) == 0) {
94  std::cout << " " << w[i];
95  }
96  }
97  std::cout << std::endl;
98  std::cout << "sum1 = " << qbpp::eval(qbpp::sum(w * x), sol) << " :";
99  for (size_t i = 0; i < size; i++) {
100  if (sol.get(x[i]) == 1) {
101  std::cout << " " << w[i];
102  }
103  }
104  std::cout << std::endl;
105 }
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++ 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.