Sparse matrix in objective function


I have the following issues with a model I’m working on.

This is my of:

minimize obj: sum {i in I, j in J} x[i,j] * 
  sqrt((C2[j] - C2[i])^2 + (C1[j] - C1[i])^2) *
  (M1[i,j]*L1/2 + M2[i,j]*L2);

First of all, I would like to exclude when i=j as it doesn’t make sense. In addition, the sqrt would cause trouble.

Secondly, M1 and M2 are sparse matrix of IxJ, but I only enter as parms the values that are 1. The rest of the values no as it would be huge. This also causes trouble because when I run this, it says that there’s no value in M1 and M2. Apart from this, M1 and M2 have different values.

To exclude cases where i = j, you can write

minimize obj: sum {i in I, j in J: i != j} ...

The simplest way to represent M1 and M2 is to define

param M1 {I,J} default 0;
param M2 {I,J} default 0;

Then if no value is given for some M1[i,j] or M2[i,j], 0 will be assumed.