-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: add a replacement for Symbolics.jl #39
base: master
Are you sure you want to change the base?
Conversation
Can we also update the package deps in this PR? |
I did in #38. I'll tag a release |
Is this still required? https://github.com/lanl-ansi/MathOptSymbolicAD.jl/blob/od/symbolic/Project.toml#L14 |
Hopefully by the time I finish this PR, no. It's still a WIP |
Welcome to Codecov 🎉Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests. Thanks for integrating Codecov - We've got you covered ☂️ |
import MathOptInterface as MOI
import MathOptSymbolicAD
function _expr_to_symbolics(expr::MathOptSymbolicAD._Node)
if expr.head == MathOptSymbolicAD._OP_COEFFICIENT
return MOI.VariableIndex(-expr.index)
elseif expr.head == MathOptSymbolicAD._OP_VARIABLE
return MOI.VariableIndex(expr.index)
elseif expr.head == MathOptSymbolicAD._OP_OPERATION
return MOI.ScalarNonlinearFunction(
expr.operation,
Any[_expr_to_symbolics(c) for c in expr.children],
)
else
@assert expr.head == MathOptSymbolicAD._OP_INTEGER_EXPONENT
return MOI.ScalarNonlinearFunction(
:^,
Any[_expr_to_symbolics(expr.children[1]), expr.index],
)
end
end
x = MOI.VariableIndex.(1:4)
model = MOI.Nonlinear.Model()
for i in 1:3
MOI.Nonlinear.add_constraint(model, :($(x[4]) * log($(x[i]) + $i)), MOI.LessThan(2.0))
end
variable_order = Dict(xi.value => i for (i, xi) in enumerate(x))
functions = map(values(model.constraints)) do c
expr = MathOptSymbolicAD._to_expr(model, c.expression, variable_order, Any[])
return MathOptSymbolicAD._Function(model, expr)
end
f = first(functions)
g = _expr_to_symbolics(f.expr)
x, ∇f, H, ∇²f = MathOptSymbolicAD.gradient_and_hessian(x -> x.value > 0, g)
julia> x
2-element Vector{MathOptInterface.VariableIndex}:
MOI.VariableIndex(1)
MOI.VariableIndex(2)
julia> ∇f
2-element Vector{Any}:
log(+(MOI.VariableIndex(2), MOI.VariableIndex(-1)))
*(MOI.VariableIndex(1), /((1), +(MOI.VariableIndex(2), MOI.VariableIndex(-1))))
julia> H
2-element Vector{Tuple{Int64, Int64}}:
(1, 2)
(2, 2)
julia> ∇²f
2-element Vector{Any}:
/((1), +(MOI.VariableIndex(2), MOI.VariableIndex(-1)))
*(MOI.VariableIndex(1), /((-1), ^(+(MOI.VariableIndex(2), MOI.VariableIndex(-1)), (2)))) |
x-ref jump-dev/MathOptInterface.jl#2533