idol
A C++ Framework for Optimization
Loading...
Searching...
No Matches
Optimizers_Cplex.h
1//
2// Created by henri on 07.04.25.
3//
4
5#ifndef IDOL_OPTIMIZERS_CPLEX_H
6#define IDOL_OPTIMIZERS_CPLEX_H
7
8#ifdef IDOL_USE_CPLEX
9#include <ilcplex/ilocplex.h>
10#include <memory>
11#include <variant>
12
13#include "idol/general/optimizers/OptimizerWithLazyUpdates.h"
14#include "idol/mixed-integer/optimizers/callbacks/Callback.h"
15#include "CplexCallbackI.h"
16
17namespace idol::Optimizers {
18 class Cplex;
19
20 namespace impl {
21 class CplexEnvKiller;
22 }
23}
24
25struct idol::Optimizers::impl::CplexEnvKiller {
26 IloEnv env;
27 ~CplexEnvKiller() { env.end(); }
28};
29
30class idol::Optimizers::Cplex : public OptimizerWithLazyUpdates<IloNumVar, IloRange, IloRange, std::variant<IloSOS1, IloSOS2>> {
31 static std::unique_ptr<impl::CplexEnvKiller> s_global_env;
32
33 IloEnv m_env;
34 IloModel m_model;
35 IloCplex m_cplex;
36 IloObjective m_objective;
37 bool m_continuous_relaxation;
38 unsigned int m_solution_index = 0;
39 bool m_lazy_cut = false;
40
41 std::unique_ptr<CplexCallbackI> m_cplex_callback;
42
43 IloNumVar::Type cplex_var_type(int t_type);
44 static double cplex_numeric(double t_value);
45protected:
46 void hook_build() override;
47 void hook_optimize() override;
48 void hook_write(const std::string &t_name) override;
49 IloNumVar hook_add(const Var& t_var, bool t_add_column) override;
50 IloRange hook_add(const Ctr& t_ctr) override;
51 IloRange hook_add(const QCtr& t_ctr) override;
52 std::variant<IloSOS1, IloSOS2> hook_add(const SOSCtr& t_ctr) override;
53 void hook_update(const Var& t_var) override;
54 void hook_update(const Ctr& t_ctr) override;
55 void hook_update_objective_sense() override;
56 void hook_update_matrix(const Ctr &t_ctr, const Var &t_var, double t_constant) override;
57 void hook_update_objective() override;
58 void hook_update_rhs() override;
59 void hook_update() override;
60 void hook_remove(const Var& t_var) override;
61 void hook_remove(const Ctr& t_ctr) override;
62 void hook_remove(const QCtr& t_ctr) override;
63 void hook_remove(const SOSCtr& t_ctr) override;
64 void update_objective_constant();
65
66 [[nodiscard]] SolutionStatus get_status() const override;
67 [[nodiscard]] SolutionReason get_reason() const override;
68 [[nodiscard]] double get_best_obj() const override;
69 [[nodiscard]] double get_best_bound() const override;
70 [[nodiscard]] double get_var_primal(const Var &t_var) const override;
71 [[nodiscard]] double get_var_reduced_cost(const Var &t_var) const override;
72 [[nodiscard]] double get_var_ray(const Var &t_var) const override;
73 [[nodiscard]] double get_ctr_dual(const Ctr &t_ctr) const override;
74 [[nodiscard]] double get_ctr_farkas(const Ctr &t_ctr) const override;
75 [[nodiscard]] double get_relative_gap() const override;
76 [[nodiscard]] double get_absolute_gap() const override;
77 [[nodiscard]] unsigned int get_n_solutions() const override;
78 [[nodiscard]] unsigned int get_solution_index() const override;
79
80 void set_solution_index(unsigned int t_index) override;
81
82 void create_callback_if_not_exists();
83public:
84 Cplex(const Model& t_model, bool t_continuous_relaxation);
85
86 ~Cplex() override;
87
88 IloEnv& env() { return m_env; }
89
90 [[nodiscard]] const IloEnv& env() const { return m_env; }
91
92 IloModel& model() { return m_model; }
93
94 [[nodiscard]] const IloModel& model() const { return m_model; }
95
96 [[nodiscard]] std::string name() const override { return "Cplex"; }
97
98 bool lazy_cuts() const { return m_lazy_cut; }
99
100 void set_lazy_cuts(bool t_lazy_cut);
101
102 void set_param_time_limit(double t_time_limit) override;
103
104 void set_param_threads(unsigned int t_thread_limit) override;
105
106 void set_param_best_obj_stop(double t_best_obj_stop) override;
107
108 void set_param_best_bound_stop(double t_best_bound_stop) override;
109
110 void set_param_presolve(bool t_value) override;
111
112 void set_param_infeasible_or_unbounded_info(bool t_value) override;
113
114 void add_callback(Callback* t_ptr_to_callback);
115
116 void set_max_n_solution_in_pool(unsigned int t_value);
117
118 void set_param_logs(bool t_value) override;
119
120 void set_tol_mip_relative_gap(double t_relative_gap_tolerance) override;
121
122 void set_tol_mip_absolute_gap(double t_absolute_gap_tolerance) override;
123
124 void set_tol_feasibility(double t_tol_feasibility) override;
125
126 void set_tol_optimality(double t_tol_optimality) override;
127
128 void set_tol_integer(double t_tol_integer) override;
129
130 static Model read_from_file(Env& t_env, const std::string& t_filename);
131
132 CplexCallbackI& get_cplex_callback_interface();
133};
134
135#endif
136
137#endif //IDOL_OPTIMIZERS_CPLEX_H