Skip to content

Implement adjacency_matrix and expression in _FunctionStorage #2750

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Nonlinear/ReverseAD/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ struct _SubexpressionStorage
end
end

function MOI.Nonlinear.expression(expr::_SubexpressionStorage)
return MOI.Nonlinear.Expression(expr.nodes, expr.const_values)
end

MOI.Nonlinear.adjacency_matrix(expr::_SubexpressionStorage) = expr.adj

struct _FunctionStorage
nodes::Vector{Nonlinear.Node}
adj::SparseArrays.SparseMatrixCSC{Bool,Int}
Expand Down Expand Up @@ -136,6 +142,12 @@ struct _FunctionStorage
end
end

function MOI.Nonlinear.expression(expr::_FunctionStorage)
return MOI.Nonlinear.Expression(expr.nodes, expr.const_values)
end

MOI.Nonlinear.adjacency_matrix(expr::_FunctionStorage) = expr.adj

"""
NLPEvaluator(
model::Nonlinear.Model,
Expand Down
5 changes: 4 additions & 1 deletion src/Nonlinear/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ tree.
struct Expression
nodes::Vector{Node}
values::Vector{Float64}
Expression() = new(Node[], Float64[])
end

Expression() = Expression(Node[], Float64[])

function Base.:(==)(x::Expression, y::Expression)
return x.nodes == y.nodes && x.values == y.values
end
Expand Down Expand Up @@ -107,6 +108,8 @@ struct Constraint
}
end

expression(c::Constraint) = c.expression

"""
ParameterIndex

Expand Down
21 changes: 21 additions & 0 deletions test/Nonlinear/ReverseAD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,27 @@ function test_hessian_reinterpret_unsafe()
return
end

function test_expression_and_adjacency_matrix()
model = Nonlinear.Model()
x = MOI.VariableIndex(1)
expr = Nonlinear.add_expression(model, :($x^2 + 1))
Nonlinear.set_objective(model, :($expr^2))
con = Nonlinear.add_constraint(model, :(2 * $expr), MOI.LessThan(1.0))
@test Nonlinear.expression(model[con]) isa Nonlinear.Expression
evaluator = Nonlinear.Evaluator(model, Nonlinear.SparseReverseMode(), [x])
MOI.initialize(evaluator, [:Grad])
d = evaluator.backend
@test Nonlinear.expression(d.objective) isa Nonlinear.Expression
A = SparseArrays.sparse([2, 3], [1, 1], Bool[1, 1], 3, 3)
@test Nonlinear.adjacency_matrix(d.objective) == A
@test Nonlinear.expression(only(d.constraints)) isa Nonlinear.Expression
@test Nonlinear.adjacency_matrix(only(d.constraints)) == A
A = SparseArrays.sparse([2, 5, 3, 4], [1, 1, 2, 2], Bool[1, 1, 1, 1], 5, 5)
@test Nonlinear.expression(only(d.subexpressions)) isa Nonlinear.Expression
@test Nonlinear.adjacency_matrix(only(d.subexpressions)) == A
return
end

end # module

TestReverseAD.runtests()
Loading