syntaxsetampldefined

AMPL Syntax Error - N is already defined, but i didnt define it


Here is the .dat code:

# ---------- Sets ----------
set N := 1 2 3 4 5 6 7 8 9 10;
set L := 1 2 3;
set N_l[1] := 1 7 8;
set N_l[2] := 2 4 5 6;
set N_l[3] := 3 9 10;

# ---------- Parameters ----------
param c := 1 2 2 1 3 3 4 2 5 2 6 3 7 3 8 2 9 1 10 2;
param p := 1 7 2 6 3 3 4 4 5 5 6 5 7 8 8 7 9 6 10 3;
param r := 1 3 2 4 3 5 4 3 5 2 6 2 7 4 8 5 9 3 10 3;
param budget := 20;
param budget_risk := 30;
param budget_risk_l :=
1 15
2 10
3 3;

and this is what happens if i include it in the ampl console.

ampl: include investment.dat;

investment.dat, line 2 (offset 34):
    N is already defined
context:  set  >>> N  <<< := 1 2 3 4 5 6 7 8 9 10;

I tried to rewrite the set N, to see if i made any mistake there. Whatever I did it didn't change the outcome. Also tried to see if I have anything in my .mod file, which would somehow be correlated, but i couldnt figure it out.


Solution

  • What you have there is just the data for the sets and parameters. Before being able to load data, you need to declare it first in the model file so that AMPL knows the indexing sets.

    For that data file, you can have the following in the model file:

    set N;
    set L;
    set N_l{L};
    param c{N};
    param p{N};
    param r{N};
    param budget;
    param budget_risk;
    param budget_risk_l{L};
    

    The following AMPL script:

    set N;
    set L;
    set N_l{L};
    param c{N};
    param p{N};
    param r{N};
    param budget;
    param budget_risk;
    param budget_risk_l{L};
    
    data;
    # ---------- Sets ----------
    set N := 1 2 3 4 5 6 7 8 9 10;
    set L := 1 2 3;
    set N_l[1] := 1 7 8;
    set N_l[2] := 2 4 5 6;
    set N_l[3] := 3 9 10;
    
    # ---------- Parameters ----------
    param c := 1 2 2 1 3 3 4 2 5 2 6 3 7 3 8 2 9 1 10 2;
    param p := 1 7 2 6 3 3 4 4 5 5 6 5 7 8 8 7 9 6 10 3;
    param r := 1 3 2 4 3 5 4 3 5 2 6 2 7 4 8 5 9 3 10 3;
    param budget := 20;
    param budget_risk := 30;
    param budget_risk_l :=
    1 15
    2 10
    3 3;
    
    model;
    display N, L, N_l, c, p, r, budget, budget_risk, budget_risk_l;
    

    produces the output:

    set N := 1 2 3 4 5 6 7 8 9 10;
    
    set L := 1 2 3;
    
    set N_l[1] := 1 7 8;
    set N_l[2] := 2 4 5 6;
    set N_l[3] := 3 9 10;
    
    :    c   p   r    :=
    1    2   7   3
    2    1   6   4
    3    3   3   5
    4    2   4   3
    5    2   5   2
    6    3   5   2
    7    3   8   4
    8    2   7   5
    9    1   6   3
    10   2   3   3
    ;
    
    budget = 20
    budget_risk = 30
    
    budget_risk_l [*] :=
    1  15
    2  10
    3   3
    ;