Version 2 (modified by Thermo, 8 years ago) (diff)

--

## Static sets

A static set is a set, which members members are assigned directly by the user and do not change. The members are enumerated completely before and then linked to the set. The used enum-statement is not part of FlopC++ but of C++.

 enum {Opladen, Suelz, numStat}; MP_set Stat(numStat); Sources Example Sets.1

In example Sets.1, the members of set Stat are Opladen, Suelz and numStat. They equal three integer constants 0(Opladen), 1 (Suelz) and 2 (numStat). There are some rules for the enumeration of the members.

• It is not allowed to use multiword names like "Op Laden".
• Members have to be unique.
• Members have to be strings.
• Members have to be enumerated completely.

To illustrate rule two and three, a small example is given.

 enum {Opladen, Suelz, numStat}; enum {Opladen, 721, x721, numDest}; MP_set Stat(numStat); Sources MP_set Dest(numDest); Destinations Example Sets.2

In example Sets.2, Opladen is a member of the sets Stat and Dest. This is a redefinition and therefor not allowed, whereas for example OpladenS in Stat and OpladenD in Dest are possible options.

The use of the label 721 is not possible. Opladen is the first element of Stat, Suelz the second element. Generally enumerations start with index 0, so that Opladen is equivalent to 0 and Suelz is equivalent to 1. So there are two ways the first member of a set can be addressed.

 enum {Opladen, 721, numStat}; MP_set Stat(numStat); Sources ... MP_data Test{Stat}; Test(Opladen)=345; OR (Note: OR is used in this case to emphasize the two ways to assgin 345 to Test(Opladen)) Test(0)=345; Example Sets.3

In example Sets.3, it can be seen, that Test(Opladen)=345; and Test(0)=345; are equivalent statements. As FlopC++ considers 721 not to be a string, it is logic, that Test(721)=23; and Test(1)=23; cannot be equivalent statements. A possible solution to this problem is to add a character like x721 or 721x.

In GAMS, the operator * can be used to shorten the enumeration of the members of a set.

 GAMS Statement Set x months /mon1*mon12/; Example Sets.4

This means that set x consists of twelve members. The name of the first member is mon1, the name of the last is mon12. In FlopC++, the operator * can't be used to do something like that. So the members of each set have to be enumerated completely. In this case, the equivalent formulation is given in the following example.

 C++/FlopC++ Statement enum {mon1, mon2, mon3, mon4, mon5, mon6, mon7,mon8, mon9, mon10, mon11, mon12, nMon}; MP_set x {nMon}; Example Sets.5

Another use of the operator * in GAMS is given in the following example.

 GAMS Statement Set x months /1*12/; Example Sets.6

In FlopC++ a way to declare sets without the explicit enumeration of each member is also offered.

 FlopC++ Statement MP_set x{12}; Example Sets.7

In example Sets.1, numStat has a couple of functions. It sizes the set Stat and links its elements to the set Stat. It is allowed to use numStat to create other sets.

 enum {Opladen, Suelz, numStat}; MP_set Stat(numStat); Sources MP_set Kopie(numStat); Copy Example Sets.8

The syntax given in example Sets.8 should not be misunderstood as a FlopC++-Version of the Alias-Statement in GAMS.

 enum {Opladen, Suelz, numStat}; MP_set Stat(numStat); Sources MP_index Stattilde; Example Sets.9

Further information about the use of MP_index in equations etc. are given in the following.

## Dynamic sets

A dynamic set is a set, whose membership can change. In FlopC++, dynamic sets are called subsets. The number between the "<>" denotes the dimension of the subset. Right now, FlopC++ has a limit of five dimensions.

 enum {Opladen, Suelz, numStat}; MP_set Stat(numStat); Sources MP_subset<2> SD(Stat,4); MP_subset<1> S(Stat); MP_subset<3> SDREI(Stat,4,2); SD.insert(Opladen,0); SD.insert(Suelz, 1); S.insert(Stat); SDREI.insert(Stat,2,1); Example Sets.10

In general, the membership to dynamic sets are assigned by insert. This is equivalent to the yes-Syntax in GAMS. Unfortunately, there is no no-Syntax in GAMS yet. In example Sets.10 the insert-Syntax effects, that only the combinations of Opladen and 0 and of Suelz and 1 are members of SD.

It is possible to do assignments over the domain of dynamic sets. In the example Sets.10, all possible members are assigned to S. In the last line, a combination of the two described options is given.

## Set projection

 MP_set Cities(10); MP_set Products(10); MP_subset<3> Routes(Products,Cities,Cities); MP_subset<2> Suppliers(Products,Cities); ... Suppliers(p,o) <<= Routes(p, o, MP_index::Any); Example Sets.11

Members have been inserted into set Routes. A part of this set is now projected into set Suppliers. As these two sets are of different dimensions, MP_index::Any is used to suppress unnecessary dimension.

## Such that

In GAMS, the dollar controls are important to do indexed operations like sum and prod. In FlopC++, the equivalent to \$ is such_that.

 enum {Opladen, Suelz, numStat}; enum {London, Cali, Paris, numDest}; MP_set Stat(numStat); Sources MP_set Dest(numDest); Destinations MP_subset<2> SD(Stat,Dest); SD.insert(Opladen,Cali); SD.insert(Suelz, Paris); MP_data cost(Stat, Dest); MP_data total(Dest); cost(Stat,Dest)=24; total(Dest)=sum(Stat.such_that(SD(Stat,Dest)), cost(Stat,Dest)); Example Sets.12

The such_that statement effects that only the members of SD are respected and not all members of Stat. The results are total(London)=0, total(Cali)=24, total(Paris)=24. Otherwise all results are 48.

The last expression can be shortened to total(Dest)=sum(SD(Stat,Dest), cost(Stat,Dest));. Note that syntax like SD(Stat,Cali), SD(Opladen, Cali) or SD(0,1) are also allowed.

### Such that and alias statement

To use another name to a previously declared set, it is necessary to use MP_index. This is equivalent to an Alias-Statement in GAMS.

 enum {Opladen, Suelz, numStat}; enum {London, Cali, Paris, numDest}; MP_set Stat(numStat); Sources MP_set Dest(numDest); Destinations MP_subset<2> SD(Stat,Dest); SD.insert(Opladen,Cali); SD.insert(Suelz, Paris); MP_data cost(Stat, Dest); MP_data total(Dest); MP_index Stattilde; cost(Stat,Dest)=24; total(Dest)=sum(Stat(Stattilde).such_that(SD(Stattilde,Dest)), cost(Stat,Dest)); Example Sets.13

The last line can be shortened to total(Dest)=sum(SD(Stattilde,Dest), cost(Stat,Dest));.

### Such that in equations

Such_that conditions can also be used in equations to restrict the equation over the domain of a dynamic set.

 AP_AlleEinplan(d,g).such_that(DG(d,g))= sum(d*g,x(d,g))==1; Example Sets.14

### Such that and any

The any-statement assures that no member of a set is used.

 enum {Opladen, Suelz, numS}; enum {hier, jetzt, nachher, numD}; MP_set S(numS); MP_set D(numD); MP_subset<2> Bink(S,D); Bink.insert(S, D.Any); OR forall(S.such_that(S==0)*D.such_that(D>1),Bink.insert(S,D)); Example Sets.15

As all members of S and no members of D are used, membership in set Bink is only given to the combinations (0,0) and (1,0) representing (Opladen, hier) and (Suelz, hier). The combination of forall and the such that-statement, only (0,2) representing (Opladen, nachher) are assigned to Bink. Any can be used with static sets as well as with dynamic sets.

### Such that and &&

The &&-syntax is equivalent to the and-statement in GAMS.

 forall(S.such_that(S==0 && D>1),Bink.insert(S,D)); Example Sets.16

### Such that and !

The !-syntax is equivalent to the not-statement in GAMS.

 forall(S.such_that(S==0 && !(D>1)),Bink.insert(S,D)); Example Sets.17

## Other syntax

### Check

The check-syntax gives back the desired index-number of a label of dynamic or static sets.

 enum {Opladen, Suelz, numS}; enum {hier, jetzt, nachher, numD}; MP_set S(numS); MP_set D(numD); MP_subset<2> Bink(S,D); int idx, idx2; idx = S.check(Opladen); idx2 = Bink.check(nachher); Example Sets.18

The value of idx is 0, the value of idx2 is 2.

### Cyclic

Some sets have to be cyclic because they represent for example seasons or times.

 enum {spring, summer, fall, winter, numSeasons); MP_set Seasons(numSeasons); Seasons.cyclic(); Example Sets.19

### Instantiation

Sets can be instantiated or uninstantiated. In FlopC++, this can be checked.

 enum {spring, summer, fall, winter, numSeasons); MP_set Seasons(numSeasons); Seasons.instantiate(); OR Seasons.uninstantiate(); bool chk; chk = Seasons.isinstantiated(); Example Sets.20

### Cardinality

To get the cardinality of a set, the size-statement is used.

 enum {spring, summer, fall, winter, numSeasons); MP_set Seasons(numSeasons); int sze; sze = Seasons.size(); Example Sets.21

The value of sze in this example is 4.

### Display

With the display-statement, the members of a dynamic set are printed out.

 enum {spring, summer, fall, winter, numSeasons); MP_set Seasons(numSeasons); MP_subset<1> CKG(Seasons); CKG.insert(summer); CKG.display("CKG members"); Example Sets.22

As summer is the only member of CKG, it is the only element to be printed out.