QUBO++ Library with QUBO Solver APIs
Author: Koji Nakano, License: Non-commercial research and evaluation purposes without any guarantees.
bin_packing_easy.cpp
Go to the documentation of this file.
1 
8 #include "qbpp.hpp"
9 #include "qbpp_easy_solver.hpp"
10 
11 int main() {
12  // The number of bins
13  const size_t bin_count = 4;
14  // The weights of items
15  qbpp::Vector<int> weights = {33, 61, 58, 41, 50, 21, 60, 64};
16 
17  // Define a matrix of binary variables
18  // x[i][j] = 1 if item j is in bin i, 0 otherwise
19  auto x = qbpp::var("x", bin_count, weights.size());
20 
21  // Each item is in exactly one bin, i.e., the sum of each column is 1
22  auto f = qbpp::sum(qbpp::vector_sum(qbpp::transpose(x)) == 1);
23 
24  // The total weight of items in each bin is at most 120
25  for (size_t i = 0; i < bin_count; i++) {
26  f += 0 <= qbpp::sum(x[i] * weights) <= 120;
27  }
28 
30 
31  std::cout << "f = " << f << std::endl;
32 
33  auto solver = qbpp::easy_solver::EasySolver(f);
34 
35  solver.set_target_energy(0);
36  solver.enable_default_callback();
37 
38  auto sol = solver.search();
39 
40  std::cout << "Solution = " << sol << std::endl;
41 
42  for (size_t i = 0; i < bin_count; i++) {
43  std::cout << "Bin " << i << " :";
44  for (size_t j = 0; j < weights.size(); j++) {
45  if (sol.get(x[i][j]) == 1) {
46  std::cout << " " << weights[j];
47  }
48  }
49  std::cout << std::endl;
50  }
51 }
int main()
Expr & simplify_as_binary()
Definition: qbpp.hpp:1956
size_t size() const
Definition: qbpp.hpp:385
Expr vector_sum(const T &items[[maybe_unused]])
Definition: qbpp.hpp:3282
Var var(const std::string &var_str)
Definition: qbpp.hpp:2172
Vector< Vector< Expr > > transpose(const Vector< Vector< T >> &vec)
Definition: qbpp.hpp:3618
Expr sum(const Vector< T > &items)
Definition: qbpp.hpp:3209
QUBO++, a C++ library for generating expressions for binary and spin variables.
Easy QUBO Solver for solving QUBO problems.