-
Notifications
You must be signed in to change notification settings - Fork 2
Move code to MOI and add JuMP layer #19
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
Changes from 12 commits
4018990
4f716b0
3185a6f
cea96e6
7b0c0a2
2c79007
9241ee1
9c83751
ed42075
87aacb2
e378889
85e00fb
38ae419
2e39b0c
997200a
e059c41
fc09db0
f0d70fa
833e6c1
a88f0e8
1cd08a3
b5253c7
4fc7565
5ecd302
ceccb9f
97cde56
0b3e9e4
e1288d6
e719b96
7e36757
90903ee
1426502
6f641ad
e9035d7
ad37c02
2fc4f3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
[deps] | ||
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" | ||
ModelAnalyzer = "d1179b25-476b-425c-b826-c7787f0fff83" | ||
ModelAnalyzer = "d1179b25-476b-425c-b826-c7787f0fff83" | ||
JuMP = "4076af6c-e467-56ae-b986-b466b2749572" | ||
|
||
[compat] | ||
JuMP = "1.24.0" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Copyright (c) 2025: Joaquim Garcia, Oscar Dowson and contributors | ||
# | ||
# Use of this source code is governed by an MIT-style license that can be found | ||
# in the LICENSE.md file or at https://opensource.org/licenses/MIT. | ||
|
||
module ModelAnalyzerJuMPExt | ||
|
||
import ModelAnalyzer | ||
import JuMP | ||
import MathOptInterface as MOI | ||
|
||
function ModelAnalyzer.analyze( | ||
analyzer::T, | ||
model::JuMP.GenericModel; | ||
kwargs..., | ||
) where {T<:ModelAnalyzer.AbstractAnalyzer} | ||
moi_model = JuMP.backend(model) | ||
result = ModelAnalyzer.analyze(analyzer, moi_model; kwargs...) | ||
return result | ||
end | ||
|
||
function ModelAnalyzer._name( | ||
ref::MOI.VariableIndex, | ||
model::JuMP.GenericModel{T}, | ||
) where {T} | ||
jump_ref = JuMP.GenericVariableRef{T}(model, ref) | ||
name = JuMP.name(jump_ref) | ||
if !isempty(name) | ||
return name | ||
end | ||
return "$jump_ref" | ||
end | ||
|
||
function ModelAnalyzer._name(ref::MOI.ConstraintIndex, model::JuMP.GenericModel) | ||
jump_ref = JuMP.constraint_ref_with_index(model, ref) | ||
name = JuMP.name(jump_ref) | ||
if !isempty(name) | ||
return name | ||
end | ||
return "$jump_ref" | ||
end | ||
|
||
""" | ||
variable(issue::ModelAnalyzer.AbstractIssue, model::JuMP.GenericModel) | ||
|
||
Return the **JuMP** variable reference associated to a particular issue. | ||
""" | ||
function ModelAnalyzer.variable( | ||
issue::ModelAnalyzer.AbstractIssue, | ||
model::JuMP.GenericModel{T}, | ||
) where {T} | ||
ref = ModelAnalyzer.variable(issue) | ||
return JuMP.GenericVariableRef{T}(model, ref) | ||
end | ||
|
||
""" | ||
variables(issue::ModelAnalyzer.AbstractIssue, model::JuMP.GenericModel) | ||
|
||
Return the **JuMP** variable references associated to a particular issue. | ||
""" | ||
function ModelAnalyzer.variables( | ||
issue::ModelAnalyzer.AbstractIssue, | ||
model::JuMP.GenericModel{T}, | ||
) where {T} | ||
refs = ModelAnalyzer.variables(issue) | ||
return JuMP.GenericVariableRef{T}.(model, refs) | ||
end | ||
|
||
""" | ||
constraint(issue::ModelAnalyzer.AbstractIssue, model::JuMP.GenericModel) | ||
|
||
Return the **JuMP** constraint reference associated to a particular issue. | ||
""" | ||
function ModelAnalyzer.constraint( | ||
issue::ModelAnalyzer.AbstractIssue, | ||
model::JuMP.GenericModel, | ||
) | ||
ref = ModelAnalyzer.constraint(issue) | ||
return JuMP.constraint_ref_with_index(model, ref) | ||
end | ||
|
||
end # module ModelAnalyzerJuMPExt |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright (c) 2025: Joaquim Garcia, Oscar Dowson and contributors | ||
# | ||
# Use of this source code is governed by an MIT-style license that can be found | ||
# in the LICENSE.md file or at https://opensource.org/licenses/MIT. | ||
|
||
function _eval_variables end | ||
|
||
function _eval_variables(value_fn::Function, t::MOI.ScalarAffineTerm) | ||
return t.coefficient * value_fn(t.variable) | ||
end | ||
|
||
function _eval_variables(value_fn::Function, t::MOI.ScalarQuadraticTerm) | ||
out = t.coefficient * value_fn(t.variable_1) * value_fn(t.variable_2) | ||
return t.variable_1 == t.variable_2 ? out / 2 : out | ||
end | ||
|
||
_eval_variables(value_fn::Function, f::MOI.VariableIndex) = value_fn(f) | ||
|
||
function _eval_variables( | ||
value_fn::Function, | ||
f::MOI.ScalarAffineFunction{T}, | ||
) where {T} | ||
# TODO: this conversion exists in JuMP, but not in MOI | ||
S = Base.promote_op(value_fn, MOI.VariableIndex) | ||
U = Base.promote_op(*, T, S) | ||
joaquimg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
out = convert(U, f.constant) | ||
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. 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 don't get it, what does not exist in MOI ? 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. 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. Ah yes, MOI only works if the return type of the function is the same as the type of |
||
for t in f.terms | ||
out += _eval_variables(value_fn, t) | ||
end | ||
return out | ||
end |
Uh oh!
There was an error while loading. Please reload this page.