Skip to content

Commit b3c63e2

Browse files
committed
Move logger entirely within PDLPSolver
1 parent 02c6dfa commit b3c63e2

File tree

6 files changed

+64
-72
lines changed

6 files changed

+64
-72
lines changed

highs/pdlp/HiPdlpWrapper.cpp

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
#include "pdlp/hipdlp/pdhg.hpp"
1616
#include "pdlp/hipdlp/restart.hpp"
1717

18-
void getHiPpdlpParamsFromOptions(const HighsOptions& options, HighsTimer& timer,
19-
PrimalDualParams& params);
20-
2118
HighsStatus solveLpHiPdlp(HighsLpSolverObject& solver_object) {
2219
return solveLpHiPdlp(solver_object.options_, solver_object.timer_,
2320
solver_object.lp_, solver_object.basis_,
@@ -33,23 +30,11 @@ HighsStatus solveLpHiPdlp(const HighsOptions& options, HighsTimer& timer,
3330
// Indicate that no imprecise solution has (yet) been found
3431
resetModelStatusAndHighsInfo(model_status, highs_info);
3532

36-
std::string log_filename = "";
37-
LogLevel log_level;
38-
if (options.log_dev_level == kHighsLogDevLevelInfo) {
39-
log_level = LogLevel::kInfo;
40-
} else if (options.log_dev_level == kHighsLogDevLevelDetailed) {
41-
log_level = LogLevel::kVerbose;
42-
} else if (options.log_dev_level == kHighsLogDevLevelVerbose) {
43-
log_level = LogLevel::kDebug;
44-
} else {
45-
log_level = LogLevel::kNone;
46-
}
47-
// --- Initialize Logger ---
48-
Logger logger(log_level);
49-
if (!log_filename.empty()) logger.set_log_file(log_filename);
33+
Logger logger;
34+
logger.setLevel(options.log_dev_level);
35+
logger.passHighsLogOptions(options.log_options);
5036
logger.print_header();
5137

52-
Timer total_timer;
5338
/*** Order of operations
5439
* Preprocess with HiPdlp
5540
* Scale with HiPdlp
@@ -62,7 +47,6 @@ HighsStatus solveLpHiPdlp(const HighsOptions& options, HighsTimer& timer,
6247
pdlp.setParams(options, timer);
6348
HighsLp preprocessed_lp;
6449
pdlp.passLp(&lp);
65-
// logger_.info("Preprocessing LP to handle ranged constraints...");
6650
pdlp.preprocessLp();
6751

6852
// 3. Scale with HiPdlp
@@ -81,16 +65,15 @@ HighsStatus solveLpHiPdlp(const HighsOptions& options, HighsTimer& timer,
8165
// return x, y
8266

8367
// --- Print Summary ---
84-
logger.print_summary(pdlp.getResults(), pdlp.getIterationCount(),
85-
total_timer.read());
68+
pdlp.logSummary();
8669

8770
highs_info.pdlp_iteration_count = pdlp.getIterationCount();
8871

8972
model_status = HighsModelStatus::kUnknown;
9073
highs_solution.clear();
9174
highs_basis.valid = false;
9275

93-
const TerminationStatus termination_status = pdlp.getResults().term_code;
76+
const TerminationStatus termination_status = pdlp.getTerminationCode();
9477
switch (termination_status) {
9578
case TerminationStatus::OPTIMAL: {
9679
model_status = HighsModelStatus::kOptimal;
@@ -142,35 +125,3 @@ HighsStatus solveLpHiPdlp(const HighsOptions& options, HighsTimer& timer,
142125
return HighsStatus::kOk;
143126
}
144127

145-
void PrimalDualParams::initialise() {
146-
this->eta = 0;
147-
this->omega = 0;
148-
this->tolerance = 0;
149-
this->max_iterations = 0;
150-
this->device_type = Device::CPU;
151-
this->time_limit = 3600.0;
152-
this->restart_strategy = RestartStrategy::NO_RESTART;
153-
this->fixed_restart_interval = 0;
154-
this->use_halpern_restart = false;
155-
this->scaling_method = ScalingMethod::NONE;
156-
this->use_ruiz_scaling = false;
157-
this->use_pc_scaling = false;
158-
this->use_l2_scaling = false;
159-
this->ruiz_iterations = 10;
160-
this->ruiz_norm = INFINITY;
161-
this->pc_alpha = 1.0;
162-
this->step_size_strategy = StepSizeStrategy::FIXED;
163-
this->malitsky_pock_params.initialise();
164-
this->adaptive_linesearch_params.initialise();
165-
}
166-
167-
void MalitskyPockParams::initialise() {
168-
this->step_size_interpolation = 0.5; // Between 0 and 1
169-
this->step_size_downscaling_factor = 0.7;
170-
this->linesearch_contraction_factor = 0.99;
171-
}
172-
173-
void AdaptiveLinesearchParams::initialise() {
174-
this->step_size_reduction_exponent = 0.3;
175-
this->step_size_growth_exponent = 0.6;
176-
}

highs/pdlp/hipdlp/defs.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct PrimalDualParams {
6969

7070
MalitskyPockParams malitsky_pock_params;
7171
AdaptiveLinesearchParams adaptive_linesearch_params;
72+
HighsLogOptions log_options_;
7273
void initialise();
7374
};
7475

highs/pdlp/hipdlp/logger.cc

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,21 @@ double Timer::read() {
3131
}
3232

3333
// Logger implementation
34-
Logger::Logger(LogLevel level) : console_level_(level) {}
35-
36-
void Logger::set_log_file(const std::string& filename) {
37-
log_file_.open(filename, std::ios::out | std::ios::trunc);
38-
if (!log_file_.is_open()) {
39-
std::cerr << "Error: Could not open log file: " << filename << std::endl;
34+
void Logger::setLevel(const HighsInt log_dev_level) {
35+
if (log_dev_level == kHighsLogDevLevelVerbose) {
36+
console_level_ = LogLevel::kDebug;
37+
} else if (log_dev_level == kHighsLogDevLevelDetailed) {
38+
console_level_ = LogLevel::kVerbose;
39+
} else if (log_dev_level == kHighsLogDevLevelInfo) {
40+
console_level_ = LogLevel::kInfo;
41+
} else {
42+
console_level_ = LogLevel::kInfo;//None;
4043
}
4144
}
4245

4346
void Logger::log(LogLevel level, const std::string& message) {
44-
if (level <= console_level_) {
45-
std::cout << message << std::endl;
46-
}
47-
if (log_file_.is_open()) {
48-
log_file_ << message << std::endl;
49-
}
47+
if (level <= console_level_)
48+
highsLogUser(log_options_, HighsLogType::kInfo, "%s\n", message.c_str());
5049
}
5150

5251
void Logger::info(const std::string& message) { log(LogLevel::kInfo, message); }

highs/pdlp/hipdlp/logger.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class Timer {
4141

4242
class Logger {
4343
public:
44-
Logger(LogLevel level = LogLevel::kInfo);
44+
void setLevel(const HighsInt log_dev_level);
45+
void passHighsLogOptions(const HighsLogOptions log_options) { log_options_ = log_options;}
4546
void set_log_file(const std::string& filename);
4647
LogLevel getLogLevel() const { return console_level_; }
4748
// Logging methods for different levels
@@ -61,7 +62,8 @@ class Logger {
6162
private:
6263
void log(LogLevel level, const std::string& message);
6364
LogLevel console_level_;
64-
std::ofstream log_file_;
65+
HighsLogOptions log_options_;
66+
6567
};
6668

6769
#endif // PDLP_HIPDLP_LOGGER_HPP

highs/pdlp/hipdlp/pdhg.cc

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
using namespace std;
2626

27-
int PDLPSolver::getIterationCount() const { return final_iter_count_; }
28-
2927
void PDLPSolver::preprocessLp() {
3028
logger_.info(
3129
"Preprocessing LP using cupdlp formulation (slack variables for "
@@ -950,6 +948,8 @@ void PDLPSolver::setParams(const HighsOptions& options, HighsTimer& timer) {
950948
// parse_options_file
951949
scaling_.passParams(&params_);
952950
restart_scheme_.passParams(&params_);
951+
// Copy what's needed to use HiGHS logging
952+
params_.log_options_ = options.log_options;
953953
}
954954

955955
void PDLPSolver::scaleProblem() {
@@ -962,3 +962,40 @@ void PDLPSolver::unscaleSolution(std::vector<double>& x,
962962
std::vector<double>& y) const {
963963
scaling_.unscaleSolution(x, y);
964964
}
965+
void PDLPSolver::logSummary() {
966+
logger_.print_summary(results_, final_iter_count_, total_timer.read());
967+
}
968+
969+
void PrimalDualParams::initialise() {
970+
this->eta = 0;
971+
this->omega = 0;
972+
this->tolerance = 0;
973+
this->max_iterations = 0;
974+
this->device_type = Device::CPU;
975+
this->time_limit = 3600.0;
976+
this->restart_strategy = RestartStrategy::NO_RESTART;
977+
this->fixed_restart_interval = 0;
978+
this->use_halpern_restart = false;
979+
this->scaling_method = ScalingMethod::NONE;
980+
this->use_ruiz_scaling = false;
981+
this->use_pc_scaling = false;
982+
this->use_l2_scaling = false;
983+
this->ruiz_iterations = 10;
984+
this->ruiz_norm = INFINITY;
985+
this->pc_alpha = 1.0;
986+
this->step_size_strategy = StepSizeStrategy::FIXED;
987+
this->malitsky_pock_params.initialise();
988+
this->adaptive_linesearch_params.initialise();
989+
this->log_options_.clear();
990+
}
991+
992+
void MalitskyPockParams::initialise() {
993+
this->step_size_interpolation = 0.5; // Between 0 and 1
994+
this->step_size_downscaling_factor = 0.7;
995+
this->linesearch_contraction_factor = 0.99;
996+
}
997+
998+
void AdaptiveLinesearchParams::initialise() {
999+
this->step_size_reduction_exponent = 0.3;
1000+
this->step_size_growth_exponent = 0.6;
1001+
}

highs/pdlp/hipdlp/pdhg.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ class PDLPSolver {
4747
void getSolution(std::vector<double>& col_value,
4848
std::vector<double>& row_dual);
4949

50-
const SolverResults& getResults() const { return results_; }
5150
void passLp(const HighsLp* lp) { original_lp_ = lp; }
52-
int getIterationCount() const;
51+
TerminationStatus getTerminationCode() const { return results_.term_code; }
52+
int getIterationCount() const { return final_iter_count_; }
53+
void logSummary();
5354

5455
private:
5556
// Problem data
@@ -93,6 +94,7 @@ class PDLPSolver {
9394
std::vector<double> dSlackNeg_;
9495

9596
FILE* pdlp_log_file_ = nullptr;
97+
Timer total_timer;
9698

9799
// Helper functions
98100
void solveReturn();

0 commit comments

Comments
 (0)