idol
A C++ Framework for Optimization
Loading...
Searching...
No Matches
Optimizers_GLPK.h
1//
2// Created by henri on 14/02/23.
3//
4
5#ifndef IDOL_OPTIMIZERS_GLPK_H
6#define IDOL_OPTIMIZERS_GLPK_H
7
8#include <stack>
9
10#ifdef IDOL_USE_GLPK
11
12#include "idol/general/optimizers/OptimizerWithLazyUpdates.h"
13#include <glpk.h>
14
15namespace idol::Optimizers {
16 class GLPK;
17}
18
19class idol::Optimizers::GLPK : public OptimizerWithLazyUpdates<int, int, int, int> {
20
21 bool m_continuous_relaxation;
22
23 glp_prob* m_model = nullptr;
24 glp_smcp m_simplex_parameters;
25 glp_iocp m_mip_parameters;
26 bool m_solved_as_mip = false;
27 bool m_rebuild_basis = false;
28
29 SolutionStatus m_solution_status = Loaded;
30 SolutionReason m_solution_reason = NotSpecified;
31 std::optional<PrimalPoint> m_unbounded_ray;
32 std::optional<DualPoint> m_farkas_certificate;
33
34 std::stack<int> m_deleted_variables;
35 std::stack<int> m_deleted_constraints;
36protected:
37 void hook_build() override;
38
39 void hook_optimize() override;
40
41 void hook_write(const std::string &t_name) override;
42
43 int hook_add(const Var &t_var, bool t_add_column) override;
44
45 int hook_add(const Ctr &t_ctr) override;
46
47 int hook_add(const QCtr &t_ctr) override;
48
49 int hook_add(const SOSCtr &t_ctr) override;
50
51 void hook_update_objective_sense() override;
52
53 void hook_update_matrix(const Ctr &t_ctr, const Var &t_var, double t_constant) override;
54
55 void hook_update() override;
56
57 void hook_update(const Var &t_var) override;
58
59 void hook_update(const Ctr &t_ctr) override;
60
61 void hook_update_objective() override;
62
63 void hook_update_rhs() override;
64
65 void hook_remove(const Var &t_var) override;
66
67 void hook_remove(const Ctr &t_ctr) override;
68
69 void hook_remove(const QCtr &t_ctr) override;
70
71 void hook_remove(const SOSCtr &t_ctr) override;
72
73 void set_var_attr(int t_index, int t_type, double t_lb, double t_ub, double t_obj);
74
75 void set_ctr_attr(int t_index, int t_type, double t_rhs);
76
77 void save_simplex_solution_status();
78 void compute_farkas_certificate();
79 void compute_unbounded_ray();
80 void save_milp_solution_status();
81
82 [[nodiscard]] SolutionStatus get_status() const override;
83 [[nodiscard]] SolutionReason get_reason() const override;
84 [[nodiscard]] double get_best_obj() const override;
85 [[nodiscard]] double get_best_bound() const override;
86 [[nodiscard]] double get_var_primal(const Var &t_var) const override;
87 [[nodiscard]] double get_var_reduced_cost(const Var &t_var) const override;
88 [[nodiscard]] double get_var_ray(const Var &t_var) const override;
89 [[nodiscard]] double get_ctr_dual(const Ctr &t_ctr) const override;
90 [[nodiscard]] double get_ctr_farkas(const Ctr &t_ctr) const override;
91 [[nodiscard]] double get_relative_gap() const override;
92 [[nodiscard]] double get_absolute_gap() const override;
93 [[nodiscard]] unsigned int get_n_solutions() const override;
94 [[nodiscard]] unsigned int get_solution_index() const override;
95 void set_solution_index(unsigned int t_index) override;
96
97 static Model read_from_glpk(idol::Env& t_env, glp_prob* t_model);
98 static Model read_from_lp_file(Env& t_env, const std::string& t_filename);
99 static Model read_from_mps_file(Env& t_env, const std::string& t_filename);
100public:
101 explicit GLPK(const Model& t_model, bool t_continuous_relaxation);
102
103 [[nodiscard]] std::string name() const override { return "GLPK"; }
104
105 void set_param_time_limit(double t_time_limit) override;
106
107 void set_param_best_obj_stop(double t_best_obj_stop) override;
108
109 void set_param_best_bound_stop(double t_best_bound_stop) override;
110
111 void set_param_presolve(bool t_value) override;
112
113 void set_param_logs(bool t_value) override;
114
115 static Model read_from_file(Env& t_env, const std::string& t_filename);
116};
117
118#endif
119
120#endif //IDOL_OPTIMIZERS_GLPK_H