Skip to content

Commit

Permalink
Improved solver construction
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Oct 6, 2024
1 parent 6e53a4e commit 6124930
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
10 changes: 9 additions & 1 deletion ext/or-tools/linear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ void init_linear(Rice::Module& m) {
.define_method("set_minimization", &MPObjective::SetMinimization);

Rice::define_class_under<MPSolver>(m, "Solver")
.define_constructor(Rice::Constructor<MPSolver, std::string, MPSolver::OptimizationProblemType>())
.define_singleton_function(
"_new",
[](const std::string& name, MPSolver::OptimizationProblemType problem_type) {
std::unique_ptr<MPSolver> solver(new MPSolver(name, problem_type));
if (!solver) {
throw std::runtime_error("Unrecognized solver type");
}
return solver;
})
.define_singleton_function(
"_create",
[](const std::string& solver_id) {
Expand Down
14 changes: 5 additions & 9 deletions lib/or_tools/solver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,12 @@ def set_objective(expr)
end
end

# hack to work with Rice constructor
m = Module.new do
def new(solver_id, *args)
if args.empty?
_create(solver_id)
else
super
end
def self.new(solver_id, *args)
if args.empty?
_create(solver_id)
else
_new(solver_id, *args)
end
end
singleton_class.prepend(m)
end
end

0 comments on commit 6124930

Please sign in to comment.