1

I want to import elements or members into a GAMS set from other sources, say SQL DB. For example set p plant /p1,p2,p3.../ These elements are successfully imported, however, it seems that they are not ordered, because errors will be reported when I use condition clause like ord(p) > 3 in constraints.

I know that elements are ordered as they initially created. So these imported elements are supposed to follow the sequence as imported. When I display this set, elements are shown as p1, p2, p3...

So I am really confused about the ordering of imported elements. I want to figure out the reason and if there are ways I can fix their order. Thanks.

Harry
  • 331
  • 1
  • 4
  • 14
  • Have a look at $onUELlist to display the order in your listing file. And SortedUELs(*,*) built-in to get the natural ordering. See: https://www.gams.com/latest/docs/UG_OrderedSets.html#UG_OrderedSets_OrderedAndUnorderedSets – Berenger Jun 05 '19 at 19:47
  • Thanks Berenger! – Harry Aug 06 '19 at 16:50

1 Answers1

1

you could add the following after loading the data

ALIAS(*,universe);
display universe;
$exit;

You should then see the order of your loaded set elements and where the misordering might have appeared. If you absolutely cant order the set in your source query (sql db) a hack would be to initiate a helper set before loading the actual data.

set helper /p1*p100000/;
matthes
  • 102
  • 2
  • Hi Matthes, thank you for your response. This method is new to me. It should be very helpful. I realize the problem I have is due to elements. I choose numbers say 1-1000 to represent elements in all sets. Once the first (big) set is imported, the order of all elements are pretty much defined. So for a set imported later say 1, 3, 5, 6, it may be impossible to use ord for this set. It's my understanding, and it may be wrong. I will try helper to see if it can solve the problem. – Harry Aug 06 '19 at 16:50
  • Yes, GAMS tracks every set element inside a universal set. This set is filled in the order of the appearence of the elements. So if your first loaded set contains elements {1,5,10}, then these are the first three elements in the universal set. if you add a second set with elements {1,2,3,4,5} then the universal set is: { 1,5,10,2,3,4 }. In this case the second set appears 'unordered' compared to the universal set, because element '2' sits before element '5' in the second set. The ORD() function won't work for unordered sets. – matthes Aug 07 '19 at 17:10