2

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?

Cynnexis
  • 63
  • 1
  • 5
  • I don't know much about AMPL however, I can tell you that `BA is not defined` error is the wrong one to fix. You should put " " around both BA, BB, and BC. The reason you need to convert them into strings is because your other values in the array have to be the same object type. I can't figure out why you'd see `Cannot convert character string to a number`, I hope someone else can help with that. – Tigerjz32 Sep 21 '18 at 14:35
  • I tried to put `" "` around BA, BB, and BC and I still get `Cannot convert character string to a number. context: "BA" >>> ; <<< ` error. – Cynnexis Sep 21 '18 at 14:38
  • What does `data test.dat;` do? Is there a way to print C right after `else BA;`? – Tigerjz32 Sep 21 '18 at 14:41
  • I made a mistake: `test.dat` is actually `file.dat`. I tried to put `display C;` after the declaration of `C` but the error is raised before this instruction is reached. – Cynnexis Sep 21 '18 at 14:49
  • What does `param C` mean in `param C{i in A}`? Seems that we're defining that variable C as an int array. Sorry, I am not too familiar with this but just trying to help. – Tigerjz32 Sep 21 '18 at 14:55
  • 1
    As far as I know, `param` is the equivalent of **dictionary** in python (or **HashMap** in Java). Here, I define `C` as a dictionary where the key-set is `A`. However, I don't know if it is possible to have a value-set which is not integers or real numbers in AMPL. – Cynnexis Sep 21 '18 at 15:20

1 Answers1

1

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.