Skip to content

Commit fd66aea

Browse files
authored
Add delete for nonlinear constraints (#3013)
1 parent 046d1dc commit fd66aea

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/nlp.jl

+12
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,18 @@ function is_valid(model::Model, c::NonlinearConstraintRef)
456456
return MOI.is_valid(model.nlp_model, index)
457457
end
458458

459+
"""
460+
delete(model::Model, c::NonlinearConstraintRef)
461+
462+
Delete the nonlinear constraint `c` from `model`.
463+
"""
464+
function delete(model::Model, c::NonlinearConstraintRef)
465+
_init_NLP(model)
466+
index = MOI.Nonlinear.ConstraintIndex(c.index.value)
467+
MOI.Nonlinear.delete(model.nlp_model, index)
468+
return
469+
end
470+
459471
"""
460472
num_nonlinear_constraints(model::Model)
461473

test/nlp.jl

+14
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,20 @@ function test_nonlinear_constraint_dual()
14061406
return
14071407
end
14081408

1409+
function test_nonlinear_delete_constraint()
1410+
model = Model()
1411+
@variable(model, x)
1412+
@NLconstraint(model, c[i = 1:3], x^i <= i)
1413+
@test num_nonlinear_constraints(model) == 3
1414+
@test all_nonlinear_constraints(model) == [c[1], c[2], c[3]]
1415+
@test is_valid.(model, c) == [true, true, true]
1416+
delete(model, c[2])
1417+
@test num_nonlinear_constraints(model) == 2
1418+
@test all_nonlinear_constraints(model) == [c[1], c[3]]
1419+
@test is_valid.(model, c) == [true, false, true]
1420+
return
1421+
end
1422+
14091423
end
14101424

14111425
TestNLP.runtests()

0 commit comments

Comments
 (0)