Summation Syntax Question


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}
1 Like