cuca
cuca

Reputation: 23

Define the function for distance matrix in ampl. Keep getting "i is not defined"

I'm trying to set up a ampl model which clusters given points in a 2-dimensional space according to the model of Saglam et al(2005). For testing purposes I want to generate randomly some datapoints and then calculate the euclidian distance matrix for them (since I need this one). I'm aware that I could only make the distance matrix without the data points but in a later step the data points will be given and then I need to calculate the distances between each the points.

Below you'll find the code I've written so far. While loading the model I keep getting the error message "i is not defined". Since i is a subscript that should run over x1 and x1 is a parameter which is defined over the set D and have one subscript, I cannot figure out why this code should be invalid. As far as I understand, I don't have to define variables if I use them only as subscripts?

reset;

# parameters to define clustered
param m; # numbers of data points
param n; # numbers of clusters

# sets
set D := 1..m; #points to be clustered
set L := 1..n; #clusters

# randomly generate datapoints
param x1 {D} = Uniform(1,m);
param x2 {D} = Uniform(1,m);
param d {D,D} = sqrt((x1[i]-x1[j])^2 + (x2[i]-x2[j])^2);

# variables
var x {D, L} binary;
var D_l {L} >=0;
var D_max >= 0;

#minimization funcion
minimize max_clus_dis: D_max;

# constraints
subject to C1 {i in D, j in D, l in L}: D_l[l] >= d[i,j] * (x[i,l] + x[j,l] - 1);
subject to C2 {i in D}: sum{l in L} x[i,l] = 1;
subject to C3 {l in L}: D_max >= D_l[l];

So far I tried to change the line form param x1 to

param x1 {i in D, j in D} = ...

as well as

param d {x1, x2} = ...

Alas, nothing of this helped. So, any help someone can offer is deeply appreciated. I searched the web but I found nothing useful for my task.

Upvotes: 0

Views: 916

Answers (1)

cuca
cuca

Reputation: 23

I found eventually what was missing. The line in which I calculated the parameter d should be

param d {i in D, j in D} = sqrt((x1[i]-x1[j])^2 + (x2[i]-x2[j])^2);

Retrospectively it's clear that the subscripts i and j should have been mentioned on the line, I don't know how I could miss that.

Upvotes: 0

Related Questions