-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteger.jl
79 lines (66 loc) · 1.66 KB
/
integer.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Integer programming (HiGHS)
- Standard translation into Linear Inequality System
- Open source solver HiGHS
"""
using JuMP
using HiGHS
function solveINT!(s::Sudoku)
model = Model(HiGHS.Optimizer)
set_silent(model)
@variable(model, x[1:9, 1:9, 1:9], Bin)
# already placed numbers
for r in 1 : 9
for c in 1 : 9
if s.field[r, c] != 0
@constraint(model, x[r, c, s.field[r, c]] == 1)
end
end
end
# exactly one value per field
for r in 1 : 9
for c in 1 : 9
@constraint(model, sum(x[r, c, :]) == 1)
end
end
# no duplicate values in row
for c in 1 : 9
for v in 1 : 9
@constraint(model, sum(x[:, c, v]) == 1)
end
end
# no duplicate values in column
for r in 1 : 9
for v in 1 : 9
@constraint(model, sum(x[r, :, v]) == 1)
end
end
# no duplicate values in group
for r in [1, 4, 7]
for c in [1, 4, 7]
for v in 1 : 9
group = Vector{VariableRef}()
for i in 0 : 2
for j in 0 : 2
push!(group, x[r + i, c + j, v])
end
end
@constraint(model, sum(group) == 1)
end
end
end
optimize!(model)
if has_values(model) # if solveable
for r in 1 : 9
for c in 1 : 9
for v in 1 : 9
if value(x[r, c, v]) > 0.5
s.field[r, c] = v
end
end
end
end
return true
end
return false
end