5#ifndef IDOL_CPLEXCALLBACKI_H
6#define IDOL_CPLEXCALLBACKI_H
9#include "idol/mixed-integer/optimizers/callbacks/Callback.h"
10#include <ilcplex/ilocplex.h>
13 namespace Optimizers {
16 class CplexUserCutCallbackI;
17 class CplexLazyConstraintCallbackI;
18 class CplexHeuristicCallbackI;
19 class CplexBranchCallbackI;
25class idol::Optimizers::impl::CplexUserCutCallbackI :
public IloCplex::UserCutCallbackI {
26 CplexCallbackI *m_callback;
28 explicit CplexUserCutCallbackI(IloEnv t_env, CplexCallbackI *t_callback) : IloCplex::UserCutCallbackI(t_env), m_callback(t_callback) {}
30 using IloCplex::UserCutCallbackI::add;
34 [[nodiscard]] CallbackI *duplicateCallback()
const override {
35 throw Exception(
"Duplicate CplexUserCutCallbackI!");
39class idol::Optimizers::impl::CplexLazyConstraintCallbackI :
public IloCplex::LazyConstraintCallbackI {
40 CplexCallbackI *m_callback =
nullptr;
42 explicit CplexLazyConstraintCallbackI(IloEnv t_env, CplexCallbackI *t_callback) : IloCplex::LazyConstraintCallbackI(t_env), m_callback(t_callback) {}
44 using IloCplex::LazyConstraintCallbackI::add;
48 [[nodiscard]] CallbackI *duplicateCallback()
const override {
49 std::cerr <<
"Warning: Cplex called duplicateCallback and it is not implemented..." << std::endl;
50 throw Exception(
"Duplicate CplexLazyConstraintCallbackI!");
54class idol::Optimizers::impl::CplexBranchCallbackI :
public IloCplex::BranchCallbackI {
55 CplexCallbackI *m_callback =
nullptr;
57 explicit CplexBranchCallbackI(IloEnv t_env, CplexCallbackI *t_callback) : IloCplex::BranchCallbackI(t_env), m_callback(t_callback) {}
59 using IloCplex::BranchCallbackI::makeBranch;
63 [[nodiscard]] CallbackI *duplicateCallback()
const override {
64 std::cerr <<
"Warning: Cplex called duplicateCallback and it is not implemented..." << std::endl;
65 throw Exception(
"Duplicate CplexBranchCallbackI!");
69class idol::CplexCallbackI :
public CallbackI {
70 Optimizers::Cplex &m_parent;
71 std::list<std::unique_ptr<Callback>> m_callbacks;
72 IloCplex::ControlCallbackI* m_caller =
nullptr;
74 std::unique_ptr<Optimizers::impl::CplexUserCutCallbackI> m_user_cut_callback;
75 std::unique_ptr<Optimizers::impl::CplexLazyConstraintCallbackI> m_lazy_constraint_callback;
77 std::unique_ptr<Optimizers::impl::CplexBranchCallbackI> m_branch_callback;
79 Optimizers::impl::CplexUserCutCallbackI* create_user_cut_callback();
80 Optimizers::impl::CplexLazyConstraintCallbackI* create_lazy_constraint_callback();
81 Optimizers::impl::CplexHeuristicCallbackI* create_heuristic_callback();
82 Optimizers::impl::CplexBranchCallbackI* create_branch_callback();
84 std::optional<IloCplex::MIPCallbackI::NodeId> m_local_bounds_node_id;
85 std::vector<std::pair<Var, double>> m_local_lbs;
86 std::vector<std::pair<Var, double>> m_local_ubs;
88 IloRange cplex_temp_constr(
const TempCtr &t_temp_ctr);
90 explicit CplexCallbackI(Optimizers::Cplex &t_parent);
92 void add_callback(Callback *t_callback);
94 void call(IloCplex::ControlCallbackI* t_caller, CallbackEvent t_event);
96 [[nodiscard]]
const Model &original_model()
const override;
98 void add_lazy_cut(
const TempCtr &t_lazy_cut)
override;
100 void add_user_cut(
const TempCtr &t_user_cut)
override;
102 void submit_heuristic_solution(PrimalPoint t_solution)
override {}
104 [[nodiscard]] PrimalPoint primal_solution()
const override;
106 [[nodiscard]]
const Timer &time()
const override;
108 [[nodiscard]]
double best_obj()
const override;
110 [[nodiscard]]
double best_bound()
const override;
112 double get_var_primal(
const Var& t_var)
const;
113 double get_var_local_lb(
const Var& t_var)
const;
114 double get_var_local_ub(
const Var& t_var)
const;
115 void set_var_local_lb(
const Var& t_var,
double t_bound);
116 void set_var_local_ub(
const Var& t_var,
double t_bound);
118 void terminate()
override;
120 friend class idol::Optimizers::Cplex;
121 friend class idol::Optimizers::impl::CplexBranchCallbackI;