• Ingen resultater fundet

DYNAMIC SETS

In document AUSER'SGUIDE GAMS (Sider 127-133)

12.1 INTRODUCTION

All the sets that have been discussed so far had their membership declared as the set itself was declared, and the membership was never changed. In this chapter we will discuss changing the membership of sets. A set whose membership can change is called a dynamic set to contrast it with a static set whose membership will never change. The distinction is important and will be discussed in detail in this chapter. This is a topic that has been avoided until now because of a potential confusion for new users. Advanced Users will, however, find it useful.

12.2 ASSIGNING MEMBERSHIP TO DYNAMIC SETS

Sets can be assigned in a similar way to other data types. One difference is that arithmetic opera-tions cannot be performed on sets in the same way that they can on "value typed" identifiers (parameters, or variables and equations subtypes). A dynamic set is most often used as a "controlling index" in an assignment or an equation definition, or as the controlling entity in a dollar-controlled indexed operation.

12.2.1 THE SYNTAX

In general, the syntax in GAMS for assigning membership to dynamic sets is:

set_name(domain_name | domain_label) = yes | no ;

set_name is the internal name of the set (also called an identifier) in GAMS. Yes and No are keywords used in GAMS to denote membership or absence respectively from the assigned set.

The most important principle to follow is that a dynamic set should always be domain checked at declaration time to be a subset of a static set (or sets).

It is of course possible to use dynamic sets that are not domain checked, and this provides additional power, flexibility, lack of intelligibility, and danger.

Any label is legal as long as the dimensionally, once established, is preserved.

12.2.2 ILLUSTRATIVE EXAMPLE

The following example, adapted from [ZLOOF], is used to illustrate the assignment of member-ship to dynamic sets.

DYNAMIC SETS 114

set item all items / dish,ink,lipstick,pen,pencil,perfume / subitem1(item) first subset of item / pen,pencil /

subitem2(item) second subset of item;

subitem1('ink’) = yes ; subitem1('lipstick') = yes ; subitem2(item) = yes ; subitem2('perfume') = no ; display subitem1, subitem2;

Note that the sets subitem1 and subitem2 are declared like any other set. The two sets be-come dynamic because of assignments. They are also domain checked: the only members they will ever be able to have must also be members of item. And item is a static set and henceforth its membership is frozen. The first two assignments each add one new element to subitem1. The third is an example of the familiar indexed assignment: subitem2 is assigned all the members of item. The output caused by the display statement, that will reveal the membership of the sets, is shown below for verification.

---- 7 SET SUBITEM1 first subset of item INK , LIPSTICK, PEN , PENCIL

---- 7 SET SUBITEM2 second subset of item DISH , INK , LIPSTICK, PEN , PENCIL

The elements are displayed in the order specified in the declaration of item.

12.2.3 DYNAMIC SETS WITH MULTIPLE INDICES

Dynamic sets, like static sets, can have up to 10 dimensions. The following example illustrates assignments for multi-dimensional sets.

Sets item items sold /pencil, pen/

sup suppliers /bic, parker, waterman / supply(item,sup) ;

supply('pencil','bic') = yes ; supply('pen',sup) = yes ;

All the mechanisms using asterisks and parenthesized lists that we introduced in the discussion on static sets in chapter 4 are available for dynamic sets as well.

12.2.4 ASSIGNMENTS OVER THE DOMAIN OF DYNAMIC SETS

One can make an assignment over the domain of a dynamic set because dynamic sets are known to be proper subsets of static sets. This is not the same as doing domain checking using a dy-namic set.

The following example, adapted from the Section 12.2.2 illustrates the use of dynamic sets as domains:

subitem1(item) = no subitem1(subitem2) = yes;

The first assignment ensures that subitem1 is empty. Note that this can also be done with pa-rameters. For example,

parameter inventory(item) ; inventory(subitem1) = 25 ;

12.3 USING DOLLAR CONTROLS WITH DYNAMIC SETS 115

12.2.5 EQUATIONS DEFINED OVER THE DOMAIN OF DYNAMIC SETS

It is sometimes necessary to define an equation over a dynamic set.

The trick is to declare the equation over the entire domain but define it over the dynamic set.

The following example illustrates its use,

set allr all regions / n, s, w, e, n-e, s-w / r(alr) region subset for particular solution ; scalar price /10/ ;

equations prodbal(allr) production balance ; variables activity(allr) first activity revenue(allr) revenue ; prodbal(r).. activity(r)*price =e= revenue(r) ;

To repeat the important point: the equation is declared over allr but referenced over r. Then arbitrary assignments can be made to r within the membership of allr.

12.3 USING DOLLAR CONTROLS WITH DYNAMIC SETS

The rest of this chapter requires an understanding of the dollar condition. All the dollar control machinery is available for use with dynamic sets. In fact, the full power of dynamic sets can be exploited using these dollar controls.

Note that the dynamic set has values of yes and no only, and can therefore be treated as a logical statement. The only operations that can be performed on dynamic sets inside the dollar operator are therefore not, and, or, or xor , as well as the set operations described in Chapter "SET OPERATIONS," page 117.

The main uses of dynamic sets inside dollar conditions are in assignments, indexed operations and in equations. Each of these will be discussed in detail in the following subsections. Examples will be used to illustrate its use in each of the three cases.

12.3.1 ASSIGNMENTS

Dynamic sets can be used inside dollar conditions within assignments defining other dynamic sets or parameters.

As an illustration of its use in defining other dynamic sets, the two statements in the example from Section 12.2.4. can be written with equivalent effect as

subitem1(item) = yes$subitem2(item) ;

which is a terse form of the following statement

subitem1(item) = yes$subitem2(item) + no$(not subitem2(item)) ;

The value used in the implied "else" that goes with "dollar on the right" is no in a set assignment, rather than zero which is used with normal data.

DYNAMIC SETS 116

The second example from Section 12.2.4 can be rewritten as follows to illustrate the use of dy-namic sets in defining parameters,

inventory(item)$subitem1(item) = 25 ;1

12.3.2 INDEXED OPERATIONS

Another important use of dollar controls with dynamic sets is to control the domain while per-forming indexed operations like sum and prod. Consider the following adaptation of the second example from Section 12.3.1,

parameter totinv total inventory ;

totinv = sum(item$subitem1(item),inventory(item)) ;

This example has been shown only for illustration. Note that the second statement above can also be rewritten tersely as

totinv = sum(subitem1,inventory(subitem1)) ;

This is not always possible. Consider the following artificially created example,

sets item items sold /pencil, pen/

sup suppliers /bic, parker, waterman / dep department /stationery, household/

supply(item,sup) ;

supply('pencil', 'bic') = yes ; supply('pen',sup) = yes ;

parameter totsales(dep) ;

totsales(dep) = sum(item$supply(item,'bic'), sales(dep,item)) ;

The assignment above is used to find the total sales of all departments that sell items supplied by bic. Note that the dynamic set is used to limit the domain of summation to those for which supply(item,'bic') is true.

12.3.3 EQUATIONS

Dynamic sets can be used inside dollar conditions in equations both as part of the equation alge-bra, or while defining the domain of the equation. The first case is similar to the case of assign-ments discussed in Section 12.3.1. The latter case is used to restrict the equation over the domain of a dynamic set. The equation defined in the example from Section 12.2.5 can be rewritten with equivalent effect as follows,

prodbal(allr)$r(allr).. activity(allr)*price =e= revenue(allr) ;

The domain of definition of equation prodbal is restricted to those elements that belong to the dynamic set r.

12.3.4 FILTERING THROUGH DYNAMIC SETS

The filtering process explained in previous sections is valid when the conditional set is a dy-namic one. Consider the following two examples as described before,

inventory(item)$subitem1(item) = 25 ;

prodbal(allr)$r(allr).. activity(allr)*price =e= revenue(allr) ;

These statements can be rewritten as,

12.4 SET OPERATIONS 117

inventory(subitem1) = 25 ;

prodbal(r).. activity(r)*price =e= revenue(r) ;

12.4 SET OPERATIONS

This section describes how various symbolic set operations can be performed in GAMS using dynamic sets. The Union, Intersection, Complement, and Difference set operations are described individually in the following subsections. Once again the example from Section 12.2.2 is used to illustrate each operation.

12.4.1 SET UNION

The symbol + performs the set union operation. Consider the following example,

subitem3(item) = subitem1(item) + subitem2(item) ;

The membership of subitem3 is set equal to all the elements of subitem1 and all the ele-ments of subitem2. The operation above is equivalent to the following longer way of repre-sentation,

subitem3(item)=no; subitem3(subitem2)=yes; subitem3(subitem1)=yes;

12.4.2 SET INTERSECTION

The symbol * performs the set intersection operation. Consider the following example,

Subitem3(item) = subitem1(item) * subitem2(item) ;

The membership of subitem3 is set equal to only those present in both subitem1 and su-bitem2. The operation above is equivalent to the following longer way of representation,

subitem3(item)=yes$(subitem1(item) and subitem2(item)) ;

12.4.3 SET COMPLEMENT

The operator not performs the set complement operation. Consider the following example,

Subitem3(item) = not subitem1(item) ;

The membership of subitem3 is set equal to all those in item but not in subitem1. The op-eration above is equivalent to the following longer way of representation,

subitem3(item)=yes; subitem3(subitem1)=no;

12.4.4 SET DIFFERENCE

The operator - performs the set difference operation. Consider the following example,

Subitem3(item) = subitem1(item) - subitem2(item) ;

The membership of subitem3 is set equal to all elements that are members of subitem1 but subitem2. The operation above is equivalent to the following longer way of representation,

subitem3(item)=yes$(subitem1(item)); subitem3(subitem2)=no;

DYNAMIC SETS 118

12.5 SUMMARY

The purpose of set assignments is to make calculations based on given data (the static sets) for use in exception handling. It is one more example of the principle of entering a small amount of data and building a model up from the most elemental information.

13

In document AUSER'SGUIDE GAMS (Sider 127-133)