[*]

Hi,

I just started using GAMS and need help regarding following issue:

t /t1*t24/

b(t) is a binary variable.

Now I want the variable s(t) to count how many times in a row b has been = 1. It should reset when b turns 0 again. For example:

t b s

t1 0 0

t2 1 1

t3 1 2

t4 0 0

t5 1 1

t6 1 2

t7 1 3

t8 1 4

t9 0 0

...

Would be great if you could help me to find an equation which gives me s(t) as shown.

Thanks in advance!

## Count how many values in a row

### Re: Count how many values in a row

This has little to do with GAMS, but modeling (and linearizing) of logical constraints. Perhaps, I am doing your homework, but here your are: Written in short what you describe means: "if b(i) = 1 then s(i) = s(i-1)+1 else s(i) = 0". That can be linearized: s(i) =e= s(i-1) + 1 sp(i) - sn(i) where sp and sn are positive slack variables that need to fulfill sp(i) =l= (1-b(1))*M and sn(i) =l= (1-b(i))*M plus the else clause s(i) =l= b(i)*M (s needs to be also declared as a positive variable). M is card(t)*card(t-1)/2.

-Michael

-Michael

### Re: Count how many values in a row

Hi Michael,

thanks for your respone. Unfortunately it doesn't work (probably because I don'T know how to use your answer )

Should I declare 'i' as a new set and M as a paramter? I tried several ways but couldn't fix it.

I know that this is not an usual GAMS-topic, maybe it hepls if I explain why I need it:

parameter c(t) stands for costs depending on the time period t.

There is a mashine which should be turned on (b(t) = 1) when costs are low in order to fulfil a certain output at minimal costs. (so far the models works!)

But now I'd like to add the technical limitation that the mashine can not be turned on for more than 5 time periods in a row. So I would like to add kind of a "Count"-function and later try to limit it to 5.

And no, it's no homework, even though I admit it sounds like that

thanks for your respone. Unfortunately it doesn't work (probably because I don'T know how to use your answer )

Should I declare 'i' as a new set and M as a paramter? I tried several ways but couldn't fix it.

I know that this is not an usual GAMS-topic, maybe it hepls if I explain why I need it:

parameter c(t) stands for costs depending on the time period t.

There is a mashine which should be turned on (b(t) = 1) when costs are low in order to fulfil a certain output at minimal costs. (so far the models works!)

But now I'd like to add the technical limitation that the mashine can not be turned on for more than 5 time periods in a row. So I would like to add kind of a "Count"-function and later try to limit it to 5.

And no, it's no homework, even though I admit it sounds like that

### Re: Count how many values in a row

Why don't you share your non-working attempts (i.e. your compilable and executable GAMS model) and point to a "smoking gun" (show what does not work). Perhaps, then you get more feedback. -Michael