conditionals in gams - department of agricultural...
Post on 25-Jun-2018
218 Views
Preview:
TRANSCRIPT
Background Features LHS & RHS Practices Alternatives
Conditionals in GAMS
Pei Huang1
1Department of Agricultural Economics
Texas A&M University
Based on materials written by Gillig & McCarl and improved upon by many previous lab instructors
Special thanks to Mario Andres Fernandez
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 1 / 20
Background Features LHS & RHS Practices Alternatives
Outline
1 Background
2 Syntax
3 LHS & RHS
4 Using Conditionals
5 Alternatives
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 2 / 20
Background Features LHS & RHS Practices Alternatives
What is conditional?
We often wish to have terms present in the algebraic expression only
IF certain conditions are met.
The syntax of conditional in GAMS is the dollar sign $, which is read
as �if�.
$ is the counterpart of logical syntax �if� in other programming
languages, say Visual Basic, C, C++.
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 3 / 20
Background Features LHS & RHS Practices Alternatives
How it works?
Example
Say index(i) has 40 elements but we only want to sum over the �rst 20. The algebraicexpression is
X =20
∑i=1
Zi .
GAMS code:X=sum(index$(ord(i) le 20),Z(i));
Example
The formula can only be calculated if it meets the condition
pivot =min
(biai
)∀i , s.t. ai > 0.
GAMS code:Pivot=smin(index$(a(i)>0),b(i)/a(i));
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 4 / 20
Background Features LHS & RHS Practices Alternatives
How it works?
Example
Say index(i) has 40 elements but we only want to sum over the �rst 20. The algebraicexpression is
X =20
∑i=1
Zi .
GAMS code:X=sum(index$(ord(i) le 20),Z(i));
Example
The formula can only be calculated if it meets the condition
pivot =min
(biai
)∀i , s.t. ai > 0.
GAMS code:Pivot=smin(index$(a(i)>0),b(i)/a(i));
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 4 / 20
Background Features LHS & RHS Practices Alternatives
Conditionals - $
Conditionals are used to control in GAMS
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 �le.
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 5 / 20
Background Features LHS & RHS Practices Alternatives
Conditionals - $
Conditionals are used to control in GAMS
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 �le.
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 5 / 20
Background Features LHS & RHS Practices Alternatives
Conditionals - $
Conditionals are used to control in GAMS
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 �le.
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 5 / 20
Background Features LHS & RHS Practices Alternatives
Conditionals - $
Conditionals are used to control in GAMS
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 �le.
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 5 / 20
Background Features LHS & RHS Practices Alternatives
Conditionals - $
Conditionals are used to control in GAMS
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 �le.
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 5 / 20
Background Features LHS & RHS Practices Alternatives
Conditionals - $
Conditionals are used to control in GAMS
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 �le.
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 5 / 20
Background Features LHS & RHS Practices Alternatives
Examples
Whether an item is calculated
X $(Y gt 0)= 10;qq $(sum(i,q(i))gt 0 )= 4;
Whether elements of an item are computed
Q(i)$(a(i)gt 0)= -a(i);A(i)$(abs(a(i))gt 0)= q(i)*a(i);
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 6 / 20
Background Features LHS & RHS Practices Alternatives
Examples
Whether an item is calculated
X $(Y gt 0)= 10;qq $(sum(i,q(i))gt 0 )= 4;
Whether elements of an item are computed
Q(i)$(a(i)gt 0)= -a(i);A(i)$(abs(a(i))gt 0)= q(i)*a(i);
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 6 / 20
Background Features LHS & RHS Practices Alternatives
Examples
Controlling inclusion of terms in equations
Eq3$(x=2).. zz=e=3;x = sum(i,q(i))$(qq gt 0)+ 4);Eq4$(x<2).. zz=e=3;
Controlling inclusion of set dependent terms
X = sum(i$(q(i)gt 0), a(i));Loop((k,j)$(cost(i,j)lt 0), x = x + cost(i,j);X = smin(i$(sum(j, cost(i,j))gt 0), q(i));
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 7 / 20
Background Features LHS & RHS Practices Alternatives
Examples
Controlling inclusion of terms in equations
Eq3$(x=2).. zz=e=3;x = sum(i,q(i))$(qq gt 0)+ 4);Eq4$(x<2).. zz=e=3;
Controlling inclusion of set dependent terms
X = sum(i$(q(i)gt 0), a(i));Loop((k,j)$(cost(i,j)lt 0), x = x + cost(i,j);X = smin(i$(sum(j, cost(i,j))gt 0), q(i));
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 7 / 20
Background Features LHS & RHS Practices Alternatives
LHS and RHS Conditionals
LHS conditionals will return the prior value when the case is not true.
Example
Q(i)= 5;Q(i)$(a(i)gt 0)= (a(i)+1);If the condition is not true, what is the value for Q(i)?
RHS conditionals will return zero when the case is not true.
Example
Q(i)= 5;Q(i)= (a(i)+1)$(a(i)gt 0);If the condition is not true, what is the value for Q(i)?
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 8 / 20
Background Features LHS & RHS Practices Alternatives
LHS and RHS Conditionals
LHS conditionals will return the prior value when the case is not true.
Example
Q(i)= 5;Q(i)$(a(i)gt 0)= (a(i)+1);If the condition is not true, what is the value for Q(i)?
RHS conditionals will return zero when the case is not true.
Example
Q(i)= 5;Q(i)= (a(i)+1)$(a(i)gt 0);If the condition is not true, what is the value for Q(i)?
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 8 / 20
Background Features LHS & RHS Practices Alternatives
LHS and RHS Conditionals
LHS conditionals will return the prior value when the case is not true.
Example
Q(i)= 5;Q(i)$(a(i)gt 0)= (a(i)+1);If the condition is not true, what is the value for Q(i)?
RHS conditionals will return zero when the case is not true.
Example
Q(i)= 5;Q(i)= (a(i)+1)$(a(i)gt 0);If the condition is not true, what is the value for Q(i)?
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 8 / 20
Background Features LHS & RHS Practices Alternatives
LHS and RHS Conditionals
LHS conditionals will return the prior value when the case is not true.
Example
Q(i)= 5;Q(i)$(a(i)gt 0)= (a(i)+1);If the condition is not true, what is the value for Q(i)?
RHS conditionals will return zero when the case is not true.
Example
Q(i)= 5;Q(i)= (a(i)+1)$(a(i)gt 0);If the condition is not true, what is the value for Q(i)?
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 8 / 20
Background Features LHS & RHS Practices Alternatives
LHS Conditionals
ItemName$(logical condition) = Term;
ItemName is set equal to Term only if the condition is true.
Example
SCALAR X, Y ;Y = 3;X = 1;X$(Y gt 2.5)= 2;Solution: X = 2
Example
SCALAR X, Y ;Y = 2;X = 1;X$(Y gt 2.5)= 2;Solution: X = 1
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 9 / 20
Background Features LHS & RHS Practices Alternatives
LHS Conditionals
ItemName$(logical condition) = Term;
ItemName is set equal to Term only if the condition is true.
Example
SCALAR X, Y ;Y = 3;X = 1;X$(Y gt 2.5)= 2;Solution: X = 2
Example
SCALAR X, Y ;Y = 2;X = 1;X$(Y gt 2.5)= 2;Solution: X = 1
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 9 / 20
Background Features LHS & RHS Practices Alternatives
LHS Conditionals
ItemName$(logical condition) = Term;
ItemName is set equal to Term only if the condition is true.
Example
SCALAR X, Y ;Y = 3;X = 1;X$(Y gt 2.5)= 2;Solution: X = 2
Example
SCALAR X, Y ;Y = 2;X = 1;X$(Y gt 2.5)= 2;Solution: X = 1
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 9 / 20
Background Features LHS & RHS Practices Alternatives
LHS Conditionals
ItemName = Term$(logical condition);
ItemName is set equal to Term only if the condition is true.
Example
SCALAR X, Y ;Y = 3;X = 1;X = 2$(Y gt 2.5);Solution: X = 2
Example
SCALAR X, Y ;Y = 2;X = 1;X = 2$(Y gt 2.5);Solution: X = 0
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 10 / 20
Background Features LHS & RHS Practices Alternatives
LHS Conditionals
ItemName = Term$(logical condition);
ItemName is set equal to Term only if the condition is true.
Example
SCALAR X, Y ;Y = 3;X = 1;X = 2$(Y gt 2.5);Solution: X = 2
Example
SCALAR X, Y ;Y = 2;X = 1;X = 2$(Y gt 2.5);Solution: X = 0
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 10 / 20
Background Features LHS & RHS Practices Alternatives
LHS Conditionals
ItemName = Term$(logical condition);
ItemName is set equal to Term only if the condition is true.
Example
SCALAR X, Y ;Y = 3;X = 1;X = 2$(Y gt 2.5);Solution: X = 2
Example
SCALAR X, Y ;Y = 2;X = 1;X = 2$(Y gt 2.5);Solution: X = 0
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 10 / 20
Background Features LHS & RHS Practices Alternatives
Equation Conditionals
EquationName$(logical condition)..
Restrict whether equations are de�ned in a model.
We can restrict whether to
include the crops that do not
have minimum land requirement
by equation conditionals.
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 11 / 20
Background Features LHS & RHS Practices Alternatives
Without Equation Conditional
Include all equations
Use LIMROW option to display equation listings
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 12 / 20
Background Features LHS & RHS Practices Alternatives
With Equation Conditional
Only include equations with positive minimum land requirement
Use LIMROW option to display equation listings
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 13 / 20
Background Features LHS & RHS Practices Alternatives
SAMEAS Statement
Perform an operation only if a set element text is matched exactly to
another set element text.
The $ command says to operate the sum if i and j are the exact same
text.
Example
X = SUM((i, j)$(SAMEAS(i,j)),c(i, j));
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 14 / 20
Background Features LHS & RHS Practices Alternatives
A Transportation Example
No transport from Chicago to Chicago
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 15 / 20
Background Features LHS & RHS Practices Alternatives
A Transportation Example
No transport from Chicago to Chicago
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 15 / 20
Background Features LHS & RHS Practices Alternatives
A Transportation Example
No transport from Chicago to Chicago
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 15 / 20
Background Features LHS & RHS Practices Alternatives
Nesting Conditionals
Nested $: statements can involve multiple $ conditions with the
following logical syntax
and: two or more conditionals apply simultaneouslyor: at least one of two or more conditionals applynot: do something when a conditional is not true
Examples
u(k)$(s(k)and t(k))= a(k);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);U(k)$(s(k)$t(k))= a(k);
Use di�erent levels of parenthesis to control the precedence of
calculations.
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 16 / 20
Background Features LHS & RHS Practices Alternatives
Nesting Conditionals
Nested $: statements can involve multiple $ conditions with the
following logical syntax
and: two or more conditionals apply simultaneouslyor: at least one of two or more conditionals applynot: do something when a conditional is not true
Examples
u(k)$(s(k)and t(k))= a(k);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);U(k)$(s(k)$t(k))= a(k);
Use di�erent levels of parenthesis to control the precedence of
calculations.
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 16 / 20
Background Features LHS & RHS Practices Alternatives
Nesting Conditionals
Nested $: statements can involve multiple $ conditions with the
following logical syntax
and: two or more conditionals apply simultaneouslyor: at least one of two or more conditionals applynot: do something when a conditional is not true
Examples
u(k)$(s(k)and t(k))= a(k);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);U(k)$(s(k)$t(k))= a(k);
Use di�erent levels of parenthesis to control the precedence of
calculations.
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 16 / 20
Background Features LHS & RHS Practices Alternatives
Alternative to Conditionals: Subset
Example
Consider the example again. Say index(i) has 40 elements but we only want to sumover the �rst 20. The algebraic expression is
X =20
∑i=1
Zi .
As an alternative, create a subset for �rst 20 elements:
Set subset(i)/i1*i20/;
X=sum(i$subset(i), z(i));
X=sum(subset, z(subset));
X=sum(subset(i), z(i));
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 17 / 20
Background Features LHS & RHS Practices Alternatives
Alternative to Conditionals: Tuples
Sometimes, the same conditional appear repeatedly. We can �rst
de�ne tuples, and then use these tuples directly in equations.
A tuple refers to a set de�ned over other sets.
Example
Tcosteq..Tcost =e= sum((plant,market)$(supply(plant)and demand(market)and distance(plant,market)), shipments(plant, market)* cost(plant,market));
Rule out non-zero supply, demand, and distance in the transportation
example. The condition $(supply(plant) anddemand(market) and distance(plant,market)) mayappear multiple times in the model.
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 18 / 20
Background Features LHS & RHS Practices Alternatives
Alternative to Conditionals: Tuples
Sometimes, the same conditional appear repeatedly. We can �rst
de�ne tuples, and then use these tuples directly in equations.
A tuple refers to a set de�ned over other sets.
Example
Tcosteq..Tcost =e= sum((plant,market)$(supply(plant)and demand(market)and distance(plant,market)), shipments(plant, market)* cost(plant,market));
Rule out non-zero supply, demand, and distance in the transportation
example. The condition $(supply(plant) anddemand(market) and distance(plant,market)) mayappear multiple times in the model.
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 18 / 20
Background Features LHS & RHS Practices Alternatives
Alternative to Conditionals: Tuples
Sometimes, the same conditional appear repeatedly. We can �rst
de�ne tuples, and then use these tuples directly in equations.
A tuple refers to a set de�ned over other sets.
Example
Tcosteq..Tcost =e= sum((plant,market)$(supply(plant)and demand(market)and distance(plant,market)), shipments(plant, market)* cost(plant,market));
Rule out non-zero supply, demand, and distance in the transportation
example. The condition $(supply(plant) anddemand(market) and distance(plant,market)) mayappear multiple times in the model.
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 18 / 20
Background Features LHS & RHS Practices Alternatives
Tuples
Example
SET thistuple(plant,market)Tuple expressing conditional;
thistuple(plant,market)$(supply(plant)and demand(market)anddistance(plant,market))=yes;
Tcosteq..
Tcost =e= sum(thistuple(plant,market), shipments(plant,market)*cost(plant, market));
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 19 / 20
Appendix
Heads On 4
I will email you the question and requirement.
You send an electronic copy of your *.gms �le via email. Put your
name in �rst line as comment.
Hand in a hard copy of *.lst �le (from �Solution Report� to the end).
Pei Huang | Texas A&M University | AGEC 641 Lab Session, Fall 2013 20 / 20
top related