1 / 20

Conditionals, Subsets and Tuples in GAMS AGEC 641 Lab, Fall 2011 Mario Andres Fernandez

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. What are “conditionals”?.

kyra-burton
Download Presentation

Conditionals, Subsets and Tuples in GAMS AGEC 641 Lab, Fall 2011 Mario Andres Fernandez

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. What are “conditionals”? • 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));

  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$conditionlistofitems;

  5. NumericalExpressions 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);

  6. NumericalExpressions (cont) 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);

  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 wereQ(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. LHS $conditionals ParameterName$ logical condition = Term ; ParameterNameis set equal to Term only if condition is true. 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).

  9. RHS $conditionals ParameterName= Term $ logical condition ; This implies that theParameterNameis 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.

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

  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) ) OrSUM( (i, j) $ diag(i, j), C(i, j)) • $command tells to operate the sum if i and j are exactly the same.

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

  14. No Transport from Chicago to Chicago

  15. Nesting Logical Condition 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 Setthistuple(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) ;

More Related