|
| 1 | +# Copyright (c) 2022 MiniZinc.jl contributors |
| 2 | +# |
| 3 | +# Use of this source code is governed by an MIT-style license that can be found |
| 4 | +# in the LICENSE.md file or at https://opensource.org/licenses/MIT. |
| 5 | + |
| 6 | +# Inspired from the square packing tutorial in https://www.minizinc.org/ |
| 7 | +function test_packing() |
| 8 | + n = 6 |
| 9 | + sizes = collect(1:n) |
| 10 | + upper_bound = sum(sizes) |
| 11 | + model = MOI.instantiate( |
| 12 | + () -> MiniZinc.Optimizer{Int}("chuffed"); |
| 13 | + with_cache_type = Int, |
| 14 | + with_bridge_type = Int, |
| 15 | + ) |
| 16 | + MOI.set(model, MOI.RawOptimizerAttribute("model_filename"), "test.mzn") |
| 17 | + # We need this `s` variable that is trivially equal to `sizes` |
| 18 | + # because `MiniZincSet` supports only VectorOfVariables |
| 19 | + s = [MOI.add_constrained_variable(model, MOI.Integer())[1] for i in 1:n] |
| 20 | + x = [MOI.add_constrained_variable(model, MOI.Integer())[1] for i in 1:n] |
| 21 | + y = [MOI.add_constrained_variable(model, MOI.Integer())[1] for i in 1:n] |
| 22 | + max_x, _ = MOI.add_constrained_variable(model, MOI.Integer()) |
| 23 | + max_y, _ = MOI.add_constrained_variable(model, MOI.Integer()) |
| 24 | + MOI.add_constraint.(model, s, MOI.EqualTo.(sizes)) |
| 25 | + MOI.add_constraint.(model, x, MOI.Interval(1, upper_bound)) |
| 26 | + MOI.add_constraint.(model, y, MOI.Interval(1, upper_bound)) |
| 27 | + MOI.add_constraint(model, max_x, MOI.Interval(1, upper_bound)) |
| 28 | + MOI.add_constraint(model, max_y, MOI.Interval(1, upper_bound)) |
| 29 | + MOI.add_constraint.(model, 1max_x .- 1x, MOI.GreaterThan.(sizes)) |
| 30 | + MOI.add_constraint.(model, 1max_y .- 1y, MOI.GreaterThan.(sizes)) |
| 31 | + MOI.add_constraint( |
| 32 | + model, |
| 33 | + MOI.VectorOfVariables([x; y; s; s]), |
| 34 | + MiniZinc.MiniZincSet( |
| 35 | + "diffn", |
| 36 | + [1:n, n .+ (1:n), 2n .+ (1:n), 3n .+ (1:n)], |
| 37 | + ), |
| 38 | + ) |
| 39 | + MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE) |
| 40 | + obj = (1max_x) * max_y |
| 41 | + MOI.set(model, MOI.ObjectiveFunction{typeof(obj)}(), obj) |
| 42 | + MOI.optimize!(model) |
| 43 | + @test MOI.get(model, MOI.TerminationStatus()) === MOI.OPTIMAL |
| 44 | + @test MOI.get(model, MOI.PrimalStatus()) === MOI.FEASIBLE_POINT |
| 45 | + @test MOI.get(model, MOI.ResultCount()) == 1 |
| 46 | + @test MOI.get(model, MOI.ObjectiveValue()) == 120 |
| 47 | + rm("test.mzn") |
| 48 | + return |
| 49 | +end |
0 commit comments