|
Defining a tuple with the matching and # operators |
Top Previous Next |
|
Mappings between tuples can be lengthy and inconvenient to enter via data statements plus difficult to compute. The matching operator (:) can be used to simplify definition and assignment. When using a matching operator one uses the general syntax setsa:setsb where elements of the set or sets specified befoore the : are matched with elements of the set or sets specified after the colon in the order both are specified in GAMS up until the matching is complete or all of the elements of one set or the other have been used. Namely the matching will follow the order of set elements in GAMS with the first element of one set matched with the first element of the second set etc. For example, the statement (matchtuple.gms) Set I / t1*t6:s3*s5 /
matches t1 with s3, t2 with s4 and t3 with s5. The elements t4, t5 and t6 are not matched because the elements in the second set specification are exhausted. The result is the same as the explicit set specification
Set j / t1.s3,t2.s4,t3.s5 / One may also construct all combinations of the elements of 2 sets using notation involving the set name and a # as follows sets h /h1*h5/, d /d1*d20/, dh(d,h) /#d.#h/;
and address whole sets in the matching operation again using the set name and # as follows
sets t /t1*t100/, tdh(t,d,h) /#t:#dh/, dht/#dh:#t/; The resulting set tdh will then have the values:
t1.d1.h1, t2.d1.h2, t3.d1.h3 .. while dht will have d1.h1.t1,d1.h2.t2, d1.h3.t3 ...
An option statement also causes the matching to occur. Namely given the set definitions
set i1 /el1*el5/,j1/jel1*jel10/,k/ka,kb,kc/,l/l1*l200/; Set ijk(I1,j1,k), x(I1,j1,k,l);
Then using an option statement that contains the matching operator (:) also causes the matching to occur. Namely given the command
Option ijk(i1:j1,k), x(ijk:l);
Results in the set ijk being emptied then the set ijk being defined according to a matching of elements of I with j for each k In turn then the x set is defined with the elements of ijk matched with l. |