Skip to content

Commit e52a787

Browse files
committed
Update
1 parent 6a746b0 commit e52a787

File tree

5 files changed

+44
-24
lines changed

5 files changed

+44
-24
lines changed

docs/src/submodules/Nonlinear/reference.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Nonlinear.Evaluator
7272
Nonlinear.AbstractAutomaticDifferentiation
7373
Nonlinear.ExprGraphOnly
7474
Nonlinear.SparseReverseMode
75+
Nonlinear.SymbolicMode
7576
```
7677

7778
## Data-structure
@@ -86,3 +87,13 @@ Nonlinear.parse_expression
8687
Nonlinear.convert_to_expr
8788
Nonlinear.ordinal_index
8889
```
90+
91+
## SymbolicAD
92+
93+
```@docs
94+
Nonlinear.SymbolicAD.simplify
95+
Nonlinear.SymbolicAD.simplify!
96+
Nonlinear.SymbolicAD.variables
97+
Nonlinear.SymbolicAD.derivative
98+
Nonlinear.SymbolicAD.gradient_and_hessian
99+
```

src/Nonlinear/Nonlinear.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ include("model.jl")
4848
include("evaluator.jl")
4949

5050
include("ReverseAD/ReverseAD.jl")
51+
include("SymbolicAD/SymbolicAD.jl")
5152

5253
end # module

src/Nonlinear/SymbolicAD/SymbolicAD.jl

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Return a simplified copy of the function `f`.
2222
simplify(f) = simplify!(copy(f))
2323

2424
###
25-
### simplify!
25+
### `simplify!`
2626
###
2727

2828
"""
@@ -468,7 +468,7 @@ end
468468
"""
469469
__DERIVATIVE__ = "__DERIVATIVE__"
470470
471-
This constant is is prefixed to the name of univariate operators to indicate
471+
This constant is prefixed to the name of univariate operators to indicate
472472
that we should compute their derivative.
473473
474474
If `f.head` is itself a __DERIVATIVE__, then this will create a second
@@ -1129,14 +1129,6 @@ end
11291129
### MOI.AbstractNLPEvaluator
11301130
###
11311131

1132-
"""
1133-
SymbolicMode() <: MOI.Nonlinear.AbstractAutomaticDifferentiation
1134-
1135-
A type for setting as the value of the `MOI.AutomaticDifferentiationBackend()`
1136-
attribute to enable symbolic automatic differentiation.
1137-
"""
1138-
struct SymbolicMode <: MOI.Nonlinear.AbstractAutomaticDifferentiation end
1139-
11401132
struct Evaluator <: MOI.AbstractNLPEvaluator
11411133
dag::Dict{UInt64,_DAG}
11421134
H::Dict{UInt64,Vector{Tuple{Int64,Int64}}}
@@ -1252,9 +1244,8 @@ function _to_symbolic_form(
12521244
)
12531245
end
12541246

1255-
function MOI.Nonlinear.Evaluator(
1247+
function Evaluator(
12561248
model::MOI.Nonlinear.Model,
1257-
::SymbolicMode,
12581249
ordered_variables::Vector{MOI.VariableIndex},
12591250
)
12601251
variable_to_column =
@@ -1308,15 +1299,14 @@ function MOI.Nonlinear.Evaluator(
13081299
push!(constraint_index_by_hash[c_sym.hash], length(constraints))
13091300
end
13101301
end
1311-
backend = Evaluator(
1302+
return Evaluator(
13121303
hash_to_dag,
13131304
hash_to_H,
13141305
objective,
13151306
constraints,
13161307
constraint_index_by_hash,
13171308
Float64[],
13181309
)
1319-
return MOI.Nonlinear.Evaluator(model, backend)
13201310
end
13211311

13221312
function _evaluate!(model::Evaluator, x)
@@ -1428,7 +1418,7 @@ function MOI.eval_hessian_lagrangian(model::Evaluator, H, x, σ, μ)
14281418
Threads.@threads for i in eachindex(model.constraints)
14291419
c = model.constraints[i]
14301420
# It is important that `offset_c` does not shadow `offset` from the
1431-
# objective block! If we use the same name, Julia creates a Core.Box
1421+
# objective block. If we use the same name, Julia creates a Core.Box
14321422
offset_c = c.hess_offset - length(c.x) - 1
14331423
for j in (2+length(c.x)):length(c.result)
14341424
H[offset_c+j] = μ[i] * c.result[j]

src/Nonlinear/types.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,19 @@ function Evaluator(
291291
)
292292
return Evaluator(model, ReverseAD.NLPEvaluator(model, ordered_variables))
293293
end
294+
295+
"""
296+
SymbolicMode() <: AbstractAutomaticDifferentiation
297+
298+
A type for setting as the value of the `MOI.AutomaticDifferentiationBackend()`
299+
attribute to enable symbolic automatic differentiation.
300+
"""
301+
struct SymbolicMode <: AbstractAutomaticDifferentiation end
302+
303+
function Evaluator(
304+
model::Model,
305+
::SymbolicMode,
306+
ordered_variables::Vector{MOI.VariableIndex},
307+
)
308+
return Evaluator(model, SymbolicAD.Evaluator(model, ordered_variables))
309+
end

test/Nonlinear/SymbolicAD.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ module TestMathOptSymbolicAD
88
using Test
99

1010
import MathOptInterface as MOI
11-
import MathOptInterface.Nonlinear: SymbolicAD
11+
import MathOptInterface: Nonlinear
12+
13+
const SymbolicAD = Nonlinear.SymbolicAD
1214

1315
function runtests()
1416
for name in names(@__MODULE__; all = true)
@@ -503,7 +505,7 @@ function test_SymbolicAD_Evaluator()
503505
end
504506
MOI.Nonlinear.add_constraint(model, :($(x[1]) * $(x[2])), MOI.LessThan(2.0))
505507
MOI.Nonlinear.set_objective(model, :($(x[1]) * $(x[2])))
506-
evaluator = MOI.Nonlinear.Evaluator(model, SymbolicAD.SymbolicMode(), x)
508+
evaluator = MOI.Nonlinear.Evaluator(model, Nonlinear.SymbolicMode(), x)
507509
@test MOI.features_available(evaluator) == [:Grad, :Jac, :Hess, :ExprGraph]
508510
@test_throws AssertionError MOI.initialize(evaluator, [:Foo])
509511
MOI.initialize(evaluator, [:Grad, :Jac, :Hess])
@@ -542,7 +544,7 @@ function test_SymbolicAD_Evaluator_squared()
542544
x = MOI.VariableIndex(1)
543545
model = MOI.Nonlinear.Model()
544546
MOI.Nonlinear.set_objective(model, :($x^2))
545-
evaluator = MOI.Nonlinear.Evaluator(model, SymbolicAD.SymbolicMode(), [x])
547+
evaluator = MOI.Nonlinear.Evaluator(model, Nonlinear.SymbolicMode(), [x])
546548
@test MOI.features_available(evaluator) == [:Grad, :Jac, :Hess, :ExprGraph]
547549
MOI.initialize(evaluator, [:Grad, :Jac, :Hess])
548550
x = [3.0]
@@ -563,7 +565,7 @@ function test_SymbolicAD_Evaluator_ifelse()
563565
x = MOI.VariableIndex(1)
564566
model = MOI.Nonlinear.Model()
565567
MOI.Nonlinear.set_objective(model, :(ifelse($x > 0, $x, -$x)))
566-
evaluator = MOI.Nonlinear.Evaluator(model, SymbolicAD.SymbolicMode(), [x])
568+
evaluator = MOI.Nonlinear.Evaluator(model, Nonlinear.SymbolicMode(), [x])
567569
@test MOI.features_available(evaluator) == [:Grad, :Jac, :Hess, :ExprGraph]
568570
MOI.initialize(evaluator, [:Grad, :Jac, :Hess])
569571
x = [3.0]
@@ -584,7 +586,7 @@ function test_SymbolicAD_Evaluator_comparison()
584586
x = MOI.VariableIndex(1)
585587
model = MOI.Nonlinear.Model()
586588
MOI.Nonlinear.set_objective(model, :(ifelse(($x < -1) || ($x > 1), $x, 0)))
587-
evaluator = MOI.Nonlinear.Evaluator(model, SymbolicAD.SymbolicMode(), [x])
589+
evaluator = MOI.Nonlinear.Evaluator(model, Nonlinear.SymbolicMode(), [x])
588590
@test MOI.features_available(evaluator) == [:Grad, :Jac, :Hess, :ExprGraph]
589591
MOI.initialize(evaluator, [:Grad, :Jac, :Hess])
590592
x = zeros(1)
@@ -663,7 +665,7 @@ function test_SymbolicAD_show()
663665
MOI.LessThan(2.0),
664666
)
665667
end
666-
evaluator = MOI.Nonlinear.Evaluator(model, SymbolicAD.SymbolicMode(), x)
668+
evaluator = MOI.Nonlinear.Evaluator(model, Nonlinear.SymbolicMode(), x)
667669
_, dag = only(evaluator.backend.dag)
668670
contents = sprint(show, dag)
669671
@test occursin("# x[1]", contents)
@@ -680,7 +682,7 @@ function test_SymbolicAD_subexpressions()
680682
MOI.Nonlinear.add_constraint(model, expr, MOI.LessThan(2.0))
681683
@test_throws(
682684
ErrorException("Subexpressions not supported"),
683-
MOI.Nonlinear.Evaluator(model, SymbolicAD.SymbolicMode(), x),
685+
MOI.Nonlinear.Evaluator(model, Nonlinear.SymbolicMode(), x),
684686
)
685687
return
686688
end
@@ -692,7 +694,7 @@ function test_SymbolicAD_parameters()
692694
MOI.Nonlinear.add_constraint(model, :($x * $p), MOI.LessThan(2.0))
693695
@test_throws(
694696
ErrorException("Parameters not supported"),
695-
MOI.Nonlinear.Evaluator(model, SymbolicAD.SymbolicMode(), [x]),
697+
MOI.Nonlinear.Evaluator(model, Nonlinear.SymbolicMode(), [x]),
696698
)
697699
return
698700
end
@@ -745,7 +747,7 @@ function test_SymbolicAD_univariate_registered()
745747
model = MOI.Nonlinear.Model()
746748
MOI.Nonlinear.register_operator(model, :op_foo, 1, x -> x^2)
747749
MOI.Nonlinear.set_objective(model, :(op_foo($x - 0.5)))
748-
evaluator = MOI.Nonlinear.Evaluator(model, SymbolicAD.SymbolicMode(), [x])
750+
evaluator = MOI.Nonlinear.Evaluator(model, Nonlinear.SymbolicMode(), [x])
749751
MOI.initialize(evaluator, [:Grad, :Jac, :Hess])
750752
x = [1.0]
751753
@test MOI.eval_objective(evaluator, x) == 0.25

0 commit comments

Comments
 (0)