Skip to content

Commit 09c1bff

Browse files
authored
[Nonlinear] add support for is_empty and empty! (#2305)
1 parent 33aae24 commit 09c1bff

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/Nonlinear/model.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@
44
# Use of this source code is governed by an MIT-style license that can be found
55
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
66

7+
function MOI.empty!(model::Model)
8+
model.objective = nothing
9+
empty!(model.expressions)
10+
empty!(model.constraints)
11+
empty!(model.parameters)
12+
model.operators = OperatorRegistry()
13+
model.last_constraint_index = 0
14+
return
15+
end
16+
17+
function MOI.is_empty(model::Model)
18+
return model.objective === nothing &&
19+
isempty(model.expressions) &&
20+
isempty(model.constraints) &&
21+
isempty(model.parameters) &&
22+
isempty(model.operators.registered_univariate_operators) &&
23+
isempty(model.operators.registered_multivariate_operators) &&
24+
model.last_constraint_index === Int64(0)
25+
end
26+
727
function Base.copy(::Model)
828
return error("Copying nonlinear problems not yet implemented")
929
end

test/Nonlinear/Nonlinear.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,34 @@ function test_parse_unsupported_operator()
11031103
return
11041104
end
11051105

1106+
function test_is_empty()
1107+
model = MOI.Nonlinear.Model()
1108+
@test MOI.is_empty(model)
1109+
x = MOI.VariableIndex(1)
1110+
Nonlinear.set_objective(model, :(log($x)))
1111+
@test !MOI.is_empty(model)
1112+
MOI.empty!(model)
1113+
@test MOI.is_empty(model)
1114+
Nonlinear.add_constraint(model, :(log($x)), MOI.GreaterThan(1.0))
1115+
@test !MOI.is_empty(model)
1116+
MOI.empty!(model)
1117+
@test MOI.is_empty(model)
1118+
Nonlinear.add_expression(model, :(sin($x)^2))
1119+
@test !MOI.is_empty(model)
1120+
MOI.empty!(model)
1121+
@test MOI.is_empty(model)
1122+
Nonlinear.add_parameter(model, 1.2)
1123+
@test !MOI.is_empty(model)
1124+
MOI.empty!(model)
1125+
@test MOI.is_empty(model)
1126+
f(x) = log(x + 1)
1127+
Nonlinear.register_operator(model, :f, 1, f)
1128+
@test !MOI.is_empty(model)
1129+
MOI.empty!(model)
1130+
@test MOI.is_empty(model)
1131+
return
1132+
end
1133+
11061134
end
11071135

11081136
TestNonlinear.runtests()

0 commit comments

Comments
 (0)