Skip to content
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

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open

WIP: add a replacement for Symbolics.jl #39

wants to merge 16 commits into from

Conversation

odow
Copy link
Collaborator

@odow odow commented Sep 11, 2024

@ccoffrin
Copy link
Member

Can we also update the package deps in this PR?

@odow
Copy link
Collaborator Author

odow commented Sep 17, 2024

I did in #38. I'll tag a release

@ccoffrin
Copy link
Member

@odow
Copy link
Collaborator Author

odow commented Sep 17, 2024

Hopefully by the time I finish this PR, no. It's still a WIP

Project.toml Outdated Show resolved Hide resolved
Copy link

codecov bot commented Jan 16, 2025

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 ☂️

@odow
Copy link
Collaborator Author

odow commented Jan 17, 2025

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))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants