Possibility of using dynamic variable names

10 views
Skip to first unread message

Lothar Löwer

unread,
Oct 24, 2023, 5:47:38 AM10/24/23
to AMPL Modeling Language
Hi,

is there any possibility to use a string (stored as a parameter) as a variable name?
As an example:

var v_bra_p_12 {x,y,z};         # <- variable defined using three subscripts
param test symbolic default '';
let test := 'v_bra_p_12';

display (test)[1,1,23];      # <- 1st try to access v_bra_p_12[1,1,23]
display ($test)[1,1,23];    # <- 2nd try to access v_bra_p_12[1,1,23]

Best regards,
Lothar

AMPL Google Group

unread,
Oct 24, 2023, 3:07:25 PM10/24/23
to AMPL Modeling Language
AMPL's var commands do not have a feature for letting a string represent a variable's name. It is possible to get this effect, however, by writing a var command to a file and then reading it. For example,

set X = 1..5;
set Y = 1..3;
set Z = 1..25;

var v_bra_p_12 {x in X, y in Y, z in Z} := x + y + z;

param test symbolic default '';
let test := 'v_bra_p_12';

printf "display %s[1,1,23];\n", test >tempfile;
include tempfile;

When I put these statements in a file test.run and execute it, I get the desired result:

ampl: include ..\test.run;
v_bra_p_12[1,1,23] = 25


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
{#HS:2399233765-120179#}

Lothar Löwer

unread,
Oct 25, 2023, 7:07:14 AM10/25/23
to AMPL Modeling Language
Ok, that's at least a workaround. I will think about it. Again, than's a lot Robert for the fast reply!

Bestr regards,
Lothar
Reply all
Reply to author
Forward
0 new messages