Skip to content

Commit 211dd33

Browse files
committed
Now to expunge last of std::cout
1 parent aa5fe7d commit 211dd33

File tree

4 files changed

+85
-87
lines changed

4 files changed

+85
-87
lines changed

highs/pdlp/hipdlp/pdhg.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,10 @@ void PDLPSolver::solve(std::vector<double>& x, std::vector<double>& y) {
276276
const double op_norm_sq = PowerMethod();
277277
// Set step sizes based on the operator norm to ensure convergence
278278
// A safe choice satisfying eta * omega * ||A||^2 < 1
279-
step::StepSizeConfig step_size = step::InitializeStepSizesPowerMethod(lp, op_norm_sq);
279+
step_.passLp(&lp_);
280+
step_.passLogOptions(&params_.log_options_);
281+
step_.passDebugLogFile(pdlp_log_file_);
282+
StepSizeConfig step_size = step_.InitializeStepSizesPowerMethod(lp, op_norm_sq);
280283
const double fixed_eta = 0.99 / sqrt(op_norm_sq);
281284
PrimalDualParams working_params = params_;
282285

@@ -344,19 +347,19 @@ void PDLPSolver::solve(std::vector<double>& x, std::vector<double>& y) {
344347

345348
switch (params_.step_size_strategy) {
346349
case StepSizeStrategy::FIXED:
347-
step::UpdateIteratesFixed(lp, working_params, fixed_eta, x, y, Ax_new,
350+
step_.UpdateIteratesFixed(lp, working_params, fixed_eta, x, y, Ax_new,
348351
x_current_, y_current_, Ax_current,
349352
pdlp_log_file_);
350353
break;
351354

352355
case StepSizeStrategy::ADAPTIVE:
353-
step::UpdateIteratesAdaptive(
356+
step_.UpdateIteratesAdaptive(
354357
lp, working_params, x, y, Ax_new, x_current_, y_current_,
355358
Ax_current, ATy_current, current_eta_, iter, pdlp_log_file_);
356359
break;
357360

358361
case StepSizeStrategy::MALITSKY_POCK:
359-
step_success = step::UpdateIteratesMalitskyPock(
362+
step_success = step_.UpdateIteratesMalitskyPock(
360363
lp, working_params, x, y, Ax_new, x_current_, y_current_,
361364
Ax_current, ATy_current, current_eta_, ratio_last_two_step_sizes_,
362365
num_rejected_steps_, first_malitsky_iteration, pdlp_log_file_);

highs/pdlp/hipdlp/pdhg.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class PDLPSolver {
5757
const HighsLp* original_lp_; // The original problem (for postsolve)
5858
PrimalDualParams params_;
5959
Logger logger_;
60+
PdlpStep step_;
6061

6162
int final_iter_count_ = 0;
6263
int original_num_col_;

highs/pdlp/hipdlp/step.cc

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,20 @@
1515
#include <limits>
1616

1717
#include "linalg.hpp"
18+
#include "io/HighsIO.h" // For pdlpLogging
1819
#include "pdlp/cupdlp/cupdlp.h" // For pdlpLogging
1920

20-
namespace step {
21-
2221
static constexpr double kDivergentMovement = 1e10;
2322

24-
StepSizeConfig InitializeStepSizesPowerMethod(const HighsLp& lp,
25-
double op_norm_sq) {
23+
StepSizeConfig PdlpStep::InitializeStepSizesPowerMethod(const HighsLp& lp,
24+
double op_norm_sq) {
2625
StepSizeConfig config;
2726
config.power_method_lambda = op_norm_sq;
2827

2928
// Compute primal weight beta = ||c||^2 / ||b||^2
3029
double cost_norm = linalg::compute_cost_norm(lp, 2.0);
3130
double rhs_norm = linalg::compute_rhs_norm(lp, 2.0);
32-
// highsLogUser(params_.log_options_, HighsLogType::kInfo, "Cost norm: %g, RHS norm: %g\n", cost_norm, rhs_norm);
31+
highsLogUser(*log_options_, HighsLogType::kInfo, "Cost norm: %g, RHS norm: %g\n", cost_norm, rhs_norm);
3332
config.beta = cost_norm * cost_norm / (rhs_norm * rhs_norm + 1e-10);
3433

3534
// cuPDLP-C style weight initialization
@@ -41,27 +40,25 @@ StepSizeConfig InitializeStepSizesPowerMethod(const HighsLp& lp,
4140
return config;
4241
}
4342

44-
bool CheckNumericalStability(const std::vector<double>& delta_x,
45-
const std::vector<double>& delta_y, double omega) {
43+
bool PdlpStep::CheckNumericalStability(const std::vector<double>& delta_x,
44+
const std::vector<double>& delta_y, double omega) {
4645
// Using omega as primal_weight for consistency
4746
double movement = ComputeMovement(delta_x, delta_y, omega);
4847

4948
if (movement == 0.0) {
50-
std::cout << "Warning: Zero movement detected - numerical termination"
51-
<< std::endl;
49+
highsLogUser(*log_options_, HighsLogType::kInfo, "Warning: Zero movement detected - numerical termination\n");
5250
return false;
5351
}
5452

5553
if (movement > kDivergentMovement) {
56-
std::cout << "Warning: Divergent movement detected: " << movement
57-
<< std::endl;
54+
highsLogUser(*log_options_, HighsLogType::kInfo, "Warning: Divergent movement detected: %g\n", movement);
5855
return false;
5956
}
6057

6158
return true;
6259
}
6360

64-
double ComputeMovement(const std::vector<double>& delta_primal,
61+
double PdlpStep::ComputeMovement(const std::vector<double>& delta_primal,
6562
const std::vector<double>& delta_dual,
6663
double primal_weight) {
6764
double primal_squared_norm = 0.0;
@@ -78,7 +75,7 @@ double ComputeMovement(const std::vector<double>& delta_primal,
7875
(0.5 / primal_weight) * dual_squared_norm;
7976
}
8077

81-
double ComputeNonlinearity(const std::vector<double>& delta_primal,
78+
double PdlpStep::ComputeNonlinearity(const std::vector<double>& delta_primal,
8279
const std::vector<double>& delta_aty,
8380
const std::vector<double>& aty_current,
8481
const std::vector<double>& aty_new) {
@@ -92,7 +89,7 @@ double ComputeNonlinearity(const std::vector<double>& delta_primal,
9289

9390
// --- Implementation of functions from step.hpp ---
9491

95-
void UpdateX(std::vector<double>& x_new, const std::vector<double>& x_current,
92+
void PdlpStep::UpdateX(std::vector<double>& x_new, const std::vector<double>& x_current,
9693
const HighsLp& lp, const std::vector<double>& y_current,
9794
double eta, double omega, FILE* pdlp_log_file_) {
9895
std::vector<double> ATy_cache(lp.num_col_);
@@ -107,7 +104,7 @@ void UpdateX(std::vector<double>& x_new, const std::vector<double>& x_current,
107104
}
108105
}
109106

110-
void UpdateY(std::vector<double>& y_new, const std::vector<double>& y_current,
107+
void PdlpStep::UpdateY(std::vector<double>& y_new, const std::vector<double>& y_current,
111108
const HighsLp& lp, const std::vector<double>& ax_new,
112109
const std::vector<double>& ax_current, double eta, double omega) {
113110
for (HighsInt j = 0; j < lp.num_row_; j++) {
@@ -141,7 +138,7 @@ void UpdateY(std::vector<double>& y_new, const std::vector<double>& y_current,
141138
}
142139
}
143140

144-
void UpdateIteratesFixed(const HighsLp& lp, const PrimalDualParams& params,
141+
void PdlpStep::UpdateIteratesFixed(const HighsLp& lp, const PrimalDualParams& params,
145142
double fixed_eta, std::vector<double>& x_new,
146143
std::vector<double>& y_new,
147144
std::vector<double>& ax_new,
@@ -155,7 +152,7 @@ void UpdateIteratesFixed(const HighsLp& lp, const PrimalDualParams& params,
155152
UpdateY(y_new, y_current, lp, ax_new, ax_current, params.eta, params.omega);
156153
}
157154

158-
void UpdateIteratesAdaptive(
155+
void PdlpStep::UpdateIteratesAdaptive(
159156
const HighsLp& lp, const PrimalDualParams& params,
160157
std::vector<double>& x_new, std::vector<double>& y_new,
161158
std::vector<double>& ax_new, const std::vector<double>& x_current,
@@ -265,7 +262,7 @@ void UpdateIteratesAdaptive(
265262
}
266263
}
267264

268-
bool UpdateIteratesMalitskyPock(
265+
bool PdlpStep::UpdateIteratesMalitskyPock(
269266
const HighsLp& lp, const PrimalDualParams& params,
270267
std::vector<double>& x_new, std::vector<double>& y_new,
271268
std::vector<double>& ax_new, const std::vector<double>& x_current,
@@ -383,33 +380,33 @@ bool UpdateIteratesMalitskyPock(
383380
}
384381

385382
if (!CheckNumericalStability(delta_x, delta_y, params.omega)) {
386-
std::cerr << "Numerical instability in Malitsky-Pock step" << std::endl;
383+
highsLogUser(*log_options_, HighsLogType::kWarning,
384+
"Numerical instability in Malitsky-Pock step\n");
387385
return false;
388386
}
389387

390388
accepted_step = true;
391389

392-
if (inner_iterations > 1) {
393-
std::cout << "Malitsky-Pock: accepted after " << inner_iterations
394-
<< " line search iterations" << std::endl;
395-
}
390+
if (inner_iterations > 1)
391+
highsLogUser(*log_options_, HighsLogType::kInfo,
392+
"Malitsky-Pock: accepted after %d line search iterations\n",
393+
inner_iterations);
396394

397395
} else {
398396
// Reduce step size and try again
399397
new_primal_step_size *=
400398
params.malitsky_pock_params.step_size_downscaling_factor;
401399

402-
if (inner_iterations % 10 == 0) {
403-
std::cout << "Malitsky-Pock line search: iteration " << inner_iterations
404-
<< ", reducing step to " << new_primal_step_size << std::endl;
405-
}
400+
if (inner_iterations % 10 == 0)
401+
highsLogUser(*log_options_, HighsLogType::kInfo,
402+
"Malitsky-Pock line search: iteration %d, reducing step to %g\n",
403+
inner_iterations, new_primal_step_size);
406404
}
407405
}
408406

409407
if (!accepted_step) {
410-
std::cerr
411-
<< "Malitsky-Pock: Failed to find acceptable step after 60 iterations"
412-
<< std::endl;
408+
highsLogUser(*log_options_, HighsLogType::kWarning,
409+
"Malitsky-Pock: Failed to find acceptable step after 60 iterations\n");
413410
return false;
414411
}
415412

@@ -418,4 +415,3 @@ bool UpdateIteratesMalitskyPock(
418415
return true;
419416
}
420417

421-
} // namespace step

highs/pdlp/hipdlp/step.hpp

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,67 +16,65 @@
1616
#include "Highs.h"
1717
#include "restart.hpp"
1818

19-
namespace step {
20-
2119
struct StepSizeConfig {
2220
double primal_step;
2321
double dual_step;
2422
double beta;
2523
double power_method_lambda;
2624
};
2725

28-
StepSizeConfig InitializeStepSizesPowerMethod(const HighsLp& lp, double op_norm_sq);
29-
30-
bool CheckNumericalStability(const std::vector<double>& delta_x,
31-
const std::vector<double>& delta_y, double omega);
32-
33-
double ComputeMovement(const std::vector<double>& delta_primal,
34-
const std::vector<double>& delta_dual,
35-
double primal_weight);
36-
37-
double ComputeNonlinearity(const std::vector<double>& delta_primal,
38-
const std::vector<double>& delta_aty,
39-
const std::vector<double>& aty_current,
40-
const std::vector<double>& aty_new);
26+
class PdlpStep {
27+
public:
4128

29+
StepSizeConfig InitializeStepSizesPowerMethod(const HighsLp& lp, double op_norm_sq);
30+
bool CheckNumericalStability(const std::vector<double>& delta_x,
31+
const std::vector<double>& delta_y, double omega);
32+
double ComputeMovement(const std::vector<double>& delta_primal,
33+
const std::vector<double>& delta_dual,
34+
double primal_weight);
35+
double ComputeNonlinearity(const std::vector<double>& delta_primal,
36+
const std::vector<double>& delta_aty,
37+
const std::vector<double>& aty_current,
38+
const std::vector<double>& aty_new);
4239
// The standard primal update step
43-
void UpdateX(std::vector<double>& x_new, const std::vector<double>& x_current,
44-
const HighsLp& lp, const std::vector<double>& y_current,
45-
double primal_step, double omega, FILE* pdlp_log_file);
46-
47-
// The standard dual update step
48-
void UpdateY(std::vector<double>& y_new, const std::vector<double>& y_current,
49-
const HighsLp& lp, const std::vector<double>& ax_new,
50-
const std::vector<double>& ax_current, double dual_step,
51-
double omega);
40+
void UpdateX(std::vector<double>& x_new, const std::vector<double>& x_current,
41+
const HighsLp& lp, const std::vector<double>& y_current,
42+
double primal_step, double omega, FILE* pdlp_log_file);
5243

53-
// The fixed-step-size update procedure
54-
void UpdateIteratesFixed(const HighsLp& lp, const PrimalDualParams& params,
55-
double fixed_eta, std::vector<double>& x_new,
56-
std::vector<double>& y_new,
57-
std::vector<double>& ax_new,
58-
const std::vector<double>& x_current,
59-
const std::vector<double>& y_current,
60-
const std::vector<double>& ax_current,
61-
FILE* pdlp_log_file);
62-
63-
void UpdateIteratesAdaptive(
64-
const HighsLp& lp, const PrimalDualParams& params,
65-
std::vector<double>& x_new, std::vector<double>& y_new,
66-
std::vector<double>& ax_new, const std::vector<double>& x_current,
67-
const std::vector<double>& y_current, const std::vector<double>& ax_current,
68-
const std::vector<double>& aty_current, double& current_eta,
69-
int& step_size_iter_count, FILE* pdlp_log_file);
70-
71-
bool UpdateIteratesMalitskyPock(
72-
const HighsLp& lp, const PrimalDualParams& params,
73-
std::vector<double>& x_new, std::vector<double>& y_new,
74-
std::vector<double>& ax_new, const std::vector<double>& x_current,
75-
const std::vector<double>& y_current, const std::vector<double>& ax_current,
76-
const std::vector<double>& aty_current, double& current_eta,
77-
double& ratio_last_two_step_sizes, int& num_rejected_steps,
78-
bool first_iteration, FILE* pdlp_log_file);
79-
80-
} // namespace step
44+
// The standard dual update step
45+
void UpdateY(std::vector<double>& y_new, const std::vector<double>& y_current,
46+
const HighsLp& lp, const std::vector<double>& ax_new,
47+
const std::vector<double>& ax_current, double dual_step,
48+
double omega);
49+
// The fixed-step-size update procedure
50+
void UpdateIteratesFixed(const HighsLp& lp, const PrimalDualParams& params,
51+
double fixed_eta, std::vector<double>& x_new,
52+
std::vector<double>& y_new,
53+
std::vector<double>& ax_new,
54+
const std::vector<double>& x_current,
55+
const std::vector<double>& y_current,
56+
const std::vector<double>& ax_current,
57+
FILE* pdlp_log_file);
58+
void UpdateIteratesAdaptive(const HighsLp& lp, const PrimalDualParams& params,
59+
std::vector<double>& x_new, std::vector<double>& y_new,
60+
std::vector<double>& ax_new, const std::vector<double>& x_current,
61+
const std::vector<double>& y_current, const std::vector<double>& ax_current,
62+
const std::vector<double>& aty_current, double& current_eta,
63+
int& step_size_iter_count, FILE* pdlp_log_file);
64+
bool UpdateIteratesMalitskyPock(const HighsLp& lp, const PrimalDualParams& params,
65+
std::vector<double>& x_new, std::vector<double>& y_new,
66+
std::vector<double>& ax_new, const std::vector<double>& x_current,
67+
const std::vector<double>& y_current, const std::vector<double>& ax_current,
68+
const std::vector<double>& aty_current, double& current_eta,
69+
double& ratio_last_two_step_sizes, int& num_rejected_steps,
70+
bool first_iteration, FILE* pdlp_log_file);
71+
void passLp(HighsLp* lp) { lp_ = lp; };
72+
void passLogOptions(const HighsLogOptions* log_options) { log_options_ = log_options; };
73+
void passDebugLogFile(const FILE* pdlp_log_file) { pdlp_log_file_ = pdlp_log_file; };
74+
private:
75+
const HighsLp* lp_;
76+
const HighsLogOptions* log_options_;
77+
const FILE* pdlp_log_file_;
78+
};
8179

8280
#endif // PDLP_HIPDLP_STEP_HPP

0 commit comments

Comments
 (0)