Skip to main content
Solved

Warning : FOR loop nested statement unnecessary.

  • January 25, 2022
  • 2 replies
  • 173 views

Forum|alt.badge.img

Hi,

I have the following warning : Usage of directly nested for statements is unnecessary and inefficient.

How am I supposed to write this code to avoid the warning ?

 

for gdm::iNode do

    for (gdm::iTime in gdm::calHorizon) do

        p1(gdm::iNode):=p2(gdm::iNode,gdm::iTime)*p3(gdm::iNode) ; 

    endfor;

endfor;

 

Thank you
 

Best answer by gdiepen

Often you will not need explicit for-loops in AIMMS. The big advantage of not using them is also speed-up of your code ;)

With regards to the nested for-loop, you could rewrite this to something like:

for (gdm::iNode, gdm::iTime) | gdm::iTime in gdm::calHorizon do
...
endfor ;

This means, loop over all combinations of iNode and iTime for which holds that iTime is in calHorizon.

However, it is not clear here what you want to do with the value of p1. Right now it would always take the p2 value of the last gdm::iTime and multiple that with the p3 value. So if you have multiple iTimes, it woud always calculate the p1 with the first iTime, then the second element, then the third, etc.

Is this what you want to achieve, or should p2 be the sum?

In both cases, you might be able to write just a single assignment statement that would do this assignment without an explicit for-loop.

2 replies

gdiepen
AIMMS Champ
Forum|alt.badge.img+7
  • AIMMS Champ
  • 155 replies
  • Answer
  • January 26, 2022

Often you will not need explicit for-loops in AIMMS. The big advantage of not using them is also speed-up of your code ;)

With regards to the nested for-loop, you could rewrite this to something like:

for (gdm::iNode, gdm::iTime) | gdm::iTime in gdm::calHorizon do
...
endfor ;

This means, loop over all combinations of iNode and iTime for which holds that iTime is in calHorizon.

However, it is not clear here what you want to do with the value of p1. Right now it would always take the p2 value of the last gdm::iTime and multiple that with the p3 value. So if you have multiple iTimes, it woud always calculate the p1 with the first iTime, then the second element, then the third, etc.

Is this what you want to achieve, or should p2 be the sum?

In both cases, you might be able to write just a single assignment statement that would do this assignment without an explicit for-loop.


Forum|alt.badge.img
  • Author
  • Explorer
  • 1 reply
  • January 26, 2022

Your answer is perfect thank you !



Didn't find what you were looking for? Try searching on our documentation pages:

AIMMS Developer & PRO | AIMMS How-To | AIMMS SC Navigator