|
| 1 | +#include "logger.hpp" |
| 2 | +#include <iostream> |
| 3 | +#include <sstream> |
| 4 | +#include <map> |
| 5 | + |
| 6 | +// Helper to convert enums to strings for pretty printing |
| 7 | +template<typename T> |
| 8 | +std::string enum_to_string(T e, const std::map<T, std::string>& map) { |
| 9 | + auto it = map.find(e); |
| 10 | + return it != map.end() ? it->second : "UNKNOWN"; |
| 11 | +} |
| 12 | + |
| 13 | +// Timer implementation |
| 14 | +Timer::Timer() { reset(); } |
| 15 | +void Timer::reset() { start_time_ = std::chrono::high_resolution_clock::now(); } |
| 16 | +double Timer::read() { |
| 17 | + auto end_time = std::chrono::high_resolution_clock::now(); |
| 18 | + std::chrono::duration<double> elapsed = end_time - start_time_; |
| 19 | + return elapsed.count(); |
| 20 | +} |
| 21 | + |
| 22 | +// Logger implementation |
| 23 | +Logger::Logger(LogLevel level) : console_level_(level) {} |
| 24 | + |
| 25 | +void Logger::set_log_file(const std::string& filename) { |
| 26 | + log_file_.open(filename, std::ios::out | std::ios::trunc); |
| 27 | + if (!log_file_.is_open()) { |
| 28 | + std::cerr << "Error: Could not open log file: " << filename << std::endl; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +void Logger::log(LogLevel level, const std::string& message) { |
| 33 | + if (level <= console_level_) { |
| 34 | + std::cout << message << std::endl; |
| 35 | + } |
| 36 | + if (log_file_.is_open()) { |
| 37 | + log_file_ << message << std::endl; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +void Logger::info(const std::string& message) { log(LogLevel::kInfo, message); } |
| 42 | +void Logger::verbose(const std::string& message) { log(LogLevel::kVerbose, message); } |
| 43 | +void Logger::debug(const std::string& message) { log(LogLevel::kDebug, message); } |
| 44 | + |
| 45 | +void Logger::print_header() { |
| 46 | + info("------------------------------------------------------------"); |
| 47 | + info(" PDLP Solver - C++ Implementation "); |
| 48 | + info("------------------------------------------------------------"); |
| 49 | +} |
| 50 | + |
| 51 | +void Logger::print_params(const PrimalDualParams& params) { |
| 52 | + info("\nSolver Parameters:"); |
| 53 | + std::stringstream ss; |
| 54 | + |
| 55 | + std::map<RestartStrategy, std::string> restart_map = {{RestartStrategy::NO_RESTART, "None"}, {RestartStrategy::FIXED_RESTART, "Fixed"}, {RestartStrategy::ADAPTIVE_RESTART, "Adaptive"}}; |
| 56 | + std::map<ScalingMethod, std::string> scaling_map = {{ScalingMethod::NONE, "None"}, {ScalingMethod::RUIZ, "Ruiz"}, {ScalingMethod::POCK_CHAMBOLLE, "Pock-Chambolle"}, {ScalingMethod::L2_NORM, "L2-Norm"}}; |
| 57 | + std::map<StepSizeStrategy, std::string> step_size_map = {{StepSizeStrategy::FIXED, "Fixed"}, {StepSizeStrategy::ADAPTIVE, "Adaptive"}, {StepSizeStrategy::MALITSKY_POCK, "Malitsky-Pock"}}; |
| 58 | + |
| 59 | + ss << " - Max Iterations: " << params.max_iterations; |
| 60 | + info(ss.str()); ss.str(""); |
| 61 | + ss << " - Tolerance: " << params.tolerance; |
| 62 | + info(ss.str()); ss.str(""); |
| 63 | + ss << " - Restart Strategy: " << enum_to_string(params.restart_strategy, restart_map); |
| 64 | + info(ss.str()); ss.str(""); |
| 65 | + ss << " - Scaling Method: " << enum_to_string(params.scaling_method, scaling_map); |
| 66 | + info(ss.str()); ss.str(""); |
| 67 | + ss << " - Step Size Strategy: " << enum_to_string(params.step_size_strategy, step_size_map); |
| 68 | + info(ss.str()); |
| 69 | + info("------------------------------------------------------------"); |
| 70 | +} |
| 71 | + |
| 72 | +void Logger::print_iteration_header() { |
| 73 | + verbose("\n-------------------------------------------------------------------------------------------------"); |
| 74 | + verbose(" Iter | Primal Feas | Dual Feas | Duality Gap | Step Size"); |
| 75 | + verbose("-------------------------------------------------------------------------------------------------"); |
| 76 | +} |
| 77 | + |
| 78 | +void Logger::print_iteration_stats(int iter, const SolverResults& results, double step_size) { |
| 79 | + std::stringstream ss; |
| 80 | + ss << std::fixed << std::setprecision(4) |
| 81 | + << " " << std::setw(6) << iter << " | " |
| 82 | + << std::scientific << std::setprecision(2) |
| 83 | + << std::setw(11) << results.primal_feasibility << " | " |
| 84 | + << std::setw(10) << results.dual_feasibility << " | " |
| 85 | + << std::setw(11) << results.duality_gap << " | " |
| 86 | + << std::fixed << std::setprecision(4) |
| 87 | + << std::setw(9) << step_size; |
| 88 | + verbose(ss.str()); |
| 89 | +} |
| 90 | + |
| 91 | +void Logger::print_summary(const SolverResults& results, int total_iter, double total_time) { |
| 92 | + info("\n-------------------- Solver Summary --------------------"); |
| 93 | + std::stringstream ss; |
| 94 | + |
| 95 | + std::map<TerminationStatus, std::string> term_map = {{TerminationStatus::OPTIMAL, "Optimal"}, {TerminationStatus::TIMEOUT, "Timeout"}, {TerminationStatus::FEASIBLE, "Feasible"}}; |
| 96 | + |
| 97 | + ss << " - Termination Status: " << enum_to_string(results.term_code, term_map); |
| 98 | + info(ss.str()); ss.str(""); |
| 99 | + ss << " - Total Iterations: " << total_iter; |
| 100 | + info(ss.str()); ss.str(""); |
| 101 | + ss << " - Total Time: " << std::fixed << std::setprecision(3) << total_time << " seconds"; |
| 102 | + info(ss.str()); ss.str(""); |
| 103 | + ss << " - Final Primal Objective: " << std::scientific << std::setprecision(6) << results.primal_obj; |
| 104 | + info(ss.str()); ss.str(""); |
| 105 | + ss << " - Final Duality Gap: " << std::scientific << std::setprecision(6) << results.duality_gap; |
| 106 | + info(ss.str()); ss.str(""); |
| 107 | + ss << " - Final Primal Feasibility: " << std::scientific << std::setprecision(6) << results.primal_feasibility; |
| 108 | + info(ss.str()); ss.str(""); |
| 109 | + ss << " - Final Dual Feasibility: " << std::scientific << std::setprecision(6) << results.dual_feasibility; |
| 110 | + info(ss.str()); |
| 111 | + info("------------------------------------------------------------"); |
| 112 | +} |
0 commit comments