Skip to content

Commit a5d35f1

Browse files
committed
Refactor moi_function and jump_function into separate file
1 parent 9e33fcd commit a5d35f1

8 files changed

Lines changed: 482 additions & 450 deletions

File tree

src/JuMP.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,7 @@ include("complement.jl")
13391339
include("copy.jl")
13401340
include("feasibility_checker.jl")
13411341
include("file_formats.jl")
1342+
include("jump_moi_function.jl")
13421343
include("lp_matrix_data.jl")
13431344
include("lp_sensitivity2.jl")
13441345
include("indicator.jl")

src/aff_expr.jl

Lines changed: 0 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -767,118 +767,6 @@ function MOI.ScalarAffineFunction(
767767
return MOI.ScalarAffineFunction(terms, a.constant)
768768
end
769769

770-
"""
771-
moi_function(x::AbstractJuMPScalar)
772-
moi_function(x::AbstractArray{<:AbstractJuMPScalar})
773-
774-
Given a JuMP object `x`, return the MathOptInterface equivalent.
775-
776-
See also: [`jump_function`](@ref).
777-
778-
## Example
779-
780-
```jldoctest
781-
julia> model = Model();
782-
783-
julia> @variable(model, x);
784-
785-
julia> f = 2.0 * x + 1.0
786-
2 x + 1
787-
788-
julia> moi_function(f)
789-
1.0 + 2.0 MOI.VariableIndex(1)
790-
```
791-
"""
792-
function moi_function end
793-
794-
function moi_function(x::AbstractArray{AbstractJuMPScalar})
795-
return error(
796-
"""
797-
Unable to convert an array of type `::$(typeof(x))` to an equivalent function
798-
in MathOptInterface because the array has the abstract element type
799-
`AbstractJuMPScalar`.
800-
801-
To fix this error, convert every element in the array to the same concrete
802-
element type.
803-
804-
For example, instead of:
805-
```julia
806-
model = Model();
807-
@variable(model, x);
808-
y = AbstractJuMPScalar[x, sin(x)]
809-
@objective(model, Min, y)
810-
```
811-
do
812-
```julia
813-
@objective(model, Min, convert.(NonlinearExpr, y))
814-
```
815-
""",
816-
)
817-
end
818-
819-
"""
820-
moi_function_type(::Type{T}) where {T}
821-
822-
Given a JuMP object type `T`, return the MathOptInterface equivalent.
823-
824-
See also: [`jump_function_type`](@ref).
825-
826-
## Example
827-
828-
```jldoctest
829-
julia> moi_function_type(AffExpr)
830-
MathOptInterface.ScalarAffineFunction{Float64}
831-
```
832-
"""
833-
function moi_function_type end
834-
835-
"""
836-
jump_function(model::AbstractModel, x::MOI.AbstractFunction)
837-
838-
Given an MathOptInterface object `x`, return the JuMP equivalent.
839-
840-
See also: [`moi_function`](@ref).
841-
842-
## Example
843-
844-
```jldoctest
845-
julia> model = Model();
846-
847-
julia> @variable(model, x);
848-
849-
julia> f = 2.0 * index(x) + 1.0
850-
1.0 + 2.0 MOI.VariableIndex(1)
851-
852-
julia> jump_function(model, f)
853-
2 x + 1
854-
```
855-
"""
856-
function jump_function end
857-
858-
"""
859-
jump_function_type(model::AbstractModel, ::Type{T}) where {T}
860-
861-
Given an MathOptInterface object type `T`, return the JuMP equivalent.
862-
863-
See also: [`moi_function_type`](@ref).
864-
865-
## Example
866-
867-
```jldoctest
868-
julia> model = Model();
869-
870-
julia> jump_function_type(model, MOI.ScalarAffineFunction{Float64})
871-
AffExpr (alias for GenericAffExpr{Float64, GenericVariableRef{Float64}})
872-
```
873-
"""
874-
function jump_function_type end
875-
876-
moi_function(a::GenericAffExpr) = MOI.ScalarAffineFunction(a)
877-
878-
function moi_function_type(::Type{<:GenericAffExpr{T}}) where {T}
879-
return MOI.ScalarAffineFunction{T}
880-
end
881-
882770
function GenericAffExpr{C,GenericVariableRef{T}}(
883771
m::GenericModel{T},
884772
f::MOI.ScalarAffineFunction,
@@ -894,50 +782,6 @@ function GenericAffExpr{C,GenericVariableRef{T}}(
894782
return aff
895783
end
896784

897-
function jump_function_type(
898-
::GenericModel{T},
899-
::Type{MOI.ScalarAffineFunction{C}},
900-
) where {C,T}
901-
S = promote_type(C, T)
902-
return GenericAffExpr{S,GenericVariableRef{T}}
903-
end
904-
905-
function jump_function(
906-
model::GenericModel{T},
907-
f::MOI.ScalarAffineFunction{C},
908-
) where {C,T}
909-
S = promote_type(C, T)
910-
return GenericAffExpr{S,GenericVariableRef{T}}(model, f)
911-
end
912-
913-
function jump_function_type(
914-
::GenericModel{T},
915-
::Type{MOI.VectorAffineFunction{C}},
916-
) where {C,T}
917-
S = promote_type(C, T)
918-
return Vector{GenericAffExpr{S,GenericVariableRef{T}}}
919-
end
920-
921-
function jump_function(
922-
model::GenericModel{T},
923-
f::MOI.VectorAffineFunction{C},
924-
) where {T,C}
925-
S = promote_type(C, T)
926-
ret = GenericAffExpr{S,GenericVariableRef{T}}[]
927-
for scalar_f in MOIU.eachscalar(f)
928-
g = GenericAffExpr{S,GenericVariableRef{T}}(scalar_f.constant)
929-
for t in scalar_f.terms
930-
add_to_expression!(
931-
g,
932-
t.coefficient,
933-
GenericVariableRef(model, t.variable),
934-
)
935-
end
936-
push!(ret, g)
937-
end
938-
return ret
939-
end
940-
941785
"""
942786
_fill_vaf!(
943787
terms::Vector{<:MOI.VectorAffineTerm},
@@ -984,12 +828,6 @@ function MOI.VectorAffineFunction(
984828
return MOI.VectorAffineFunction(terms, constant)
985829
end
986830

987-
moi_function(a::Vector{<:GenericAffExpr}) = MOI.VectorAffineFunction(a)
988-
989-
function moi_function_type(::Type{<:Vector{<:GenericAffExpr{T}}}) where {T}
990-
return MOI.VectorAffineFunction{T}
991-
end
992-
993831
"""
994832
_eval_as_variable(f::F, x::GenericAffExpr, args...) where {F}
995833

src/callbacks.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,13 @@ function MOI.submit(
123123
cb::MOI.LazyConstraint,
124124
con::ScalarConstraint,
125125
)
126-
return MOI.submit(backend(model), cb, moi_function(con.func), con.set)
126+
f = moi_function(model, con.func)
127+
return MOI.submit(backend(model), cb, f, con.set)
127128
end
128129

129130
function MOI.submit(model::GenericModel, cb::MOI.UserCut, con::ScalarConstraint)
130-
return MOI.submit(backend(model), cb, moi_function(con.func), con.set)
131+
f = moi_function(model, con.func)
132+
return MOI.submit(backend(model), cb, f, con.set)
131133
end
132134

133135
function MOI.submit(

src/constraints.jl

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -757,28 +757,6 @@ function add_constraint(
757757
return add_constraint(model, con.constraint, name)
758758
end
759759

760-
"""
761-
jump_function(constraint::AbstractConstraint)
762-
763-
Return the function of the constraint `constraint` in the function-in-set form
764-
as a `AbstractJuMPScalar` or `Vector{AbstractJuMPScalar}`.
765-
"""
766-
jump_function(constraint::AbstractConstraint) = constraint.func
767-
768-
"""
769-
moi_function(constraint::AbstractConstraint)
770-
771-
Return the function of the constraint `constraint` in the function-in-set form
772-
as a `MathOptInterface.AbstractFunction`.
773-
"""
774-
function moi_function(constraint::AbstractConstraint)
775-
return moi_function(jump_function(constraint))
776-
end
777-
778-
function moi_function(model, constraint::AbstractConstraint)
779-
return moi_function(model, jump_function(constraint))
780-
end
781-
782760
"""
783761
moi_set(constraint::AbstractConstraint)
784762
@@ -1045,11 +1023,6 @@ function check_belongs_to_model(f::Vector, model)
10451023
return
10461024
end
10471025

1048-
function moi_function(model, f)
1049-
check_belongs_to_model(f, model)
1050-
return moi_function(f)
1051-
end
1052-
10531026
"""
10541027
add_constraint(
10551028
model::GenericModel,
@@ -1066,6 +1039,7 @@ function add_constraint(
10661039
name::String = "",
10671040
)
10681041
con = model_convert(model, con)
1042+
check_belongs_to_model(con, model)
10691043
func, set = moi_function(model, con), moi_set(con)
10701044
# The type of backend(model) is unknown so we directly redirect to another
10711045
# function.

0 commit comments

Comments
 (0)