Skip to content

Commit 046d1dc

Browse files
authored
Refactor to use MOI.Nonlinear (#2955)
1 parent 8ce8452 commit 046d1dc

31 files changed

+1004
-6009
lines changed

Project.toml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,18 @@ repo = "https://github.com/jump-dev/JuMP.jl.git"
44
version = "1.1.1"
55

66
[deps]
7-
Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
8-
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
9-
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
107
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
118
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
129
MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
13-
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
1410
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
1511
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1612
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
17-
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
1813

1914
[compat]
20-
Calculus = "0.5"
21-
DataStructures = "0.18"
22-
ForwardDiff = "~0.5.0, ~0.6, ~0.7, ~0.8, ~0.9, ~0.10"
2315
JSON = "0.21"
24-
MathOptInterface = "1.1.1"
16+
MathOptInterface = "1.3.0"
2517
MutableArithmetics = "1"
26-
NaNMath = "0.3, 1"
2718
OrderedCollections = "1"
28-
SpecialFunctions = "0.8, 1, 2"
2919
julia = "1.6"
3020

3121
[extras]

docs/src/manual/nlp.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ julia> @NLconstraint(model, *(x...) <= 1.0)
245245
x[1] * x[2] * x[3] - 1.0 ≤ 0
246246
247247
julia> @NLconstraint(model, *((x / 2)...) <= 0.0)
248-
ERROR: LoadError: Unexpected expression in (*)(x / 2...). JuMP supports splatting only symbols. For example, x... is ok, but (x + 1)..., [x; y]... and g(f(y)...) are not.
248+
ERROR: Unsupported use of the splatting operator. JuMP supports splatting only symbols. For example, `x...` is ok, but `(x + 1)...`, `[x; y]...` and `g(f(y)...)` are not.
249249
```
250250

251251
## User-defined Functions
@@ -531,8 +531,7 @@ The [`NLPEvaluator`](@ref) *does not evaluate derivatives of linear or quadratic
531531
constraints or objectives*.
532532

533533
The [`index`](@ref) method applied to a nonlinear constraint reference object
534-
returns its index as a [`NonlinearConstraintIndex`](@ref). The `.value` field of
535-
[`NonlinearConstraintIndex`](@ref) stores the raw integer index. For example:
534+
returns its index as a [`MOI.Nonlinear.ConstraintIndex`](@ref). For example:
536535

537536
```jldoctest
538537
julia> model = Model();
@@ -544,13 +543,13 @@ julia> @NLconstraint(model, cons1, sin(x) <= 1);
544543
julia> @NLconstraint(model, cons2, x + 5 == 10);
545544
546545
julia> typeof(cons1)
547-
NonlinearConstraintRef{ScalarShape} (alias for ConstraintRef{Model, NonlinearConstraintIndex, ScalarShape})
546+
NonlinearConstraintRef{ScalarShape} (alias for ConstraintRef{Model, MathOptInterface.Nonlinear.ConstraintIndex, ScalarShape})
548547
549548
julia> index(cons1)
550-
NonlinearConstraintIndex(1)
549+
MathOptInterface.Nonlinear.ConstraintIndex(1)
551550
552551
julia> index(cons2)
553-
NonlinearConstraintIndex(2)
552+
MathOptInterface.Nonlinear.ConstraintIndex(2)
554553
```
555554

556555
```@meta

docs/src/reference/nlp.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
More information can be found in the [Nonlinear Modeling](@ref) section of the
44
manual.
55

6+
## [Models](@id ref_nl_models)
7+
8+
9+
```@docs
10+
nonlinear_model
11+
```
12+
613
## [Constraints](@id ref_nl_constraints)
714

815
```@docs
916
@NLconstraint
1017
@NLconstraints
11-
NonlinearConstraintIndex
1218
num_nonlinear_constraints
1319
add_nonlinear_constraint
1420
all_nonlinear_constraints

src/JuMP.jl

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,12 @@ Shorthand for the MathOptInterface.Bridges package.
4646
"""
4747
const MOIB = MOI.Bridges
4848

49-
import Calculus
5049
import OrderedCollections.OrderedDict
51-
import ForwardDiff
52-
include("_Derivatives/_Derivatives.jl")
53-
using ._Derivatives
5450

5551
include("Containers/Containers.jl")
5652

5753
# Exports are at the end of the file.
5854

59-
include("utils.jl")
60-
6155
const _MOIVAR = MOI.VariableIndex
6256
const _MOICON{F,S} = MOI.ConstraintIndex{F,S}
6357

@@ -149,7 +143,7 @@ mutable struct Model <: AbstractModel
149143
# where kwargs get passed along to subsequent solve calls.
150144
optimize_hook::Any
151145
# TODO: Document.
152-
nlp_data::Any
146+
nlp_model::Union{Nothing,MOI.Nonlinear.Model}
153147
# Dictionary from variable and constraint names to objects.
154148
obj_dict::Dict{Symbol,Any}
155149
# Number of times we add large expressions. Incremented and checked by
@@ -166,6 +160,23 @@ mutable struct Model <: AbstractModel
166160
set_string_names_on_creation::Bool
167161
end
168162

163+
function Base.getproperty(model::Model, name::Symbol)
164+
if name == :nlp_data
165+
error(
166+
"The internal field `.nlp_data` was removed from `Model` in JuMP " *
167+
"v.1.2.0. If you encountered this message without going " *
168+
"`model.nlp_data`, it means you are using a package that is " *
169+
"incompatible with your installed version of JuMP. As a " *
170+
"temporary fix, install a compatible version with " *
171+
"`import Pkg; Pkg.pkg\"add [email protected]\"`, then restart Julia for " *
172+
"the changes to take effect. In addition, you should open a " *
173+
"GitHub issue for the package you are using so that the issue " *
174+
"can be fixed for future users.",
175+
)
176+
end
177+
return getfield(model, name)
178+
end
179+
169180
"""
170181
Model()
171182
@@ -590,7 +601,7 @@ function Base.empty!(model::Model)::Model
590601
# message (so keeping it helps to discover inefficiencies).
591602
MOI.empty!(model.moi_backend)
592603
empty!(model.shapes)
593-
model.nlp_data = nothing
604+
model.nlp_model = nothing
594605
empty!(model.obj_dict)
595606
empty!(model.ext)
596607
model.is_model_dirty = false
@@ -607,7 +618,7 @@ apart from optimizer attributes.
607618
function Base.isempty(model::Model)
608619
MOI.is_empty(model.moi_backend) || return false
609620
isempty(model.shapes) || return false
610-
model.nlp_data === nothing || return false
621+
model.nlp_model === nothing || return false
611622
isempty(model.obj_dict) && isempty(model.ext) || return false
612623
return !model.is_model_dirty
613624
end
@@ -619,15 +630,6 @@ Returns number of variables in `model`.
619630
"""
620631
num_variables(model::Model)::Int64 = MOI.get(model, MOI.NumberOfVariables())
621632

622-
"""
623-
num_nonlinear_constraints(model::Model)
624-
625-
Returns the number of nonlinear constraints associated with the `model`.
626-
"""
627-
function num_nonlinear_constraints(model::Model)
628-
return model.nlp_data !== nothing ? length(model.nlp_data.nlconstr) : 0
629-
end
630-
631633
"""
632634
object_dictionary(model::Model)
633635
@@ -1346,35 +1348,11 @@ function operator_warn(model::Model)
13461348
end
13471349
end
13481350

1349-
"""
1350-
NonlinearExpression <: AbstractJuMPScalar
1351-
1352-
A struct to represent a nonlinear expression.
1353-
1354-
Create an expression using [`@NLexpression`](@ref).
1355-
"""
1356-
struct NonlinearExpression <: AbstractJuMPScalar
1357-
model::Model
1358-
index::Int
1359-
end
1360-
1361-
"""
1362-
NonlinearParameter <: AbstractJuMPScalar
1363-
1364-
A struct to represent a nonlinear parameter.
1365-
1366-
Create a parameter using [`@NLparameter`](@ref).
1367-
"""
1368-
struct NonlinearParameter <: AbstractJuMPScalar
1369-
model::Model
1370-
index::Int
1371-
end
1372-
13731351
include("copy.jl")
1352+
include("nlp.jl")
13741353
include("operators.jl")
13751354
include("macros.jl")
13761355
include("optimizer_interface.jl")
1377-
include("nlp.jl")
13781356
include("print.jl")
13791357
include("solution_summary.jl")
13801358
include("lp_sensitivity2.jl")

src/_Derivatives/_Derivatives.jl

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)