I try to set up a minimization problem. When trying to load the mod-file, I keep getting this Error message:
basemodel.mod, line 4 (offset 86):
D is already defined
context: set >>> D; <<<
I work with the ampl console and want to load the mod-file first, then the dat-file and then solve the problem. The error message appears after loading the mod-file and before loading the dat-file.
I tried to exclude various parameters and variables in hope to identify the code line which is wrong. Alas, every time I tried a new combination of excluded lines, the error persisted.
Here's the code I have written so far:
set D;
param clus {1..k} integer;
param x1 {D};
param x2 {D};
# euclidian distance
param d {i in D, j in D} = sqrt((x1[i]-x1[j])^2 + (x2[i]-x2[j])^2);
var x {i in D, l in clus} binary;
var x {j in D, l in clus} binary;
var D_l {l in k} >= 0;
var D_max;
minimize cost_function: D_max;
subject to C1: D_l[l] >= d[i,j] * (x[i,l] + x[j,l] - 1);
subject to C2 {i in D}: sum {l in k} x[l] = 1;
subject to C3 {i in D}:D_max >= D_l;
I'm guessing that you ran the code once without getting the "D is already defined" error, then tried to rerun it without first clearing the definitions you created in the first run. Set/param/var definitions will hang around until you do something to clear them, so if you rerun the same code you will get errors for anything that's still defined from last run.
I usually begin my AMPL models with a reset;
to clear everything so I don't have to deal with this issue. (Unless I'm planning to run them as part of a larger workflow, of course.)
A couple of other issues with your sample code: