From e8f4e95673909da90110632c52a2867b8a2d4ad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 23 Feb 2022 15:03:58 -0500 Subject: [PATCH 1/5] Throw DeleteNotAllowed when deleted constraint index of bridged variables --- src/Bridges/bridge_optimizer.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Bridges/bridge_optimizer.jl b/src/Bridges/bridge_optimizer.jl index a627f4cc0d..28b02ad305 100644 --- a/src/Bridges/bridge_optimizer.jl +++ b/src/Bridges/bridge_optimizer.jl @@ -569,11 +569,11 @@ function MOI.delete(b::AbstractBridgeOptimizer, ci::MOI.ConstraintIndex) MOI.throw_if_not_valid(b, ci) br = bridge(b, ci) if is_variable_bridged(b, ci) - error( + throw(MOI.DeleteNotAllowed(ci, string( "Cannot delete constraint index of bridged constrained", " variables. Delete the scalar variable or the vector of", " variables instead.", - ) + ))) else delete!(Constraint.bridges(b), ci) end From aac3cda7129e25812cf909f64524b7be1cfa3cab Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 24 Feb 2022 09:15:42 +1300 Subject: [PATCH 2/5] Update bridge_optimizer.jl --- src/Bridges/bridge_optimizer.jl | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Bridges/bridge_optimizer.jl b/src/Bridges/bridge_optimizer.jl index 28b02ad305..a961bb4216 100644 --- a/src/Bridges/bridge_optimizer.jl +++ b/src/Bridges/bridge_optimizer.jl @@ -569,11 +569,14 @@ function MOI.delete(b::AbstractBridgeOptimizer, ci::MOI.ConstraintIndex) MOI.throw_if_not_valid(b, ci) br = bridge(b, ci) if is_variable_bridged(b, ci) - throw(MOI.DeleteNotAllowed(ci, string( - "Cannot delete constraint index of bridged constrained", - " variables. Delete the scalar variable or the vector of", - " variables instead.", - ))) + throw( + MOI.DeleteNotAllowed( + ci, + "Cannot delete constraint index of bridged constrained " * + "variables. Delete the scalar variable or the vector of " * + "variables instead.", + ), + ) else delete!(Constraint.bridges(b), ci) end From 179b1c7c2af13cd8cbef0a7026b84f52a08fe35b Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 24 Feb 2022 09:51:44 +1300 Subject: [PATCH 3/5] Update src/Bridges/bridge_optimizer.jl --- src/Bridges/bridge_optimizer.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bridges/bridge_optimizer.jl b/src/Bridges/bridge_optimizer.jl index a961bb4216..3c5dde6e82 100644 --- a/src/Bridges/bridge_optimizer.jl +++ b/src/Bridges/bridge_optimizer.jl @@ -571,7 +571,7 @@ function MOI.delete(b::AbstractBridgeOptimizer, ci::MOI.ConstraintIndex) if is_variable_bridged(b, ci) throw( MOI.DeleteNotAllowed( - ci, + ci, "Cannot delete constraint index of bridged constrained " * "variables. Delete the scalar variable or the vector of " * "variables instead.", From bc02e3fa9930f2727738543242577e4b856cc185 Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 24 Feb 2022 10:35:15 +1300 Subject: [PATCH 4/5] Fix --- perf/large_gc_pressure.jl | 96 ++++++++++++++++++++++++++++++++++ test/Bridges/Variable/zeros.jl | 7 +-- 2 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 perf/large_gc_pressure.jl diff --git a/perf/large_gc_pressure.jl b/perf/large_gc_pressure.jl new file mode 100644 index 0000000000..6d6d206e5b --- /dev/null +++ b/perf/large_gc_pressure.jl @@ -0,0 +1,96 @@ +using Revise +using ProfileView +using BenchmarkTools +using MathOptInterface +const MOI = MathOptInterface +using GLPK + +function create_model(I, T = 10000) + model = MOI.Utilities.CachingOptimizer( + MOI.Utilities.Model{Float64}(), + GLPK.Optimizer(), + ) + v = MOI.VariableIndex[] + for _ in 1:I + x = MOI.add_variables(model, T) + MOI.add_constraint.(model, x, MOI.GreaterThan(0.0)) + MOI.add_constraint.(model, x, MOI.LessThan(100.0)) + for t in 2:T + f = MOI.ScalarAffineFunction( + MOI.ScalarAffineTerm.([1.0, -1.0], [x[t], x[t-1]]), + 0.0, + ) + MOI.add_constraint(model, f, MOI.GreaterThan(-10.0)) + MOI.add_constraint(model, f, MOI.LessThan(10.0)) + end + append!(v, x) + end + g = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(1.0, v), 0.0) + MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE) + MOI.set(model, MOI.ObjectiveFunction{typeof(g)}(), g) + MOI.Utilities.attach_optimizer(model) + return model +end + +using Revise +using JuMP, BenchmarkTools, GLPK + +function create_model(I, T = 10000) + model = Model(GLPK.Optimizer) + @variable(model, 0 <= x[1:I, 1:T] <= 100) + @constraint(model, [i in 1:I, t in 2:T], x[i, t] - x[i, t-1] <= 10) + @constraint(model, [i in 1:I, t in 2:T], x[i, t] - x[i, t-1] >= -10) + @objective(model, Min, sum(x)) + return model +end +@benchmark create_model(100, 1000) + +function create_model_2(I, T = 10000) + model = Model(GLPK.Optimizer) + x = @variable(model, [1:I, 1:T], lower_bound = 0, upper_bound = 100) + @constraint(model, [i in 1:I, t in 2:T], x[i, t] - x[i, t-1] <= 10) + @constraint(model, [i in 1:I, t in 2:T], x[i, t] - x[i, t-1] >= -10) + @objective(model, Min, sum(x)) + return model +end +@benchmark create_model_2(100, 1000) + + +using ProfileView +@profview create_model(100, 1000) + + +using Revise +using JuMP, BenchmarkTools, GLPK + +function variables(I, T) + model = Model() + @variable(model, [1:I, 1:T], lower_bound = 0, upper_bound = 100) + return model +end + +@benchmark variables(1000, 10000) + +function variables_with_names(I, T) + model = Model() + @variable(model, 0 <= x[1:I, 1:T] <= 100) + return model +end + +@profview variables_with_names(100, 1000) + + +function test_names(N) + x = Dict{Int,String}() + for i in 1:N + x[i] = string("x[", i, "]") + end + return x +end + +function test_names_jump(N) + model = Model() + @variable(model, x[1:N]) + return model +end + diff --git a/test/Bridges/Variable/zeros.jl b/test/Bridges/Variable/zeros.jl index 3e1662eecd..a858eb82e4 100644 --- a/test/Bridges/Variable/zeros.jl +++ b/test/Bridges/Variable/zeros.jl @@ -72,9 +72,10 @@ function test_zeros() MOI.Bridges.IndexInVector(2), ) === nothing - err = ErrorException( - "Cannot delete constraint index of bridged constrained variables. Delete" * - " the scalar variable or the vector of variables instead.", + err = MOI.DeleteNotAllowed( + cyz, + "Cannot delete constraint index of bridged constrained variables. " * + "Delete the scalar variable or the vector of variables instead.", ) @test_throws err MOI.delete(bridged_mock, cyz) From b815fc163228bef80825e939ae10b310361b2b24 Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 24 Feb 2022 10:36:17 +1300 Subject: [PATCH 5/5] Remove perf file --- perf/large_gc_pressure.jl | 96 --------------------------------------- 1 file changed, 96 deletions(-) delete mode 100644 perf/large_gc_pressure.jl diff --git a/perf/large_gc_pressure.jl b/perf/large_gc_pressure.jl deleted file mode 100644 index 6d6d206e5b..0000000000 --- a/perf/large_gc_pressure.jl +++ /dev/null @@ -1,96 +0,0 @@ -using Revise -using ProfileView -using BenchmarkTools -using MathOptInterface -const MOI = MathOptInterface -using GLPK - -function create_model(I, T = 10000) - model = MOI.Utilities.CachingOptimizer( - MOI.Utilities.Model{Float64}(), - GLPK.Optimizer(), - ) - v = MOI.VariableIndex[] - for _ in 1:I - x = MOI.add_variables(model, T) - MOI.add_constraint.(model, x, MOI.GreaterThan(0.0)) - MOI.add_constraint.(model, x, MOI.LessThan(100.0)) - for t in 2:T - f = MOI.ScalarAffineFunction( - MOI.ScalarAffineTerm.([1.0, -1.0], [x[t], x[t-1]]), - 0.0, - ) - MOI.add_constraint(model, f, MOI.GreaterThan(-10.0)) - MOI.add_constraint(model, f, MOI.LessThan(10.0)) - end - append!(v, x) - end - g = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(1.0, v), 0.0) - MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE) - MOI.set(model, MOI.ObjectiveFunction{typeof(g)}(), g) - MOI.Utilities.attach_optimizer(model) - return model -end - -using Revise -using JuMP, BenchmarkTools, GLPK - -function create_model(I, T = 10000) - model = Model(GLPK.Optimizer) - @variable(model, 0 <= x[1:I, 1:T] <= 100) - @constraint(model, [i in 1:I, t in 2:T], x[i, t] - x[i, t-1] <= 10) - @constraint(model, [i in 1:I, t in 2:T], x[i, t] - x[i, t-1] >= -10) - @objective(model, Min, sum(x)) - return model -end -@benchmark create_model(100, 1000) - -function create_model_2(I, T = 10000) - model = Model(GLPK.Optimizer) - x = @variable(model, [1:I, 1:T], lower_bound = 0, upper_bound = 100) - @constraint(model, [i in 1:I, t in 2:T], x[i, t] - x[i, t-1] <= 10) - @constraint(model, [i in 1:I, t in 2:T], x[i, t] - x[i, t-1] >= -10) - @objective(model, Min, sum(x)) - return model -end -@benchmark create_model_2(100, 1000) - - -using ProfileView -@profview create_model(100, 1000) - - -using Revise -using JuMP, BenchmarkTools, GLPK - -function variables(I, T) - model = Model() - @variable(model, [1:I, 1:T], lower_bound = 0, upper_bound = 100) - return model -end - -@benchmark variables(1000, 10000) - -function variables_with_names(I, T) - model = Model() - @variable(model, 0 <= x[1:I, 1:T] <= 100) - return model -end - -@profview variables_with_names(100, 1000) - - -function test_names(N) - x = Dict{Int,String}() - for i in 1:N - x[i] = string("x[", i, "]") - end - return x -end - -function test_names_jump(N) - model = Model() - @variable(model, x[1:N]) - return model -end -