Formulating Objective Function Summed over Indices


I am trying to formulate a somewhat complicated objective function. This is my current code:

minimize ((DC* de) + (W(M,S,P) * ED(S) * EP ( P ) ) + sum(E(J) * e+ T(J) * t(J)));

This is how these terms are defined as:

P = 32;

variable de Nonnegative;

variable W(M,S,P) binary;
ED= [0 5 2 6 1]; %corresponding to S from 1 to 5
EP = [0.093 0.034 0.027 … 0.092]; %corresponding to P from 1 to 32

variable E(J,1) Nonnegative integer;
e = [2 3 2 1]; %corresponding to J from 1 to 4

variable T(J,1) Nonnegative integer;
t= [3 3 1 4]; %corresponding to J from 1 to 4

This is what mathermatical formulation looks like:
((DC*de) + ∑(i∈M)∑(sϵS)∑(pϵP)(W(i,s,p) * ED(s) * EP ( p ) ) +∑jϵJ(E(j) * e(j) + T(j) * t(j)))

I am not getting any errors particularly to objective function but I want to make sure if this is the correct way to forumulate it. And if not, I would really appreciate someone pointing me in the right direction.

Thank you in advance!

You seem to just have one term (just the “last” term) in W(M,S,P) * ED(S) * EP§, not a triple summation. Getting that correct is really a MATLAB, not CVX, matter. If you make everything a numerical MATLAB variable rather than CVX variable, whatever expression you formulate should correctly evaluate to your desired objective function for those inputs. Once you have done that, declare the CVX variables as such, and your code should be correct. You are allowed to use for loops to build up CVX expressions.

edit: I missed closing parenthesis after the EP§. It is supposed to be a triple summation over all the indices of W, multiplied with ED and EP over their respective index.

The summation of the 2nd part should look like this:

[W(1,1,1) * ED (1) * EP(1)] + [W(2,1,1) * ED (1) * EP (1)] + [W(1,2,1) * ED (2) * EP (1)] + …+ [W(2,2,1) * ED (2) * EP (1)] + … + [W(1,1,32) * ED(1) * EP(32)] +…+ [W (2,5,32) * ED(5) * (32)]

I have used for loops to model the constraints. Are they allowed to model the objective function as well? Like breaking the objective function down into more constraints?

I am sorry I could not exactly understand the things you mentioned in your reply. But I greatly appreciate your input and help!

Thank you very much!!

You can do this:

 objective = 0
 for i = 1:10   
   objective = objective + a(i)

You can build up any CVX expression that way, whether used in the objective or constraints…
You can make double loops, triple loops, whatever.

If you build up an expression using only numerical MATLAB variables, i.e., outside of CVX and it evaluates correctly in MATLAB, then it should also be correct in CVX (if CVX accepts it) and some of those variables are declared as CVX variables - that is a way of checking you have the formula correct in CVX.

Really appreciate you showing me this technique! I evaluated all my for loops for the indices that were going in the constraints but now I will use this technique to check.

However, will I not need to declare “objective”, “a”, and “i” as symbols/variables?

Thank you very much

If you add CVX variables and expressions to objective, it will automatically become a CVX expression without any explicit declaration needed. In my example, presumably `a(i) was already a CVX variable or expression. Just add whatever terms you want each time through the for loop. The for loop index will never and can never be a CVX variable.