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

Sum of constraints: e.g. @constraint(model, sum([x[i,k] == 1 && x[j,k] == 1 for k in 1:n] <= 1) #254

Open
hakank opened this issue Feb 13, 2021 · 2 comments

Comments

@hakank
Copy link

hakank commented Feb 13, 2021

In certain models, e.g. Steiner problem ( http://hakank.org/julia/constraints/steiner.jl ) there would be a great modelling benefit if one can sum over a list of constraints/conditions.

Example (from the Steiner model). Instead of the following, using booleans to first count the number of common elements and then constrain the sum to be atmost 1 (using the upcoming feature of && in reification which is a great feature in itself)

    # ...
    # atmost 1 element in common
    for i in 1:nb
        @constraint(model,sum(x[i,:]) == 3)
        for j in i+1:nb
            b = @variable(model, [1:n], Bin)
            for k in 1:n 
                @constraint(model, b[k] := { x[i,k] == 1 && x[j,k] == 1 })
            end
            @constraint(model, sum(b) <= 1)
        end
    end 
    # ...

the following formulation would be much simpler to state:

    # ...
    # atmost 1 element in common
    for i in 1:nb
        @constraint(model,sum(x[i,:]) == 3)
        for j in i+1:nb
            @constraint(model, sum([x[i,k] == 1 && x[j,k] == 1 for k in 1:n] <= 1)
        end
    end 
    # ...
@Wikunia
Copy link
Owner

Wikunia commented Feb 14, 2021

Thanks for opening the issue. It seems like you forgot a bracket in the sum constraint but I'm quite surprised that this doesn't throw an error for me. Will open an issue at JuMP about this.

@Wikunia Wikunia added the needs JuMP change needs some changes in JuMP.jl label Jul 23, 2021
@Wikunia Wikunia added new constraint type and removed needs JuMP change needs some changes in JuMP.jl labels Mar 26, 2022
@Wikunia
Copy link
Owner

Wikunia commented Mar 26, 2022

The user would need to write something like:

@constraint(
    model, 
    BooleanConstraint(sum(x == i && x == i for i = 1:2) <= 1),
)

which isn't implemented yet though but the suggested syntax here doesn't work due to how JuMP works internally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants