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 <json/reader.h>
8
9#include <algorithm>
10#include <cmath>
11#include <filesystem>
12#include <fstream>
13#include <iomanip>
14#include <iostream>
15#include <limits>
16#include <list>
17#include <map>
18#include <memory>
19#include <set>
20#include <sstream>
21#include <string>
22#include <thread>
23#include <tuple>
24#include <vector>
25
26#include "ProblemFormat.h"
27
28enum class MasterFormulation { INTEGER, RELAXED };
29enum class SOLVER { BENDERS, OUTER_LOOP, MERGE_MPS };
30
31struct Predicate;
32typedef std::map<std::string, double> Point;
33
34typedef std::shared_ptr<Point> PointPtr;
35
36double const EPSILON_PREDICATE = 1e-8;
37
38typedef std::set<std::string> problem_names;
39typedef std::map<std::string, int> VariableMap;
40typedef std::map<int, std::string> Int2Str;
41typedef std::map<std::string, double> Str2Dbl;
42typedef std::vector<int> IntVector;
43typedef std::vector<char> CharVector;
44typedef std::vector<double> DblVector;
45typedef std::vector<std::string> StrVector;
46typedef std::map<std::string, VariableMap> CouplingMap;
47
48typedef std::map<std::string, IntVector> SlaveCutId;
49typedef std::tuple<int, std::string, int, bool> ActiveCut;
50typedef std::vector<ActiveCut> ActiveCutStorage;
51
52typedef std::pair<std::string, std::string> mps_coupling;
53typedef std::list<mps_coupling> mps_coupling_list;
54
55enum class BENDERSMETHOD {
56 BENDERS,
57 BENDERS_BY_BATCH,
58 BENDERS_OUTERLOOP,
59 BENDERS_BY_BATCH_OUTERLOOP
60};
61
62inline std::string bendersmethod_to_string(BENDERSMETHOD method) {
63 switch (method) {
64 case BENDERSMETHOD::BENDERS:
65 return "Benders";
66 case BENDERSMETHOD::BENDERS_BY_BATCH:
67 return "Benders by batch";
68 case BENDERSMETHOD::BENDERS_OUTERLOOP:
69 return "Outerloop around Benders";
70 case BENDERSMETHOD::BENDERS_BY_BATCH_OUTERLOOP:
71 return "Outerloop around Benders by batch";
72 default:
73 return "Unknown";
74 }
75}
76
77struct Predicate {
78 bool operator()(PointPtr const &lhs, PointPtr const &rhs) const {
79 return *lhs < *rhs;
80 }
81 bool operator()(Point const &lhs, Point const &rhs) const {
82 Point::const_iterator it1(lhs.begin());
83 Point::const_iterator it2(rhs.begin());
84
85 Point::const_iterator end1(lhs.end());
86 Point::const_iterator end2(rhs.end());
87
88 while (it1 != end1 && it2 != end2) {
89 if (it1->first != it2->first) {
90 return it1->first < it2->first;
91 } else {
92 if (std::fabs(it1->second - it2->second) < EPSILON_PREDICATE) {
93 ++it1;
94 ++it2;
95 } else {
96 return it1->second < it2->second;
97 }
98 }
99 }
100
101 if (it1 == end1 && it2 == end2) {
102 return false;
103 } else {
104 return (it1 == end1);
105 }
106 }
107};
108
116inline std::ostream &operator<<(std::ostream &stream, Point const &rhs) {
117 for (auto const &kvp : rhs) {
118 if (kvp.second > 0) {
119 if (kvp.second == 1) {
120 stream << "+";
121 stream << kvp.first;
122 } else {
123 stream << "+";
124 stream << kvp.second;
125 stream << kvp.first;
126 }
127 } else if (kvp.second < 0) {
128 stream << kvp.second;
129 stream << kvp.first;
130 }
131 }
132 return stream;
133}
134
135double norm_point(Point const &x0, Point const &x1);
136
137std::ostream &operator<<(std::ostream &stream,
138 std::vector<IntVector> const &rhs);
139
140const std::string SUBPROBLEM_WEIGHT_CST_STR("CONSTANT");
141const std::string SUBPROBLEM_WEIGHT_UNIFORM_CST_STR("UNIFORM");
142const std::string WEIGHT_SUM_CST_STR("WEIGHT_SUM");
143const std::string MPS_SUFFIX = ".mps";
144const std::string SAVE_SUFFIX = ".svf";
145
147 std::string OUTPUTROOT;
148 std::string INPUTROOT;
149 std::string STRUCTURE_FILE;
150 std::string LAST_ITERATION_JSON_FILE;
151 std::string MASTER_NAME;
152 ProblemsFormat PROBLEMS_FORMAT = ProblemsFormat::MPS_FILE;
153 std::string SOLVER_NAME;
154 std::string SLAVE_WEIGHT;
155 std::string AREA_FILE;
156
157 int LOG_LEVEL = 0;
158
159 double SLAVE_WEIGHT_VALUE = 0;
160 bool RESUME = false;
161
162 Str2Dbl weights;
163};
165
167 bool DO_OUTER_LOOP = false;
168 std::string OUTER_LOOP_OPTION_FILE;
169};
170
172 explicit BendersBaseOptions(const BaseOptions &base_to_copy)
173 : BaseOptions(base_to_copy) {}
174
175 int MAX_ITERATIONS = -1;
176
177 double ABSOLUTE_GAP = 0;
178 double RELATIVE_GAP = 0;
179 double RELAXED_GAP = 0;
180 double TIME_LIMIT = 0;
181 double SEPARATION_PARAM = 1;
182
183 bool AGGREGATION = false;
184 bool TRACE = false;
185 bool BOUND_ALPHA = false;
186
187 MasterFormulation MASTER_FORMULATION;
188
189 std::string CSV_NAME;
190 std::string LAST_MASTER_MPS;
191 std::string LAST_MASTER_BASIS;
192
193 size_t BATCH_SIZE;
194 ExternalLoopOptions EXTERNAL_LOOP_OPTIONS;
195};
196
197void usage(int argc);
198Json::Value get_json_file_content(const std::filesystem::path &json_file);
Definition common.h:146
Definition common.h:171
Definition common.h:166
Definition common.h:77