Creating a set of parameters

71 views
Skip to first unread message

Shiva, Susan

unread,
Jan 9, 2015, 4:24:42 PM1/9/15
to am...@googlegroups.com

AMPL Support,

 

                I’m attempting to print output from my model in an automated fashion. I have an excel sheet I read from that contains the list I want to print. I’m having trouble reading my table.

My code looks like the following:

               

                print.xlsx

                Name    PrintExpression

                n1           1var[t]+2var[2]

                n2           3var[t]

 

                ampl code:

                param T:=24;

                var 1var {t in 1..T};

                var 2var{t in 1..T};

                var 3var{t in 1..T};

 

                set Names;

                param PrintExpressions{Names};

 

                table table_name IN “ODBC” “print.xlsx”:

                                Names <- [s  ~ Name],

                                (PrintExpressions[s]) ~PrintExpression;

                read table table_name;

 

                I get the following type of error:  Cannot assign '1var[t]+2var[t]' to PrintExpressions['n1']

      

How can I make this work?   After the model solves, I want to be able to print these expressions to a database. I have 1,000’s of print statements, and that’s why I’m opting for automating it.

 

 

Thank you,

 

Susan Shiva

Power Supply Analyst

 

Avista Corporation

1411 E. Mission Ave.

Spokane, WA 99202

susan...@avistacorp.com

Work (509) 495-2710

Cell (509) 703-0102

 

Robert Fourer

unread,
Jan 10, 2015, 11:38:21 AM1/10/15
to am...@googlegroups.com
Your immediate problem is that PrintExpressions is (by default) a numerical parameter, while the values for it in the spreadsheet are character strings. If you define instead

param PrintExpressions{Names} symbolic;

then AMPL will treat PrintExpressions as a string-valued parameter, and you will be a able to execute the "read table" statement without getting the "Cannot assign ..." error.

But also, there isn't any builtin AMPL feature for interpreting string values as AMPL expressions. There's a way of getting around this by writing something like the following:

for {n in Names} {
print PrintExpressions[n] >tmp.txt;
print {t in 1..T}: "%d\n", include tmp.txt;
close tmp.txt;
}

This relies on fact that in general, an include statement can be placed anywhere within another AMPL statement, and it will have the effect of replacing itself with the contents of the specified file.

Bob Fourer
am...@googlegroups.com

=======
Reply all
Reply to author
Forward
0 new messages