### How to get a length of a set with condition

Posted:

**Tue Aug 06, 2019 3:51 pm**Hello everyone,

I am new to GAMS and I have a question about set in GAMS.

I defined sets: i and k. I also defined a variable z(i,k) where z(i,k) is a binary variable in which z.lo(i,k) = 0 and z.up(i,k) = 1.

set i 'samples' /1*10/;

set k 'components' /k1*k3/;

binary variable z(i,k);

I have another sets fz(i,k), fixedzero(i,k), and fixedone(i,k) which is used to fixed values of z(i,k). During the iterative solving, some z(i,k) are fixed to one, some are fixed to zero.

set fz(i,k);

set fixedzero(i,k);

set fixedone(i,k);

fz(i,k) = fixedzero(i,k);

z.fx(fz) = 0;

fz(i,k) = fixedone(i,k);

z.fx(fz) = 1;

In the end, I would like to find the size of fixed z(i,k) for a given k, i.e. given that k = 'k1', how many z(i,'k1') are fixed to one and how many are fixed to zero. For example, if index i=1,2,3 of z(i,'k1') is fixed to one, then count_fixedone('k1') should be 3. Here is how I tried, but it does not work.

set count_fixedone(k);

set count_fixedzero(k);

count_fixedone(k) = card( i$(z.l(i,k)=1) )

* if z(i,k) is fixed to one, its lower bound is 1

display count_fixedone;

This is the errors:

8 ')' expected

149 Uncontrolled set entered as constant

Thank you very much!

I am new to GAMS and I have a question about set in GAMS.

I defined sets: i and k. I also defined a variable z(i,k) where z(i,k) is a binary variable in which z.lo(i,k) = 0 and z.up(i,k) = 1.

set i 'samples' /1*10/;

set k 'components' /k1*k3/;

binary variable z(i,k);

I have another sets fz(i,k), fixedzero(i,k), and fixedone(i,k) which is used to fixed values of z(i,k). During the iterative solving, some z(i,k) are fixed to one, some are fixed to zero.

set fz(i,k);

set fixedzero(i,k);

set fixedone(i,k);

fz(i,k) = fixedzero(i,k);

z.fx(fz) = 0;

fz(i,k) = fixedone(i,k);

z.fx(fz) = 1;

In the end, I would like to find the size of fixed z(i,k) for a given k, i.e. given that k = 'k1', how many z(i,'k1') are fixed to one and how many are fixed to zero. For example, if index i=1,2,3 of z(i,'k1') is fixed to one, then count_fixedone('k1') should be 3. Here is how I tried, but it does not work.

set count_fixedone(k);

set count_fixedzero(k);

count_fixedone(k) = card( i$(z.l(i,k)=1) )

* if z(i,k) is fixed to one, its lower bound is 1

display count_fixedone;

This is the errors:

8 ')' expected

149 Uncontrolled set entered as constant

Thank you very much!