Agricultural Production and
Policy Impact Modelling
(731.386)
Erwin Schmid
[email protected]
Institute for Sustainable Economic Development
Zoom-link
https://bokuvienna.zoom.us/j/64361436180
1
Why is mathematical programming a
success?
Real Life Models
Mathematical insights
Efficient algorithms, professional software packages
Fast and cheap computers
Goals:
Learn real-life problem and model formulations.
Learn how to use GAMS to build these models and to do analyses.
Learn good model building in GAMS.
Learn how to interpret model solutions as well as to synthesize and
report model results.
2
References
McCarl B.A., and T.H. Spreen (1999). Applied Mathematical
Programming using Algebraic Systems.
Online available:
http://agecon2.tamu.edu/people/faculty/mccarl-
bruce/books.htm
3
Book: Bruce McCarl and Thomas Spreen
4
Course Outline:
1. Introduction to Linear Programming
2. Introduction to GAMS (General Algebraic Modeling Systems)
- Sets, Parameters and Conditions
3. Impact Analysis
- Farm Level Analysis
- Comparative Static/Dynamic Analysis
- Regional Analysis
- Sector (partial equilibrium) Analysis
4. Fixing unbounded and infeasible Models using GAMSCHK
5. Reporting Model Results
5
Course Material at BOKUlearn
6
GAMS installation
Installation Process
Download: Visit the GAMS website (www.gams.com) and download the installation file for
Windows (windows_x64_64.exe).
Start Installation:
Run the downloaded file. Administrator rights may be required.
Choose the installation mode (standard or advanced). In advanced mode, you can
configure additional settings like start menu folder, PATH variable, and desktop icons.
Specify the installation path (e.g., C:\GAMS).
Select GAMS Studio as the primary editor if prompted during installation.
GAMS installation II
Set up License:
Use Access Code: 47125672-d6ef-4756-95e0-b0520658d7fe
Completion: After installation, a desktop icon for GAMS Studio will be created, which you
can use to launch the application.
Using GAMS Studio
Launch: Open GAMS Studio using the desktop icon.
Create or Open Files:
Go to the file selection in the upper left corner to create new files or open existing
ones.
GAMS installation III
Prepare Models:
Edit your models in Studio's text editor and ensure they are ready to run.
Execute:
Click the "Run" button (or press F9) to execute the model.
Analyze Results:
Navigate through the output files and check the results in the "Process Log" window.
GAMS Studio offers a user-friendly interface with
www.gams.com
Select proper platform download
Check “use advanced installation
mode”
Select folder on your computer
License access code:
Access Code: 47125672-d6ef-4756-95e0-b0520658d7fe
Assignment
Develop a model and perform some analysis.
Latest submission date: 20th June 2025
Submission by email to:
[email protected] max 2 pages (single space) + appendix
Structure:
Title, Name, Matrikelnummer, Date
Research problem description
Research objective
Data and model description (the model mathematically)
Results
Conclusions
Appendix with gams code 16
Notes on Linear Programming
Based on Uwe Schneider (Uni Hamburg), and
Bruce McCarl (TAMU)
17
Linear Programming (LP)
Definition: A linear programming problem (LP) is an optimization problem
for which:
1. We attempt to maximize (minimize) a linear function of the
decision variables. (objective function)
2. The values of the decision variables must satisfy a set of
constraints, each of which must be a linear inequality or linear
equality.
3. A sign restriction on each variable. For each variable xi the sign
restriction can either be (a) xi ≥ 0; (b) xi ≤ 0; or (c) xi is unrestricted
(objv).
Assumptions:
Proportionality: If one item brings in a profit of x, then k items bring in a
profit of kx.
Additivity: The decisions made are independent; prohibits cross-product
terms e.g. 5x1x2 which might represent interaction effects between var.
Divisibility: Decision variables can take on fractional values.
Certainty: The values of various parameters are known with certainty.
9
Modelling Assumptions for Linear
Programming
Whenever we solve a linear programming
model, we should ask ourselves whether
these assumption hold, and whether the
solution makes sense
=> CONTEXT is KING!!!
19
Linear Programming, 1
Max c1 *X1 +…+ cn *Xn = z
s.t. a11*X1 +…+ a1n*Xn ≤ b1
…
am1*X1 +…+ amn*Xn ≤ bm
X1 , Xn ≥ 0
Standard form
20
LP Tableau
x1 x2 s1 s2 Rhs
Obj c1 c2 0 0
Eq1 a11 a12 1 0 <= b1
Eq2 a21 a22 0 1 <= b2
Type + + + +
21
Units
X1 (ha) X2 (ha) Rhs
Obj (Euro) 120 €/ha 180 €/ha
Eq1 (ha) 1 (ha/ha) 1 (ha/ha) 50(ha)
Eq2 (h) 2.2 (h/ha) 2.5 (h/ha) 100(h)
22
Linear Programming, 2
Max c1 *X1 +…+ cn *Xn = z
s.t. a11*X1 +…+ a1n*Xn ≤ b1
…
am1*X1 +…+ amn*Xn ≤ bm
X1 , Xn ≥ 0
n +1 Variables
23
Linear Programming, 3
Max c1 *X1 +…+ cn *Xn = z
s.t. a11*X1 +…+ a1n*Xn ≤ b1
…
am1*X1 +…+ amn*Xn ≤ bm
X1 , Xn ≥ 0
Objective function
24
Linear Programming, 4
Max c1 *X1 +…+ cn *Xn = z
s.t. a11*X1 +…+ a1n*Xn ≤ b1
…
am1*X1 +…+ amn*Xn ≤ bm
X1 , Xn ≥ 0
n Objective function coefficients (data)
25
Linear Programming, 5
Max c1 *X1 +…+ cn *Xn = z
s.t. a11*X1 +…+ a1n*Xn ≤ b1
…
am1*X1 +…+ amn*Xn ≤ bm
X1 , Xn ≥ 0
m Constraints
26
Linear Programming, 6
Max c1 *X1 +…+ cn *Xn = z
s.t. a11*X1 +…+ a1n*Xn ≤ b1
…
am1*X1 +…+ amn*Xn ≤ bm
X1 , Xn ≥ 0
m*n technical coefficients (data)
27
Linear Programming, 7
Max c1 *X1 +…+ cn *Xn = z
s.t. a11*X1 +…+ a1n*Xn ≤ b1
…
am1*X1 +…+ amn*Xn ≤ bm
X1 , Xn ≥ 0
m resource limits (data)
28
Linear Programming Example
Max 2*X1 + 3*X2 = z
s.t. X1 + 2*X2 ≤ 10
X1 , X2 ≥ 0
29
Feasibility Region
X2
Max 2*X1 + 3*X2 = z
s.t. X1 + 2*X2 ≤ 10
X1 , X2 ≥ 0
5
X1,X2 ≥ 0
X2 ≤ 5 – 0.5*X1
10 X1
30
Feasibility Region
X2
Convex Set
10 X1
31
Objective Function Isoclines - 1
X2
Max 2*X1 + 3*X2 = z
s.t. X1 + 2*X2 ≤ 10
5
X1 , X2 ≥ 0
X2 = z/3 – 2/3*X1
10 X1
z=6
X2=2–2/3*X1
32
Objective Function Isoclines - 2
X2
Max 2*X1 + 3*X2 = z
s.t. X1 + 2*X2 ≤ 10
5
X1 , X2 ≥ 0
X2 = z/3 – 2/3 * X1
10 X1
z=6 z=15
z=0
33
Graphical Solution
X2
Max 2*X1 + 3*X2 = z
s.t. X1 + 2*X2 ≤ 10
X1 , X2 ≥ 0
5
X2 = z/3 – 2/3 * X1
z=20 X1
10
34
Graphical Solution
X2
Is at extreme point!
5
z=20 X1
10
35
Other Extreme Points
X2
10 X1
36
Solving Arithmetically
Max c1 *X1 +…+ cn *Xn = z
s.t. a11*X1 +…+ a1n*Xn ≤ b1
…
am1*X1 +…+ amn*Xn ≤ bm
X1 , Xn ≥ 0
37
Convert Inequalities
Max c1 *X1 +…+cn *Xn = z
s.t. a11*X1 +…+a1n*Xn ≤ b1
…
am1*X1 +…+amn*Xn ≤ bm
X1 , X2 ≥ 0
Max c1 *X1 +…+cn *Xn +0*S1 +…+0*Sm = z
s.t. a11*X1 +…+a1n*Xn +1*S1 +…+0*Sm = b1
…
am1*X1 +…+amn*Xn +0*S1 +…+1*Sm = bm
X1 , X2 , S1 , Sm ≥ 0
38
Arithmetic Problem, 1
Number of unknown variables: n+m
Number of equations (constraints): m
Can only solve m equations for m
unknown variables
39
Arithmetic Problem, 2
From graphical illustration, we know
Solution occurs at extreme point
40
1 Constraint ⇒1 Non-Zero Variable
Max Objective
X2 s.t. X1 + 2*X2 + S = 10
X1 , X2 , S ≥ 0
X1 = 0
X2 = 5
S=0
5
X1 = 1 0
X1 = 0
X 2 = 0
X2 = 0
S = 10 S=0
10 X1
32
2 Constraints
X2
10 Max Objective
s.t. X1 + 2*X2 + S1 = 10
2*X1 + X2 + S2 = 10
X1 , X2 , S1, S2 ≥ 0
5 10 X1
33
2 Constraints ⇒ 2 Non-Zero Variables
X2 Max Objective
10
s.t. X1 + 2*X2 + S1 = 10
X1 = 0
2*X1 + X2 + S2 = 10
X2 = 5
X1 , X2 , S1, S2 ≥ 0
X1 = 10
S1 = 0 3
X2 = 10
S2 = 5 3
5 S1 = 0
S2 = 0
X1 = 5
X1 = 0 X2 = 0
X2 = 0 S1 = 5
S1 = 10
S2 = 0
S2 = 10 5 10 X1
43
Arithmetic Problem, 3
Extreme points have only m non-zero
variables and n-m zero variables
Thus, if we knew which n-m variables are
zero, we could solve for the remaining m
variables using our m equation system
44
Optimal solution is extreme point
Number of non-zero variables = number of
equations
Non-zero (positive) variables are called basic
variables
All remaining variables are called
non-basic variables
Non-basic variables are zero.
45
Decomposed LP in Matrix Notation
Max CBXB + CNBXNB
s.t. BXB + ANBXNB = b
XB , XNB ≥ 0
Solution of LP in Matrix Notation
BBXB = b - ANBXNB
BXB = b
XB = B-1b
Solver tasks
Determine which variables form basis
Invert coefficient matrix to calculate
optimal variable values
48
If optimal solution is an extreme
point, why not simply calculate all
extreme points and choose the one
with highest objective function
value?
49
Number of Possible Extreme Points
n!
(n − m)!⋅ m!
n .. Number of Variables
m .. Number of Equations
50
Calculating all extreme points?
(n) (m) n!/[m!*(n-m)!]
Variables Equations Combinations Time
10 5 252 ~0 Seconds
20 10 184.756 58 Seconds
30 15 155.117.520 17 Hours
40 20 137.846.528.820 2 Years
50 25 126.410.606.437.752 2 Millenia
Linear Program
MAX CBXB + CNBXNB
s.t. BXB + ANBXNB = b
XB , XNB ≥ 0
Important LP Equations
=Z Cb X b + Cnb X nb
X b B −1b − B −1 Anb X nb
=
Cb B −1b − (Cb B −1 Anb − Cnb ) X nb
Z=
∂Z −1
= C=
b B U SHADOW PRICE
∂b
∂Z
− ( Cb B −1 Anb − Cnb )
=
∂X nb
REDUCED COST
Duality
Pr imal Dual
Max CX Min U′b
s.t. AX ≤ b s.t. U′A ≥ C
X ≥0 U ≥0
54
Complementary Slackness
derived from duality
U (b − AX )
*' * = 0
(U A − C )X
*' * = 0
55
Unbounded Solution
56
Infeasible Solution
x2
A
B x1
57
Multiple Optima
Isocline with
highest
x2
objective
P1
P2 x1
58
Degeneracy
x2
x1
P1
59
GAMS
General Algebraic Modeling System
60
GAMS Homepage
61
GAMS Software
62
Our Text - Model
Max 2*X1 + 3*X2 = z
s.t. X1 + 2*X2 ≤ 10
2*X1 + X2 ≤ 10
X1 , X2 ≥ 0
63
GAMS Model
Positive Variables x1,x2;
Declare variables
Free Variables Z; and equations
Equations obj,e1,e2;
obj.. 2*x1 + 3*x2 =E= z;
e1.. 1*x1 + 2*x2 =L= 10; Assign equations
e2.. 2*x1 + 1*x2 =L= 10;
Model name (slide)
Model slide /all/;
Solve statement
solve slide using lp maximizing z;
64
GAMS Commands and Functions
Sets ... setting of model dimensions and elements
Parameter ... for parameter specification and calculation
Scalar ... scalar specification
Table ... data input and output
$ Conditions ... for calculations, model equations & variables
The Model
Variables ... defining decision variables and obj-variable
Equations ... defining equations (constraints & objective func.)
etc.
65
Sets in GAMS
66
Tables in GAMS
67
external table input and output reading
from prn files:
$INCLUDE 'D:\WPR\Lehre\AgSectResMod\Chapter_1\Tab2.prn'
from xls files:
Parameter Tab3(Lines,Columns);
needs to be written in
one line!!!
$CALL GDXXRW D:\WPR\Lehre\AgSectResMod\Chapter_1\Tab3.xls
output=D:\WPR\Lehre\AgSectResMod\Chapter_1\Tab3.gdx Par=Tab3 Rng=Tab!a1:d22 Rdim=1 Cdim=1
$GDXIN D:\WPR\Lehre\AgSectResMod\Chapter_1\Tab3.gdx
$LOAD Tab3
$GDXIN
output into xls files
Execute_Unload "D:\WPR\Lehre\AgSectResMod\Chapter_1\Tab3.gdx",Lines,Columns,Tab3;
Execute 'GDXXRW.EXE D:\WPR\Lehre\AgSectResMod\Chapter_1\Tab3.gdx needs to be written
o=D:\WPR\Lehre\AgSectResMod\Chapter_1\Tab4.xls par=Tab3 Rng=Tab1!a1:d22'; in one line!!!
68
$ Conditions
In ordinary algebra we have ways of controlling the
terms considered in indexed expressions. In
particularly, if i had 40 elements and we whished to
sum elements 1 to 20 we would write
20
X = ∑ Zi
i=1
GAMS introduces that qualifications on indexes like i via
the use of conditionals ($ and if) or special sets.
69
$ Conditions
GAMS conditionals can be applied in several settings:
To control whether an equation is calculated
x$y = 10+z;
To control whether a term appears in an equation
x = 10+z$y
To control whether a model equation is set up
constraint$y.. z =L=10;
To control whether a term is included in a model equation
constraint.. z+x$y =L= 10;
70
$ Conditions
To control inclusion of terms in sum, loop, smax and other
commands indexing over a set
x = sum(i$y, z(i));
loop (i$y, x=x+z(i));
To control whether a set of code is executed
if(y,x=10);
e.g., x$y=3;
z$y=4;
can become
if(y,
x=3;
y=4);
71
$ Conditions
right versus left hand conditionals
Conditional placement affects speed and results
1) x(i,j,k)$data(i,j,k)=1+y(i,j,k);
2) x(i,j,k)=(1+y(i,j,k))$data(i,j,k);
The first will only go through cases where the data array
contains non-zeros entries. it will be faster!
If x(i,j,k) has numbers where data(i,j,k) does not, those numbers
will be retained!!!
The second will cover all cases and will zero cases of x that do
not correspond to non-zero data. The answer will be different
and the statement will likely take longer to execute (GAMS has
been introducing internal optimisation to make it perform
better in such cases)!!!
72
Parameter Calculation
Calculate from table:
mean
mean per lines
mean per columns
standard deviation 1 2
Std = (xi − x )
standard deviation per lines n
standard deviation per columns
73
Example: Feed Valuation Model
Valuation of non-market goods (e.g. hay, silage, green
fodder, etc.) by nutrient and energy contents (MJ, RP)
of comparable feeds (e.g. barley, soybean, etc)
Model:
min cx
s.t.
Ax ≥ b
x≥0
74
Example: Data
Calculate the value of 1 kg hay using barley, wheat and
soybeans as comparable goods
MJ/kg gRP/kg €/kg
Barley 12.6 88 0.179
Soybeans 13.1 460 0.375
Wheat 13.6 112 0.164
Hay 9.3 117 ?
straw 5.6 - ?
75
Model Development
Develop mathematical structure
Implement in GAMS
Declare and assign sets
Declare parameters
Declare variables
Declare equations
Assign equation structure
76
LP Tableau
x1 x2 x3 Rhs
Obj 0.179 0.375 0.164 = Z min
(€/kg) (€/kg) (€/kg)
Eq1 12.6 13.1 13.6 ≥ 9.3
(MJ/kg) (MJ/kg) (MJ/kg) (MJ)
Eq2 88 460 112 ≥ 117
(gRP/kg) (gRP/kg) (gRP/kg) (gRP)
Type + + +
77