Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MOI] Allow accessing multiple results #392

Merged
merged 2 commits into from
Feb 6, 2021
Merged

[MOI] Allow accessing multiple results #392

merged 2 commits into from
Feb 6, 2021

Conversation

odow
Copy link
Member

@odow odow commented Feb 4, 2021

Solves JuMP roadmap item "Accessing multiple results from solvers."

Here's what the test looks like at the JuMP level:

using JuMP, Gurobi, Random, Test

function _is_binary(x; atol = 1e-6)
    return isapprox(x, 0; atol = atol) || isapprox(x, 1; atol = atol)
end

N = 30
Random.seed!(1)
item_weights, item_values = rand(N), rand(N)

model = Model(Gurobi.Optimizer)
@variable(model, x[1:N], Bin, start = 0)
@constraint(model, item_weights' * x <= 10)
@objective(model, Max, item_values' * x)
optimize!(model)
RC = result_count(model)
@test RC > 1

for n in [0, RC + 1]
    @test_throws MOI.ResultIndexBoundsError value.(x; result = n)
    @test_throws MOI.ResultIndexBoundsError objective_value(model; result = n)
end

for n = 1:RC
    xn = value.(x; result = n)
    @test all(_is_binary, xn)
    @test isapprox(
        objective_value(model; result = n),
        item_values' * xn,
        atol=1e-6,
    )
end

Tests pass:

image

src/MOI_wrapper.jl Outdated Show resolved Hide resolved
src/MOI_wrapper.jl Outdated Show resolved Hide resolved
src/MOI_wrapper.jl Outdated Show resolved Hide resolved
@odow
Copy link
Member Author

odow commented Feb 5, 2021

Oops. Yeah there was a much simpler implementation.

@odow odow merged commit be9a745 into master Feb 6, 2021
@odow odow deleted the od/multiple_results branch February 6, 2021 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants