ContentsIndexHome
PreviousUpNext
Reuse of tuples

Using the Tuple class allows for fast prototyping, but if positions are used multiple times, the ITuple interface is more efficient.

In the Basic Example, a Tuple is created each time a value is assigned to a position:

  parameterUnitTransportCost.setValue(new Tuple("London", "Oslo"), 7.0);
  parameterUnitTransportCost.setValue(new Tuple("London", "Madrid"), 12.0);

Not only is a Tuple object created twice, also the label "London" is looked up twice to find the corresponding element, and the domain set is checked to see whether it contains that element. 

An ITuple can be used to avoid these inefficiencies. Use the [] operator (in C++ of C#) or getElement method (in Java) to alter labels efficiently. Consider the code snippet below:

  ITuple tuple = parameterUnitTransportCost.createTuple();
  tuple[0].Label = "London";
  tuple[1].Label = "Oslo";
  parameterUnitTransportCost.setValue(tuple, 7.0);

  tuple[1].Label = "Madrid";
  parameterUnitTransportCost.setValue(tuple, 12.0);

On the first line, an ITuple is created. An ITuple consists of IElements which have references to the domains of the data. The labels are looked up and validated directly at the assign of the label. To assign the tuple ("London", "Madrid"), to the parameter, only the element on the second dimension has to be altered. 

The two snippets have exactly the same results, and can be applied under the same circumstances. It makes sense to use the more elaborate ITuple object, if you are transferring large amounts of data (e.g. inside a for loop), and you want to minimize the time spent in object creation and validation. 

Another use of ITuples is possible if the set values are known by their order. in that case the IElements of the ITuple can be initialized by setting the Ordinal. Let's say that "London" is the first element of the first index domain of the parameter, and "Oslo" and "Madrid" are the first and second element of the second index domain:

  ITuple tuple = parameterUnitTransportCost.createTuple();
  tuple[0].Ordinal = 1;
  tuple[1].Ordinal = 1;
  parameterUnitTransportCost.setValue(tuple, 7.0);

  tuple[1].Ordinal = 2;
  parameterUnitTransportCost.setValue(tuple, 12.0);

This eliminates all string lookups, but will not be applicable in all cases. Be sure to understand ordinals before using them, see also the topic on ordinals.