Element text comparison: Sameas and Diag

Top  Previous  Next

One may wish to do conditional processing dependent upon the text defining a name of a set element matching the text for a particular text string or matching up with the text for a name of  a set element in another set.  This can be done in GAMS using the sameas or diag commands. SAMEAS returns a value of true or false using the syntax

 

SAMEAS(setelement,othersetelement)

or

sameas(asetelement,"texttotest")

 

where the first sameas is true if the name of setelement is the same as the name of othersetelement and is false otherwise.  Similarly, sameas(asetelement,"texttotest") is true if name of asetelement is the same as the texttotest and false otherwise.

DIAG works identically but returns numerical values of one if the text matches and zero otherwise.

 

Diag(setelement,othersetelement)

or

Diag(asetelement,"text")

Examples:

(setconditional.gms)

The following red uses of sameas and diag will only permit the case of I and j to be part of the sum where the elements for both the same and in this case will only work for the element named Boston and do not require the sets to be subsets of each other.  The blue cases will only operate for the element of I which has the name "new york".

 

Set         cityI         / "new york", Chicago, boston/;

Set         cityj         /boston/;

Scalar ciz,cir,cirr;

ciZ=sum(sameas(cityI,cityj),1);

ciR=sum((cityI,cityj)$ sameas(cityI,cityj),1);

ciRR=sum(sameas(cityI,"new york"),1);

ciZ=sum((cityi,cityj)$diag(cityI,cityj) ,1);

ciRR=sum(cityi$diag(cityI,"new york"),1);