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>;
56
57using SlaveCutId = std::map<std::string, IntVector>;
58using ActiveCut = std::tuple<int, std::string, int, bool>;
59using ActiveCutStorage = std::vector<ActiveCut>;
60
61using mps_coupling = std::pair<std::string, std::string>;
62using mps_coupling_list = std::list<mps_coupling>;
63
64using SubProblemNamesInCut = std::vector<std::pair<std::string, int>>;
65
67{
68 bool operator()(const PointPtr& lhs, const PointPtr& rhs) const
69 {
70 return *lhs < *rhs;
71 }
72
73 bool operator()(const Point& lhs, const Point& rhs) const
74 {
75 Point::const_iterator it1(lhs.begin());
76 Point::const_iterator it2(rhs.begin());
77
78 Point::const_iterator end1(lhs.end());
79 Point::const_iterator end2(rhs.end());
80
81 while (it1 != end1 && it2 != end2)
82 {
83 if (it1->first != it2->first)
84 {
85 return it1->first < it2->first;
86 }
87 else
88 {
89 if (std::fabs(it1->second - it2->second) < EPSILON_PREDICATE)
90 {
91 ++it1;
92 ++it2;
93 }
94 else
95 {
96 return it1->second < it2->second;
97 }
98 }
99 }
100
101 if (it1 == end1 && it2 == end2)
102 {
103 return false;
104 }
105 else
106 {
107 return (it1 == end1);
108 }
109 }
110};
111
119inline std::ostream& operator<<(std::ostream& stream, const Point& rhs)
120{
121 for (const auto& kvp: rhs)
122 {
123 if (kvp.second > 0)
124 {
125 if (kvp.second == 1)
126 {
127 stream << "+";
128 stream << kvp.first;
129 }
130 else
131 {
132 stream << "+";
133 stream << kvp.second;
134 stream << kvp.first;
135 }
136 }
137 else if (kvp.second < 0)
138 {
139 stream << kvp.second;
140 stream << kvp.first;
141 }
142 }
143 return stream;
144}
145
146double norm_point(const Point& x0, const Point& x1);
147
148std::ostream& operator<<(std::ostream& stream, const std::vector<IntVector>& rhs);
149
150const std::string SUBPROBLEM_WEIGHT_CST_STR("CONSTANT");
151const std::string SUBPROBLEM_WEIGHT_UNIFORM_CST_STR("UNIFORM");
152const std::string WEIGHT_SUM_CST_STR("WEIGHT_SUM");
153const std::string MPS_SUFFIX = ".mps";
154const std::string SAVE_SUFFIX = ".svf";
155
157{
158 int LOG_LEVEL = 0;
159
160 std::string INPUTROOT;
161 std::string OUTPUTROOT;
162 std::string STRUCTURE_FILE;
163 std::string MASTER_NAME;
164 std::string SOLVER_NAME;
165
166 ProblemsFormat PROBLEMS_FORMAT = ProblemsFormat::MPS_FILE;
167};
168
170{
171 PresolveOptions() = default;
172
173 explicit PresolveOptions(const BaseOptions& other):
174 BaseOptions(other)
175 {
176 }
177
178 bool KEEP_FULL;
179 std::string FULL_DIR;
180};
181
183{
184 SolverBaseOptions() = default;
185
186 explicit SolverBaseOptions(const BaseOptions& other):
187 BaseOptions(other)
188 {
189 }
190
191 std::string SLAVE_WEIGHT;
192 double SLAVE_WEIGHT_VALUE = 0;
193 Str2Dbl weights;
194};
195
197
199{
200 bool DO_OUTER_LOOP = false;
201 std::string OUTER_LOOP_OPTION_FILE;
202};
203
205{
206 explicit BendersBaseOptions(const SolverBaseOptions& other):
207 SolverBaseOptions(other)
208 {
209 }
210
211 int MAX_ITERATIONS = -1;
212
213 double ABSOLUTE_GAP = 0;
214 double RELATIVE_GAP = 0;
215 double RELAXED_GAP = 0;
216 double TIME_LIMIT = 0;
217 double SEPARATION_PARAM = 1;
218 double MASTER_SOLUTION_TOLERANCE = 1e-4;
219 double CUT_COEFFICIENT_TOLERANCE = 5e-3;
220
221 bool RESUME = false;
222 int NB_CUTS_PER_ITER = 0;
223 bool TRACE = false;
224 bool BOUND_ALPHA = false;
225 bool CACHE_PROBLEMS = false;
226
227 MasterFormulation MASTER_FORMULATION;
228
229 std::string AREA_FILE;
230 std::string CSV_NAME;
231 std::string LAST_MASTER_MPS;
232 std::string LAST_MASTER_BASIS;
233 std::string LAST_ITERATION_JSON_FILE;
234
235 size_t BATCH_SIZE;
236
237 ExternalLoopOptions EXTERNAL_LOOP_OPTIONS;
238};
239
240void usage(int argc);
241
242Json::Value get_json_file_content(const std::filesystem::path& json_file);
243
244bool mkdir(const std::filesystem::path& path_to_folder);
245
246template<typename T>
247concept OStreamable = requires(std::ostream& os, T obj) {
248 { os << obj } -> std::same_as<std::ostream&>;
249};
250
251template<typename T>
252concept OStreamableIntegral = OStreamable<T> && std::integral<T>;
253
254template<OStreamable MPSPath, OStreamable CandidateName, OStreamableIntegral ColId>
255void export_structure_file(const std::filesystem::path& output_path,
256 const std::map<MPSPath, std::map<CandidateName, ColId>>& structure)
257{
258 std::ofstream structure_file{output_path};
259 for (const auto& [mps_file_path, candidates_name_and_colId]: structure)
260 {
261 for (const auto& [candidate_name, colId]: candidates_name_and_colId)
262 {
263 // Adding the space to make sure there is a demarkation even when the names might exceed
264 // 50 characters
265 structure_file << std::setw(50) << mps_file_path;
266 structure_file << " " << std::setw(50) << candidate_name;
267 structure_file << " " << std::setw(10) << colId;
268 structure_file << std::endl;
269 }
270 }
271 structure_file.close();
272}
Definition common.h:252
Definition common.h:247
Definition common.h:157
Definition common.h:205
Definition common.h:199
Definition common.h:67
Definition common.h:170
Definition common.h:183