Constraint violation due to the loops ietration?

I’m trying to model this problem using CVX

for t = 1:T
ff = (W(t)-L(t)-(sum(x(:,t))))^2;
cvx_begin
variables x(N,T)
minimize (ff)
subject to
for i = 1:N
sum(x(i,1:T)) + SOC_I(i) <= MC(i);
x(i,t) <= MP(i);
x(i,t) >= 0;
end
cvx_end
end

But the constraints is violated. x(i,t) is negative in some cases.

Show us the output from running it. Do the solver and CVX say that an optimum has been found?

What is the largest magnitude negative number returned for x? Solvers only enforce the constraints to within a tolerance, so if a constraint is only violated by a small amount, say by 1e-11, that is not considered to be a violation, because the constraint holds to within tolerance.

I solved the violation problem, but the problem now that CVX doesn’t care about other inputs, Let’s say the problem that I have attached , changing the values of W, and L doesn’t make any change of the output . it’s clear from the formulation that at each different value of W and L there should be a different decision, why the decision are constant whatever the input values ?

I think there is something wrong about how to formulate the loops…

Are you saying that you get the same optimal solution for x for each of the T times through the outer for loop? Since you haven’t shown us the values of W and L, we have no way of knowing whether the optimal values o should differ.

If you want help, show a complete reproducible example with output, preferably with small dimensions, which exhibits the difficulty you are encountering.

How did you solve the violation problem?

Thanks Mark for your interest and reply, Actually I solved the violation of nonnegative constraint by changing x(i,t) > 0 to x( : ) > 0 as I saw in one of your posts. which is not logical as i understand. they should do the same, am I right ?
The problem now . It should be a distributed decision that at each time interval t there is a different decision according to the values of W and L. Here is a sample of W, L :
W = [92 30 70 100 85]
L = [78 35 35 20 60]
MP = [1.38 1.38 1.38];
MC = [9.54 5.4 5.4];
SOC_I = [8.95 3.0 4.4];
I = 3; T = 5;

and here is my code and output

for t = 1:T
f =(W(t)-L(t)-(sum(x(:,t))))^2;
cvx_begin quiet
variables x(I,T)
minimize (f)
subject to
for i = 1:I
sum(x(i,1:t)) + SOC_I(i) <= MC(i);
if t==T
sum(x(i,1:T)) + SOC_I(i) == MC(i);
end
x(i,t) <= MP(i);
x( : ) >= 0;
end
cvx_end
end

The output X is
0.121799912593636 0.121799935299994 0.121799935298391 0.121800010212466 0.102800206590508
0.516430896320261 0.516430904566148 0.516430869472851 0.516430905109652 0.334276424528842
0.209032099878975 0.209032063308784 0.209032186731757 0.209031960412325 0.163871689666090

which is the same at each t rather than the last one which there is a constraint inforced it to a certain output.
But even at the 2nd t where L > W still gives the same response .

Why is `cvx_begin`/`cvx_end` inside the for loop? If I’m reading your formulation properly, they should be outside the loop.

`x(i,t) > 0` only constrains the `i,t` element of x.
`x(:) > 0` will work,
or in this case, more simply
`x > 0`

Note that > will be interpreted as >= 0, so if you really need x to be strictly > 0, you need to use
`x >= small_number`, for a value of small_number which is a small number.

As for the rest of it, it sounds like mcg is steering you in the right direction.

Please note that putting cvx_end outside the for loop doesn’t affect the results , its just gives warning "non-empty CVX problem already exists in this scope "
I think because it is solving the optimization problem I times inside each t "overwrite ", but my problem still there why it gives equally divided results where W, L are different for each t
I hope you can comment on that ?
Thanks a lot

Is this what you want?

for t = 1:T
cvx_begin
variables x(I,T)
f =(W(t)-L(t)-(sum(x(:,t))))^2;
minimize (f)
for i = 1:I
sum(x(i,1:t)) + SOC_I(i) <= MC(i);
if t==T
sum(x(i,1:T)) + SOC_I(i) == MC(i);
end
x(i,t) <= MP(i);
x( : ) >= 0;
end
cvx_end;
x
end

Note that I have moved the `f =(W(t)-L(t)-(sum(x(:,t))))^2;` line to after x is defined as a CVX variable. The way you were doing it, the x in f just had the optimal value from the previous iteration, and so f had no relation to your CVX varriable x, so you were just minimizing a constant, but you were minimizing the constant with respect to constraints on your optimization variable x. I.e., you had a constant x in your objective function f, and the CVX variable x in the constraints. Hopefully this gives you enough of a clue that you can fix it to do what you really want.

I’m too tired to look at the problem you are trying to solve, but perhaps mcg is right… But you need to change where the f = line goes though, per the above. Maybe after you make that change, the mcg suggestion will work out for you.

It’'s also a good idea to not use the quiet option until after you resolve all difficulties you are having.

2 Likes