-
Notifications
You must be signed in to change notification settings - Fork 81
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
Improve performance of coefficient modification #135
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -255,7 +255,7 @@ function get_sos(model::Model, start::Integer, len::Integer) | |
n = num_vars(model) | ||
numnzP = Array{Cint}(1) | ||
cbeg = Array{Cint}(len+1) | ||
|
||
ret = @grb_ccall(getsos, Cint, ( | ||
Ptr{Void}, | ||
Ptr{Cint}, | ||
|
@@ -314,11 +314,23 @@ function del_constrs!(model::Model, idx::Vector{Cint}) | |
end | ||
end | ||
|
||
function chg_coeffs!(model::Model, cidx::Real, vidx::Real, val::Real) | ||
ret = @grb_ccall(chgcoeffs, Cint, ( | ||
Ptr{Void}, | ||
Cint, | ||
Ref{Cint}, | ||
Ref{Cint}, | ||
Ref{Float64}), | ||
model, 1, cidx - 1, vidx - 1, val) | ||
if ret != 0 | ||
throw(GurobiError(model.env, ret)) | ||
end | ||
end | ||
|
||
chg_coeffs!{T<:Real, S<:Real}(model::Model, cidx::T, vidx::T, val::S) = chg_coeffs!(model, Cint[cidx], Cint[vidx], Float64[val]) | ||
chg_coeffs!{T<:Real, S<:Real}(model::Model, cidx::Vector{T}, vidx::Vector{T}, val::Vector{S}) = chg_coeffs!(model, convert(Vector{Cint},cidx), convert(Vector{Cint},vidx), fvec(val)) | ||
function chg_coeffs!(model::Model, cidx::Vector{Cint}, vidx::Vector{Cint}, val::FVec) | ||
chg_coeffs!(model::Model, cidx::AbstractVector{<:Real}, vidx::AbstractVector{<:Real}, val::AbstractVector{<:Real}) = | ||
chg_coeffs!(model, ivec(cidx), ivec(vidx), fvec(val)) | ||
|
||
function chg_coeffs!(model::Model, cidx::IVec, vidx::IVec, val::FVec) | ||
(length(cidx) == length(vidx) == length(val)) || error("Inconsistent argument dimensions.") | ||
|
||
numchgs = length(cidx) | ||
|
@@ -335,6 +347,7 @@ function chg_coeffs!(model::Model, cidx::Vector{Cint}, vidx::Vector{Cint}, val:: | |
end | ||
|
||
function getcoeff!(val::FVec, model::Model, cidx::Integer, vidx::Integer) | ||
Base.depwarn("getcoeff!(val, model, cidx, vidx) is deprecated. Instead you can retrieve a coefficient without allocating a vector by doing `val = getcoeff(model, cidx, vidx)`", :grb_getcoeff) | ||
@assert length(val) == 1 | ||
ret = @grb_ccall(getcoeff, Cint, ( | ||
Ptr{Void}, | ||
|
@@ -346,8 +359,17 @@ function getcoeff!(val::FVec, model::Model, cidx::Integer, vidx::Integer) | |
throw(GurobiError(model.env, ret)) | ||
end | ||
end | ||
|
||
function getcoeff(model::Model, cidx::Integer, vidx::Integer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same with these? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just added a couple more tests to verify that this works fine even when |
||
out = Array{Float64}(1) | ||
getcoeff!(out::FVec, model::Model, cidx::Integer, vidx::Integer) | ||
return out[1] | ||
end | ||
out = Ref{Float64}() | ||
ret = @grb_ccall(getcoeff, Cint, ( | ||
Ptr{Void}, | ||
Cint, | ||
Cint, | ||
Ref{Float64}), | ||
model, cidx - 1, vidx - 1, out) | ||
if ret != 0 | ||
throw(GurobiError(model.env, ret)) | ||
end | ||
out[] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Gurobi | ||
using Base.Test | ||
using Gurobi: getcoeff | ||
|
||
|
||
@testset "changing coefficients" begin | ||
A = spzeros(2, 2) | ||
A[1, 1] = 1.0 | ||
A[1, 2] = 2.0 | ||
A[2, 1] = 3.0 | ||
b = [0.1, 0.2] | ||
f = [0.0, 0.0] | ||
model = gurobi_model(Gurobi.Env(); f=f, A=A, b=b) | ||
|
||
@test getcoeff(model, 1, 1) == 1.0 | ||
|
||
# Verify that we can pass any kind of `::Integer` to `getcoeff` | ||
@test getcoeff(model, 1, Int8(1)) == 1.0 | ||
@test getcoeff(model, UInt32(1), UInt32(1)) == 1.0 | ||
|
||
@test getcoeff(model, 1, 2) == 2.0 | ||
@test getcoeff(model, 2, 1) == 3.0 | ||
@test getcoeff(model, 2, 2) == 0.0 | ||
|
||
chg_coeffs!(model, 2, 1, 1.5) | ||
# before updating the model, we still get the old coefficient value | ||
@test getcoeff(model, 2, 1) == 3.0 | ||
# after updating the model, we see the new coefficient value | ||
update_model!(model) | ||
@test getcoeff(model, 2, 1) == 1.5 | ||
|
||
# Verify that we are automatically converting the scalars to | ||
# Cint and Float64 as necessary | ||
chg_coeffs!(model, Int8(2), Int128(1), Float32(2.0)) | ||
update_model!(model) | ||
@test getcoeff(model, 2, 1) == 2.0 | ||
|
||
chg_coeffs!(model, [1, 2], [1, 1], [5.0, 6.5]) | ||
update_model!(model) | ||
@test getcoeff(model, 1, 1) == 5.0 | ||
@test getcoeff(model, 2, 1) == 6.5 | ||
@test getcoeff(model, 1, 2) == 2.0 | ||
@test getcoeff(model, 2, 2) == 0.0 | ||
|
||
chg_coeffs!(model, Int8[1, 2], Int8[1, 1], Float32[1.0, 2.0]) | ||
update_model!(model) | ||
@test getcoeff(model, 1, 1) == 1.0 | ||
@test getcoeff(model, 2, 1) == 2.0 | ||
@test getcoeff(model, 1, 2) == 2.0 | ||
@test getcoeff(model, 2, 2) == 0.0 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,5 @@ end | |
@testset "MathOptInterface Tests" begin | ||
evalfile("MOIWrapper.jl") | ||
end | ||
|
||
include("constraint_modification.jl") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these types okay? Shouldn't
cidx
andvidx
beCint
s andval
be aFloat64
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The types specified in the
ccall
call areRef{Cint}
andRef{Float64}
, so Julia will automatically insert a call to Base.cconvert which will do the correct conversion (or fail with a MethodError). For example:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Has it always done this? I'm sure I've been stung by it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. I think a lot of this code was written before I'd even heard of Julia...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah a lot of it can be cleaned up