Calculating time periods between generator shutdown and startup

Hello,

I am struggling with calculation of the length of time periods between the last shutdown of power generator and its current startup.

Say I have such states of generator (on - 1, off - 0):

0 0 0 1 1 1 0 0 0 0 1 1 1 1 1 0 0 1 1

Startups look like this:

0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0

Shutdowns look like this:

0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0

I would like to have variable, which will result in such values:

0 0 0 3 0 0 0 0 0 0 4 0 0 0 0 0 0 2 0

I prepared very simple model just to solve this issue and once solved I am going implement it to the actual model of a power plant.

I am using MIP solver.

Here is my code, I can calculate cumulative time of a generator working and standing idle, but it didn’t help me much. Could you please give me a hint on how to push this problem forward?

Sets
t /1*24/;

alias(t, tt);

Parameters

price(t) /
1 100
2 100
3 200
4 200
5 200
6 100
7 100
8 100
9 100
10 100
11 100
12 200
13 200
14 200
15 200
16 200
17 200
18 200
19 100
20 100
21 100
22 300
23 300
24 100
/

production_cost(t) /
1 150
2 150
3 150
4 150
5 150
6 150
7 150
8 150
9 150
10 150
11 150
12 150
13 150
14 150
15 150
16 150
17 150
18 150
19 150
20 150
21 150
22 150
23 150
24 150
/

startup_cost /100/

;

Binary Variables
on(t)
off(t)
start(t)
stop(t);

Variables
on_period(t)
off_period(t)
on_off_period(t)
margin
;

Equations
eq_start(t)
eq_stop(t)
eq_on(t)
eq_off(t)
eq_on_period(t)
eq_off_period(t)
eq_on_off_period(t)
eq_margin;

eq_start(t)… start(t) =g= on(t) - on(t-1);

eq_stop(t)… stop(t) =g= on(t-1) - on(t);

eq_on(t)… on(t) =g= start(t) - stop(t);

eq_off(t)… off(t) =e= 1-on(t);

eq_on_period(t)… on_period(t) =e= sum(tt$(ord(tt)<= ord(t)), on(tt));

eq_off_period(t)… off_period(t) =e= sum(tt$(ord(tt)<= ord(t)), off(tt));

eq_on_off_period(t)… on_off_period(t) =e= off_period(t) - on_period(t);

eq_margin… margin =e= sum(t, on(t) * price(t)) - sum(t, on(t) * production_cost(t)) - sum(t, start(t) * startup_cost) - sum(t, stop(t) * 1); ;

Model power_plant /all/;
Solve power_plant using mip maximizing margin;

Display on.l, off.l, stop.l, start.l, on_period.l, off_period.l, on_off_period.l;