Skip to content

Commit c7b83fd

Browse files
authored
Merge pull request #56 from JuliaStochOpt/bl/delete
Add support for deleted constraints
2 parents 8e33e5b + 4175d4c commit c7b83fd

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

src/ParameterJuMP.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,31 @@ function initialize_parameter_data(m::JuMP.Model)
287287
m.ext[:params] = ParameterData()
288288
end
289289

290+
# The constraint has been deleted. We cannot keep `dict` in sync with deletions
291+
# as we return a `JuMP.ConstraintRef`, not a custom type when the user create a
292+
# parametrized constraint.
293+
function _update_dicts_with_deletion(data::ParameterData, S::Type)
294+
dict = _get_param_dict(data, S)
295+
to_delete = eltype(keys(dict))[]
296+
for (cref, gaep) in dict
297+
if !JuMP.is_valid(cref.model, cref)
298+
push!(to_delete, cref)
299+
end
300+
end
301+
for cref in to_delete
302+
delete!(dict, cref)
303+
end
304+
end
305+
function _update_dicts_with_deletion(data::ParameterData)
306+
_update_dicts_with_deletion(data, EQ)
307+
_update_dicts_with_deletion(data, LE)
308+
_update_dicts_with_deletion(data, GE)
309+
end
290310
function parameter_optimizehook(m::JuMP.Model; kwargs...)
291311
data = _getparamdata(m)::ParameterData
292312

313+
_update_dicts_with_deletion(data)
314+
293315
# sync model rhs to newest parameter values
294316
sync(data)
295317

src/constraints.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ function delete_from_constraints(::Type{S}, p::ParameterRef) where S
369369
end
370370
end
371371
if lazy_duals(data)
372-
if !isempty(data.constraints_map[ind])
372+
if !isempty(data.constraints_map[ind])
373373
data.constraints_map[ind] = ParametrizedConstraintRef[]
374374
if !iszero(data.future_values[ind])
375375
data.sync = false

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ include("tests.jl")
2727
test13(factory)
2828
test14(factory)
2929
test15(factory)
30+
test16(factory)
3031
end
3132
;

test/tests.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,26 @@ function test15(args...)
460460
@test_expression_with_string a + b - x + y + 1.2 "y - x + a + b + 1.2"
461461
end
462462
end
463+
464+
function test16(args...)
465+
@testset "Test deletion of constraint" begin
466+
model = ModelWithParams(args...)
467+
α = add_parameter(model, -1.0)
468+
@variable(model, x)
469+
cref1 = @constraint(model, x ≤ α/2)
470+
cref2 = @constraint(model, x ≤ α)
471+
cref3 = @constraint(model, x ≤ 2α)
472+
@objective(model, Max, x)
473+
JuMP.delete(model, cref3)
474+
JuMP.optimize!(model)
475+
@test JuMP.value(x) == -1.0
476+
@test JuMP.dual(cref1) == 0.0
477+
@test JuMP.dual(cref2) == -1.0
478+
@test JuMP.dual(α) == -1.0
479+
JuMP.delete(model, cref2)
480+
JuMP.optimize!(model)
481+
@test JuMP.value(x) == -0.5
482+
@test JuMP.dual(cref1) == -1.0
483+
@test JuMP.dual(α) == -0.5
484+
end
485+
end

0 commit comments

Comments
 (0)