Using the let command with an indexing expression

44 views
Skip to first unread message

Mahmoud Kamel

unread,
Sep 10, 2014, 2:48:05 PM9/10/14
to am...@googlegroups.com
Hello AMPLers
       
     I am using the let command to update a param in my model with the value of the optimization variable after solving the model to iterate and solve with the new value of this parameter 
I wrote the statement like:

 
let {n in 1..N, t in 1..T, c in 1..C} pt[n,t,c] := p[n,t,c];

where the variable is defined as 

var p {n in 1..N, t in 1..T, c in 1..C} >=0   <= Ptotal ;

and the parameter is defined as

param pt {n in 1..N, t in 1..T, c in 1..C}  >= 0 <= Ptotal default  Ptotal / C;

but I receive a syntax error :

dc.run, line 17 (offset 274):
 syntax error


so what is wrong with my expression??


victor.z...@gmail.com

unread,
Sep 10, 2014, 3:01:16 PM9/10/14
to am...@googlegroups.com
Hi Mahmoud,

I don't see anything wrong with your code provided that N, T, C and Ptotal are defined appropriately. Could you give the full error message (including the part with "context") as well as show what's in line 17 and around it.

Victor

--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
To post to this group, send email to am...@googlegroups.com.
Visit this group at http://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.

Mahmoud Kamel

unread,
Sep 10, 2014, 3:23:37 PM9/10/14
to am...@googlegroups.com
Thanks Vector

   the context is :
options solver snopt; 
reset;
model DC_prog.mod;
data main.dat;
data channel.dat;
data power.dat;
data x.dat;
#data pt.dat;
#drop ServiceContract;
for {t in 1..10}
{
solve;
let {n in 1..N , t in 1..T , c in 1..C} pt[n,t,c] := p[n,t,c]; # this is line 17


}

Mahmoud Kamel

unread,
Sep 10, 2014, 3:33:23 PM9/10/14
to am...@googlegroups.com
Ohhhhh, I have found it ....
it is the t in the for statement   
Message has been deleted

maziar kermani

unread,
Sep 19, 2016, 10:40:25 AM9/19/16
to AMPL Modeling Language
Hi,

I have a question on the difference between a stand-alone "let" command and a for loop:

for the moment I have:

let {lhc in HeatCascades, lc in ClustersOfLayer[lhc], t in Time, s in HC_Hot_loc[lhc,lc,t]}         Streams_Tin[lhc,s,t]  := Streams_Tin[lhc,s,t]  - dt[lhc,s,t];
let {lhc in HeatCascades, lc in ClustersOfLayer[lhc], t in Time, s in HC_Hot_loc[lhc,lc,t]}         Streams_Tout[lhc,s,t] := Streams_Tout[lhc,s,t]  - dt[lhc,s,t];
let {lhc in HeatCascades, lc in ClustersOfLayer[lhc], t in Time, s in HC_Cold_loc[lhc,lc,t]}  Streams_Tin[lhc,s,t]  := Streams_Tin[lhc,s,t]  + dt[lhc,s,t];
let {lhc in HeatCascades, lc in ClustersOfLayer[lhc], t in Time, s in HC_Cold_loc[lhc,lc,t]}  Streams_Tout[lhc,s,t] := Streams_Tout[lhc,s,t]  + dt[lhc,s,t];

However what I can do is:

for {lhc in HeatCascades, lc in ClustersOfLayer[lhc], , t in Time, s in HC_Hot_loc[lhc,lc,t] union HC_Cold_loc[lhc,lc,t]}
{ if (Streams_Tin[lhc,s,t] > Streams_Tout[lhc,s,t]) then
{ let Streams_Tin[lhc,s,t]  := Streams_Tin[lhc,s,t] - dt[lhc,s,t];
let Streams_Tout[lhc,s,t]  := Streams_Tout[lhc,s,t] - dt[lhc,s,t];
}
if (Streams_Tin[lhc,s,t] < Streams_Tout[lhc,s,t]) then
{ let Streams_Tin[lhc,s,t]  := Streams_Tin[lhc,s,t] + dt[lhc,s,t];
let Streams_Tout[lhc,s,t]  := Streams_Tout[lhc,s,t] + dt[lhc,s,t];
}
}

I was wondering if there is a difference in their performances. and if there is any other way to imrpove it.

many thanks in advance,
Maziar

Robert Fourer

unread,
Sep 19, 2016, 1:10:04 PM9/19/16
to am...@googlegroups.com
In general, AMPL will be much faster executing an indexed "let" command than executing many individual "let" commands in a loop. However it looks like what you want is

let {lhc in HeatCascades, lc in ClustersOfLayer[lhc], t in Time, s in HC_Hot_loc[lhc,lc,t]}
Streams_Tin[lhc,s,t] :=
if (Streams_Tin[lhc,s,t] > Streams_Tout[lhc,s,t])
then Streams_Tin[lhc,s,t] - dt[lhc,s,t]
else Streams_Tin[lhc,s,t] + dt[lhc,s,t];

and similarly for Streams_Tout.

Bob Fourer
am...@googlegroups.com

=======

maziar kermani

unread,
Sep 19, 2016, 1:17:45 PM9/19/16
to AMPL Modeling Language
Thanks a lot for your reply. Having what you said i can reduce the problem to two indexed let commands with if condition.
Reply all
Reply to author
Forward
0 new messages