Search code examples
ampl

AMPL - param that maps from set to set


I'm new to AMPL, and I would like to create a param C that map from a set A to set B:

file.mod:

set A;
set B;
param C{i in A} =
    if i == "AA"
    then
        BA
    else if i == "AB"
    then
        BB
    else if i == "AC"
    then
        BC
    else
        BA;

data file.dat;

file.dat:

data;

set A := AA, AB, AC;
set B := BA, BB, BC;

When I try to compile this code, I get BA is not defined. If I replace the set element by string (BA becomes "BA"), then the error `Cannot convert character string to a number.``showed up. Is there a way to achieve what I want to do?


Solution

  • Parameters in AMPL default to number. If you want to set a string parameter, you have to declare it as symbolic. (And yes, you need the quotes on those values.)

    This seems to do what you want:

    set A;
    set B;
    param C{i in A} in B symbolic =
        if i == "AA"
        then
            "BA"
        else if i == "AB"
        then
            "BB"
        else if i == "AC"
        then
            "BC"
        else
            "BA";
    
     data;
    
     set A := AA, AB, AC;
     set B := BA, BB, BC;
    

    See section 7.8 of the AMPL Book for more information about symbolic parameters.