Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indicator update best bound #175

Merged
merged 7 commits into from
Jun 26, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
benchmark to compare v0.2.1 with v0.2.2
Wikunia committed Jun 26, 2020
commit 16852c14011a1ad2599a2286fe409000c9c80554
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ConstrainSolver.jl - Changelog

## v0.2.1
## v0.2.1 (26th of June 2020)
- Bugfixes in indicator constraint [#170](https://github.com/Wikunia/ConstraintSolver.jl/issues/170)
- Calling finished constraints and other functions for i.e `TableConstraint` as an inner constraint
- Use correct best bound when inactive vs active
2 changes: 2 additions & 0 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
@@ -23,6 +23,8 @@ SUITE["eternity"] = BenchmarkGroup(["alldifferent", "table", "equal"])
# compiling run
solve_eternity("eternity_6x5"; height=6, width=5)
SUITE["eternity"]["6x5"] = @benchmarkable solve_eternity("eternity_6x5"; height=6, width=5) seconds=30
SUITE["eternity"]["6x5"] = @benchmarkable solve_eternity("eternity_6x5"; height=6, width=5, optimize=true) seconds=30
SUITE["eternity"]["6x5"] = @benchmarkable solve_eternity("eternity_6x5"; height=6, width=5, optimize=true, indicator=true) seconds=30
SUITE["eternity"]["5x5_all"] = @benchmarkable solve_eternity("eternity_5x5"; all_solutions=true) seconds=30

include(joinpath(dir, "benchmark/lp/benchmark.jl"))
28 changes: 25 additions & 3 deletions benchmark/eternity/benchmark.jl
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ function get_rotations(puzzle)
return rotations
end

function solve_eternity(fname="eternity_7"; height=nothing, width=nothing, all_solutions=false)
function solve_eternity(fname="eternity_7"; height=nothing, width=nothing, all_solutions=false, optimize=false, indicator=false)
puzzle = read_puzzle(fname)
rotations = get_rotations(puzzle)
npieces = size(puzzle)[1]
@@ -36,15 +36,27 @@ function solve_eternity(fname="eternity_7"; height=nothing, width=nothing, all_s
ncolors = maximum(puzzle[:,2:end])

m = Model(optimizer_with_attributes(CS.Optimizer, "logging" => [], "all_solutions"=>all_solutions))
if optimize
cbc_optimizer = optimizer_with_attributes(Cbc.Optimizer, "logLevel" => 0)
m = Model(optimizer_with_attributes(CS.Optimizer, "logging" => [], "all_solutions"=>all_solutions, "lp_optimizer" => cbc_optimizer))
end

@variable(m, 1 <= p[1:height, 1:width] <= npieces, Int)
@variable(m, 0 <= pu[1:height, 1:width] <= ncolors, Int)
@variable(m, 0 <= pr[1:height, 1:width] <= ncolors, Int)
@variable(m, 0 <= pd[1:height, 1:width] <= ncolors, Int)
@variable(m, 0 <= pl[1:height, 1:width] <= ncolors, Int)
if indicator
@variable(m, b, Bin)
end

@constraint(m, p[:] in CS.AllDifferentSet())
for i=1:height, j=1:width
@constraint(m, [p[i,j], pu[i,j], pr[i,j], pd[i,j], pl[i,j]] in CS.TableSet(rotations))
if indicator
@constraint(m, b => {[p[i,j], pu[i,j], pr[i,j], pd[i,j], pl[i,j]] in CS.TableSet(rotations)})
else
@constraint(m, [p[i,j], pu[i,j], pr[i,j], pd[i,j], pl[i,j]] in CS.TableSet(rotations))
end
end

# borders
@@ -75,9 +87,19 @@ function solve_eternity(fname="eternity_7"; height=nothing, width=nothing, all_s
@constraint(m, pr[i,j] == pl[i, j+1])
end

if width == height
if !optimize && indicator
@constraint(m, b == 1)
end

if !optimize && width == height
start_piece = findfirst(i->count(c->c == 0, puzzle[i,:]) == 2,1:npieces)
@constraint(m, p[1,1] == start_piece)
elseif optimize
if indicator
@objective(m, Min, 1000*b + p[1,1] + p[1,2])
else
@objective(m, Min, p[1,1] + p[1,2])
end
end

optimize!(m)