|
25 | 25 | function _test_roundtrip(src_str, relaxed_str)
|
26 | 26 | model = MOI.Utilities.Model{Float64}()
|
27 | 27 | MOI.Utilities.loadfromstring!(model, src_str)
|
28 |
| - MOI.modify(model, MOI.Utilities.PenaltyRelaxation()) |
| 28 | + map = MOI.modify(model, MOI.Utilities.PenaltyRelaxation()) |
| 29 | + for (c, v) in map |
| 30 | + @test v isa MOI.ScalarAffineFunction{Float64} |
| 31 | + end |
29 | 32 | dest = MOI.Utilities.Model{Float64}()
|
30 | 33 | MOI.Utilities.loadfromstring!(dest, relaxed_str)
|
31 | 34 | MOI.Bridges._test_structural_identical(model, dest)
|
@@ -218,11 +221,105 @@ function test_relax_quadratic_greaterthanthan()
|
218 | 221 | return
|
219 | 222 | end
|
220 | 223 |
|
221 |
| -function test_penalties() |
| 224 | +function test_penalty_dict() |
| 225 | + model = MOI.Utilities.Model{Float64}() |
| 226 | + x = MOI.add_variable(model) |
| 227 | + c = MOI.add_constraint(model, 1.0 * x, MOI.EqualTo(2.0)) |
| 228 | + map = MOI.modify(model, MOI.Utilities.PenaltyRelaxation(Dict(c => 2.0))) |
| 229 | + @test map[c] isa MOI.ScalarAffineFunction{Float64} |
| 230 | + @test sprint(print, model) === """ |
| 231 | + Minimize ScalarAffineFunction{Float64}: |
| 232 | + 0.0 + 2.0 v[2] + 2.0 v[3] |
| 233 | +
|
| 234 | + Subject to: |
| 235 | +
|
| 236 | + ScalarAffineFunction{Float64}-in-EqualTo{Float64} |
| 237 | + 0.0 + 1.0 v[1] + 1.0 v[2] - 1.0 v[3] == 2.0 |
| 238 | +
|
| 239 | + VariableIndex-in-GreaterThan{Float64} |
| 240 | + v[2] >= 0.0 |
| 241 | + v[3] >= 0.0 |
| 242 | + """ |
| 243 | + return |
| 244 | +end |
| 245 | + |
| 246 | +function test_default() |
222 | 247 | model = MOI.Utilities.Model{Float64}()
|
223 | 248 | x = MOI.add_variable(model)
|
224 | 249 | c = MOI.add_constraint(model, 1.0 * x, MOI.EqualTo(2.0))
|
225 |
| - MOI.modify(model, MOI.Utilities.PenaltyRelaxation(Dict(c => 2.0))) |
| 250 | + map = MOI.modify(model, MOI.Utilities.PenaltyRelaxation(default = 2.0)) |
| 251 | + @test map[c] isa MOI.ScalarAffineFunction{Float64} |
| 252 | + @test sprint(print, model) === """ |
| 253 | + Minimize ScalarAffineFunction{Float64}: |
| 254 | + 0.0 + 2.0 v[2] + 2.0 v[3] |
| 255 | +
|
| 256 | + Subject to: |
| 257 | +
|
| 258 | + ScalarAffineFunction{Float64}-in-EqualTo{Float64} |
| 259 | + 0.0 + 1.0 v[1] + 1.0 v[2] - 1.0 v[3] == 2.0 |
| 260 | +
|
| 261 | + VariableIndex-in-GreaterThan{Float64} |
| 262 | + v[2] >= 0.0 |
| 263 | + v[3] >= 0.0 |
| 264 | + """ |
| 265 | + return |
| 266 | +end |
| 267 | + |
| 268 | +function test_default_nothing() |
| 269 | + model = MOI.Utilities.Model{Float64}() |
| 270 | + x = MOI.add_variable(model) |
| 271 | + c = MOI.add_constraint(model, 1.0 * x, MOI.EqualTo(2.0)) |
| 272 | + map = MOI.modify(model, MOI.Utilities.PenaltyRelaxation(default = nothing)) |
| 273 | + @test !haskey(map, c) |
| 274 | + @test sprint(print, model) === """ |
| 275 | + Minimize ScalarAffineFunction{Float64}: |
| 276 | + 0.0 |
| 277 | +
|
| 278 | + Subject to: |
| 279 | +
|
| 280 | + ScalarAffineFunction{Float64}-in-EqualTo{Float64} |
| 281 | + 0.0 + 1.0 v[1] == 2.0 |
| 282 | + """ |
| 283 | + return |
| 284 | +end |
| 285 | + |
| 286 | +function test_brige_optimizer() |
| 287 | + model = MOI.instantiate( |
| 288 | + MOI.Utilities.Model{Float64}; |
| 289 | + with_bridge_type = Float64, |
| 290 | + ) |
| 291 | + x = MOI.add_variable(model) |
| 292 | + c = MOI.add_constraint(model, 1.0 * x, MOI.EqualTo(2.0)) |
| 293 | + map = MOI.modify(model, MOI.Utilities.PenaltyRelaxation(default = 2.0)) |
| 294 | + @test map[c] isa MOI.ScalarAffineFunction{Float64} |
| 295 | + @test sprint(print, model) === """ |
| 296 | + Minimize ScalarAffineFunction{Float64}: |
| 297 | + 0.0 + 2.0 v[2] + 2.0 v[3] |
| 298 | +
|
| 299 | + Subject to: |
| 300 | +
|
| 301 | + ScalarAffineFunction{Float64}-in-EqualTo{Float64} |
| 302 | + 0.0 + 1.0 v[1] + 1.0 v[2] - 1.0 v[3] == 2.0 |
| 303 | +
|
| 304 | + VariableIndex-in-GreaterThan{Float64} |
| 305 | + v[2] >= 0.0 |
| 306 | + v[3] >= 0.0 |
| 307 | + """ |
| 308 | + return |
| 309 | +end |
| 310 | + |
| 311 | +function test_caching_optimizer() |
| 312 | + model = MOI.Utilities.CachingOptimizer( |
| 313 | + MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()), |
| 314 | + MOI.Bridges.full_bridge_optimizer( |
| 315 | + MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}()), |
| 316 | + Float64, |
| 317 | + ), |
| 318 | + ) |
| 319 | + x = MOI.add_variable(model) |
| 320 | + c = MOI.add_constraint(model, 1.0 * x, MOI.EqualTo(2.0)) |
| 321 | + map = MOI.modify(model, MOI.Utilities.PenaltyRelaxation(default = 2.0)) |
| 322 | + @test map[c] isa MOI.ScalarAffineFunction{Float64} |
226 | 323 | @test sprint(print, model) === """
|
227 | 324 | Minimize ScalarAffineFunction{Float64}:
|
228 | 325 | 0.0 + 2.0 v[2] + 2.0 v[3]
|
|
0 commit comments