Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditionals, Subsets and Tuples in GAMS AGEC 641 Lab, Fall 2011 Mario Andres Fernandez Based on material written by McCarl and Elbakidze; Improved upon.

Similar presentations


Presentation on theme: "Conditionals, Subsets and Tuples in GAMS AGEC 641 Lab, Fall 2011 Mario Andres Fernandez Based on material written by McCarl and Elbakidze; Improved upon."— Presentation transcript:

1 Conditionals, Subsets and Tuples in GAMS AGEC 641 Lab, Fall 2011 Mario Andres Fernandez Based on material written by McCarl and Elbakidze; Improved upon by many previous lab instructors. Special thanks to Yuquan “Wolfgang” Zhang

2  If we have a set containing 40 elements and we wish to sum over the first 20 elements only, we would write X=sum(i$(ord(i) le 20), z(i));  The formula can only be calculated if it meets the condition  Pivot=smin(i$(a(i)>0), b(i)/a(i)); What are “conditionals”?

3 Conditionals Allow to write expressions that operate over less than full sets or incorporate various model features conditionally on the data. In order to control …  … whether an item is calculated on an element by element basis.  … the inclusion of terms in equations.  … the inclusion of set dependent terms in sums.  … the inclusion of equations in a model on an element by element basis.  … whether a display is output to *.lst file.

4 Basics $ Syntax uses $ sign: READ $ SIGN AS “IF” Term$logical condition Namedparameter$logical condition Namedset$set dependent logical condition Equationname$logical condition Display$condition listofitems;

5 Eq3$(x=2).. zz=e=3; Xx=Sum(I$(x eq sum(j, y(I, j))+2), 1); Xx=Sum(I$(x <> sum(j, y(I, j))+2), 1); Xx=Sum(I$(x ne 22), 1); Eq4$(x<2).. zz=e=3; Xx=Sum(I$( sum(j, y(I, j)) lt yx+2), z+1); Numerical Expressions

6 Loop(I$(x > yx+2), z=z+1); Loop(I$(x gt yx+2), z=z+1); Eq5$(x>=2).. zz=e=3; Xx=Sum(I$(x ge yx+2), z=z+1); Eq6(i,j)$(y(I, j)<=2).. y(I,j)*zz=e=3; Loop(I$(x le yx+2), z=z+1); Numerical Expressions (cont)

7 Right Hand Side Vs. Left Hand Side RHS conditionals will return zero when the case is not true Q(i)=(a(i)+1)$(a(i) gt 0) As if it were Q(i)=0+(a(i)+1)$(a(i) gt 0); LHS conditionals will return the prior value when the case is not true Q(i)$(a(i) gt 0) = (a(i) +1);

8 SCALAR X, Y ; Y = 3; X = 1; X$(Y GT 2.5) = 2; Solution: X = 2 SCALAR X, Y ; Y = 2; X = 1; X$(Y GT 2.5) = 2; Solution: X = 1 $ command says that X = 2 if Y is greater than 2.5 Otherwise, the value of X remains (X =1). ParameterName $ logical condition = Term ; ParameterName is set equal to Term only if condition is true. LHS $conditionals

9 ParameterName = Term $ logical condition ; This implies that the ParameterName is set equal to the Term only if the logical condition is true. SCALAR X, Y ; Y = 3; X = 1; X = 2$(Y GT 2.5); Solution: X = 2 SCALAR X, Y ; Y = 2; X = 1; X = 2$(Y GT 2.5); Solution: X = 0 the $ command says that X = 2 if Y is greater than 2.5; otherwise, X = 0. RHS $conditionals

10 Equation $ conditionals the $ command says that this equation would be defined if there were positive minimum land requirement. Restrict whether equations are defined in a model: EquationName $ logical condition..

11 Using LIMROW option to display equation listings

12 Sameas statements Perform an operation only if a set element text is matched exactly to another set element text. $ on SAMEAS. SUM( (i, j) $ (SAMEAS(i, j) ), C(i, j) ) Or SUM( (i, j) $ diag(i, j), C(i, j)) $ command tells to operate the sum if i and j are exactly the same.

13 SUM( (i, j) $ (NOT SAMEAS(i, j) ), C(i, j) ) $ command says to operate the sum if i and j are not the same. No Transport from Chicago to Chicago

14

15 Nesting Logical Condition OperationOperatorPrecedence Exponentiation**1 Multiplication, Division * /2 Addition, Subtraction+ -3 Numerical Relationships, >=, >, lt, le, eq, ne, ge, gt 4 Notnot5 Andand6 Or, Xoror, xor7 Nested operators are acted on in a fixed precedence order and are operated on in an order relative to mathematical operators

16 When operators with the same precedence appear in an expression or logical condition they are performed from left to right. It is always advisable to use parentheses rather than relying on the precedence order of operators. u(k)$([not s(k)] and {u(k) or t(k)}) = a(k); u(k)$(s(k) or {u(k) and t(k)}) = a(k); u(k)$(s(k) or [not {u(k) and t(k)}]) = a(k);

17 Help with Conditionals

18 Alternative to Conditionals: Subset Consider As an alternative, create a subset Set subset(i) /i1*i20/; X=sum(i$subset(i), z(i)); X=sum(subset, z(subset)); X=sum(subset(i), z(i));

19 Alternative to Conditionals: Tuples A tuple refers to a set defined over other sets Set thistuple(plant,market) tuple expressing conditional; thistuple(plant,market)$(supply(plant) and demand(market) and distance(plant,market))=yes; Tcosteq.. Tcost =e= sum(thistuple(plant,market), shipments(plant,market)*cost(plant,market));

20 Other Conditional Implementation Possibilities If -- also involves the else and elseif statements. In general, can be written as $ conditions, but the use of if can make GAMS code more readable. if (x ne 0, DATA(i)=12 ; ); While -- repeatedly execute a block of statements until a logical condition is satisfied. while(x<10, x=x+0.01; ); Repeat -- execute a block of statements over and over until a logical condition is satisfied. repeat( x=x+0.01; until x>=10) ;


Download ppt "Conditionals, Subsets and Tuples in GAMS AGEC 641 Lab, Fall 2011 Mario Andres Fernandez Based on material written by McCarl and Elbakidze; Improved upon."

Similar presentations


Ads by Google