QUBO++ Library with QUBO Solver APIs
Author: Koji Nakano, License: Non-commercial research and evaluation purposes without any guarantees.
ilp_exhaustive.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
32 
33 #include "qbpp.hpp"
35 
36 int main() {
37  // Define variables
38  auto x1 = 0 <= qbpp::var_int("x1") <= 5;
39  auto x2 = 0 <= qbpp::var_int("x2") <= 4;
40  auto x3 = 0 <= qbpp::var_int("x3") <= 3;
41  auto x4 = 0 <= qbpp::var_int("x4") <= 6;
42 
43  // Define the objective function
44  auto obj = 6 * x1 + 4 * x2 + 3 * x3 + 5 * x4;
45 
46  // Define the constraints
47  auto c1 = x1 + 2 * x2 + 3 * x3 + 4 * x4;
48  auto c2 = 3 * x1 + x2 + 2 * x3 + x4;
49 
50  // Define QUBO expression
51  auto f = -obj + 100 * ((0 <= c1 <= 15) + (0 <= c2 <= 10));
52 
53  // Simplify the QUBO expression
54  f.simplify_as_binary();
55 
56  // Define the solver
58 
59  // Finds all optimal solutions
60  auto sol_optimal = solver.search_optimal_solutions();
61 
62  // Print the solution
63  std::cout << sol_optimal;
64 
65  // Gets the first optimal solution
66  auto sol = sol_optimal.get_sol();
67 
68  // Print the first optimal solution
69  std::cout << "x1 = " << eval(x1, sol) << std::endl;
70  std::cout << "x2 = " << eval(x2, sol) << std::endl;
71  std::cout << "x3 = " << eval(x3, sol) << std::endl;
72  std::cout << "x4 = " << eval(x4, sol) << std::endl;
73  std::cout << "c1 = " << eval(c1, sol) << std::endl;
74  std::cout << "c2 = " << eval(c2, sol) << std::endl;
75  std::cout << "Objective value = " << eval(obj, sol) << std::endl;
76 }
int main()
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.
Exhaustive QUBO Solver for solving QUBO problems.