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 "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
64enum class BENDERSMETHOD
65{
66 BENDERS,
67 BENDERS_BY_BATCH,
68 BENDERS_OUTERLOOP,
69 BENDERS_BY_BATCH_OUTERLOOP
70};
71
72inline std::string bendersmethod_to_string(BENDERSMETHOD method)
73{
74 switch (method)
75 {
76 case BENDERSMETHOD::BENDERS:
77 return "Benders";
78 case BENDERSMETHOD::BENDERS_BY_BATCH:
79 return "Benders by batch";
80 case BENDERSMETHOD::BENDERS_OUTERLOOP:
81 return "Outerloop around Benders";
82 case BENDERSMETHOD::BENDERS_BY_BATCH_OUTERLOOP:
83 return "Outerloop around Benders by batch";
84 default:
85 return "Unknown";
86 }
87}
88
90{
91 bool operator()(const PointPtr& lhs, const PointPtr& rhs) const
92 {
93 return *lhs < *rhs;
94 }
95
96 bool operator()(const Point& lhs, const Point& rhs) const
97 {
98 Point::const_iterator it1(lhs.begin());
99 Point::const_iterator it2(rhs.begin());
100
101 Point::const_iterator end1(lhs.end());
102 Point::const_iterator end2(rhs.end());
103
104 while (it1 != end1 && it2 != end2)
105 {
106 if (it1->first != it2->first)
107 {
108 return it1->first < it2->first;
109 }
110 else
111 {
112 if (std::fabs(it1->second - it2->second) < EPSILON_PREDICATE)
113 {
114 ++it1;
115 ++it2;
116 }
117 else
118 {
119 return it1->second < it2->second;
120 }
121 }
122 }
123
124 if (it1 == end1 && it2 == end2)
125 {
126 return false;
127 }
128 else
129 {
130 return (it1 == end1);
131 }
132 }
133};
134
142inline std::ostream& operator<<(std::ostream& stream, const Point& rhs)
143{
144 for (const auto& kvp: rhs)
145 {
146 if (kvp.second > 0)
147 {
148 if (kvp.second == 1)
149 {
150 stream << "+";
151 stream << kvp.first;
152 }
153 else
154 {
155 stream << "+";
156 stream << kvp.second;
157 stream << kvp.first;
158 }
159 }
160 else if (kvp.second < 0)
161 {
162 stream << kvp.second;
163 stream << kvp.first;
164 }
165 }
166 return stream;
167}
168
169double norm_point(const Point& x0, const Point& x1);
170
171std::ostream& operator<<(std::ostream& stream, const std::vector<IntVector>& rhs);
172
173const std::string SUBPROBLEM_WEIGHT_CST_STR("CONSTANT");
174const std::string SUBPROBLEM_WEIGHT_UNIFORM_CST_STR("UNIFORM");
175const std::string WEIGHT_SUM_CST_STR("WEIGHT_SUM");
176const std::string MPS_SUFFIX = ".mps";
177const std::string SAVE_SUFFIX = ".svf";
178
180{
181 std::string OUTPUTROOT;
182 std::string INPUTROOT;
183 std::string STRUCTURE_FILE;
184 std::string LAST_ITERATION_JSON_FILE;
185 std::string MASTER_NAME;
186 ProblemsFormat PROBLEMS_FORMAT = ProblemsFormat::MPS_FILE;
187 std::string SOLVER_NAME;
188 std::string SLAVE_WEIGHT;
189 std::string AREA_FILE;
190
191 int LOG_LEVEL = 0;
192
193 double SLAVE_WEIGHT_VALUE = 0;
194 bool RESUME = false;
195
196 Str2Dbl weights;
197};
198
200
202{
203 bool DO_OUTER_LOOP = false;
204 std::string OUTER_LOOP_OPTION_FILE;
205};
206
208{
209 explicit BendersBaseOptions(const BaseOptions& base_to_copy):
210 BaseOptions(base_to_copy)
211 {
212 }
213
214 int MAX_ITERATIONS = -1;
215
216 double ABSOLUTE_GAP = 0;
217 double RELATIVE_GAP = 0;
218 double RELAXED_GAP = 0;
219 double TIME_LIMIT = 0;
220 double SEPARATION_PARAM = 1;
221
222 bool AGGREGATION = false;
223 bool TRACE = false;
224 bool BOUND_ALPHA = false;
225
226 MasterFormulation MASTER_FORMULATION;
227
228 std::string CSV_NAME;
229 std::string LAST_MASTER_MPS;
230 std::string LAST_MASTER_BASIS;
231
232 size_t BATCH_SIZE;
233 ExternalLoopOptions EXTERNAL_LOOP_OPTIONS;
234};
235
236void usage(int argc);
237
238Json::Value get_json_file_content(const std::filesystem::path& json_file);
Definition common.h:180
Definition common.h:208
Definition common.h:202
Definition common.h:90