Skip to content

Commit 55a9e76

Browse files
committed
updated tests.
1 parent 8ce7338 commit 55a9e76

2 files changed

Lines changed: 70 additions & 61 deletions

File tree

src/mbm.jl

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,6 @@ function reformulate_disjunct_constraint(
8383
return new_ref_cons
8484
end
8585

86-
function reformulate_disjunct_constraint(
87-
model::JuMP.AbstractModel,
88-
con::Disjunction,
89-
bconref::Union{Dict{<:LogicalVariableRef,<:JuMP.AbstractVariableRef},
90-
Dict{<:LogicalVariableRef,<:JuMP.GenericAffExpr}},
91-
method::MBM
92-
)
93-
ref_cons = reformulate_disjunction(model, con, method)
94-
new_ref_cons = Vector{JuMP.AbstractConstraint}()
95-
for ref_con in ref_cons
96-
append!(new_ref_cons, reformulate_disjunct_constraint(model, ref_con, bconref, method))
97-
end
98-
return new_ref_cons
99-
end
100-
10186
function reformulate_disjunct_constraint(
10287
model::JuMP.AbstractModel,
10388
con::JuMP.VectorConstraint{T, S, R},

test/constraints/mbm.jl

Lines changed: 70 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -148,73 +148,75 @@ function test_reformulate_disjunct_constraint()
148148
Disjunct(Y[4]))
149149
@constraint(model, zeros, -x .+ 1 in MOI.Zeros(2), Disjunct(Y[5]))
150150

151-
M = Dict{LogicalVariableRef,Float64}(Y[i] => Float64(i) for i in 1:5)
152-
bconref = Dict{LogicalVariableRef,AbstractVariableRef}(
153-
Y[i] => binary_variable(Y[i]) for i in 1:5)
151+
method = MBM(HiGHS.Optimizer)
152+
for i in 1:5
153+
method.M[Y[i]] = Float64(i)
154+
end
155+
bconref = Dict(Y[i] => binary_variable(Y[i]) for i in 1:5)
156+
154157
reformulated_constraints = [reformulate_disjunct_constraint(model,
155-
constraint_object(constraints), bconref, M, MBM(HiGHS.Optimizer))
158+
constraint_object(constraints), bconref, method)
156159
for constraints in [lessthan, greaterthan, equalto, nonpositives,
157160
nonnegatives, zeros]]
158161
@test reformulated_constraints[1][1].func == JuMP.@expression(model,
159-
x[1] - sum(M[i] * bconref[i] for i in keys(M))) &&
162+
x[1] - sum(method.M[i] * bconref[i] for i in keys(method.M))) &&
160163
reformulated_constraints[1][1].set == MOI.LessThan(1.0)
161164
@test reformulated_constraints[2][1].func == JuMP.@expression(model,
162-
x[1] + sum(M[i] * bconref[i] for i in keys(M))) &&
165+
x[1] + sum(method.M[i] * bconref[i] for i in keys(method.M))) &&
163166
reformulated_constraints[2][1].set == MOI.GreaterThan(1.0)
164167
@test reformulated_constraints[3][1].func == JuMP.@expression(model,
165-
x[1] + sum(M[i] * bconref[i] for i in keys(M))) &&
168+
x[1] + sum(method.M[i] * bconref[i] for i in keys(method.M))) &&
166169
reformulated_constraints[3][1].set == MOI.GreaterThan(1.0)
167170
@test reformulated_constraints[3][2].func == JuMP.@expression(model,
168-
x[1] - sum(M[i] * bconref[i] for i in keys(M))) &&
171+
x[1] - sum(method.M[i] * bconref[i] for i in keys(method.M))) &&
169172
reformulated_constraints[3][2].set == MOI.LessThan(1.0)
170173
@test reformulated_constraints[4][1].func == JuMP.@expression(model,
171-
-x .- sum(M[i] * bconref[i] for i in keys(M))) &&
174+
-x .- sum(method.M[i] * bconref[i] for i in keys(method.M))) &&
172175
reformulated_constraints[4][1].set == MOI.Nonpositives(2)
173176
@test reformulated_constraints[5][1].func == JuMP.@expression(model,
174-
x .+ sum(M[i] * bconref[i] for i in keys(M))) &&
177+
x .+ sum(method.M[i] * bconref[i] for i in keys(method.M))) &&
175178
reformulated_constraints[5][1].set == MOI.Nonnegatives(2)
176179
@test reformulated_constraints[6][1].func == JuMP.@expression(model,
177-
-x .+(1 + sum(M[i] * bconref[i] for i in keys(M)))) &&
180+
-x .+(1 + sum(method.M[i] * bconref[i] for i in keys(method.M)))) &&
178181
reformulated_constraints[6][1].set == MOI.Nonnegatives(2)
179182
@test reformulated_constraints[6][2].func == JuMP.@expression(model,
180-
-x .+(1 - sum(M[i] * bconref[i] for i in keys(M)))) &&
183+
-x .+(1 - sum(method.M[i] * bconref[i] for i in keys(method.M)))) &&
181184
reformulated_constraints[6][2].set == MOI.Nonpositives(2)
182185
@test_throws ErrorException reformulate_disjunct_constraint(model,
183-
"odd", bconref, M, MBM(HiGHS.Optimizer))
186+
"odd", bconref, method)
184187
end
185188

186189
function test_reformulate_disjunct()
187190
model = GDPModel()
188-
@variable(model, 1 <= x[1:2] <= 50)
191+
@variable(model, 1 <= x[1:2] <= 5)
189192
@variable(model, Y[1:2], Logical)
190-
@constraint(model, lessthan, x[1] <= 2, Disjunct(Y[1]))
191193
@constraint(model, greaterthan, x[1] >= 1, Disjunct(Y[1]))
192-
@constraint(model, interval, x[1] == 55, Disjunct(Y[2]))
194+
@constraint(model, interval, x[1] == 2.5, Disjunct(Y[2]))
193195

194-
bconref = [binary_variable(Y[i]) for i in 1:2]
195-
reformulated_disjunct = DP._reformulate_disjunct(model,
196-
Vector{JuMP.AbstractConstraint}(),Y[1], LogicalVariableRef[Y[2]],
197-
MBM(HiGHS.Optimizer))
198-
M = [0, 1e9]
199-
@test reformulated_disjunct[1].func == JuMP.@expression(model,
200-
x[1] - sum(M[i] * bconref[i] for i in 1:length(M))) &&
201-
reformulated_disjunct[1].set == MOI.LessThan(2.0)
202-
@test reformulated_disjunct[2].func == JuMP.@expression(model,
203-
x[1] + sum(M[i] * bconref[i] for i in 1:length(M))) &&
204-
reformulated_disjunct[2].set == MOI.GreaterThan(1.0)
205-
206-
reformulated_disjunct = DP._reformulate_disjunct(model,
207-
Vector{JuMP.AbstractConstraint}(),Y[2], LogicalVariableRef[Y[1]],
208-
MBM(HiGHS.Optimizer))
209-
M = [54, 0]
196+
method = MBM(HiGHS.Optimizer)
197+
disj = constraint_object(disjunction(model, [Y[1], Y[2]]))
198+
reformulated_disjunct = reformulate_disjunction(model, disj, method)
199+
200+
@test length(reformulated_disjunct) == 3
201+
202+
@test reformulated_disjunct[1].set == MOI.GreaterThan(1.0)
203+
@test reformulated_disjunct[2].set == MOI.GreaterThan(2.5)
204+
@test reformulated_disjunct[3].set == MOI.LessThan(2.5)
210205

211-
@test reformulated_disjunct[2].func == JuMP.@expression(model,
212-
x[1] - sum(M[i] * bconref[i] for i in 1:length(M))) &&
213-
reformulated_disjunct[2].set == MOI.LessThan(55.0)
214-
@test reformulated_disjunct[1].func == JuMP.@expression(model,
215-
x[1] + sum(M[i] * bconref[i] for i in 1:length(M))) &&
216-
reformulated_disjunct[1].set == MOI.GreaterThan(55.0)
206+
# Test that the expressions have the right structure
207+
# Check coefficients and variables in the affine expressions
208+
func_1 = reformulated_disjunct[1].func
209+
func_2 = reformulated_disjunct[2].func
210+
func_3 = reformulated_disjunct[3].func
217211

212+
@test JuMP.coefficient(func_1, x[1]) == 1.0
213+
@test JuMP.coefficient(func_1, binary_variable(Y[2])) == -1.5
214+
215+
@test JuMP.coefficient(func_2, x[1]) == 1.0
216+
@test JuMP.coefficient(func_2, binary_variable(Y[1])) == 2.5
217+
218+
@test JuMP.coefficient(func_3, x[1]) == 1.0
219+
@test JuMP.coefficient(func_3, binary_variable(Y[1])) == -2.5
218220
end
219221

220222
function test_reformulate_disjunction()
@@ -225,14 +227,36 @@ function test_reformulate_disjunction()
225227
@constraint(model, greaterthan, x >= 1, Disjunct(Y[1]))
226228
@constraint(model, interval, 0 <= x <= 55, Disjunct(Y[2]))
227229
disj = disjunction(model, [Y[1], Y[2]])
228-
ref_cons = reformulate_disjunction(model, constraint_object(disj),
229-
MBM(HiGHS.Optimizer))
230-
@test ref_cons[1].func == JuMP.@expression(model,
231-
x - 53 * binary_variable(Y[2])) &&
232-
ref_cons[1].set == MOI.LessThan(2.0)
233-
@test ref_cons[2].func == JuMP.@expression(model,
234-
x + 53 * binary_variable(Y[2])) &&
235-
ref_cons[2].set == MOI.GreaterThan(1.0)
230+
231+
method = MBM(HiGHS.Optimizer)
232+
ref_cons = reformulate_disjunction(model, constraint_object(disj), method)
233+
234+
@test length(ref_cons) == 4
235+
236+
@test ref_cons[1].set == MOI.LessThan(2.0)
237+
238+
@test ref_cons[2].set == MOI.GreaterThan(1.0)
239+
240+
@test ref_cons[3].set == MOI.GreaterThan(0.0)
241+
242+
@test ref_cons[4].set == MOI.LessThan(55.0)
243+
244+
func_1 = ref_cons[1].func # x - 53 Y[2] <= 2.0
245+
func_2 = ref_cons[2].func # x + 53 Y[2] >= 1.0
246+
func_3 = ref_cons[3].func # x - Y[1] >= 0.0
247+
func_4 = ref_cons[4].func # x + Y[1] <= 55.0
248+
249+
@test JuMP.coefficient(func_1, x) == 1.0
250+
@test JuMP.coefficient(func_1, binary_variable(Y[2])) == -53.0
251+
252+
@test JuMP.coefficient(func_2, x) == 1.0
253+
@test JuMP.coefficient(func_2, binary_variable(Y[2])) == 53.0
254+
255+
@test JuMP.coefficient(func_3, x) == 1.0
256+
@test JuMP.coefficient(func_3, binary_variable(Y[1])) == -1.0
257+
258+
@test JuMP.coefficient(func_4, x) == 1.0
259+
@test JuMP.coefficient(func_4, binary_variable(Y[1])) == 1.0
236260
end
237261

238262
@testset "MBM" begin

0 commit comments

Comments
 (0)