Solved

Finding the first element of a multi-indexed parameter

  • 28 September 2022
  • 3 replies
  • 53 views

Badge

I have a multi-indexed parameter, say pMatrix(i,j). I want to efficiently get the first element of the matrix.

Applying the First-operator (i.e. First((i,j) | pMatrix(i,j))) did not work; it results in the following error: “The iterative operator first can have only one iterative index.”

I can think of some workarounds (such as to start by finding the first i, and given this i finding the first j), but this is quite cumbersome, especially when there are more than two indices (in our project we sometimes have up to 7 indices).

How should this be elegantly addressed?

icon

Best answer by Chris Kuip 28 September 2022, 11:39

View original

3 replies

Userlevel 5
Badge +7

How about a small for loop? 
For instance the following:

ep_firstI := '’ ;

ep_firstJ := '’ ;

for (i,j) | pMatrix(i,j) do

       ep_firstI := i ;

       ep_firstJ := j ;

       break ;

endfor ;


The condition in the for loop is used by the AIMMS Sparse execution engine to enter the loop only for existing elements in pMatrix.

The break statement ensures that there is at most one iteration in the for loop.

In case pMatrix is empty, then ep_firstI and ep_firstJ remain empty.

Does this help?

Badge

Thank you for the quick reply. This does indeed work nicely - although I had hoped there existed a built-in AIMMS functionality ;-).

Other question: in your response, you initialize ep_firstI and ep_firstJ by an empty element, but this element is enclosed by two different apostrophes. Is this deliberate? If so, what is their significance?

Userlevel 5
Badge +7

No that is not deliberate.  It should have been ''.  Something with this editor.

Reply


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

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