idol
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#ifdef IDOL_USE_GUROBI
9#include "gurobi_c++.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 "GurobiCallbackI.h"
16
17namespace idol::Optimizers {
18 class Gurobi;
19}
20
21class idol::Optimizers::Gurobi : public OptimizerWithLazyUpdates<GRBVar, GRBConstr, GRBQConstr, GRBSOS> {
22 friend class ::idol::GurobiCallbackI;
23 static std::unique_ptr<GRBEnv> s_global_env;
24
25 static GRBEnv& get_global_env();
26
27 GRBEnv& m_env;
28 GRBModel m_model;
29 bool m_continuous_relaxation;
30
31 std::unique_ptr<GurobiCallbackI> m_gurobi_callback;
32
33 char gurobi_var_type(int t_type);
34 static char gurobi_ctr_type(int t_type);
35 static char gurobi_obj_sense(int t_sense);
36 static double gurobi_numeric(double t_value);
37 static VarType idol_var_type(char t_type);
38 static CtrType idol_ctr_type(char t_type);
39 static ObjectiveSense idol_obj_sense(int t_sense);
40 [[nodiscard]] std::pair<SolutionStatus, SolutionReason> gurobi_status(int t_status) const;
41protected:
42 void hook_build() override;
43 void hook_optimize() override;
44 void hook_write(const std::string &t_name) override;
45 GRBVar hook_add(const Var& t_var, bool t_add_column) override;
46 GRBConstr hook_add(const Ctr& t_ctr) override;
47 GRBQConstr hook_add(const QCtr& t_ctr) override;
48 GRBSOS hook_add(const SOSCtr& t_ctr) override;
49 void hook_update(const Var& t_var) override;
50 void hook_update(const Ctr& t_ctr) override;
51 void hook_update_objective_sense() override;
52 void hook_update_matrix(const Ctr &t_ctr, const Var &t_var, double t_constant) override;
53 void hook_update_objective() override;
54 void hook_update_rhs() override;
55 void hook_update() override;
56 void hook_remove(const Var& t_var) override;
57 void hook_remove(const Ctr& t_ctr) override;
58 void hook_remove(const QCtr& t_ctr) override;
59 void hook_remove(const SOSCtr& t_ctr) override;
60 void update_objective_constant();
61
62 [[nodiscard]] SolutionStatus get_status() const override;
63 [[nodiscard]] SolutionReason get_reason() const override;
64 [[nodiscard]] double get_best_obj() const override;
65 [[nodiscard]] double get_best_bound() const override;
66 [[nodiscard]] double get_var_primal(const Var &t_var) const override;
67 [[nodiscard]] double get_var_reduced_cost(const Var &t_var) const override;
68 [[nodiscard]] double get_var_ray(const Var &t_var) const override;
69 [[nodiscard]] double get_ctr_dual(const Ctr &t_ctr) const override;
70 [[nodiscard]] double get_ctr_farkas(const Ctr &t_ctr) const override;
71 [[nodiscard]] double get_relative_gap() const override;
72 [[nodiscard]] double get_absolute_gap() const override;
73 [[nodiscard]] unsigned int get_n_solutions() const override;
74 [[nodiscard]] unsigned int get_solution_index() const override;
75
76 void set_solution_index(unsigned int t_index) override;
77
78public:
79 Gurobi(const Model& t_model, bool t_continuous_relaxation, GRBEnv& t_env);
80 explicit Gurobi(const Model& t_model, bool t_continuous_relaxation) : Gurobi(t_model, t_continuous_relaxation, Gurobi::get_global_env()) {}
81
82 GRBEnv& env() { return m_env; }
83
84 [[nodiscard]] const GRBEnv& env() const { return m_env; }
85
86 GRBModel& model() { return m_model; }
87
88 [[nodiscard]] const GRBModel& model() const { return m_model; }
89
90 [[nodiscard]] std::string name() const override { return "Gurobi"; }
91
92 void set_param_time_limit(double t_time_limit) override;
93
94 void set_param_threads(unsigned int t_thread_limit) override;
95
96 void set_param_best_obj_stop(double t_best_obj_stop) override;
97
98 void set_param_best_bound_stop(double t_best_bound_stop) override;
99
100 void set_param_presolve(bool t_value) override;
101
102 void set_param_infeasible_or_unbounded_info(bool t_value) override;
103
104 void add_callback(Callback* t_ptr_to_callback);
105
106 void set_lazy_cut(bool t_value);
107
108 void set_max_n_solution_in_pool(unsigned int t_value);
109
110 void set_param_logs(bool t_value) override;
111
112 void set_param(GRB_IntParam t_param, int t_value);
113
114 void set_param(GRB_DoubleParam t_param, double t_value);
115
116 void set_tol_mip_relative_gap(double t_relative_gap_tolerance) override;
117
118 void set_tol_mip_absolute_gap(double t_absolute_gap_tolerance) override;
119
120 void set_tol_feasibility(double t_tol_feasibility) override;
121
122 void set_tol_optimality(double t_tol_optimality) override;
123
124 void set_tol_integer(double t_tol_integer) override;
125
126 static Model read_from_file(Env& t_env, const std::string& t_filename);
127};
128
129#endif
130
131#endif //IDOL_OPTIMIZERS_GUROBI_H