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