Antares Xpansion
Investment simulations for Antares studies
Loading...
Searching...
No Matches
ILogger.h
1#ifndef ANTARESXPANSION_ILOGGER_H
2#define ANTARESXPANSION_ILOGGER_H
3
4#include <filesystem>
5#include <map>
6#include <memory>
7#include <sstream>
8#include <string>
9#include <vector>
10
11#include "antares-xpansion/xpansion_interfaces/LogUtils.h"
12
13typedef std::map<std::string, double> LogPoint;
14
15enum class StoppingCriterion
16{
17 empty,
18 timelimit,
19 relative_gap,
20 absolute_gap,
21 max_iteration
22};
23
24inline std::string criterion_to_str(const StoppingCriterion stopping_criterion)
25{
26 std::string stop_crit("");
27 switch (stopping_criterion)
28 {
29 case StoppingCriterion::absolute_gap:
30 stop_crit = "absolute gap";
31 break;
32
33 case StoppingCriterion::relative_gap:
34 stop_crit = "relative gap";
35 break;
36
37 case StoppingCriterion::max_iteration:
38 stop_crit = "maximum iterations";
39 break;
40
41 case StoppingCriterion::timelimit:
42 stop_crit = "timelimit";
43 break;
44
45 default:
46 break;
47 }
48 return stop_crit;
49}
50
51struct LogData
52{
53 double lb;
54 double best_ub;
55 double ub;
56 int it;
57 int best_it;
58 double subproblem_cost;
59 double invest_cost;
60 LogPoint x_in;
61 LogPoint x_out;
62 LogPoint x_cut;
63 LogPoint min_invest;
64 LogPoint max_invest;
65 double optimality_gap;
66 double relative_gap;
67 int max_iterations;
68 double benders_elapsed_time;
69 double master_time;
70 double subproblem_time;
71 int cumulative_number_of_subproblem_resolved;
72};
73
79{
84 virtual void display_message(const std::string& str) = 0;
85
90 void display_message(const std::ostringstream& msg)
91 {
92 display_message(msg.str());
93 }
94
95 virtual void display_message(const std::string& msg,
96 LogUtils::LOGLEVEL level,
97 const std::string& context)
98 = 0;
99
100 virtual void PrintIterationSeparatorBegin() = 0;
101
102 virtual void PrintIterationSeparatorEnd() = 0;
103
104 virtual ~ILoggerXpansion() = default;
105};
106
111{
112 void display_message(const std::string& str) override
113 {
114 }
115
116 void display_message(const std::string& str,
117 LogUtils::LOGLEVEL level,
118 const std::string& context) override
119 {
120 }
121
122 void PrintIterationSeparatorBegin() override {};
123
124 void PrintIterationSeparatorEnd() override {};
125
126 virtual ~EmptyLogger()
127 {
128 }
129};
130
135{
136 void display_message(const std::string& str) override
137 {
138 for (auto logger: loggers)
139 {
140 logger->display_message(str);
141 }
142 }
143
144 void AddLogger(std::shared_ptr<ILoggerXpansion> logger)
145 {
146 loggers.push_back(logger);
147 }
148
149 virtual void PrintIterationSeparatorBegin() override
150 {
151 for (auto logger: loggers)
152 {
153 logger->PrintIterationSeparatorBegin();
154 }
155 }
156
157 virtual void PrintIterationSeparatorEnd() override
158 {
159 for (auto logger: loggers)
160 {
161 logger->PrintIterationSeparatorEnd();
162 }
163 }
164
165 virtual void display_message(const std::string& msg,
166 LogUtils::LOGLEVEL level,
167 const std::string& context) override
168 {
169 for (auto logger: loggers)
170 {
171 logger->display_message(msg, level, context);
172 }
173 }
174
175private:
176 std::vector<std::shared_ptr<ILoggerXpansion>> loggers;
177};
178
184{
185public:
186 virtual ~ILogger() = default;
187
188 void display_message(const std::string& str) override = 0;
189
190 void display_message(const std::string& str,
191 LogUtils::LOGLEVEL level,
192 const std::string& context) override
193 = 0;
194
195 void PrintIterationSeparatorBegin() override = 0;
196
197 void PrintIterationSeparatorEnd() override = 0;
198
199 virtual void log_at_initialization(const int it_number) = 0;
200
201 virtual void log_iteration_candidates(const LogData& d) = 0;
202
203 virtual void log_master_solving_duration(double durationInSeconds) = 0;
204
205 virtual void LogSubproblemsSolvingWalltime(double durationInSeconds) = 0;
206
207 virtual void LogSubproblemsSolvingCumulativeCpuTime(double durationInSeconds) = 0;
208
209 virtual void log_at_iteration_end(const LogData& d) = 0;
210
211 virtual void log_at_ending(const LogData& d) = 0;
212
213 virtual void log_total_duration(double durationInSeconds) = 0;
214
215 virtual void log_stop_criterion_reached(const StoppingCriterion stopping_criterion) = 0;
216
217 virtual void display_restart_message() = 0;
218
219 virtual void restart_elapsed_time(const double elapsed_time) = 0;
220
221 virtual void number_of_iterations_before_restart(const int num_iterations) = 0;
222
223 virtual void restart_best_iteration(const int best_iterations) = 0;
224
225 virtual void restart_best_iterations_infos(const LogData& best_iterations_data) = 0;
226
227 virtual void LogAtInitialRelaxation() = 0;
228
229 virtual void LogAtSwitchToInteger() = 0;
230
231 virtual void cumulative_number_of_sub_problem_solved(int number) = 0;
232
233 const std::string CONTEXT = "Benders";
234};
235
236using Logger = std::shared_ptr<ILogger>;
237
238#endif // ANTARESXPANSION_ILOGGER_H
abstract class for operational logs
Definition ILogger.h:184
void display_message(const std::string &str) override=0
pure virtual method to display a std::string message
Definition ILogger.h:135
void display_message(const std::string &str) override
pure virtual method to display a std::string message
Definition ILogger.h:136
Definition ILogger.h:111
void display_message(const std::string &str) override
pure virtual method to display a std::string message
Definition ILogger.h:112
Xpansion Unique log Interface.
Definition ILogger.h:79
virtual void display_message(const std::string &str)=0
pure virtual method to display a std::string message
void display_message(const std::ostringstream &msg)
Definition ILogger.h:90
Definition ILogger.h:52