A C++ Framework for Optimization
Loading...
Searching...
No Matches
Optimizers_Gurobi.h
1//
2// Created by henri on 31/01/23.
3//
4
5#ifndef IDOL_OPTIMIZERS_GUROBI_H
6#define IDOL_OPTIMIZERS_GUROBI_H
7
8#include <memory>
9
10#include "idol/general/optimizers/OptimizerWithLazyUpdates.h"
11#include "idol/mixed-integer/optimizers/callbacks/Callback.h"
12#include "GurobiCallbackI.h"
13
14namespace idol::Optimizers {
15 class Gurobi;
16}
17
18namespace idol::impl {
19 static int grb_callback(GRBmodel *t_model, void *t_cbdata, int t_where, void *t_usrdata);
20}
21
22class idol::Optimizers::Gurobi : public OptimizerWithLazyUpdates<int, int, int, int> {
23 friend class ::idol::GurobiCallbackI;
24
25 class DynamicLib;
26 static std::unique_ptr<DynamicLib> m_dynamic_lib;
27
28 GRBenv* m_env = nullptr;
29 GRBmodel* m_model = nullptr;
30 bool m_continuous_relaxation;
31
32 std::unique_ptr<GurobiCallbackI> m_gurobi_callback;
33
34 char gurobi_var_type(int t_type);
35 static char gurobi_ctr_type(int t_type);
36 static char gurobi_obj_sense(int t_sense);
37 static double gurobi_numeric(double t_value);
38 static VarType idol_var_type(char t_type);
39 static CtrType idol_ctr_type(char t_type);
40 static ObjectiveSense idol_obj_sense(int t_sense);
41 [[nodiscard]] std::pair<SolutionStatus, SolutionReason> gurobi_status(int t_status) const;
42protected:
43 static DynamicLib& get_dynamic_lib();
44 static GRBenv* get_new_env();
45
46 void hook_build() override;
47 void hook_optimize() override;
48 void hook_write(const std::string &t_name) override;
49 int hook_add(const Var& t_var, bool t_add_column) override;
50 int hook_add(const Ctr& t_ctr) override;
51 int hook_add(const QCtr& t_ctr) override;
52 int 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
82public:
83 Gurobi(const Model& t_model, bool t_continuous_relaxation, GRBenv* t_env);
84 Gurobi(const Model& t_model, bool t_continuous_relaxation);
85 ~Gurobi() override;
86
87 GRBenv* env() { return m_env; }
88 [[nodiscard]] const GRBenv* env() const { return m_env; }
89 GRBmodel* model() { return m_model; }
90 [[nodiscard]] const GRBmodel* model() const { return m_model; }
91
92 [[nodiscard]] std::string name() const override { return "Gurobi"; }
93 void set_param_time_limit(double t_time_limit) override;
94 void set_param_threads(unsigned int t_thread_limit) override;
95 void set_param_best_obj_stop(double t_best_obj_stop) override;
96 void set_param_best_bound_stop(double t_best_bound_stop) override;
97 void set_param_presolve(bool t_value) override;
98 void set_param_infeasible_or_unbounded_info(bool t_value) override;
99 void add_callback(Callback* t_ptr_to_callback);
100 void set_lazy_cut(bool t_value);
101 void set_max_n_solution_in_pool(unsigned int t_value);
102 void set_param_logs(bool t_value) override;
103 void set_param(const std::string& t_param, int t_value);
104 void set_param(const std::string& t_param, double t_value);
105 void set_tol_mip_relative_gap(double t_relative_gap_tolerance) override;
106 void set_tol_mip_absolute_gap(double t_absolute_gap_tolerance) override;
107 void set_tol_feasibility(double t_tol_feasibility) override;
108 void set_tol_optimality(double t_tol_optimality) override;
109 void set_tol_integer(double t_tol_integer) override;
110
111 // static Model read_from_file(Env& t_env, const std::string& t_filename);
112};
113
114#define GUROBI_SYM_PTR(name) \
115typedef decltype(::name)* name##_t; \
116name##_t name = nullptr
117
119 void* m_handle = nullptr;
120
121 static std::string find_library();
122public:
123 GUROBI_SYM_PTR(GRBversion);
124 GUROBI_SYM_PTR(GRBloadenvinternal);
125 GUROBI_SYM_PTR(GRBgetintattr);
126 GUROBI_SYM_PTR(GRBgeterrormsg);
127 GUROBI_SYM_PTR(GRBfreeenv);
128 GUROBI_SYM_PTR(GRBgetenv);
129 GUROBI_SYM_PTR(GRBnewmodel);
130 GUROBI_SYM_PTR(GRBfreemodel);
131 GUROBI_SYM_PTR(GRBaddvar);
132 GUROBI_SYM_PTR(GRBaddconstr);
133 GUROBI_SYM_PTR(GRBaddqconstr);
134 GUROBI_SYM_PTR(GRBsetdblattrelement);
135 GUROBI_SYM_PTR(GRBsetcharattrelement);
136 GUROBI_SYM_PTR(GRBsetobjective);
137 GUROBI_SYM_PTR(GRBdelvars);
138 GUROBI_SYM_PTR(GRBdelconstrs);
139 GUROBI_SYM_PTR(GRBdelsos);
140 GUROBI_SYM_PTR(GRBoptimize);
141 GUROBI_SYM_PTR(GRBwrite);
142 GUROBI_SYM_PTR(GRBsetintattr);
143 GUROBI_SYM_PTR(GRBchgcoeffs);
144 GUROBI_SYM_PTR(GRBupdatemodel);
145 GUROBI_SYM_PTR(GRBsetintparam);
146 GUROBI_SYM_PTR(GRBsetdblparam);
147 GUROBI_SYM_PTR(GRBsetstrparam);
148 GUROBI_SYM_PTR(GRBsetcallbackfunc);
149 GUROBI_SYM_PTR(GRBgetintparam);
150 GUROBI_SYM_PTR(GRBgetdblparam);
151 GUROBI_SYM_PTR(GRBgetstrparam);
152 GUROBI_SYM_PTR(GRBgetdblattr);
153 GUROBI_SYM_PTR(GRBgetdblattrarray);
154 GUROBI_SYM_PTR(GRBgetdblattrelement);
155 GUROBI_SYM_PTR(GRBsetdblattr);
156 GUROBI_SYM_PTR(GRBdelqconstrs);
157 GUROBI_SYM_PTR(GRBaddsos);
158 GUROBI_SYM_PTR(GRBcbget);
159 GUROBI_SYM_PTR(GRBcbsolution);
160 GUROBI_SYM_PTR(GRBcbcut);
161 GUROBI_SYM_PTR(GRBcblazy);
162 GUROBI_SYM_PTR(GRBterminate);
163
164 DynamicLib();
165
166 ~DynamicLib();
167};
168
169#endif //IDOL_OPTIMIZERS_GUROBI_H