Page 1 of 1

### 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!

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

Posted: Tue Aug 06, 2019 4:07 pm
Hi
If I understand your question well, you just want to count the number of variables fixed to 1. The following code sums 1 every time the condition that Z is fixed to 1 is met (upperbound and lower bound equal to 1):

Code: Select all

``````set i 'samples' /1*10/;
set k 'components' /k1*k3/;
binary variable z(i,k);

z.l(i,"k1") = 0;
z.UP(i,"k2") = 1;
z.Lo(i,"k2") = 1;

parameter  count_fixedone(k);

count_fixedone(k) = sum(i\$(Z.UP(i,k) =1 AND Z.LO(i,k) = 1), 1);

display count_fixedone;
``````
Cheers
Renger

PS. You can post properly formatted code by klicking the icon </> in the menu. That saves the person to answer your question some work when trying to reproduce your problem.