Loading...
Searching...
No Matches
Env.h
1//
2// Created by henri on 27/01/23.
3//
4
5#ifndef IDOL_ENV_H
6#define IDOL_ENV_H
7
8#include <list>
9#include <thread>
10
11#include "idol/mixed-integer/modeling/variables/Var.h"
12#include "idol/mixed-integer/modeling/variables/VarVersion.h"
13#include "idol/mixed-integer/modeling/variables/TempVar.h"
14#include "Versions.h"
15#include "ObjectId.h"
16#include "idol/mixed-integer/modeling/annotations/impl_Annotation.h"
17#include "idol/mixed-integer/modeling/constraints/QCtrVersion.h"
18#include "idol/mixed-integer/modeling/constraints/QCtr.h"
19#include "idol/mixed-integer/modeling/constraints/TempQCtr.h"
20#include "idol/mixed-integer/modeling/constraints/SOSCtrVersion.h"
21#include "idol/mixed-integer/modeling/constraints/SOSCtr.h"
22
23namespace idol {
24
25 class Model;
26
27 namespace impl {
28 class Env;
29
30 template<unsigned int Start>
31 class IdProvider;
32 }
33
34}
35
36template<unsigned int Start>
37class idol::impl::IdProvider {
38 unsigned int m_max_id = Start;
39 std::list<unsigned int> m_free_ids;
40public:
41 void free(unsigned int t_id) { m_free_ids.emplace_back(t_id); }
42
43 unsigned int create() {
44 if (m_free_ids.empty()) {
45 return m_max_id++;
46 }
47
48 auto result = m_free_ids.back();
49 m_free_ids.pop_back();
50 return result;
51 }
52};
53
54class idol::impl::Env {
55 // Tolerances
56 double m_tol_mip_relative_gap = 1e-4;
57 double m_tol_mip_absolute_gap = 1e-5;
58 double m_tol_integer = 1e-5;
59 double m_tol_feasibility = 1e-6;
60 double m_tol_optimality = 1e-6;
61
62 // Parameters
63 bool m_param_logs = false;
64 bool m_param_presolve = true;
65 double m_param_time_limit = std::numeric_limits<double>::max();
66 unsigned int m_param_thread_limit;
67 double m_param_best_bound_stop = Inf;
68 double m_param_best_obj_stop = -Inf;
69 unsigned int m_param_iteration_limit = std::numeric_limits<unsigned int>::max();
70 bool m_param_infeasible_or_unbounded_info = false;
71
72 // Objects
73 unsigned int m_max_object_id = 0;
74
75 IdProvider<1> m_model_ids;
76 IdProvider<0> m_annotation_ids;
77
78 std::list<Versions<VarVersion>> m_variables;
79 std::list<Versions<CtrVersion>> m_constraints;
80 std::list<Versions<QCtrVersion>> m_qconstraints;
81 std::list<Versions<SOSCtrVersion>> m_sosconstraints;
82
83 template<class T, class VersionT, class ...ArgsT>
84 ObjectId<VersionT> create(std::string t_name, const std::string& t_default_name, std::list<Versions<VersionT>>& t_container, ArgsT&& ...t_args) {
85 const unsigned int id = m_max_object_id++;
86 std::string name = t_name.empty() ? t_default_name + '_' + std::to_string(id) : std::move(t_name);
87 t_container.emplace_front(-1, std::forward<ArgsT>(t_args)...);
88 return { t_container.begin(), id, std::move(name) };
89 }
90protected:
91 unsigned int create_model_id() { return m_model_ids.create(); }
92
93 void free_model_id(const ::idol::Model& t_model);
94
95 unsigned int create_annotation_id() { return m_annotation_ids.create(); }
96
97 void free_annotation_id(const impl::Annotation& t_annotation);
98
99 template<class T, class ...ArgsT>
100 void create_version(const Model& t_model, const T& t_object, unsigned int t_index, ArgsT&& ...t_args) {
101 t_object.create_version(t_model, t_index, std::forward<ArgsT&&>(t_args)...);
102 }
103
104 template<class T>
105 void remove_version(const Model& t_model, const T& t_object) {
106 t_object.remove_version(t_model);
107 }
108
109 template<class T>
110 bool has_version(const Model& t_model, const T& t_object) const {
111 return t_object.is_in(t_model);
112 }
113
114 template<class T>
115 const auto& version(const Model& t_model, const T& t_object) const {
116 return t_object.versions().get(t_model);
117 }
118
119 template<class T>
120 auto& version(const Model& t_model, const T& t_object) {
121 return const_cast<T&>(t_object).versions().get(t_model);
122 }
123
124 ObjectId<VarVersion> create_var(std::string t_name, TempVar&& t_temp_var) {
125 return create<Var>(std::move(t_name), "x", m_variables, std::move(t_temp_var));
126 }
127
128 ObjectId<CtrVersion> create_ctr(std::string t_name, TempCtr&& t_temp_ctr) {
129 return create<Ctr>(std::move(t_name), "c", m_constraints, std::move(t_temp_ctr));
130 }
131
132 ObjectId<QCtrVersion> create_qctr(std::string t_name, TempQCtr&& t_temp_ctr) {
133 return create<QCtr>(std::move(t_name), "c", m_qconstraints, std::move(t_temp_ctr));
134 }
135
136 ObjectId<SOSCtrVersion> create_sosctr(std::string t_name, bool t_is_sos1, std::vector<Var>&& t_vars, std::vector<double>&& t_weights) {
137 return create<SOSCtr>(std::move(t_name), "sos", m_sosconstraints, t_is_sos1, std::move(t_vars), std::move(t_weights));
138 }
139
140public:
141 Env() : m_param_thread_limit(std::max<unsigned int>(std::thread::hardware_concurrency(), 1)) {}
142
143 Env(const Env&) = delete;
144 Env(Env&&) noexcept = delete;
145
146 Env& operator=(const Env&) = delete;
147 Env& operator=(Env&&) noexcept = delete;
148
155 template<class T>
156 const auto& operator[](const T& t_object) const {
157 return t_object.versions().get_default();
158 }
159
160 // Tolerances
161 [[nodiscard]] double get_tol_mip_relative_gap() const { return m_tol_mip_relative_gap; }
162 [[nodiscard]] double get_tol_mip_absolute_gap() const { return m_tol_mip_absolute_gap; }
163 [[nodiscard]] double get_tol_integer() const { return m_tol_integer; }
164 [[nodiscard]] double get_tol_feasibility() const { return m_tol_feasibility; }
165 [[nodiscard]] double get_tol_optimality() const { return m_tol_optimality; }
166
167 // Parameters
168 [[nodiscard]] bool get_param_logs() const { return m_param_logs; }
169 [[nodiscard]] bool get_param_presolve() const { return m_param_presolve; }
170 [[nodiscard]] double get_param_time_limit() const { return m_param_time_limit; }
171 [[nodiscard]] unsigned int get_param_thread_limit() const { return m_param_thread_limit; }
172 [[nodiscard]] double get_param_best_bound_stop() const { return m_param_best_bound_stop; }
173 [[nodiscard]] double get_param_best_obj_stop() const { return m_param_best_obj_stop; }
174 [[nodiscard]] unsigned int get_param_iteration_limit() const { return m_param_iteration_limit; }
175 [[nodiscard]] bool get_param_infeasible_or_unbounded_info() const { return m_param_infeasible_or_unbounded_info; }
176
177};
178
200class idol::Env : public impl::Env {
201public:
207 Env() = default;
208
209 friend class Model;
210 friend class Var;
211 friend class Ctr;
212 friend class QCtr;
213 friend class SOSCtr;
214 friend class impl::Annotation;
215};
216
217
218#endif //IDOL_ENV_H
Env()=default