idol
A C++ Framework for Optimization
Loading...
Searching...
No Matches
Optimizers_Mosek.h
1//
2// Created by henri on 20/02/23.
3//
4
5#ifndef IDOL_OPTIMIZERS_MOSEK_H
6#define IDOL_OPTIMIZERS_MOSEK_H
7
8#ifdef IDOL_USE_MOSEK
9
10#include "idol/general/optimizers/OptimizerWithLazyUpdates.h"
11#include "MosekCallbackI.h"
12#include <fusion.h>
13
14namespace idol {
15
16 struct MosekKiller {
17 ~MosekKiller();
18 };
19
20 static const MosekKiller s_mosek_killer;
21
22 struct MosekVar {
23 mosek::fusion::Variable::t variable;
24 mosek::fusion::Constraint::t lower_bound;
25 mosek::fusion::Constraint::t upper_bound;
26 };
27
28 struct MosekCtr {
29 mosek::fusion::Constraint::t constraint;
30 };
31
32 namespace Optimizers {
33 class Mosek;
34 }
35}
36
37class idol::Optimizers::Mosek : public OptimizerWithLazyUpdates<MosekVar, MosekCtr, MosekCtr, bool> {
38 bool m_continuous_relaxation;
39
40 mosek::fusion::Model::t m_model;
41 SolutionStatus m_solution_status = Loaded;
42 SolutionReason m_solution_reason = NotSpecified;
43
44 std::unique_ptr<MosekCallbackI> m_mosek_callback;
45protected:
46 void set_var_attr(MosekVar& t_mosek_var, int t_type, double t_lb, double t_ub, double t_obj);
47 void set_var_lb(MosekVar& t_mosek_var, double t_bound);
48 void set_var_ub(MosekVar& t_mosek_var, double t_bound);
49 [[nodiscard]] mosek::fusion::Expression::t to_mosek_expression(const LinExpr<Var>& t_expr) const;
50 [[nodiscard]] mosek::fusion::Expression::t to_mosek_expression(const AffExpr<Var>& t_expr) const;
51
52 void hook_build() override;
53 void hook_optimize() override;
54 void hook_write(const std::string &t_name) override;
55 MosekVar hook_add(const Var &t_var, bool t_add_column) override;
56 MosekCtr hook_add(const Ctr &t_ctr) override;
57 MosekCtr hook_add(const QCtr &t_ctr) override;
58 bool hook_add(const SOSCtr &t_ctr) override;
59 void hook_update_objective_sense() override;
60 void hook_update_matrix(const Ctr &t_ctr, const Var &t_var, double t_constant) override;
61 void hook_update() override;
62 void hook_update(const Var &t_var) override;
63 void hook_update(const Ctr &t_ctr) override;
64 void hook_update_objective() override;
65 void hook_update_rhs() override;
66 void hook_remove(const Var &t_var) override;
67 void hook_remove(const Ctr &t_ctr) override;
68 void hook_remove(const QCtr &t_ctr) override;
69 void hook_remove(const SOSCtr &t_ctr) override;
70
71 [[nodiscard]] SolutionStatus get_status() const override;
72 [[nodiscard]] SolutionReason get_reason() const override;
73 [[nodiscard]] double get_best_obj() const override;
74 [[nodiscard]] double get_best_bound() const override;
75 [[nodiscard]] double get_var_primal(const Var &t_var) const override;
76 [[nodiscard]] double get_var_reduced_cost(const Var &t_var) const override;
77 [[nodiscard]] double get_var_ray(const Var &t_var) const override;
78 [[nodiscard]] double get_ctr_dual(const Ctr &t_ctr) const override;
79 [[nodiscard]] double get_ctr_farkas(const Ctr &t_ctr) const override;
80 [[nodiscard]] double get_relative_gap() const override;
81 [[nodiscard]] double get_absolute_gap() const override;
82 [[nodiscard]] unsigned int get_n_solutions() const override;
83 [[nodiscard]] unsigned int get_solution_index() const override;
84 void set_solution_index(unsigned int t_index) override;
85public:
86 explicit Mosek(const Model& t_model, bool t_continuous_relaxation);
87
88 ~Mosek() override;
89
90 [[nodiscard]] std::string name() const override { return "Mosek"; }
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 set_parameter(const std::string& t_param, double t_value);
105
106 void set_parameter(const std::string& t_param, int t_value);
107
108 void set_parameter(const std::string& t_param, const std::string& t_value);
109
110 void set_param_logs(bool t_value) override;
111
112 void add_callback(Callback* t_ptr_to_callback);
113};
114
115#endif
116
117#endif //IDOL_OPTIMIZERS_MOSEK_H