QUBO++ Library with QUBO Solver APIs
Author: Koji Nakano, License: Non-commercial research and evaluation purposes without any guarantees.
qbpp_abs2.hpp
Go to the documentation of this file.
1 
7 #ifndef QBPP_ABS2_HPP
8 #define QBPP_ABS2_HPP
9 
10 #include <algorithm>
11 
12 #include "abs2.hpp"
13 #include "qbpp.hpp"
14 
16 namespace qbpp_abs2 {
17 
18 //===========================
19 // Class forward declarationsmake
20 //===========================
21 class Solver;
22 class QuadModel;
23 class Param;
24 class Sol;
25 class Callback;
26 
27 //===================
28 // Class declarations
29 //===================
47 
49 
53 class Solver : public abs2::Solver {
54  public:
59  Sol operator()(const QuadModel &quad_model, Param &param) const;
60 
67  Sol operator()(const QuadModel &quad_model, Param &param,
68  const Sol &start) const;
69 
71 };
72 
76 class QuadModel : public qbpp::QuadModel {
78  std::shared_ptr<abs2::Model> abs2model_ptr;
79 
81  QuadModel() = delete;
82 
83  public:
86  QuadModel(const qbpp::QuadModel &quad_model);
87 
90  QuadModel(const QuadModel &quad_model)
91  : qbpp::QuadModel(quad_model), abs2model_ptr(quad_model.abs2model_ptr) {};
92 
95  const abs2::Model &get_abs2_model() const { return *abs2model_ptr; }
96 };
97 
104 class Param : public abs2::Param {
108  std::optional<qbpp::energy_t> target_energy;
109 
110  public:
113 
116  Param() = default;
117 
121  this->target_energy = target_energy;
122  }
123 
126  std::optional<qbpp::energy_t> get_target_energy() const {
127  return target_energy;
128  }
129 
132  void set_time_limit(uint32_t time_limit) {
133  abs2::Param::set("time_limit", std::to_string(time_limit));
134  }
135 
138  void set_arithmetic_bits(uint32_t bits) {
139  abs2::Param::set("arithmetic_bits", std::to_string(bits));
140  }
141 
143 };
144 
148 class Sol : public qbpp::Sol {
151  const std::shared_ptr<abs2::Sol> abs2sol_ptr;
152 
153  public:
156 
160  Sol(const QuadModel &quad_model, const abs2::Sol &sol);
161 
164  Sol(const qbpp::Sol &sol);
165 
167 
170  Sol(const Sol &sol) = default;
171 
174  void print(const std::string &attrs) const { abs2sol_ptr->print(attrs); }
175 
179  void set(int index, bool value) {
180  qbpp::Sol::set(index, value);
181  abs2sol_ptr->set(index, value);
182  }
183 
187  void set(qbpp::Var var, bool value) {
188  qbpp::Sol::set(var, value);
189  abs2sol_ptr->set(get_index(var), value);
190  }
191 
192  double get_tts() const { return std::stod(abs2sol_ptr->get("tts")); }
193 
196  const std::shared_ptr<abs2::Sol> get_abs2sol_ptr() const {
197  return abs2sol_ptr;
198  }
199 };
200 
204 class Callback : public abs2::Callback {
205  protected:
208 
209  public:
212 
217 
218  virtual ~Callback() = default;
219 
222  Sol get_sol() const { return Sol(quad_model, abs2::Callback::get()); };
223 
227  void set_hint(const Sol &hint) {
229  }
236  virtual void callback(const std::string &event) {
237  if (event == "init") {
239  } else if (event == "new") {
240  auto sol = get_sol();
241  std::cout << "TTS = " << std::fixed << std::setprecision(3)
242  << sol.get_tts() << "s Energy=" << sol.get_energy()
243  << std::endl;
244  }
245  }
246 
248 
249 }; // namespace qbpp_abs2
250 
251 //============================
252 // Class QuadModel member function
253 //============================
254 inline QuadModel::QuadModel(const qbpp::QuadModel &quad_model)
255  : qbpp::QuadModel(quad_model) {
256  // Create an empty ABS2 model
257  abs2model_ptr = std::make_unique<abs2::Model>(
258  var_count(), quad_model.get_min_coeff(), quad_model.get_max_coeff());
259 
260  // Set the linear and quadratic terms
261  for (qbpp::vindex_t i = 0; i < var_count(); i++) {
262  if (quad_model.get_linear(i) != 0)
263  abs2model_ptr->set(i, i, quad_model.get_linear(i));
264  }
265  for (qbpp::vindex_t i = 0; i < var_count(); i++) {
266  for (qbpp::vindex_t j = 0; j < quad_model.get_degree(i); j++) {
267  auto [k, coeff] = quad_model.get_quadratic(i, j);
268  abs2model_ptr->set(i, k, coeff);
269  }
270  }
271 }
272 
273 //===============================
274 // Class Solver member functions
275 //===============================
276 
277 // Execute ABS2 for "quad_model" with "param" and returns "sol".
278 inline Sol Solver::operator()(const QuadModel &quad_model, Param &param) const {
279  // target_energy is adjusted by the constant term of the QUBO model because
280  // ABS2 Solver does not handle constant terms.
281  if (param.get_target_energy().has_value()) {
282  param.set("target_energy",
283  std::to_string(param.get_target_energy().value() -
284  quad_model.get_constant()));
285  }
286  return Sol(quad_model,
287  abs2::Solver::operator()(quad_model.get_abs2_model(), param));
288 }
289 
290 // Execute ABS2 for model with "quad_param" and "start", and returns "sol".
291 inline Sol Solver::operator()(const QuadModel &quad_model, Param &param,
292  const Sol &start) const {
293  // target_energy is adjusted by the constant term of the QUBO model because
294  // ABS2 Solver does not handle constant terms.
295  if (param.get_target_energy().has_value()) {
296  param.set("target_energy",
297  std::to_string(param.get_target_energy().value() -
298  quad_model.get_constant()));
299  }
300  return Sol(quad_model,
301  abs2::Solver::operator()(quad_model.get_abs2_model(), param,
302  *start.get_abs2sol_ptr()));
303 }
304 
305 //=============================
306 // Class Sol member functions
307 //=============================
308 inline Sol::Sol(const QuadModel &quad_model, const abs2::Sol &sol)
309  : qbpp::Sol(quad_model), abs2sol_ptr(std::make_shared<abs2::Sol>(sol)) {
310  for (qbpp::vindex_t i = 0; i < quad_model.var_count(); i++) {
311  qbpp::Sol::set(i, abs2sol_ptr->get(i));
312  }
313  energy_ = std::stod(abs2sol_ptr->get("energy")) + quad_model.get_constant();
314 }
315 
316 inline Sol::Sol(const qbpp::Sol &sol)
317  : qbpp::Sol(sol), abs2sol_ptr(std::make_shared<abs2::Sol>(var_count())) {
318  for (qbpp::vindex_t i = 0; i < var_count(); i++) {
319  abs2sol_ptr->set(i, sol.get(i));
320  }
321 }
322 
323 } // namespace qbpp_abs2
324 #endif // QBPP_ABS2_HPP
API for the ABS2 GPU QUBO Solver.
Class to manage callback function for ABS2 QUBO solver.
Definition: abs2.hpp:258
void set(const std::string &operation)
Set the operation to the ABS2 Callback.
const Sol & get() const
Get the current solution.
Class to store and manipulate a QUBO model.
Definition: abs2.hpp:91
Class to store parameters for ABS2 QUBO solver.
Definition: abs2.hpp:162
void set(const std::string &key, const std::string &val)
Set "val" to "key".
Class to store a solution computed by ABS2 QUBO solver.
Definition: abs2.hpp:205
Class to configure the ABS2 QUBO solver.
Definition: abs2.hpp:43
energy_t get_constant() const
Definition: qbpp.hpp:1168
vindex_t var_count() const
Definition: qbpp.hpp:1154
coeff_t get_max_coeff() const
Definition: qbpp.hpp:1272
const std::vector< std::vector< std::pair< vindex_t, coeff_t > > > & get_quadratic() const
Definition: qbpp.hpp:1245
const std::vector< coeff_t > & get_linear() const
Definition: qbpp.hpp:1241
const std::vector< vindex_t > & get_degree() const
Definition: qbpp.hpp:1243
coeff_t get_min_coeff() const
Definition: qbpp.hpp:1270
vindex_t var_count() const
Definition: qbpp.hpp:1556
void set(vindex_t index, bool value)
Definition: qbpp.hpp:1523
vindex_t get_index(Var var) const
Definition: qbpp.hpp:1562
std::optional< energy_t > energy_
Definition: qbpp.hpp:1448
var_val_t get(vindex_t index) const
Definition: qbpp.hpp:1500
Sol()=delete
A class for defining the ABS2 callback function.
Definition: qbpp_abs2.hpp:204
const QuadModel quad_model
QuadModel object created by QUBO++ library.
Definition: qbpp_abs2.hpp:207
A class for setting parameters for the ABS2 QUBO solver.
Definition: qbpp_abs2.hpp:104
std::optional< qbpp::energy_t > target_energy
The target energy given by C++ library.
Definition: qbpp_abs2.hpp:108
A class for storing both the qbpp::QuadModel and abs2::Model.
Definition: qbpp_abs2.hpp:76
std::shared_ptr< abs2::Model > abs2model_ptr
ABS2 model associated with the QUBO model.
Definition: qbpp_abs2.hpp:78
const abs2::Model & get_abs2_model() const
Get the ABS2 model.
Definition: qbpp_abs2.hpp:95
QuadModel()=delete
Default constructor is deleted to avoid creating an empty model.
QuadModel(const QuadModel &quad_model)
Copy constructor.
Definition: qbpp_abs2.hpp:90
A class for storing the ABS2 QUBO solution.
Definition: qbpp_abs2.hpp:148
void set(qbpp::Var var, bool value)
Set the value of the variable "var" to "value".
Definition: qbpp_abs2.hpp:187
const std::shared_ptr< abs2::Sol > abs2sol_ptr
Sol object created by ABS2 QUBO solver.
Definition: qbpp_abs2.hpp:151
void print(const std::string &attrs) const
print the solution
Definition: qbpp_abs2.hpp:174
void set(int index, bool value)
Set the value of the variable with "index" to "value".
Definition: qbpp_abs2.hpp:179
const std::shared_ptr< abs2::Sol > get_abs2sol_ptr() const
Returns the reference to the ABS2 solution.
Definition: qbpp_abs2.hpp:196
Sol(const Sol &sol)=default
Copy constructor.
double get_tts() const
Definition: qbpp_abs2.hpp:192
A class for calling the ABS2 QUBO solver.
Definition: qbpp_abs2.hpp:53
Param()=default
Construct a new Param object from a QuadModel object.
void set_arithmetic_bits(uint32_t bits)
Set the arithmetic bits.
Definition: qbpp_abs2.hpp:138
void set_time_limit(uint32_t time_limit)
Set the time limit for ABS2 QUBO solver.
Definition: qbpp_abs2.hpp:132
Sol get_sol() const
Get the solution from the ABS2 solver.
Definition: qbpp_abs2.hpp:222
virtual void callback(const std::string &event)
The default callback function for ABS2 QUBO solver.
Definition: qbpp_abs2.hpp:236
Callback(const QuadModel &quad_model)
Construct a new Callback object.
Definition: qbpp_abs2.hpp:215
std::optional< qbpp::energy_t > get_target_energy() const
Get the target energy for ABS2 QUBO solver.
Definition: qbpp_abs2.hpp:126
Sol operator()(const QuadModel &quad_model, Param &param) const
Executes ABS2 for "quad_model" with "param" and returns "sol".
Definition: qbpp_abs2.hpp:278
virtual ~Callback()=default
Destructor for deleting a callback object.
void set_target_energy(qbpp::energy_t target_energy)
Set the target energy for ABS2 QUBO solver.
Definition: qbpp_abs2.hpp:120
void set_hint(const Sol &hint)
Provide a hint solution to the ABS2 solver.
Definition: qbpp_abs2.hpp:227
Namespace to call ABS2 GPU QUBO solver.
Definition: abs2.hpp:26
Namespace to use ABS2 QUBO solver from QUBO++ library.
Definition: qbpp_abs2.hpp:16
Generates a QUBO Expression for the Graph Coloring Problem using QUBO++ library.
Definition: qbpp.hpp:53
Var var(const std::string &var_str)
Definition: qbpp.hpp:2172
uint32_t vindex_t
Definition: qbpp.hpp:122
ENERGY_TYPE energy_t
Definition: qbpp.hpp:130
QUBO++, a C++ library for generating expressions for binary and spin variables.