Skip to content

Commit

Permalink
Support MA.Zero in objective and constraints (jump-dev#2219)
Browse files Browse the repository at this point in the history
* Support MA.Zero in objective and cosntraints

* Update macros.jl
  • Loading branch information
odow authored Apr 7, 2020
1 parent fb52b47 commit c4596a8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ function build_constraint(_error::Function, α::Number,
set::MOI.AbstractScalarSet)
return build_constraint(_error, convert(AffExpr, α), set)
end
function build_constraint(
_error::Function, ::MutableArithmetics.Zero, set::MOI.AbstractScalarSet
)
return build_constraint(_error, zero(AffExpr), set)
end

function build_constraint(_error::Function, x::Vector{<:AbstractJuMPScalar},
set::MOI.AbstractVectorSet)
Expand Down Expand Up @@ -662,7 +667,7 @@ Adds multiple variables to model at once, in the same fashion as `@variable` mac
end)
!!! note
Keyword arguments must be contained within parentheses (refer to the example above).
Keyword arguments must be contained within parentheses (refer to the example above).
""" :(@variables)

"""
Expand Down
10 changes: 8 additions & 2 deletions src/objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
relative_gap(model::Model)
Return the final relative optimality gap after a call to `optimize!(model)`.
Exact value depends upon implementation of MathOptInterface.RelativeGap()
by the particular solver used for optimization.
Exact value depends upon implementation of MathOptInterface.RelativeGap()
by the particular solver used for optimization.
"""
function relative_gap(model::Model)::Float64
return MOI.get(model, MOI.RelativeGap())
Expand Down Expand Up @@ -124,6 +124,12 @@ function set_objective(
set_objective_function(model, func)
end

function set_objective(
model::Model, sense::MOI.OptimizationSense, ::MutableArithmetics.Zero
)
set_objective(model, sense, 0.0)
end

function set_objective(model::Model, sense::MOI.OptimizationSense, func)
error("The objective function `$(func)` is not supported by JuMP.")
end
Expand Down
9 changes: 9 additions & 0 deletions test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ end
@test constraint_object(c).set == MOI.EqualTo(0.0)
end

@testset "MutableArithmetics.Zero (Issue #2087)" begin
model = Model()
@objective(model, Min, sum(1 for _ in 1:0))
@test objective_function(model) == AffExpr(0.0)
c = @constraint(model, sum(1 for _ in 1:0) in MOI.EqualTo(0.0))
@test constraint_object(c).func == AffExpr(0.0)
@test constraint_object(c).set == MOI.EqualTo(0.0)
end

mutable struct MyVariable
test_kw::Int
info::JuMP.VariableInfo
Expand Down

0 comments on commit c4596a8

Please sign in to comment.