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"
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"
30 template<
unsigned int Start>
36template<
unsigned int Start>
37class idol::impl::IdProvider {
38 unsigned int m_max_id = Start;
39 std::list<unsigned int> m_free_ids;
41 void free(
unsigned int t_id) { m_free_ids.emplace_back(t_id); }
43 unsigned int create() {
44 if (m_free_ids.empty()) {
48 auto result = m_free_ids.back();
49 m_free_ids.pop_back();
54class idol::impl::Env {
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;
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;
73 unsigned int m_max_object_id = 0;
75 IdProvider<1> m_model_ids;
76 IdProvider<0> m_annotation_ids;
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;
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) };
91 unsigned int create_model_id() {
return m_model_ids.create(); }
93 void free_model_id(const ::idol::Model& t_model);
95 unsigned int create_annotation_id() {
return m_annotation_ids.create(); }
97 void free_annotation_id(
const impl::Annotation& t_annotation);
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)...);
105 void remove_version(
const Model& t_model,
const T& t_object) {
106 t_object.remove_version(t_model);
110 bool has_version(
const Model& t_model,
const T& t_object)
const {
111 return t_object.is_in(t_model);
115 const auto& version(
const Model& t_model,
const T& t_object)
const {
116 return t_object.versions().get(t_model);
120 auto& version(
const Model& t_model,
const T& t_object) {
121 return const_cast<T&
>(t_object).versions().get(t_model);
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));
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));
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));
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));
141 Env() : m_param_thread_limit(std::max<unsigned int>(std::thread::hardware_concurrency(), 1)) {}
143 Env(
const Env&) =
delete;
144 Env(Env&&) noexcept = delete;
146 Env& operator=(const Env&) = delete;
147 Env& operator=(Env&&) noexcept = delete;
156 const auto& operator[](const T& t_object)
const {
157 return t_object.versions().get_default();
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; }
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; }
214 friend class impl::Annotation;