Summation Syntax Question

Hello,

I am trying to use a double summation in my Objective function - first sum over i from 1 to n, second sum over j from 1 to n ; where j is not i.

Not sure how to write this in AMPL, so what I got is

(sum{i in 1..n, j in 1..n}
  ((if j=i then 0 else 1) * rho[i,j]*sigma[i]*sigma[j]*x[i]*x[j]))

Is this accomplishing what I hope to accomplish?

The best way is to include ā€œj not equal to iā€ in the summation set, like this:

(sum{i in 1..n, j in 1..n: j!=i}
   (rho[i,j]*sigma[i]*sigma[j]*x[i]*x[j]))
1 Like