Skip to content

Commit 187b74d

Browse files
committed
refactor solve
1 parent 055fbb9 commit 187b74d

File tree

6 files changed

+93
-127
lines changed

6 files changed

+93
-127
lines changed

highs/pdlp/HiPdlpWrapper.cpp

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,75 @@ HighsStatus solveLpHiPdlp(const HighsOptions& options, HighsTimer& timer,
5353
PrimalDualParams params{};
5454

5555
getHiPpdlpParamsFromOptions(options, timer, params);
56-
std::vector<double> x, y;
57-
PDLPSolver pdlp(logger);
5856

5957
Timer total_timer;
60-
pdlp.Solve((HighsLp&)lp, params, x, y);
58+
/*** Order of operations
59+
* Presolve with HiGHS
60+
* Preprocess with HiPdlp
61+
* Scale with HiPdlp
62+
* Solve with HiPdlp
63+
* Unscale with HiPdlp
64+
* Postprocess with HiPDLP
65+
* Postsolve with HiGHS
66+
* ***/
67+
// 1. Presolve with HiGHS
68+
Highs highs;
69+
highs.passModel(lp);
70+
HighsLp presolved_lp;
71+
HighsSolution presolve_solution;
72+
bool do_presolve = (options.presolve != "off");
73+
74+
if (do_presolve) {
75+
HighsStatus presolve_status = highs.presolve();
76+
if (presolve_status != HighsStatus::kOk) {
77+
return HighsStatus::kError; // Handle presolve errors
78+
}
79+
80+
// If the problem is solved during presolve, get the solution and return.
81+
if (highs.getModelStatus() == HighsModelStatus::kOptimal ||
82+
highs.getModelStatus() == HighsModelStatus::kInfeasible ||
83+
highs.getModelStatus() == HighsModelStatus::kUnbounded) {
84+
presolve_solution = highs.getSolution();
85+
highs_info.pdlp_iteration_count = 0;
86+
highs_solution = presolve_solution;
87+
model_status = highs.getModelStatus();
88+
return HighsStatus::kOk;
89+
}
90+
91+
// Get the presolved LP for the next steps.
92+
presolved_lp = highs.getLp();
93+
} else {
94+
// If presolve is off, the problem to solve is the original LP.
95+
presolved_lp = lp;
96+
}
97+
98+
// 2. Preprocess with HiPdlp
99+
PDLPSolver pdlp(logger);
100+
HighsLp preprocessed_lp;
101+
//logger_.info("Preprocessing LP to handle ranged constraints...");
102+
pdlp.PreprocessLp(presolved_lp, preprocessed_lp);
103+
104+
// 3. Scale with HiPdlp
105+
pdlp.scaling_.ScaleProblem(preprocessed_lp, params);
106+
107+
// 4. Solve with HiPdlp
108+
std::vector<double> x, y;
109+
pdlp.Solve(preprocessed_lp, params, x, y);
110+
111+
// 5. Unscale with HiPdlp
112+
pdlp.scaling_.UnscaleSolution(x, y);
113+
114+
// 6. Postprocess with HiPDLP
115+
HighsSolution pdlp_solution;
116+
pdlp.Postsolve(presolved_lp, preprocessed_lp, x, y, pdlp_solution);
117+
118+
// 7. Postsolve with HiGHS
119+
if (do_presolve) {
120+
logger.info("Postsolving with HiGHS...");
121+
highs.postsolve(pdlp_solution);
122+
} else {
123+
pdlp_solution = pdlp_solution; // No presolve, so nothing to do
124+
}
61125

62126
// --- Print Summary ---
63127
logger.print_summary(pdlp.GetResults(), pdlp.GetIterationCount(),

highs/pdlp/hipdlp/pdhg.cc

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,6 @@ void PDLPSolver::Solve(HighsLp& original_lp, const PrimalDualParams& params,
271271

272272
HighsSolution solution;
273273

274-
// 1. Preprocess original LP to handle ranged constraints
275-
logger_.info("Preprocessing LP to handle ranged constraints...");
276-
HighsLp lp;
277-
PreprocessLp(original_lp, lp);
278-
279274
// 2. Pass the PREPROCESSED LP to HiGHS and run presolve
280275
/*
281276
logger_.info("Running HiGHS presolve on the preprocessed problem...");
@@ -293,18 +288,7 @@ void PDLPSolver::Solve(HighsLp& original_lp, const PrimalDualParams& params,
293288
return;
294289
}
295290
*/
296-
// --- SCALING ---
297-
ScalingParams scaling_params;
298-
scaling_params.method = params.scaling_method;
299-
scaling_params.use_ruiz = params.use_ruiz_scaling;
300-
scaling_params.use_pc = params.use_pc_scaling;
301-
scaling_params.use_l2 = params.use_l2_scaling;
302-
scaling_params.ruiz_iterations = params.ruiz_iterations;
303-
scaling_params.ruiz_norm = params.ruiz_norm;
304-
scaling_params.pc_alpha = params.pc_alpha;
305-
scaling_.Initialize(lp);
306-
scaling_.ScaleProblem(lp, scaling_params);
307-
291+
HighsLp lp = original_lp;
308292
// --- 0.Using PowerMethod to estimate the largest eigenvalue ---
309293
double op_norm_sq = 1.0; // Default value
310294
HighsStatus status = PowerMethod(lp, op_norm_sq);

highs/pdlp/hipdlp/pdhg.hpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,28 @@
3434
// --- Classes ---
3535
class PDLPSolver {
3636
public:
37+
HighsLp lp_;
3738
PDLPSolver(Logger& logger);
38-
39-
// Main solve method
40-
void Solve(HighsLp& lp, const PrimalDualParams& params,
39+
void PreprocessLp(const HighsLp& original_lp, HighsLp& processed_lp);
40+
void ScaleProblem(HighsLp& lp, const PrimalDualParams& params);
41+
void Solve(HighsLp& original_lp, const PrimalDualParams& params,
4142
std::vector<double>& x, std::vector<double>& y);
43+
void Postsolve(const HighsLp& original_lp, HighsLp& processed_lp,
44+
const std::vector<double>& x_processed,
45+
const std::vector<double>& y_processed,
46+
HighsSolution& solution);
4247

4348
const SolverResults& GetResults() const { return results_; }
4449

45-
void SetScalingParams(const ScalingParams& scaling_params) {
46-
scaling_params_ = scaling_params;
47-
}
50+
// Scaling
51+
//ScalingParams scaling_params_;
52+
Scaling scaling_;
53+
void SetScalingParams(const PrimalDualParams& scaling_params);
4854

4955
int GetIterationCount() const;
5056

5157
private:
5258
// Problem data
53-
HighsLp lp_;
5459
PrimalDualParams params_;
5560
// HighsInterface highs_interface_;
5661
Logger& logger_;
@@ -80,25 +85,12 @@ class PDLPSolver {
8085
std::vector<double> dSlackPos_;
8186
std::vector<double> dSlackNeg_;
8287

83-
// Scaling
84-
ScalingParams scaling_params_;
85-
Scaling scaling_;
86-
8788
FILE* pdlp_log_file_ = nullptr;
8889

89-
// HighsStatus TransformGxLeqToGeq(HighsLp& lp);
90-
void PreprocessLp(const HighsLp& original_lp, HighsLp& processed_lp);
91-
void Postsolve(const HighsLp& original_lp, HighsLp& processed_lp,
92-
const std::vector<double>& x_processed,
93-
const std::vector<double>& y_processed,
94-
HighsSolution& solution);
95-
9690
// Helper functions
9791
void solveReturn();
9892
void Initialize(const HighsLp& lp, std::vector<double>& x,
9993
std::vector<double>& y);
100-
bool RunPresolve(const HighsLp& original_lp, Highs& highs,
101-
HighsLp& presolved_lp);
10294
SolverResults results_;
10395
TerminationStatus SolvePresolvedProblem(HighsLp& presolved_lp,
10496
const PrimalDualParams& params,

highs/pdlp/hipdlp/restart.hpp

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,60 +17,7 @@
1717

1818
#include "scaling.hpp"
1919
#include "solver_results.hpp"
20-
21-
enum class Device { CPU, GPU };
22-
23-
enum class RestartStrategy { NO_RESTART, FIXED_RESTART, ADAPTIVE_RESTART };
24-
25-
enum class StepSizeStrategy { FIXED, ADAPTIVE, MALITSKY_POCK };
26-
27-
struct MalitskyPockParams {
28-
double step_size_interpolation = 0.5; // Between 0 and 1
29-
double step_size_downscaling_factor = 0.7;
30-
double linesearch_contraction_factor = 0.99;
31-
void initialise();
32-
};
33-
34-
struct AdaptiveLinesearchParams {
35-
double step_size_reduction_exponent = 0.3;
36-
double step_size_growth_exponent = 0.6;
37-
void initialise();
38-
};
39-
40-
struct PrimalDualParams {
41-
double eta;
42-
double omega;
43-
double tolerance;
44-
size_t max_iterations;
45-
Device device_type;
46-
double time_limit = 3600.0;
47-
48-
// Restart parameters
49-
RestartStrategy restart_strategy;
50-
int fixed_restart_interval;
51-
52-
bool use_halpern_restart = false;
53-
54-
// Scaling parameters
55-
ScalingMethod scaling_method = ScalingMethod::NONE;
56-
bool use_ruiz_scaling = false;
57-
bool use_pc_scaling = false;
58-
bool use_l2_scaling = false;
59-
60-
// Ruiz scaling parameters
61-
int ruiz_iterations = 10;
62-
double ruiz_norm = INFINITY;
63-
64-
// Pock-Chambolle scaling parameters
65-
double pc_alpha = 1.0;
66-
67-
// Step sizes strategy
68-
StepSizeStrategy step_size_strategy = StepSizeStrategy::FIXED;
69-
70-
MalitskyPockParams malitsky_pock_params;
71-
AdaptiveLinesearchParams adaptive_linesearch_params;
72-
void initialise();
73-
};
20+
#include "defs.hpp"
7421

7522
// Struct to communicate restart decisions
7623
struct RestartInfo {

highs/pdlp/hipdlp/scaling.cc

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,35 +64,31 @@ void LogMatrixNorms(const HighsLp& lp, const std::string& stage) {
6464
std::cout << "-------------------------\n" << std::endl;
6565
}
6666

67-
void Scaling::ScaleProblem(HighsLp& lp, const ScalingParams& params) {
68-
if (params.method == ScalingMethod::NONE) {
67+
void Scaling::ScaleProblem(HighsLp& lp, const PrimalDualParams& params) {
68+
if (params.scaling_method == ScalingMethod::NONE) {
6969
std::cout << "No scaling applied." << std::endl;
7070
return;
7171
}
72-
// LogMatrixNorms(lp, "Before Scaling");
7372

74-
std::cout << "Applying scaling method: " << static_cast<int>(params.method)
73+
std::cout << "Applying scaling method: " << static_cast<int>(params.scaling_method)
7574
<< std::endl;
76-
if (params.use_pc) {
75+
if (params.use_pc_scaling) {
7776
std::cout << "Applying Pock-Chambolle scaling..." << std::endl;
7877
ApplyPockChambolleScaling(lp, params);
7978
}
80-
if (params.use_ruiz) {
79+
if (params.use_ruiz_scaling) {
8180
std::cout << "Applying Ruiz scaling..." << std::endl;
8281
ApplyRuizScaling(lp, params);
8382
}
84-
85-
if (params.use_l2 || params.method == ScalingMethod::L2_NORM) {
83+
if (params.use_l2_scaling || params.scaling_method == ScalingMethod::L2_NORM) {
8684
std::cout << "Applying L2 norm scaling..." << std::endl;
8785
ApplyL2Scaling(lp);
8886
}
8987

90-
// LogMatrixNorms(lp, "After Scaling");
91-
9288
is_scaled_ = true;
9389
}
9490

95-
void Scaling::ApplyRuizScaling(HighsLp& lp, const ScalingParams& params) {
91+
void Scaling::ApplyRuizScaling(HighsLp& lp, const PrimalDualParams& params) {
9692
std::vector<double> current_col_scaling(lp.num_col_);
9793
std::vector<double> current_row_scaling(lp.num_row_);
9894

@@ -159,7 +155,7 @@ void Scaling::ApplyRuizScaling(HighsLp& lp, const ScalingParams& params) {
159155
}
160156

161157
void Scaling::ApplyPockChambolleScaling(HighsLp& lp,
162-
const ScalingParams& params) {
158+
const PrimalDualParams& params) {
163159
if (params.pc_alpha < 0.0 || params.pc_alpha > 2.0) {
164160
std::cerr << "PC alpha should be in [0, 2]" << std::endl;
165161
exit(1);

highs/pdlp/hipdlp/scaling.hpp

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

1717
#include "Highs.h"
18-
19-
enum class ScalingMethod { NONE, RUIZ, POCK_CHAMBOLLE, L2_NORM, COMBINED };
20-
21-
struct ScalingParams {
22-
ScalingMethod method = ScalingMethod::NONE;
23-
24-
// Ruiz scaling parameters
25-
int ruiz_iterations = 10;
26-
double ruiz_norm = INFINITY; // Currently only infinity norm is supported
27-
28-
// Pock-Chambolle scaling parameters
29-
double pc_alpha = 1.0; // Should be in [0, 2]
30-
31-
// Whether to apply multiple scaling methods in sequence
32-
bool use_ruiz = false;
33-
bool use_pc = false;
34-
bool use_l2 = false;
35-
};
18+
#include "defs.hpp"
3619

3720
class Scaling {
3821
public:
3922
Scaling() = default;
4023

4124
void Initialize(const HighsLp& lp);
42-
void ScaleProblem(HighsLp& lp, const ScalingParams& params);
25+
void ScaleProblem(HighsLp& lp, const PrimalDualParams& params);
4326
void UnscaleSolution(std::vector<double>& x, std::vector<double>& y) const;
4427

4528
// Get scaling vectors (for unscaling solution later)
@@ -59,8 +42,8 @@ class Scaling {
5942
double norm_rhs_;
6043

6144
// Individual scaling methods
62-
void ApplyRuizScaling(HighsLp& lp, const ScalingParams& params);
63-
void ApplyPockChambolleScaling(HighsLp& lp, const ScalingParams& params);
45+
void ApplyRuizScaling(HighsLp& lp, const PrimalDualParams& params);
46+
void ApplyPockChambolleScaling(HighsLp& lp, const PrimalDualParams& params);
6447
void ApplyL2Scaling(HighsLp& lp);
6548

6649
// Helper function to apply scaling factors to the problem

0 commit comments

Comments
 (0)