# GAMS Support Wiki

### Site Tools

gams:reverse_loops_in_gams

# Differences

This shows you the differences between two versions of the page.

 gams:reverse_loops_in_gams [2007/03/20 09:21]127.0.0.1 external edit gams:reverse_loops_in_gams [2020/05/20 11:44] (current)Frederik Fiand 2020/05/20 11:44 Frederik Fiand 2007/03/20 09:21 external edit 2020/05/20 11:44 Frederik Fiand 2007/03/20 09:21 external edit Line 9: Line 9: alias (i,ri); alias (i,ri); set revi(i,ri); set revi(i,ri); - * the idea is to populate the sw-ne diagonal of revi. + * The idea is to populate the sw-ne diagonal of revi. - * then looping through i in forward order gives us + * Then looping through i in forward order gives us - * i in reverse order by taking the second index of revi + * i in reverse order by taking the second index of revi. - * this will work, but it is very slow, o(n^2)! + * The follwing assignment would create revi, but it is very slow, o(n^2)! - * revi(i,​ri)\$(ord(i) + ord(ri) eq n+1) = yes; + * revi(i,​ri)\$(ord(i) + ord(ri) eq card(i)+1) = yes; - * this is much faster, o(n) + * + * This formulation ​is much faster, o(n). revi(i,​i+[card(i)-2*ord(i)+1]) = yes; revi(i,​i+[card(i)-2*ord(i)+1]) = yes; + parameter c(i); parameter c(i); c(i) = uniform(0,​1);​ c(i) = uniform(0,​1);​ Line 38: Line 40: 1                   0.17 1                   0.17 ​ - Just a note on efficiency: The loop over revi goes very fast, since we are taking the first index, i, in order, and each row or revi has only one element. However, accessing c(ri) in reverse order is not so efficient, and may be a problem for very large i. To see this work, increase the dimension of set i and  ​run the model using profiling. + Just a note on efficiency: The loop over revi goes very fast, since we are taking the first index, i, in order, and each row of revi has only one element. However, accessing c(ri) in reverse order is not so efficient, and may be a problem for very large i. + To see this work, increase the dimension of set i, run the model using [[https://​www.gams.com/​latest/​docs/​UG_GamsCall.html#​GAMSAOprofile|profiling]] and analyze what happens if you replace the "​slow"​ <​code>​put ri.tl, c(ri)/;​ by <​code>​put i.tl, c(i)/;​. 