Skip to content

Commit

Permalink
Fix writing integer variables with float bounds (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Jul 22, 2024
1 parent 8c7a7d3 commit bc8751f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ function _write_variables(io::IO, model::Model{T}) where {T}
end
elseif info.is_int
if typemin(T) < lb && ub < typemax(T)
lb, ub = ceil(Int, lb), floor(Int, ub)
println(io, "var $lb .. $ub: $(info.name);")
else
println(io, "var int: $(info.name);")
if ub < typemax(T)
ub = floor(Int, ub)
println(io, "constraint int_le($(info.name), $ub);")
end
if typemin(T) < lb
lb = ceil(Int, lb)
println(io, "constraint int_le($lb, $(info.name));")
end
end
Expand Down
69 changes: 69 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,35 @@ function test_write_int()
return
end

function test_write_int_float_interval()
model = MiniZinc.Model{Float64}()
x, _ = MOI.add_constrained_variable(model, MOI.Integer())
MOI.add_constraint(model, x, MOI.Interval(1.5, 3.2))
@test sprint(write, model) == """
var 2 .. 3: x1;
solve satisfy;
"""
return
end

function test_write_int_float_greater_than()
model = MiniZinc.Model{Float64}()
x, _ = MOI.add_constrained_variable(model, MOI.Integer())
MOI.add_constraint(model, x, MOI.GreaterThan(1.5))
@test sprint(write, model) ==
"var int: x1;\nconstraint int_le(2, x1);\nsolve satisfy;\n"
return
end

function test_write_int_float_less_than()
model = MiniZinc.Model{Float64}()
x, _ = MOI.add_constrained_variable(model, MOI.Integer())
MOI.add_constraint(model, x, MOI.LessThan(1.5))
@test sprint(write, model) ==
"var int: x1;\nconstraint int_le(x1, 1);\nsolve satisfy;\n"
return
end

function test_write_interval()
model = MiniZinc.Model{Int}()
x, _ = MOI.add_constrained_variable(model, MOI.Integer())
Expand Down Expand Up @@ -1630,6 +1659,46 @@ function test_infix_unary_addition()
return
end

function test_integer_bounds_interval()
model = MOI.Utilities.Model{Float64}()
x = MOI.add_variables(model, 3)
MOI.add_constraint.(model, x, MOI.Integer())
MOI.add_constraint.(model, x, MOI.Interval(1.0, 3.0))
MOI.add_constraint(model, MOI.VectorOfVariables(x), MOI.AllDifferent(3))
mzn = MiniZinc.Optimizer{Float64}("highs")
index_map, _ = MOI.optimize!(mzn, model)
@test MOI.get(mzn, MOI.TerminationStatus()) == MOI.OPTIMAL
y = [MOI.get(mzn, MOI.VariablePrimal(), index_map[xi]) for xi in x]
@test sort(y) == [1, 2, 3]
return
end

function test_integer_bounds_greater_than()
model = MOI.Utilities.Model{Float64}()
x, _ = MOI.add_constrained_variable(model, MOI.Integer())
MOI.add_constraint(model, x, MOI.GreaterThan(1.5))
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{MOI.VariableIndex}(), x)
mzn = MiniZinc.Optimizer{Float64}("highs")
index_map, _ = MOI.optimize!(mzn, model)
@test MOI.get(mzn, MOI.TerminationStatus()) == MOI.OPTIMAL
@test MOI.get(mzn, MOI.VariablePrimal(), index_map[x]) == 2
return
end

function test_integer_bounds_less_than_than()
model = MOI.Utilities.Model{Float64}()
x, _ = MOI.add_constrained_variable(model, MOI.Integer())
MOI.add_constraint(model, x, MOI.LessThan(1.5))
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.set(model, MOI.ObjectiveFunction{MOI.VariableIndex}(), x)
mzn = MiniZinc.Optimizer{Float64}("highs")
index_map, _ = MOI.optimize!(mzn, model)
@test MOI.get(mzn, MOI.TerminationStatus()) == MOI.OPTIMAL
@test MOI.get(mzn, MOI.VariablePrimal(), index_map[x]) == 1
return
end

end

TestMiniZinc.runtests()

2 comments on commit bc8751f

@odow
Copy link
Member Author

@odow odow commented on bc8751f Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/111476

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.11 -m "<description of version>" bc8751ff0e01c00a6bd5daf42ff7685acc234ac0
git push origin v0.3.11

Please sign in to comment.