Search code examples
sumconstraintsamplglpkmathprog

Sum the binary variables in GLPK


I am new in GLPK. This is some of my code:

set I := setof{(i,r,p,d) in T} i;
var Y{I,I}, binary;
s.t. c1{i in I, j in I}: sum{Y[i,j]} = 6;

I want to have only six values in Y that are 1. Can anyone tell me how to do it in proper way? Because s.t. c1{i in I, j in I}: sum{Y[i,j]} = 6;always produces an error.

Thank you.


Solution

  • This is just a syntax problem. The constraint should look like the following:

    s.t. c1: sum{i in I, j in I}(Y[i,j]) = 6;
    

    The first brackets after the name of your constraints imply that the constraint is applied to every single [I, I]. What you want is to fix the sum of all Y in your problem, so you need the constraint to only apply once to your problem (so delete these brackets). In the sum-syntax don't put the variable you want to sum in the brackets, they belong after them. Inside the brackets you can define the range of the sum.