Skip to content

Commit

Permalink
Pass time limit to inner solver (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Mar 23, 2023
1 parent 1e3032c commit b5ff8f2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/MultiObjectiveAlgorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,14 @@ function _time_limit_exceeded(model::Optimizer, start_time::Float64)
if time_limit === nothing
return false
end
return time() - start_time >= time_limit
time_remaining = time_limit - (time() - start_time)
if time_remaining <= 0
return true
end
if MOI.supports(model.inner, MOI.TimeLimitSec())
MOI.set(model.inner, MOI.TimeLimitSec(), time_remaining)
end
return false
end

### ObjectiveFunction
Expand Down Expand Up @@ -498,6 +505,9 @@ function MOI.optimize!(model::Optimizer)
if solutions !== nothing
model.solutions = solutions
end
if MOI.supports(model.inner, MOI.TimeLimitSec())
MOI.set(model.inner, MOI.TimeLimitSec(), nothing)
end
return
end

Expand Down
27 changes: 27 additions & 0 deletions test/algorithms/EpsilonConstraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,33 @@ function test_time_limit()
return
end

function test_time_limit_large()
p1 = [77, 94, 71, 63, 96, 82, 85, 75, 72, 91, 99, 63, 84, 87, 79, 94, 90]
p2 = [65, 90, 90, 77, 95, 84, 70, 94, 66, 92, 74, 97, 60, 60, 65, 97, 93]
w = [80, 87, 68, 72, 66, 77, 99, 85, 70, 93, 98, 72, 100, 89, 67, 86, 91]
model = MOA.Optimizer(HiGHS.Optimizer)
MOI.set(model, MOA.Algorithm(), MOA.EpsilonConstraint())
MOI.set(model, MOI.TimeLimitSec(), 1.0)
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variables(model, length(w))
MOI.add_constraint.(model, x, MOI.ZeroOne())
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
f = MOI.Utilities.operate(
vcat,
Float64,
[sum(1.0 * p[i] * x[i] for i in 1:length(w)) for p in [p1, p2]]...,
)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.add_constraint(
model,
sum(1.0 * w[i] * x[i] for i in 1:length(w)),
MOI.LessThan(900.0),
)
MOI.optimize!(model)
@test MOI.get(model, MOI.ResultCount()) >= 0
return
end

end

TestEpsilonConstraint.run_tests()

0 comments on commit b5ff8f2

Please sign in to comment.