I try to do one optimization, I need to consider how many cycle usage. the “cycle” variable is updated by other binary variables, which equal to “if c1 and c2 then cycle = cycle + 1”.current, I got the correct “cycle” value update.

But I also need to reset cycle = 0, when “cycle == 5”, which is used to simulate restart process. I try to solve this question by the big-M method or semi-continuous set method, the cycle value will not update.

“if cycle == 5 then set cycle =0” condition, I try to use the following code:

cl(i-1) >= 4+0.00001-M*C3(i); % if cl(i-1) == 5 then C3(i) = 0
cl(i-1) <= 4+M*(1-C3(i));

1e-8-M

*C3(i)<=cl(i)<= 1e-8+M*C3(i); % through C3(i) = 0 to set cl(i) = 0

However, after adding this constrain, the cycle value will stop at 4, it seems the whole system stays idle, there is no operating for the whole system.

for day = 1:12

cvx_begin

variables bc(24,1) bd(24,1) Pg(24,1) E(24,1) Bmax(24,1)

variable q(24,1) binary

variable p(24,1) binary

variable C1(24,1) binary

variable C2(24,1) binary

variable C3(24,1) binary

variable C4(24,1) binary

variable C(24,1) binary

variable CC(24,1) binary

variable cl(24,1) integer

```
minimize sum(bill_rates.*Pg)+ Prmax*max(Pg)/30+...
0.12*sum(emis_rates.*(Pg+(bc*(1-eff)+bd*(1/eff-1))))
subject to
zeros(24,1) <= bc <= 100*q;
zeros(24,1) <= bd <= 100*p;
zeros(24,1) <= (p + q) <= ones(24,1);
l-s+bc == Pg+bd;
E(1) == E_iter;
cl(1) == cl_iter;
p(1)+q(1) == 0;
Bmax(1) == 400*(1-0.05*cl(1));
for i = 2:24
E(i) == E(i-1)+ bc(i)*eff-bd(i)/eff;
E(i) <= Bmax(i-1);
E(i) >= 0;
ec+sum(bc(1:i))*eff-400*cl(i-1) >= 400+1-M*(1-C1(i))
ec+sum(bc(1:i))*eff-400*cl(i-1) <= 400+M*C1(i)
ed+sum(bd(1:i))/eff-400*cl(i-1) >= 400+1-M*(1-C2(i))
ed+sum(bd(1:i))/eff-400*cl(i-1) <= 400+M*C2(i)
C1(i)>= C(i)
C2(i)>= C(i)
C(i)+1 >= C1(i) + C2(i)
cl(i) == cl(i-1) + C(i)
Bmax(i) == 400*(1-0.05*cl(i));
cl(i-1) >= 4+0.00001-M*C3(i);
cl(i-1) <= 4+M*(1-C3(i));
1e-8-M*C3(i)<=cl(i)<= 1e-8+M*C3(i);
end
cvx_end
```

end