Skip to content

Commit 321c4b4

Browse files
committed
Fix breaking changes in CBLS, JuMP and MOI
1 parent f1a9e8f commit 321c4b4

File tree

8 files changed

+62
-152
lines changed

8 files changed

+62
-152
lines changed

src/ConstraintModels.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export qap
1616
export magic_square
1717
export mincut
1818
export n_queens
19-
export scheduling
19+
# export scheduling
2020
export sudoku
2121

2222
include("assignment.jl")
@@ -25,7 +25,7 @@ include("cut.jl")
2525
include("golomb.jl")
2626
include("magic_square.jl")
2727
include("n_queens.jl")
28-
include("scheduling.jl")
28+
# include("scheduling.jl")
2929
include("sudoku.jl")
3030

3131
end

src/cut.jl

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@ function mincut(graph, source, sink, interdiction, ::Val{:raw})
1010
foreach(_ -> variable!(m, d), 0:n)
1111

1212
# Extract error function from usual_constraint
13-
e1 = (x; param=nothing, dom_size=n + 1) -> error_f(
14-
usual_constraints[:ordered])(x; param, dom_size
15-
)
16-
e2 = (x; param=nothing, dom_size=n + 1) -> error_f(
17-
usual_constraints[:all_different])(x; param, dom_size
18-
)
13+
e1 = (x; kargs...) -> error_f(USUAL_CONSTRAINTS[:ordered])(x; kargs...)
14+
e2 = (x; kargs...) -> error_f(USUAL_CONSTRAINTS[:all_different])(x; kargs...)
1915

2016
# Add constraint
2117
constraint!(m, e1, [source, separator, sink])
22-
constraint!(m, e2, 1:(n + 1))
18+
constraint!(m, e2, 1:(n+1))
2319

2420
# Add objective
2521
objective!(m, (x...) -> o_mincut(graph, x...; interdiction))
@@ -56,6 +52,6 @@ Compute the minimum cut of a graph.
5652
- `interdiction`: indicates the number of forbidden links
5753
- `modeler`: Default to `:JuMP`.
5854
"""
59-
function mincut(graph; source, sink, interdiction =0, modeler = :JuMP)
55+
function mincut(graph; source, sink, interdiction=0, modeler=:JuMP)
6056
return mincut(graph, source, sink, interdiction, Val(modeler))
6157
end

src/golomb.jl

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,14 @@ function golomb(n, L, ::Val{:raw})
66
foreach(_ -> variable!(m, d), 1:n)
77

88
# Extract error function from usual_constraint
9-
e1 = (x; param=nothing, dom_size=n) -> error_f(
10-
usual_constraints[:all_different])(x; param, dom_size
11-
)
12-
e2 = (x; param=nothing, dom_size=n) -> error_f(
13-
usual_constraints[:all_equal_param])(x; param, dom_size
14-
)
15-
e3 = (x; param=nothing, dom_size=n) -> error_f(
16-
usual_constraints[:dist_different])(x; param, dom_size
17-
)
9+
e1 = (x; kargs...) -> error_f(USUAL_CONSTRAINTS[:all_different])(x; kargs...)
10+
e2 = (x; kargs...) -> error_f(USUAL_CONSTRAINTS[:all_equal])(x; kargs...)
11+
e3 = (x; kargs...) -> error_f(USUAL_CONSTRAINTS[:dist_different])(x; kargs...)
1812

1913
# # Add constraints
2014
constraint!(m, e1, 1:n)
21-
constraint!(m, x -> e2(x; param=0), 1:1)
22-
for i in 1:(n - 1), j in (i + 1):n, k in i:(n - 1), l in (k + 1):n
15+
constraint!(m, e2, 1:1)
16+
for i in 1:(n-1), j in (i+1):n, k in i:(n-1), l in (k+1):n
2317
(i, j) < (k, l) || continue
2418
constraint!(m, e3, [i, j, k, l])
2519
end
@@ -39,7 +33,7 @@ function golomb(n, L, ::Val{:JuMP})
3933
@constraint(m, X in Ordered()) # for output convenience, keep them ordered
4034

4135
# No two pairs have the same length
42-
for i in 1:(n - 1), j in (i + 1):n, k in i:(n - 1), l in (k + 1):n
36+
for i in 1:(n-1), j in (i+1):n, k in i:(n-1), l in (k+1):n
4337
(i, j) < (k, l) || continue
4438
@constraint(m, [X[i], X[j], X[k], X[l]] in DistDifferent())
4539
end
@@ -55,4 +49,4 @@ end
5549
5650
Model the Golomb problem of `n` marks on the ruler `0:L`. The `modeler` argument accepts :raw, and :JuMP (default), which refer respectively to the solver internal model, the MathOptInterface model, and the JuMP model.
5751
"""
58-
golomb(n, L=n^2; modeler = :JuMP) = golomb(n, L, Val(modeler))
52+
golomb(n, L=n^2; modeler=:JuMP) = golomb(n, L, Val(modeler))

src/magic_square.jl

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ function magic_square(n, ::Val{:JuMP})
77
@constraint(model, vec(X) in AllDifferent())
88

99
for i in 1:n
10-
@constraint(model, X[i,:] in SumEqualParam(magic_constant))
11-
@constraint(model, X[:,i] in SumEqualParam(magic_constant))
10+
@constraint(model, X[i, :] in AllEqual(; val=magic_constant))
11+
@constraint(model, X[:, i] in AllEqual(; val=magic_constant))
1212
end
13-
@constraint(model, [X[i,i] for i in 1:n] in SumEqualParam(magic_constant))
14-
@constraint(model, [X[i,n + 1 - i] for i in 1:n] in SumEqualParam(magic_constant))
13+
@constraint(model, [X[i, i] for i in 1:n] in AllEqual(; val=magic_constant))
14+
@constraint(model, [X[i, n+1-i] for i in 1:n] in AllEqual(; val=magic_constant))
1515

1616
return model, X
1717
end
@@ -21,12 +21,4 @@ end
2121
2222
Create a model for the magic square problem of order `n`. The `modeler` argument accepts :JuMP (default), which refer to the solver the JuMP model.
2323
"""
24-
magic_square(n; modeler = :JuMP) = magic_square(n, Val(modeler))
25-
26-
27-
28-
29-
30-
31-
32-
24+
magic_square(n; modeler=:JuMP) = magic_square(n, Val(modeler))

src/scheduling.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function scheduling(processing_times, due_dates, ::Val{:raw})
2-
m = model(; kind = :scheduling)
2+
m = model(; kind=:scheduling)
33
n = length(processing_times) # number of jobs
44
max_time = sum(processing_times)
55

src/sudoku.jl

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function sudoku(n, start, ::Val{:raw})
22
N = n^2
33
d = domain(1:N)
44

5-
m = model(;kind=:sudoku)
5+
m = model(; kind=:sudoku)
66

77
# Add variables
88
if isnothing(start)
@@ -12,19 +12,17 @@ function sudoku(n, start, ::Val{:raw})
1212
end
1313

1414

15-
e = (x; param=nothing, dom_size=N) -> error_f(
16-
usual_constraints[:all_different])(x; param=param, dom_size=dom_size
17-
)
15+
e = (x; kargs...) -> error_f(USUAL_CONSTRAINTS[:all_different])(x; kargs...)
1816

1917
# Add constraints: line, columns; blocks
20-
foreach(i -> constraint!(m, e, (i * N + 1):((i + 1) * N)), 0:(N - 1))
21-
foreach(i -> constraint!(m, e, [j * N + i for j in 0:(N - 1)]), 1:N)
18+
foreach(i -> constraint!(m, e, (i*N+1):((i+1)*N)), 0:(N-1))
19+
foreach(i -> constraint!(m, e, [j * N + i for j in 0:(N-1)]), 1:N)
2220

23-
for i in 0:(n - 1)
24-
for j in 0:(n - 1)
21+
for i in 0:(n-1)
22+
for j in 0:(n-1)
2523
vars = Vector{Int}()
2624
for k in 1:n
27-
for l in 0:(n - 1)
25+
for l in 0:(n-1)
2826
push!(vars, (j * n + l) * N + i * n + k)
2927
end
3028
end
@@ -45,16 +43,16 @@ function sudoku(n, start, ::Val{:MOI})
4543
foreach(i -> MOI.add_constraint(m, VI(i), DiscreteSet(1:N)), 1:N^2)
4644

4745
# Add constraints: line, columns; blocks
48-
foreach(i -> MOI.add_constraint(m, VOV(map(VI, (i * N + 1):((i + 1) * N))),
49-
MOIAllDifferent(N)), 0:(N - 1))
50-
foreach(i -> MOI.add_constraint(m, VOV(map(VI, [j * N + i for j in 0:(N - 1)])),
46+
foreach(i -> MOI.add_constraint(m, VOV(map(VI, (i*N+1):((i+1)*N))),
47+
MOIAllDifferent(N)), 0:(N-1))
48+
foreach(i -> MOI.add_constraint(m, VOV(map(VI, [j * N + i for j in 0:(N-1)])),
5149
MOIAllDifferent(N)), 1:N)
5250

53-
for i in 0:(n - 1)
54-
for j in 0:(n - 1)
51+
for i in 0:(n-1)
52+
for j in 0:(n-1)
5553
vars = Vector{Int}()
5654
for k in 1:n
57-
for l in 0:(n - 1)
55+
for l in 0:(n-1)
5856
push!(vars, (j * n + l) * N + i * n + k)
5957
end
6058
end
@@ -72,16 +70,16 @@ function sudoku(n, start, ::Val{:JuMP})
7270
@variable(m, 1 X[1:N, 1:N] N, Int)
7371
if !isnothing(start)
7472
for i in 1:N, j in 1:N
75-
v_ij = start[i,j]
73+
v_ij = start[i, j]
7674
if 1 v_ij N
77-
@constraint(m, X[i,j] == v_ij)
75+
@constraint(m, X[i, j] == v_ij)
7876
end
7977
end
8078
end
8179

8280
for i in 1:N
83-
@constraint(m, X[i,:] in AllDifferent()) # rows
84-
@constraint(m, X[:,i] in AllDifferent()) # columns
81+
@constraint(m, X[i, :] in AllDifferent()) # rows
82+
@constraint(m, X[:, i] in AllDifferent()) # columns
8583
end
8684
for i in 0:(n-1), j in 0:(n-1)
8785
@constraint(m, vec(X[(i*n+1):(n*(i+1)), (j*n+1):(n*(j+1))]) in AllDifferent()) # blocks
@@ -119,7 +117,7 @@ solution = value.(grid)
119117
display(solution, Val(:sudoku))
120118
```
121119
"""
122-
sudoku(n; start=nothing, modeler = :JuMP) = sudoku(n, start, Val(modeler))
120+
sudoku(n; start=nothing, modeler=:JuMP) = sudoku(n, start, Val(modeler))
123121

124122
@doc raw"""
125123
```julia
@@ -138,10 +136,10 @@ SudokuInstance(P::Pair{Tuple{Int, Int}, T}...) # again, default to 9×9 sudoku,
138136
```
139137
Constructor functions for the `SudokuInstance` `struct`.
140138
"""
141-
mutable struct SudokuInstance{T <: Integer} <: AbstractMatrix{T}
142-
A::AbstractMatrix{T} where {T <: Integer}
139+
mutable struct SudokuInstance{T<:Integer} <: AbstractMatrix{T}
140+
A::AbstractMatrix{T} where {T<:Integer}
143141

144-
function SudokuInstance(A::AbstractMatrix{T}) where {T <: Integer}
142+
function SudokuInstance(A::AbstractMatrix{T}) where {T<:Integer}
145143
size(A, 1) == size(A, 2) || throw(error("Sodokus must be square; received matrix of size $(size(A, 1))×$(size(A, 2))."))
146144
isequal(sqrt(size(A, 1)), isqrt(size(A, 1))) || throw(error("SudokuInstances must be able to split into equal boxes (e.g., a 9×9 SudokuInstance has three 3×3 squares). Size given is $(size(A, 1))×$(size(A, 2))."))
147145
new{T}(A)
@@ -172,8 +170,8 @@ Construct a `SudokuInstance` with the values `X` of a solver as input.
172170
function SudokuInstance(X::Dictionary)
173171
n = isqrt(length(X))
174172
A = zeros(Int, n, n)
175-
for (k,v) in enumerate(Base.Iterators.partition(X, n))
176-
A[k,:] = v
173+
for (k, v) in enumerate(Base.Iterators.partition(X, n))
174+
A[k, :] = v
177175
end
178176
return SudokuInstance(A)
179177
end
@@ -238,7 +236,7 @@ function _format_line_segment(r, col_pos, M)
238236
line = string()
239237
for k in axes(r, 1)
240238
n_spaces = 1
241-
Δ = maximum((ndigits(i) for i in M[:, (col_pos * sep_length) + k])) - ndigits(r[k])
239+
Δ = maximum((ndigits(i) for i in M[:, (col_pos*sep_length)+k])) - ndigits(r[k])
242240
if Δ 0
243241
n_spaces = Δ + 1
244242
end
@@ -259,7 +257,7 @@ function _format_line(r, M)
259257
line = _rules[:column]
260258
for i in 1:sep_length
261259
abs_sep_pos = sep_length * i
262-
line *= _format_line_segment(r[(abs_sep_pos - sep_length + 1):abs_sep_pos], i - 1, M)
260+
line *= _format_line_segment(r[(abs_sep_pos-sep_length+1):abs_sep_pos], i - 1, M)
263261
end
264262

265263
return line

test/JuMP.jl

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111

1212
@constraint(m, X in AllDifferent())
1313
@constraint(m, X in AllEqual())
14-
@constraint(m, X in AllEqualParam(2))
15-
@constraint(m, X in AlwaysTrue())
1614
@constraint(m, X[1:4] in DistDifferent())
17-
@constraint(m, X[1:2] in Eq())
1815
@constraint(m, X in Ordered())
1916
end
2017

@@ -64,15 +61,15 @@ end
6461
@variable(model, x in DiscreteSet(0:20))
6562
@variable(model, y in DiscreteSet(0:20))
6663

67-
@constraint(model, [x,y] in Predicate(v -> 6v[1] + 8v[2] >= 100 ))
68-
@constraint(model, [x,y] in Predicate(v -> 7v[1] + 12v[2] >= 120 ))
64+
@constraint(model, [x, y] in Predicate(v -> 6v[1] + 8v[2] >= 100))
65+
@constraint(model, [x, y] in Predicate(v -> 7v[1] + 12v[2] >= 120))
6966

7067
objFunc = v -> 12v[1] + 20v[2]
7168
@objective(model, Min, ScalarFunction(objFunc))
7269

7370
optimize!(model)
7471
@info solution_summary(model)
75-
@info "JuMP: basic opt" value(x) value(y) (12*value(x)+20*value(y))
72+
@info "JuMP: basic opt" value(x) value(y) (12 * value(x) + 20 * value(y))
7673
end
7774

7875
@testset "JuMP: Chemical equilibrium" begin
@@ -84,22 +81,22 @@ end
8481
@info "JuMP: $compounds_names$mixture_name" value.(X)
8582
end
8683

87-
@testset "JuMP: Scheduling" begin
88-
model, completion_times, start_times = scheduling(processing_times, due_times)
89-
optimize!(model)
90-
@info solution_summary(model)
91-
@info "JuMP: scheduling" value.(start_times) value.(completion_times) processing_times due_times
92-
@info (value.(start_times)+processing_times == value.(completion_times))
93-
end
84+
# @testset "JuMP: Scheduling" begin
85+
# model, completion_times, start_times = scheduling(processing_times, due_times)
86+
# optimize!(model)
87+
# @info solution_summary(model)
88+
# @info "JuMP: scheduling" value.(start_times) value.(completion_times) processing_times due_times
89+
# @info (value.(start_times)+processing_times == value.(completion_times))
90+
# end
9491

9592
@testset "JuMP: Minimum Cut" begin
9693
graph = zeros(5, 5)
97-
graph[1,2] = 1.0
98-
graph[1,3] = 2.0
99-
graph[1,4] = 3.0
100-
graph[2,5] = 1.0
101-
graph[3,5] = 2.0
102-
graph[4,5] = 3.0
94+
graph[1, 2] = 1.0
95+
graph[1, 3] = 2.0
96+
graph[1, 4] = 3.0
97+
graph[2, 5] = 1.0
98+
graph[3, 5] = 2.0
99+
graph[4, 5] = 3.0
103100

104101
model, links = mincut(graph; source=1, sink=5, interdiction=1)
105102
optimize!(model)

0 commit comments

Comments
 (0)