Antares Xpansion
Investment simulations for Antares studies
Loading...
Searching...
No Matches
common.h
1#pragma once
2
3#ifdef _MSC_VER
4#pragma warning(disable : 4267) // implicit conversion, possible loss of data
5#endif
6
7#include <algorithm>
8#include <cmath>
9#include <filesystem>
10#include <fstream>
11#include <iomanip>
12#include <iostream>
13#include <json/reader.h>
14#include <limits>
15#include <list>
16#include <map>
17#include <memory>
18#include <set>
19#include <sstream>
20#include <string>
21#include <thread>
22#include <tuple>
23#include <vector>
24
25#include "antares-xpansion/core/ProblemFormat.h"
26
27enum class MasterFormulation
28{
29 INTEGER,
30 RELAXED
31};
32
33enum class SOLVER
34{
35 BENDERS,
36 OUTER_LOOP,
37 MERGE_MPS
38};
39
40struct Predicate;
41typedef std::map<std::string, double> Point;
42
43typedef std::shared_ptr<Point> PointPtr;
44
45const double EPSILON_PREDICATE = 1e-8;
46
47using problem_names = std::set<std::string>;
48using VariableMap = std::map<std::string, int>;
49using Int2Str = std::map<int, std::string>;
50using Str2Dbl = std::map<std::string, double>;
51using IntVector = std::vector<int>;
52using CharVector = std::vector<char>;
53using DblVector = std::vector<double>;
54using StrVector = std::vector<std::string>;
55using CouplingMap = std::map<std::string, VariableMap>;
56using SubProblemConstraintMap = std::map<std::string, std::string>;
57
58using SlaveCutId = std::map<std::string, IntVector>;
59using ActiveCut = std::tuple<int, std::string, int, bool>;
60using ActiveCutStorage = std::vector<ActiveCut>;
61
62using mps_coupling = std::pair<std::string, std::string>;
63using mps_coupling_list = std::list<mps_coupling>;
64
65using SubProblemNamesInCut = std::vector<std::pair<std::string, int>>;
66using AddedConstraints = std::map<std::string, std::vector<std::string>>;
67
69{
70 bool operator()(const PointPtr& lhs, const PointPtr& rhs) const
71 {
72 return *lhs < *rhs;
73 }
74
75 bool operator()(const Point& lhs, const Point& rhs) const
76 {
77 Point::const_iterator it1(lhs.begin());
78 Point::const_iterator it2(rhs.begin());
79
80 Point::const_iterator end1(lhs.end());
81 Point::const_iterator end2(rhs.end());
82
83 while (it1 != end1 && it2 != end2)
84 {
85 if (it1->first != it2->first)
86 {
87 return it1->first < it2->first;
88 }
89 else
90 {
91 if (std::fabs(it1->second - it2->second) < EPSILON_PREDICATE)
92 {
93 ++it1;
94 ++it2;
95 }
96 else
97 {
98 return it1->second < it2->second;
99 }
100 }
101 }
102
103 if (it1 == end1 && it2 == end2)
104 {
105 return false;
106 }
107 else
108 {
109 return (it1 == end1);
110 }
111 }
112};
113
121inline std::ostream& operator<<(std::ostream& stream, const Point& rhs)
122{
123 for (const auto& kvp: rhs)
124 {
125 if (kvp.second > 0)
126 {
127 if (kvp.second == 1)
128 {
129 stream << "+";
130 stream << kvp.first;
131 }
132 else
133 {
134 stream << "+";
135 stream << kvp.second;
136 stream << kvp.first;
137 }
138 }
139 else if (kvp.second < 0)
140 {
141 stream << kvp.second;
142 stream << kvp.first;
143 }
144 }
145 return stream;
146}
147
148double norm_point(const Point& x0, const Point& x1);
149
150std::ostream& operator<<(std::ostream& stream, const std::vector<IntVector>& rhs);
151
152const std::string SUBPROBLEM_WEIGHT_CST_STR("CONSTANT");
153const std::string SUBPROBLEM_WEIGHT_UNIFORM_CST_STR("UNIFORM");
154const std::string WEIGHT_SUM_CST_STR("WEIGHT_SUM");
155const std::string MPS_SUFFIX = ".mps";
156const std::string SAVE_SUFFIX = ".svf";
157
159{
160 int LOG_LEVEL = 0;
161
162 std::string INPUTROOT;
163 std::string OUTPUTROOT;
164 std::string STRUCTURE_FILE;
165 std::string MASTER_NAME;
166 std::string SOLVER_NAME;
167
168 ProblemsFormat PROBLEMS_FORMAT = ProblemsFormat::MPS_FILE;
169};
170
172{
173 PresolveOptions() = default;
174
175 explicit PresolveOptions(const BaseOptions& other):
176 BaseOptions(other)
177 {
178 }
179
180 bool KEEP_FULL;
181 std::string FULL_DIR;
182};
183
185{
186 SolverBaseOptions() = default;
187
188 explicit SolverBaseOptions(const BaseOptions& other):
189 BaseOptions(other)
190 {
191 }
192
193 std::string SLAVE_WEIGHT;
194 double SLAVE_WEIGHT_VALUE = 0;
195 Str2Dbl weights;
196};
197
199
201{
202 bool DO_OUTER_LOOP = false;
203 std::string OUTER_LOOP_OPTION_FILE;
204};
205
207{
208 explicit BendersBaseOptions(const SolverBaseOptions& other):
209 SolverBaseOptions(other)
210 {
211 }
212
213 int MAX_ITERATIONS = -1;
214
215 double ABSOLUTE_GAP = 0;
216 double RELATIVE_GAP = 0;
217 double RELAXED_GAP = 0;
218 double TIME_LIMIT = 0;
219 double SEPARATION_PARAM = 1;
220 double MASTER_SOLUTION_TOLERANCE = 1e-4;
221 double CUT_COEFFICIENT_TOLERANCE = 5e-3;
222
223 bool RESUME = false;
224 bool MICRO_ITERATIONS = false;
225 int NB_CUTS_PER_ITER = 0;
226 bool TRACE = false;
227 bool BOUND_ALPHA = false;
228 bool CACHE_PROBLEMS = false;
229
230 MasterFormulation MASTER_FORMULATION;
231
232 std::string AREA_FILE;
233 std::string CSV_NAME;
234 std::string LAST_MASTER_MPS;
235 std::string LAST_MASTER_BASIS;
236 std::string LAST_ITERATION_JSON_FILE;
237
238 size_t BATCH_SIZE;
239
240 ExternalLoopOptions EXTERNAL_LOOP_OPTIONS;
241};
242
243void usage(int argc);
244
245Json::Value get_json_file_content(const std::filesystem::path& json_file);
246
247bool mkdir(const std::filesystem::path& path_to_folder);
248
249template<typename T>
250concept OStreamable = requires(std::ostream& os, T obj) {
251 { os << obj } -> std::same_as<std::ostream&>;
252};
253
254template<typename T>
255concept OStreamableIntegral = OStreamable<T> && std::integral<T>;
256
257template<OStreamable MPSPath, OStreamable CandidateName, OStreamableIntegral ColId>
258void export_structure_file(const std::filesystem::path& output_path,
259 const std::map<MPSPath, std::map<CandidateName, ColId>>& structure)
260{
261 std::ofstream structure_file{output_path};
262 for (const auto& [mps_file_path, candidates_name_and_colId]: structure)
263 {
264 for (const auto& [candidate_name, colId]: candidates_name_and_colId)
265 {
266 // Adding the space to make sure there is a demarkation even when the names might exceed
267 // 50 characters
268 structure_file << std::setw(50) << mps_file_path;
269 structure_file << " " << std::setw(50) << candidate_name;
270 structure_file << " " << std::setw(10) << colId;
271 structure_file << std::endl;
272 }
273 }
274 structure_file.close();
275}
Definition common.h:255
Definition common.h:250
Definition common.h:159
Definition common.h:207
Definition common.h:201
Definition common.h:69
Definition common.h:172
Definition common.h:185