Solved

sorting a multiple dimensional array by values with in it

  • 18 August 2021
  • 1 reply
  • 132 views

Badge

Hi Aimms Community, 

 

I’m having trouble using the Sort function in order to obtain the values of a mulit-dimensional array:

 

For example:

multi-dimensional array = foo(size,car,color,location) 

            size, car, color, location = some float value

 [ ‘small’, ‘toyota’, ‘white’, ‘usa’] = 1.25

 [ ‘small’, ‘toyota’, ‘green’, ‘greenland’] = .5

 [ ‘small’, ‘nissan’, ‘blue’, ‘france’’] = 1.0

[ ‘small’, ‘nissan’, ‘green’, ‘france’’] = .3

 

In this case we are looking to sort foo by the values for all colors and locations where the size and car are the same, meaning that the result would be put into another variable 

 

bar(size,car,row)

[ ‘small’, ‘toyota’, 1] = .5

[ ‘small’, ‘toyota’, 2] = 1.25

[ ‘small’, ‘nissan’, 1] = .3

[ ‘small’, ‘nissan’, 2] = 1.0

 

How do I approach this problem? I looked through the documentation but I see that the Sort operator is really trying to sort by an index of an array and not the values for given indexes. 

 

Thanks for any help

icon

Best answer by Chris Kuip 22 August 2021, 15:04

View original

1 reply

Userlevel 4
Badge +7

Hello,

 

The concepts 'ordering’ and 'sorting’ in AIMMS pertain to one-dimensional sets. In AIMMS there is no predefined operator that can handle an example as given above by a single call; you will have to do some coding yourself. 

The remainder of this post illustrates one possible approach, perhaps there are also other approaches.

 

There are several remarks about the above code. 

  1. The code handles the input from p_foo per slice.  In your example, by selecting a size and a car manufacturer, a slice is selected.
  2. See line 5: for each slice, the number of elements is counted.
  3. In the for loop of line 7 to line 26, one slice at a time is handled.
  4. On line 10: A set is created that maps to the elements in the slice, number 1 .. n, n=number of elements in the slice.
  5. Each element in the slice is given a number and copied to a one dimensional array p_onedim. Note that each element is handled individually.
  6. As the data to be operated upon is now one-dimensional, the sort operator can be used.
    Important here: the set s_orderedPositions is declared to be "order by user”.
  7. Based on the result of the sort operator, in line 25, the output parameter p_bar is filled.

The application that does this is attached to this post and a screenshot of the interface is shown below.

From the side panel, you can select size and car manufacturer.

 

With kind regards,

 

Chris

Reply


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

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