Help with a JuMP.jl problem

The task is fairly straightforward. I need to group the numbers in A into pairs, such that the sum of each pair corresponds to one of the numbers in S.

using HiGHS
using JuMP

m = Model(HiGHS.Optimizer)
A = [
	119 225 511 259 173 28
	486 374 375 227 164 314
	389 136  72 241  81 326
]

S = reshape(100:100:900, 3, 3)'

@variable(m, x[i=1:3, j=1:6, z=1:9], binary=true)

# you can only have 2 numbers from A in a group
for i ∈ 1:9
	@constraint(m, sum(x[:, :, i] .== true) == 2)
end

# the sums of the products of each group and A must equal S
@constraint(m, reshape([sum(x[:, :, i] .* A) for i ∈ 1:9], 3, 3)' .== S)

optimize!(m)

With HiGHS, it just tells me that it’s infeasible…

The issue is

for i in 1:9
	@constraint(m, sum(x[:, :, i] .== true) == 2)
end

because you are comparing a VariableRef == Bool, which is always false:

julia> i = 1
1

julia> sum(x[:, :, i] .== true)
0

So the constraint says 0 == 2.

Here’s how you can write it

using HiGHS
using JuMP
model = Model(HiGHS.Optimizer)
A = [
	119 225 511 259 173 28
	486 374 375 227 164 314
	389 136  72 241  81 326
]
S = reshape(100:100:900, 3, 3)'
@variable(model, x[1:3, 1:6, 1:9], Bin)
@constraint(model, [i in 1:9], sum(x[:, :, i]) == 2)
@constraint(model, reshape([sum(x[:, :, i] .* A) for i in 1:9], 3, 3)' .== S)
optimize!(model)
1 Like