idol
A C++ Framework for Optimization
Loading...
Searching...
No Matches
Formulation.h
1//
2// Created by henri on 18.09.24.
3//
4
5#ifndef IDOL_ADM_FORMULATION_H
6#define IDOL_ADM_FORMULATION_H
7
8#include <utility>
9
10#include "idol/mixed-integer/modeling/models/Model.h"
11#include "idol/general/utils/Map.h"
12#include "idol/general/utils/GenerationPattern.h"
13#include <variant>
14
15namespace idol {
16 class PenaltyUpdate;
17 namespace ADM {
18 class Formulation;
19 }
20}
21
23public:
24 Formulation(const Model& t_src_model,
25 Annotation<unsigned int> t_decomposition,
26 std::optional<Annotation<double>> t_penalized_constraints,
27 double t_rescaling_threshold);
28
29 struct SubProblem {
30
31 struct RhsFixation {
32 Ctr ctr;
33 QuadExpr<Var> rhs_pattern;
34
35 RhsFixation(Ctr t_sub_problem_ctr, QuadExpr<Var> t_pattern)
36 : ctr(std::move(t_sub_problem_ctr)), rhs_pattern(std::move(t_pattern)) {}
37 };
38
39 struct RowFixation {
40 std::variant<Ctr, QCtr> ctr;
42
43 RowFixation(const Ctr& t_sub_problem_ctr, QuadExpr<Var, QuadExpr<Var>> t_row) : ctr(t_sub_problem_ctr), row(std::move(t_row)) {}
44 RowFixation(const QCtr& t_sub_problem_ctr, QuadExpr<Var, QuadExpr<Var>> t_row) : ctr(t_sub_problem_ctr), row(std::move(t_row)) {}
45 };
46
47 Model model;
48 std::list<Var> l1_epigraph_vars;
49 std::list<RhsFixation> rhs_fixations;
50 std::list<RowFixation> row_fixations;
51 QuadExpr<Var, QuadExpr<Var>> obj_fixation;
52
53 explicit SubProblem(Env& t_env);
54 };
55
56 SubProblem& sub_problem(const Var& t_var);
57
58 [[nodiscard]] const SubProblem& sub_problem(const Var& t_var) const;
59
60 [[nodiscard]] unsigned int sub_problem_id(const Var& t_var) const;
61
62 SubProblem& sub_problem(unsigned int t_sub_problem_id) { return m_sub_problems[t_sub_problem_id]; }
63
64 [[nodiscard]] const SubProblem& sub_problem(unsigned int t_sub_problem_id) const { return m_sub_problems[t_sub_problem_id]; }
65
66 [[nodiscard]] unsigned int n_sub_problems() const { return m_sub_problems.size(); }
67
68 auto sub_problems() { return IteratorForward(m_sub_problems); }
69
70 [[nodiscard]] auto sub_problems() const { return ConstIteratorForward(m_sub_problems); }
71
72 [[nodiscard]] auto l1_epigraph_vars() const { return ConstIteratorForward(m_l1_epigraph_vars); }
73
74 [[nodiscard]] bool has_penalized_constraints() const { return m_initial_penalty_parameters.has_value(); }
75
76 void initialize_penalty_parameters(bool t_use_inverse_penalties);
77
78 bool update_penalty_parameters(const std::vector<PrimalPoint>& t_primals, PenaltyUpdate& t_penalty_update); // Returns true if penalty parameters have been resacled
79
80 void update(unsigned int t_sub_problem_id, const std::vector<PrimalPoint>& t_primals);
81
83 const Var variable;
84 const double max_violation;
85 double penalty;
86 CurrentPenalty(Var t_variable, double t_max_violation, double t_penalty)
87 : variable(std::move(t_variable)), max_violation(t_max_violation), penalty(t_penalty) {}
88 };
89
90private:
91 Annotation<unsigned int> m_decomposition;
92 std::optional<Annotation<double>> m_initial_penalty_parameters;
93 double m_rescaling_threshold;
94
95 std::vector<SubProblem> m_sub_problems;
96 Map<unsigned int, Var> m_l1_epigraph_vars; // object id -> l1 epigraph variable
97
98 [[nodiscard]] unsigned int compute_n_sub_problems(const Model& t_src_model) const;
99 void initialize_sub_problems(const Model& t_src_model, unsigned int n_sub_problems);
100 void dispatch_vars(const Model& t_src_model);
101 void dispatch_ctrs(const Model& t_src_model);
102 void dispatch_ctr(const Model& t_src_model, const Ctr& t_ctr, unsigned int t_sub_problem_id);
103 void dispatch_qctrs(const Model& t_src_model);
104 void dispatch_qctr(const Model& t_src_model, const QCtr& t_ctr, unsigned int t_sub_problem_id);
105 void dispatch_obj(const Model& t_src_model);
106 void dispatch_obj(const Model& t_src_model, unsigned int t_sub_problem_id);
107 double evaluate(const QuadExpr<Var>& t_expr, const std::vector<Point<Var>>& t_primals);
108 QuadExpr<Var> evaluate(const QuadExpr<Var, QuadExpr<Var>>& t_expr, const std::vector<Point<Var>>& t_primals);
109 std::pair<LinExpr<Var>, AffExpr<Var>> dispatch(const LinExpr<Var>& t_expr, unsigned int t_sub_problem_id);
110 void set_penalty_in_all_sub_problems(const Var& t_var, double t_value);
111 bool rescale_penalty_parameters(std::list<CurrentPenalty>& t_penalties);
112 LinExpr<Var> add_l1_vars(const Ctr& t_ctr, CtrType t_type, unsigned int t_sub_problem_id);
113 LinExpr<Var> add_l1_vars(const QCtr& t_ctr, CtrType t_type, unsigned int t_sub_problem_id);
114 LinExpr<Var> add_l1_vars(unsigned int t_ctr_id, double t_initial_penalty_parameter, CtrType t_type, unsigned int t_sub_problem_id);
115};
116
117
118#endif //IDOL_ADM_FORMULATION_H