Trouble running an optimizaton program in CVX

t=1000; Preq=zeros();
for tn=2:1:t
Pb=randi([0 15000],tn,1)';
cvx_solver sedumi
    variables Pb(tn) Psca(tn) Psc(tn) Esc(tn)
    minimize (0.01481*max(((Pb(tn)-Pb(tn-1))/Pb_thres_var-1),0))
    subject to 
    Pb(tn)+Psca(tn)-Preq(tn)== 0;
    Psca(tn)-Psc(tn)== -10000;
    Esc(tn)-Esc(tn-delta)== -Psc(tn)*delta;
    Esc_min <= Esc(tn) <= Esc_max;

Above code is a part of optimization i am working on(Optimal control of hybrid energy storage system). While running the code, the values of variables(Pb, Psca,Esc,Psc) are zero til t=(tn-2) and incorrect values for Pb(tn-1) & Pb(tn).

Your objective and constraints only apply to the “tn” elements - maybe that’s not what you want… When the CVX portion of the code is run, tn has the last value it had in the for loop, which is 1000.

Pb which you set before the CVX portion of the code, is overwritten inside the CVX code by declaring it a CVX variable, which then has no relation to the previous Pb.

There may be other problems as well. I suggest you look at these comments, carefully lay out what your model is, then carefully implement it in MATLAB and CVX,

1 Like

Thank you.

Apart from the second point you mentioned(True as you said).

How would I model/represent the same for all the values of variables from 1:tn?

i.e if Pb(tn) takes only the value of the final value(@tn=1000) , how to model the code so that t varies from 1:tn.

    variables Esc(tn) Pb(tn) Psc(tn) Psca(tn)
    minimize (A1*max(0,abs((Pb(tn)/Pb_thres)-1))+A2*max((Pb(tn)-Pb(tn-1))/Pb_thres_var-1)+A3*(Pb(tn)/Vb(tn-1)).^2*Rb...
    subject to 
    Pb(tn)+Psca(tn)-Preq(tn)== 0;
    Psca(tn)== Psc(tn)-abs(Pconv_loss(tn));
    Esc(tn)-Esc(tn-delta)== -Psc(tn)*delta;

This is the exact code layout am working. Besides the decision variables, all other variables are given or calculated before the ‘cvx begin’.

What I am trying to implement is,

  1. Every variable is a function of t as t varies from t=1 to tn.
  2. Even the non decision variables are also function of t(1:tn).
  3. For final result i will have the plot for t vs Pb(t) and other variables.
  4. As of now, the CVX runs, either it shows inaccurate/solved or sometimes solved but not the expected values.

This is based on the Penalty Function approximation given in CVX examples. I have checked most of the examples regarding the same and still checking if anyone has done similar in the forum. If you have come across similar optimization problem then please link the same.

Note: The problem was solved using CVX by the author of the paper. But i am sure my model layout is wrong.

The code is running for “tn” elements by adding one "‘for’ loop inside the CVX portion. Even though not the expected results, but its close to the expected one. Will have to carefully look at the mathematical model for any errors.

Thank you very much.

Is your objective function supposed to only be in terms of the 1000th and 999th elements of various vectors? I don’t know, but that’s what you have done. You are allowed to use sum or a for loop to build up an objective function expression prior to the minimize statement.

You can use a for loop for the constraint, and perhaps you need to do so forEsc(tn)-Esc(tn-delta)== -Psc(tn)*delta;. But for instance,Pb+Psca+Preq== 0; without use of a for loop will impose this equality on all 1000 elements, i.e., specifies 1000 inequalities. Similarly, Esc_min<=Esc<=Esc_max; .

You need to figure out what you are doing with Pb. As I wrote, you’re setting of Pb before cvx_begin is overridden once you declare it a CVX variable after cvx_begin.

1 Like

Thank you, Sir.
After the changes, the code is working fine for all the ‘1000’ elements and got the expected output.