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