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