Skip to content

Commit

Permalink
Linear solver parameters (#69)
Browse files Browse the repository at this point in the history
* Introduce parameters for Solver.

* Add all double parameters to MPSolverParameters

* Add extraction of best bound from objective
  • Loading branch information
tbolender authored Dec 3, 2024
1 parent 0fbcf46 commit 5626082
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
42 changes: 39 additions & 3 deletions ext/or-tools/linear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using operations_research::MPConstraint;
using operations_research::MPObjective;
using operations_research::MPSolverParameters;
using operations_research::MPSolver;
using operations_research::MPVariable;

Expand Down Expand Up @@ -56,8 +57,43 @@ void init_linear(Rice::Module& m) {
.define_method("set_coefficient", &MPObjective::SetCoefficient)
.define_method("set_offset", &MPObjective::SetOffset)
.define_method("set_maximization", &MPObjective::SetMaximization)
.define_method("best_bound", &MPObjective::BestBound)
.define_method("set_minimization", &MPObjective::SetMinimization);

Rice::define_class_under<MPSolverParameters>(m, "MPSolverParameters")
.define_constructor(Rice::Constructor<MPSolverParameters>())
.define_method("reset", &MPSolverParameters::Reset)
.define_method(
"relative_mip_gap=",
[](MPSolverParameters& self, double relative_mip_gap) {
self.SetDoubleParam(MPSolverParameters::DoubleParam::RELATIVE_MIP_GAP, relative_mip_gap);
})
.define_method(
"relative_mip_gap",
[](MPSolverParameters& self) {
return self.GetDoubleParam(MPSolverParameters::DoubleParam::RELATIVE_MIP_GAP);
})
.define_method(
"primal_tolerance=",
[](MPSolverParameters& self, double primal_tolerance) {
self.SetDoubleParam(MPSolverParameters::DoubleParam::PRIMAL_TOLERANCE, primal_tolerance);
})
.define_method(
"primal_tolerance",
[](MPSolverParameters& self) {
return self.GetDoubleParam(MPSolverParameters::DoubleParam::PRIMAL_TOLERANCE);
})
.define_method(
"dual_tolerance=",
[](MPSolverParameters& self, double dual_tolerance) {
self.SetDoubleParam(MPSolverParameters::DoubleParam::DUAL_TOLERANCE, dual_tolerance);
})
.define_method(
"dual_tolerance",
[](MPSolverParameters& self) {
return self.GetDoubleParam(MPSolverParameters::DoubleParam::DUAL_TOLERANCE);
});

Rice::define_class_under<MPSolver>(m, "Solver")
.define_singleton_function(
"_new",
Expand Down Expand Up @@ -109,9 +145,9 @@ void init_linear(Rice::Module& m) {
return self.MakeRowConstraint(lb, ub);
})
.define_method(
"solve",
[](MPSolver& self) {
auto status = self.Solve();
"_solve",
[](MPSolver& self, MPSolverParameters& params) {
auto status = self.Solve(params);

if (status == MPSolver::ResultStatus::OPTIMAL) {
return Symbol("optimal");
Expand Down
8 changes: 8 additions & 0 deletions lib/or_tools/solver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ def minimize(expr)
objective.set_minimization
end

def solve
_solve(parameters)
end

def parameters
@parameters ||= MPSolverParameters.new
end

private

def set_objective(expr)
Expand Down
21 changes: 21 additions & 0 deletions test/linear_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,25 @@ def test_unrecognized_solver_type
end
assert_equal "Unrecognized solver type", error.message
end

def test_relative_mip_gap_parameter
params = ORTools::MPSolverParameters.new

params.relative_mip_gap = 42
assert_equal 42, params.relative_mip_gap
end

def test_primal_tolerance_parameter
params = ORTools::MPSolverParameters.new

params.primal_tolerance = 42
assert_equal 42, params.primal_tolerance
end

def test_dual_tolerance_parameter
params = ORTools::MPSolverParameters.new

params.dual_tolerance = 42
assert_equal 42, params.dual_tolerance
end
end

0 comments on commit 5626082

Please sign in to comment.