Skip to content

Commit

Permalink
[Nonlinear] add support for is_empty and empty\!
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Oct 8, 2023
1 parent 7914147 commit 3f78ad4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Nonlinear/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@
# 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 === 0
return

Check warning on line 25 in src/Nonlinear/model.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/model.jl#L25

Added line #L25 was not covered by tests
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 3f78ad4

Please sign in to comment.