Antares Xpansion
Investment simulations for Antares studies
Loading...
Searching...
No Matches
BendersMathLogger.h
1#pragma once
2
3#include <fstream>
4#include <iostream>
5#include <list>
6#include <memory>
7
8#include "BendersStructsDatas.h"
9#include "antares-xpansion/xpansion_interfaces/ILogger.h"
10#include "common.h"
11const std::string MATHLOGGERCONTEXT = "Benders";
12
13enum class HEADERSTYPE { SHORT, LONG };
15 explicit HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method);
16
17 HEADERSTYPE type_;
18 BENDERSMETHOD method_;
19
20 virtual std::vector<std::string> HeadersList();
21};
23 explicit HeadersManagerExternalLoop(HEADERSTYPE type,
24 const BENDERSMETHOD& method);
25 std::vector<std::string> HeadersList() override;
26};
27
29 public:
30 explicit LogDestination(std::streamsize width = 40);
31 explicit LogDestination(const std::filesystem::path& file_path,
32 std::streamsize width = 40);
33
34 std::ostream& operator<<(std::ostream& (*function)(std::ostream&)) {
35 return function(*stream_);
36 }
37
38 template <class T>
39 std::ostream& operator<<(const T& obj);
40 std::ostream& InsertDelimiter() { return *stream_ << delimiter_; }
41
42 private:
43 std::ofstream file_stream_;
44 std::ostream* stream_;
45 std::streamsize width_ = 40;
46 std::string delimiter_ = "\t";
47
48 public:
49 void setDelimiter(const std::string& delimiter);
50};
51template <class T>
52std::ostream& LogDestination::operator<<(const T& obj) {
53 return (*stream_) << std::left << std::setw(width_) << obj;
54}
55void PrintBendersData(LogDestination& log_destination,
56 const CurrentIterationData& data, const HEADERSTYPE& type,
57 const BENDERSMETHOD& method);
58
59void PrintExternalLoopData(LogDestination& log_destination,
60 const CurrentIterationData& data,
61 const HEADERSTYPE& type,
62 const BENDERSMETHOD& method);
63
65 void write_header();
66
67 virtual void Print(const CurrentIterationData& data) = 0;
68 virtual std::vector<std::string> Headers() const = 0;
69 virtual LogDestination& LogsDestination() = 0;
70
71 virtual void PrintIterationSeparatorBegin() override;
72 virtual void PrintIterationSeparatorEnd() override;
73 virtual void setHeadersList() = 0;
74 virtual ~MathLoggerBehaviour() = default;
75};
76
78 explicit MathLogger(const std::filesystem::path& file_path,
79 std::streamsize width = 40,
80 HEADERSTYPE type = HEADERSTYPE::LONG);
81
82 explicit MathLogger(std::streamsize width = 40,
83 HEADERSTYPE type = HEADERSTYPE::SHORT);
84
85 void display_message(const std::string& str) override;
86 void display_message(const std::string& str, LogUtils::LOGLEVEL level,
87 const std::string& context) override;
88 virtual void Print(const CurrentIterationData& data) = 0;
89 std::vector<std::string> Headers() const override;
90 LogDestination& LogsDestination() override;
91 virtual void setHeadersList() = 0;
92 HEADERSTYPE HeadersType() const;
93 virtual ~MathLogger() = default;
94
95 protected:
96 void setHeadersList(const std::vector<std::string>& headers);
97
98 private:
99 std::vector<std::string> headers_;
100 LogDestination log_destination_;
101 HEADERSTYPE type_;
102};
103
104struct MathLoggerBase : public MathLogger {
105 using MathLogger::MathLogger;
106 void Print(const CurrentIterationData& data) override;
107
108 void setHeadersList() override;
109 virtual ~MathLoggerBase() = default;
110};
111
113 using MathLoggerBase::MathLoggerBase;
114 void Print(const CurrentIterationData& data) override;
115 void setHeadersList() override;
116 virtual ~MathLoggerBaseExternalLoop() = default;
117};
118
120 using MathLogger::MathLogger;
121 void Print(const CurrentIterationData& data) override;
122 void setHeadersList() override;
123 virtual ~MathLoggerBendersByBatch() = default;
124};
126 using MathLoggerBendersByBatch::MathLoggerBendersByBatch;
127 void Print(const CurrentIterationData& data) override;
128 void setHeadersList() override;
129 virtual ~MathLoggerBendersByBatchExternalLoop() = default;
130};
131
132template <class T>
135 const std::filesystem::path& file_path,
136 const std::vector<std::string>& headers,
138 : MathLogger(file_path), ptr_(ptr) {
139 headers_.push_back("Outer loop");
140 headers_.push_back("Ite");
141 headers_.insert(headers_.end(), headers.begin(), headers.end());
142 }
143
144 void setHeadersList() override;
145 void Print(const CurrentIterationData& data) override;
146 void display_message(const std::string& msg) override;
147 void display_message(const std::string& str, LogUtils::LOGLEVEL level,
148 const std::string& context) override;
149 virtual ~MathLoggerExternalLoopSpecific() = default;
150
151 private:
152 std::vector<std::string> headers_;
154};
155
157 public:
158 explicit MathLoggerImplementation(const BENDERSMETHOD& method,
159 const std::filesystem::path& file_path,
160 std::streamsize width = 40,
161 HEADERSTYPE type = HEADERSTYPE::LONG);
162 explicit MathLoggerImplementation(const BENDERSMETHOD& method,
163 std::streamsize width = 40,
164 HEADERSTYPE type = HEADERSTYPE::LONG);
165 explicit MathLoggerImplementation(std::shared_ptr<MathLogger> implementation);
166
167 void display_message(const std::string& str) override;
168 void display_message(const std::string& str, LogUtils::LOGLEVEL level,
169 const std::string& context) override;
170 void Print(const CurrentIterationData& data) override;
171 void PrintIterationSeparatorBegin() override;
172 void PrintIterationSeparatorEnd() override;
173
174 virtual ~MathLoggerImplementation() = default;
175
176 protected:
177 void setHeadersList() override;
178 std::vector<std::string> Headers() const override;
179 LogDestination& LogsDestination() override;
180
181 private:
182 std::shared_ptr<MathLogger> implementation_;
183};
184
186 public:
187 MathLoggerDriver() = default;
188 void write_header();
189 void display_message(const std::string& str) override;
190 void display_message(const std::string& str, LogUtils::LOGLEVEL level,
191 const std::string& context) override;
192 void add_logger(std::shared_ptr<MathLoggerImplementation> logger);
193 template <class T>
194 void add_logger(const std::filesystem::path& file_path,
195 const std::vector<std::string>& headers,
197 void Print(const CurrentIterationData& data);
198 virtual void PrintIterationSeparatorBegin() override;
199 virtual void PrintIterationSeparatorEnd() override;
200 virtual ~MathLoggerDriver() = default;
201
202 private:
203 std::vector<std::shared_ptr<MathLoggerImplementation>> math_loggers_;
204};
205
206template <class T>
207void MathLoggerDriver::add_logger(const std::filesystem::path& file_path,
208 const std::vector<std::string>& headers,
210 auto impl = std::make_shared<MathLoggerExternalLoopSpecific<T>>(file_path,
211 headers, t);
212 add_logger(std::make_shared<MathLoggerImplementation>(impl));
213}
214
215template <class T>
217 MathLogger::setHeadersList(headers_);
218}
219template <class T>
221 const CurrentIterationData& data) {
222 LogsDestination().InsertDelimiter();
223 LogsDestination() << data.criteria_current_iteration_data.benders_num_run;
224 LogsDestination().InsertDelimiter();
225 LogsDestination() << data.it;
226 LogsDestination().InsertDelimiter();
227 for (const auto& t : data.criteria_current_iteration_data.*ptr_) {
228 LogsDestination() << std::scientific << std::setprecision(10) << t;
229 LogsDestination().InsertDelimiter();
230 }
231 LogsDestination() << std::endl;
232}
233
234template <class T>
236 const std::string& msg) {
237 // keep empty
238}
239
240template <class T>
242 const std::string& str, LogUtils::LOGLEVEL level,
243 const std::string& context) {
244 // keep empty
245}
Definition BendersMathLogger.h:28
Definition BendersMathLogger.h:185
void display_message(const std::string &str) override
pure virtual method to display a std::string message
Definition BendersMathLogger.cpp:291
Definition BendersMathLogger.h:156
void display_message(const std::string &str) override
pure virtual method to display a std::string message
Definition BendersMathLogger.cpp:371
Definition BendersStructsDatas.h:8
Definition BendersStructsDatas.h:24
Definition BendersMathLogger.h:22
Definition BendersMathLogger.h:14
Xpansion Unique log Interface.
Definition ILogger.h:74
Definition BendersMathLogger.h:112
Definition BendersMathLogger.h:104
Definition BendersMathLogger.h:64
Definition BendersMathLogger.h:125
Definition BendersMathLogger.h:119
Definition BendersMathLogger.h:133
void display_message(const std::string &msg) override
pure virtual method to display a std::string message
Definition BendersMathLogger.h:235
Definition BendersMathLogger.h:77
void display_message(const std::string &str) override
pure virtual method to display a std::string message
Definition BendersMathLogger.cpp:118