mathprog

Lookup table for strings (i.e., dictionary of strings/symbols) in MathProg?


Is there a way in MathProg to have strings associated with integers with which I can use to output labels for an answer?

For instance, the following code does not work, but hopefully conveys what I'm trying to do:

# attempt via set results in error, "mylabels cannot be subscripted":
set mylabels := "label1" "label2" "label3";
printf "first label: %s\n", mylabels[1];

# attempt via param results in error, "mylabels requires numeric data":
param mylabels := 
  1 "label1"
  2 "label2"
  3 "label3";
printf "first label: %s\n", mylabels[1];

I also found a discussion on the lack of ordered sets in MathProg, and a suggested hack sounded like it may help me, but didn't work:

# attempt via ordered-set hack results in error, "no value for mylabels_ref[1]":
set mylabels := "label1" "label2" "label3";
param mylabels_ref{i in 1..card(mylabels)}, symbolic, in mylabels;
printf "first label: %s\n", mylabels_ref[1];

This is obviously not a huge deal since I can do the lookup outside of MathProg (i.e., manually or w/ some other scripting language); but I'm mainly just curious if the syntax supports what I'm looking to do.


Solution

  • Late answer but this can probably help someone who wants a similar lookup.

    What is actually working (without hacking to much) is a mix of both of your approaches. You will need a set containing your labels (since params can only hold numerical values) and you will also need a param for the lookup.

    set mylabels;
    param mylabels2{i in mylabels};
    
    for {i in mylabels}{
        for{{0}: mylabels2[i] = 1}
            printf "\n first label: %s\n\n", i;
    }
    
    data;
    set mylabels := label1 label2 label3;
    param mylabels2 := 
      label1 1
      label2 2 
      label3 3;
    end;
    

    In the first for-loop we simply loop over all labels. The second for-loop is a conditional which is doing the lookup. The print statement is only executed when the parameter indexed by the label equals the given value.