QUBO++ Library with QUBO Solver APIs
Author: Koji Nakano, License: Non-commercial research and evaluation purposes without any guarantees.
ilp_easy.cpp
Go to the documentation of this file.
1 
32 #include <iostream>
33 
34 #include "qbpp.hpp"
35 #include "qbpp_easy_solver.hpp"
36 
37 int main() {
38  // Define variables
39  auto x1 = 0 <= qbpp::var_int("x1") <= 5;
40  auto x2 = 0 <= qbpp::var_int("x2") <= 4;
41  auto x3 = 0 <= qbpp::var_int("x3") <= 3;
42  auto x4 = 0 <= qbpp::var_int("x4") <= 6;
43 
44  // Define the objective function
45  auto obj = 6 * x1 + 4 * x2 + 3 * x3 + 5 * x4;
46 
47  // Define the constraints
48  auto c1 = x1 + 2 * x2 + 3 * x3 + 4 * x4;
49  auto c2 = 3 * x1 + x2 + 2 * x3 + x4;
50 
51  // Define QUBO expression
52  auto f = -obj + 100 * ((0 <= c1 <= 15) + (0 <= c2 <= 10));
53 
54  // Simplify the QUBO expression
55  f.simplify_as_binary();
56 
57  // Define the solver
58  auto solver = qbpp::easy_solver::EasySolver(f);
59 
60  // Set the time limit
61  solver.set_time_limit(5);
62 
63  // Enable the default callback
64  solver.enable_default_callback();
65 
66  // Solve the ILP problem
67  auto sol = solver.search();
68 
69  // Print the solution
70  std::cout << sol << std::endl;
71 
72  std::cout << "x1 = " << eval(x1, sol) << std::endl;
73  std::cout << "x2 = " << eval(x2, sol) << std::endl;
74  std::cout << "x3 = " << eval(x3, sol) << std::endl;
75  std::cout << "x4 = " << eval(x4, sol) << std::endl;
76  std::cout << "c1 = " << eval(c1, sol) << std::endl;
77  std::cout << "c2 = " << eval(c2, sol) << std::endl;
78  std::cout << "Objective value = " << eval(obj, sol) << std::endl;
79 }
int main()
Definition: ilp_easy.cpp:37
VarIntCore var_int(const std::string &var_str)
Definition: qbpp.hpp:2217
energy_t eval(const Expr &expr, const Sol &sol)
Definition: qbpp.hpp:2109
QUBO++, a C++ library for generating expressions for binary and spin variables.
Easy QUBO Solver for solving QUBO problems.