Skip to content

Commit

Permalink
[Nonlinear] add support for is_empty and empty! (#2305)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Oct 9, 2023
1 parent 33aae24 commit 09c1bff
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Nonlinear/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.

function MOI.empty!(model::Model)
model.objective = nothing
empty!(model.expressions)
empty!(model.constraints)
empty!(model.parameters)
model.operators = OperatorRegistry()
model.last_constraint_index = 0
return
end

function MOI.is_empty(model::Model)
return model.objective === nothing &&
isempty(model.expressions) &&
isempty(model.constraints) &&
isempty(model.parameters) &&
isempty(model.operators.registered_univariate_operators) &&
isempty(model.operators.registered_multivariate_operators) &&
model.last_constraint_index === Int64(0)
end

function Base.copy(::Model)
return error("Copying nonlinear problems not yet implemented")
end
Expand Down
28 changes: 28 additions & 0 deletions test/Nonlinear/Nonlinear.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,34 @@ function test_parse_unsupported_operator()
return
end

function test_is_empty()
model = MOI.Nonlinear.Model()
@test MOI.is_empty(model)
x = MOI.VariableIndex(1)
Nonlinear.set_objective(model, :(log($x)))
@test !MOI.is_empty(model)
MOI.empty!(model)
@test MOI.is_empty(model)
Nonlinear.add_constraint(model, :(log($x)), MOI.GreaterThan(1.0))
@test !MOI.is_empty(model)
MOI.empty!(model)
@test MOI.is_empty(model)
Nonlinear.add_expression(model, :(sin($x)^2))
@test !MOI.is_empty(model)
MOI.empty!(model)
@test MOI.is_empty(model)
Nonlinear.add_parameter(model, 1.2)
@test !MOI.is_empty(model)
MOI.empty!(model)
@test MOI.is_empty(model)
f(x) = log(x + 1)
Nonlinear.register_operator(model, :f, 1, f)
@test !MOI.is_empty(model)
MOI.empty!(model)
@test MOI.is_empty(model)
return
end

end

TestNonlinear.runtests()
Expand Down

0 comments on commit 09c1bff

Please sign in to comment.