Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,7 @@ include("complement.jl")
include("copy.jl")
include("feasibility_checker.jl")
include("file_formats.jl")
include("jump_moi_function.jl")
include("lp_matrix_data.jl")
include("lp_sensitivity2.jl")
include("indicator.jl")
Expand Down
162 changes: 0 additions & 162 deletions src/aff_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -767,118 +767,6 @@ function MOI.ScalarAffineFunction(
return MOI.ScalarAffineFunction(terms, a.constant)
end

"""
moi_function(x::AbstractJuMPScalar)
moi_function(x::AbstractArray{<:AbstractJuMPScalar})

Given a JuMP object `x`, return the MathOptInterface equivalent.

See also: [`jump_function`](@ref).

## Example

```jldoctest
julia> model = Model();

julia> @variable(model, x);

julia> f = 2.0 * x + 1.0
2 x + 1

julia> moi_function(f)
1.0 + 2.0 MOI.VariableIndex(1)
```
"""
function moi_function end

function moi_function(x::AbstractArray{AbstractJuMPScalar})
return error(
"""
Unable to convert an array of type `::$(typeof(x))` to an equivalent function
in MathOptInterface because the array has the abstract element type
`AbstractJuMPScalar`.

To fix this error, convert every element in the array to the same concrete
element type.

For example, instead of:
```julia
model = Model();
@variable(model, x);
y = AbstractJuMPScalar[x, sin(x)]
@objective(model, Min, y)
```
do
```julia
@objective(model, Min, convert.(NonlinearExpr, y))
```
""",
)
end

"""
moi_function_type(::Type{T}) where {T}

Given a JuMP object type `T`, return the MathOptInterface equivalent.

See also: [`jump_function_type`](@ref).

## Example

```jldoctest
julia> moi_function_type(AffExpr)
MathOptInterface.ScalarAffineFunction{Float64}
```
"""
function moi_function_type end

"""
jump_function(model::AbstractModel, x::MOI.AbstractFunction)

Given an MathOptInterface object `x`, return the JuMP equivalent.

See also: [`moi_function`](@ref).

## Example

```jldoctest
julia> model = Model();

julia> @variable(model, x);

julia> f = 2.0 * index(x) + 1.0
1.0 + 2.0 MOI.VariableIndex(1)

julia> jump_function(model, f)
2 x + 1
```
"""
function jump_function end

"""
jump_function_type(model::AbstractModel, ::Type{T}) where {T}

Given an MathOptInterface object type `T`, return the JuMP equivalent.

See also: [`moi_function_type`](@ref).

## Example

```jldoctest
julia> model = Model();

julia> jump_function_type(model, MOI.ScalarAffineFunction{Float64})
AffExpr (alias for GenericAffExpr{Float64, GenericVariableRef{Float64}})
```
"""
function jump_function_type end

moi_function(a::GenericAffExpr) = MOI.ScalarAffineFunction(a)

function moi_function_type(::Type{<:GenericAffExpr{T}}) where {T}
return MOI.ScalarAffineFunction{T}
end

function GenericAffExpr{C,GenericVariableRef{T}}(
m::GenericModel{T},
f::MOI.ScalarAffineFunction,
Expand All @@ -894,50 +782,6 @@ function GenericAffExpr{C,GenericVariableRef{T}}(
return aff
end

function jump_function_type(
::GenericModel{T},
::Type{MOI.ScalarAffineFunction{C}},
) where {C,T}
S = promote_type(C, T)
return GenericAffExpr{S,GenericVariableRef{T}}
end

function jump_function(
model::GenericModel{T},
f::MOI.ScalarAffineFunction{C},
) where {C,T}
S = promote_type(C, T)
return GenericAffExpr{S,GenericVariableRef{T}}(model, f)
end

function jump_function_type(
::GenericModel{T},
::Type{MOI.VectorAffineFunction{C}},
) where {C,T}
S = promote_type(C, T)
return Vector{GenericAffExpr{S,GenericVariableRef{T}}}
end

function jump_function(
model::GenericModel{T},
f::MOI.VectorAffineFunction{C},
) where {T,C}
S = promote_type(C, T)
ret = GenericAffExpr{S,GenericVariableRef{T}}[]
for scalar_f in MOIU.eachscalar(f)
g = GenericAffExpr{S,GenericVariableRef{T}}(scalar_f.constant)
for t in scalar_f.terms
add_to_expression!(
g,
t.coefficient,
GenericVariableRef(model, t.variable),
)
end
push!(ret, g)
end
return ret
end

"""
_fill_vaf!(
terms::Vector{<:MOI.VectorAffineTerm},
Expand Down Expand Up @@ -984,12 +828,6 @@ function MOI.VectorAffineFunction(
return MOI.VectorAffineFunction(terms, constant)
end

moi_function(a::Vector{<:GenericAffExpr}) = MOI.VectorAffineFunction(a)

function moi_function_type(::Type{<:Vector{<:GenericAffExpr{T}}}) where {T}
return MOI.VectorAffineFunction{T}
end

"""
_eval_as_variable(f::F, x::GenericAffExpr, args...) where {F}

Expand Down
6 changes: 4 additions & 2 deletions src/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,13 @@ function MOI.submit(
cb::MOI.LazyConstraint,
con::ScalarConstraint,
)
return MOI.submit(backend(model), cb, moi_function(con.func), con.set)
f = moi_function(model, con.func)
return MOI.submit(backend(model), cb, f, con.set)
end

function MOI.submit(model::GenericModel, cb::MOI.UserCut, con::ScalarConstraint)
return MOI.submit(backend(model), cb, moi_function(con.func), con.set)
f = moi_function(model, con.func)
return MOI.submit(backend(model), cb, f, con.set)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These ones were missing from #4032

end

function MOI.submit(
Expand Down
28 changes: 1 addition & 27 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -757,28 +757,6 @@ function add_constraint(
return add_constraint(model, con.constraint, name)
end

"""
jump_function(constraint::AbstractConstraint)

Return the function of the constraint `constraint` in the function-in-set form
as a `AbstractJuMPScalar` or `Vector{AbstractJuMPScalar}`.
"""
jump_function(constraint::AbstractConstraint) = constraint.func

"""
moi_function(constraint::AbstractConstraint)

Return the function of the constraint `constraint` in the function-in-set form
as a `MathOptInterface.AbstractFunction`.
"""
function moi_function(constraint::AbstractConstraint)
return moi_function(jump_function(constraint))
end

function moi_function(model, constraint::AbstractConstraint)
return moi_function(model, jump_function(constraint))
end

"""
moi_set(constraint::AbstractConstraint)

Expand Down Expand Up @@ -1045,11 +1023,6 @@ function check_belongs_to_model(f::Vector, model)
return
end

function moi_function(model, f)
check_belongs_to_model(f, model)
return moi_function(f)
end

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is a very weird method. It's not just a fallback. I think the only reason it was needed was because of check_belongs_to_model?

"""
add_constraint(
model::GenericModel,
Expand All @@ -1066,6 +1039,7 @@ function add_constraint(
name::String = "",
)
con = model_convert(model, con)
check_belongs_to_model(con, model)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just do it explicitly here now instead.

func, set = moi_function(model, con), moi_set(con)
# The type of backend(model) is unknown so we directly redirect to another
# function.
Expand Down
Loading
Loading