Skip to content

Commit 1a206a0

Browse files
committed
fix problem checking logic
1 parent 1469558 commit 1a206a0

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

cpp/include/cuopt/linear_programming/solve.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ optimization_problem_solution_t<i_t, f_t> solve_lp(
7171
* representation of a linear program
7272
* @param[in] settings A pdlp_solver_settings_t<i_t, f_t> object with the settings for the PDLP
7373
* solver.
74-
* @param[in] problem_checking If true, the problem is checked for consistency.
7574
* @param[in] use_pdlp_solver_modes If true, the PDLP hyperparameters coming from the
7675
* pdlp_solver_mode are used (instead of the ones comming from a potential hyper-params file).
7776
* @param[in] inside_mip If true, the problem is being solved in the context of a MIP.
@@ -81,7 +80,6 @@ template <typename i_t, typename f_t>
8180
optimization_problem_solution_t<i_t, f_t> solve_lp(
8281
detail::problem_t<i_t, f_t>& problem,
8382
pdlp_solver_settings_t<i_t, f_t> const& settings = pdlp_solver_settings_t<i_t, f_t>{},
84-
bool problem_checking = true,
8583
bool use_pdlp_solver_mode = true,
8684
bool inside_mip = false);
8785

cpp/src/linear_programming/solve.cu

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,6 @@ optimization_problem_solution_t<i_t, f_t> solve_lp_with_method(
561561
template <typename i_t, typename f_t>
562562
optimization_problem_solution_t<i_t, f_t> solve_lp(detail::problem_t<i_t, f_t>& problem,
563563
pdlp_solver_settings_t<i_t, f_t> const& settings,
564-
bool problem_checking,
565564
bool use_pdlp_solver_mode,
566565
bool inside_mip)
567566
{
@@ -575,18 +574,10 @@ optimization_problem_solution_t<i_t, f_t> solve_lp(detail::problem_t<i_t, f_t>&
575574

576575
raft::common::nvtx::range fun_scope("Running solver");
577576

578-
if (problem_checking) {
579-
raft::common::nvtx::range fun_scope("Check problem representation");
580-
// This is required as user might forget to set some fields
581-
if (!inside_mip) {
582-
problem_checking_t<i_t, f_t>::check_problem_representation(*problem.original_problem_ptr);
583-
problem_checking_t<i_t, f_t>::check_initial_solution_representation(
584-
*problem.original_problem_ptr, settings);
585-
} else {
586-
problem.check_problem_representation(true, true);
587-
problem_checking_t<i_t, f_t>::check_initial_solution_representation(problem, settings);
588-
}
589-
}
577+
cuopt_func_call(problem.check_problem_representation(true, inside_mip));
578+
using problem_checking_t = problem_checking_t<i_t, f_t>; // Can't use "," inside a macro
579+
cuopt_func_call(problem_checking_t::check_initial_solution_representation(problem, settings));
580+
590581
CUOPT_LOG_INFO(
591582
"Solving a problem with %d constraints %d variables (%d integers) and %d nonzeros",
592583
problem.n_constraints,
@@ -632,8 +623,24 @@ optimization_problem_solution_t<i_t, f_t> solve_lp(optimization_problem_t<i_t, f
632623
bool problem_checking,
633624
bool use_pdlp_solver_mode)
634625
{
635-
detail::problem_t<i_t, f_t> problem(op_problem);
636-
return solve_lp(problem, settings, problem_checking, use_pdlp_solver_mode);
626+
try {
627+
if (problem_checking) {
628+
raft::common::nvtx::range fun_scope("Check problem representation");
629+
problem_checking_t<i_t, f_t>::check_problem_representation(op_problem);
630+
problem_checking_t<i_t, f_t>::check_initial_solution_representation(op_problem, settings);
631+
}
632+
detail::problem_t<i_t, f_t> problem(op_problem);
633+
const bool inside_mip = false;
634+
return solve_lp(problem, settings, use_pdlp_solver_mode, inside_mip);
635+
} catch (const cuopt::logic_error& e) {
636+
CUOPT_LOG_ERROR("Error in solve_lp: %s", e.what());
637+
return optimization_problem_solution_t<i_t, f_t>{e, op_problem.get_handle_ptr()->get_stream()};
638+
} catch (const std::bad_alloc& e) {
639+
CUOPT_LOG_ERROR("Error in solve_lp: %s", e.what());
640+
return optimization_problem_solution_t<i_t, f_t>{
641+
cuopt::logic_error("Memory allocation failed", cuopt::error_type_t::RuntimeError),
642+
op_problem.get_handle_ptr()->get_stream()};
643+
}
637644
}
638645

639646
template <typename i_t, typename f_t>
@@ -727,7 +734,6 @@ optimization_problem_solution_t<i_t, f_t> solve_lp(
727734
template optimization_problem_solution_t<int, F_TYPE> solve_lp( \
728735
detail::problem_t<int, F_TYPE>& problem, \
729736
pdlp_solver_settings_t<int, F_TYPE> const& settings, \
730-
bool problem_checking, \
731737
bool use_pdlp_solver_mode, \
732738
bool inside_mip); \
733739
template optimization_problem_solution_t<int, F_TYPE> solve_lp( \

cpp/src/mip/relaxed_lp/relaxed_lp.cu

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,10 @@ optimization_problem_solution_t<i_t, f_t> get_relaxed_lp_solution(
106106
// before LP flush the logs as it takes quite some time
107107
cuopt::default_logger().flush();
108108
// Check the problem only when in assert or debug mode
109-
#ifdef ASSERT_MODE
110-
const bool problem_checking = true;
111-
#else
112-
const bool problem_checking = false;
113-
#endif
114109
const bool use_pdlp_solver_mode = true;
115110
const bool inside_mip = true;
116111

117-
auto solver_response =
118-
solve_lp(op_problem, settings, problem_checking, use_pdlp_solver_mode, inside_mip);
112+
auto solver_response = solve_lp(op_problem, settings, use_pdlp_solver_mode, inside_mip);
119113

120114
if (solver_response.get_primal_solution().size() != 0 &&
121115
solver_response.get_dual_solution().size() != 0 && save_state) {

python/cuopt/cuopt/tests/linear_programming/test_lp_solver.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def test_check_data_model_validity():
324324

325325
data_model_obj = data_model.DataModel()
326326

327-
# Test if exception is thrown when A_CSR is not set
327+
# Test if error is returned when A_CSR is not set
328328
solution = solver.Solve(data_model_obj)
329329
assert solution.get_error_status() == ErrorStatus.ValidationError
330330

@@ -334,15 +334,15 @@ def test_check_data_model_validity():
334334
A_offsets = np.array([0, 1], dtype=np.int32)
335335
data_model_obj.set_csr_constraint_matrix(A_values, A_indices, A_offsets)
336336

337-
# Test if exception is thrown when b is not set
337+
# Test if error is returned when b is not set
338338
solution = solver.Solve(data_model_obj)
339339
assert solution.get_error_status() == ErrorStatus.ValidationError
340340

341341
# Set b with np.array
342342
b = np.array([1.0], dtype=np.float64)
343343
data_model_obj.set_constraint_bounds(b)
344344

345-
# Test if exception is thrown when c is not set
345+
# Test if error is returned when c is not set
346346
solution = solver.Solve(data_model_obj)
347347
assert solution.get_error_status() == ErrorStatus.ValidationError
348348

@@ -353,22 +353,22 @@ def test_check_data_model_validity():
353353
# Set maximize
354354
data_model_obj.set_maximize(True)
355355

356-
# Test if exception is thrown when maximize is set to true
356+
# Test if error is returned when maximize is set to true
357357
solution = solver.Solve(data_model_obj)
358358
assert solution.get_error_status() == ErrorStatus.ValidationError
359359

360360
# Set maximize to correct value
361361
data_model_obj.set_maximize(False)
362362

363-
# Test if exception is thrown when row_type is not set
363+
# Test if error is returned when row_type is not set
364364
solution = solver.Solve(data_model_obj)
365365
assert solution.get_error_status() == ErrorStatus.ValidationError
366366

367367
# Set row_type with np.array
368368
row_type = np.array(["E"])
369369
data_model_obj.set_row_types(row_type)
370370

371-
# Test if no exception is thrown when row_type is set
371+
# Test if no error is returned when row_type is set
372372
solver.Solve(data_model_obj)
373373

374374
# Set constraint_lower_bounds with np.array
@@ -379,7 +379,7 @@ def test_check_data_model_validity():
379379
constraint_upper_bounds = np.array([1.0], dtype=np.float64)
380380
data_model_obj.set_constraint_upper_bounds(constraint_upper_bounds)
381381

382-
# Test if no exception is thrown when upper constraints bounds are not set
382+
# Test if no error is returned when upper constraints bounds are not set
383383
solver.Solve(data_model_obj)
384384

385385

0 commit comments

Comments
 (0)