Skip to main content
Solved

moving elements in a set to the end

  • July 21, 2025
  • 3 replies
  • 34 views

Forum|alt.badge.img+1

Hi,

 Let’s say I have a set consisting of 5 elements such that: Set1 = {‘A’,‘B’,‘C’,‘D’,‘E’}.

How can I move some elements to the end of the set. For example:

Set1 = {‘A’, ‘D’,’E’,’B’,’C’}

 

Thanks!

Best answer by gabiservidone

Below is a step-by-step example with some code to implement!

  1. Define the original set:

    Set Set1 {
    Index : i;
    Members : {'A', 'B', 'C', 'D', 'E'};
    }
  2. Create a parameter to define the sorting order:

    Parameter p_NewOrder {
    IndexDomain : i;
    Definition : Data { 'A': 1, 'D': 2, 'E': 3, 'B': 4, 'C': 5 };
    }
  3. Define a new set with the OrderBy attribute:

    Set OrderedSet1 {
    SubsetOf : Set1;
    OrderBy : p_NewOrder(i);
    }

By executing this code, OrderedSet1 will be automatically sorted based on the values defined in p_NewOrder.

3 replies

gabiservidone
Administrator
Forum|alt.badge.img+6

In AIMMS, this transformation can be achieved by creating a new set where the order of elements is specified dynamically. A structured approach is as follows:

  1. Define the Original Set: Create the initial set (e.g., Set1) and populate it with the predefined elements: {'A', 'B', 'C', 'D', 'E'}.

  2. Create a Parameter: Define a parameter to represent the new order. Assign indices to rearrange the elements. For instance, define a parameter p_NewOrder(i) where i represents the elements of Set1. Assign values corresponding to the target order: {'A': 1, 'D': 2, 'E': 3, 'B': 4, 'C': 5}.

  3. Sort Dynamically: Utilize the indexing capabilities to sort the set dynamically based on the parameter values. AIMMS allows reorganization of sets using such logical parameters.

 


Forum|alt.badge.img+1
  • Author
  • Enthusiast
  • July 22, 2025

Thank you so much for your detailed response.

I got a bit lost on the third step. Would you mind sharing the code you would use to do this on your end?

 

Thanks a lot.


gabiservidone
Administrator
Forum|alt.badge.img+6
  • AIMMSian
  • Answer
  • July 22, 2025

Below is a step-by-step example with some code to implement!

  1. Define the original set:

    Set Set1 {
    Index : i;
    Members : {'A', 'B', 'C', 'D', 'E'};
    }
  2. Create a parameter to define the sorting order:

    Parameter p_NewOrder {
    IndexDomain : i;
    Definition : Data { 'A': 1, 'D': 2, 'E': 3, 'B': 4, 'C': 5 };
    }
  3. Define a new set with the OrderBy attribute:

    Set OrderedSet1 {
    SubsetOf : Set1;
    OrderBy : p_NewOrder(i);
    }

By executing this code, OrderedSet1 will be automatically sorted based on the values defined in p_NewOrder.


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

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