PLS help for the following constraint

The integer variable k, need to be constrainted to changed less than 5 times during 24 hours. The constraint sum(sub~=0)<=5 is not following CVX style, please help.

cvx begin

variable k(24) integer

max/min objective

subject to

	for i=2:24
cvx end

The largest possible difference between two adjacent values of k is 12. We can use that to specify the problem as follows:

variable k(24) integer
variable b(24) binary
-6 <= k <= 6;
-12 * b <= k - [ initial;k(1:end-1) ] <= 12 * b;
sum( b ) <= 5;

The b variable is 1 when there is a change, and 0 when there is not. When b(i) is 0, it forces k(i)-k(i-1) to be zero. When b(i) is 1, it allows the same quantity to float between -12 and 12.

many many thanks to mcg, it works.

Can I add a new sign function like sign in matlab to the atom library for the solving? In the function creation in CVX, is if …then… allowed? Thanks.


No to both questions. You have to do these kinds of things by hand. In particular you have to supply the minimum and maximum values of k to even be able to do this.