GAMS GoodOptimizationModelingPractices
GAMS GoodOptimizationModelingPractices
with GAMS
All You Wanted to Know About Practical Optimization but Were Afraid to Ask
Andres Ramos
http://www.iit.comillas.edu/aramos/
Andres.Ramos@comillas.edu
Source:
Good http://www.gams.com/presentations/present_modlang.pdf
Optimization Modeling Practices with GAMS. July 2016 3
Questions to deal with
• What you don’t know how to do it in GAMS?
• What you would like to do and has not been able to?
• It is optimization for
shepherds (i.e., practitioners)
Good Optimization Modeling Practices with GAMS. July 2016 5
Contents
1. Programming Style
2. GAMS Code
4. Mathematical Formulation
5. Advanced Algorithms
Programming Style
Programming
• Discipline whose control is basic in many engineering projects
• Science: thinking, discipline, rigorousness and experimentation
• Art: beauty and elegance
B.W. Kernighan and P.J. Plauger, The Elements of Programming Style, McGraw Hill,
Good Optimization Modeling Practices with GAMS. July 2016
New York, 1978 http://en.wikipedia.org/wiki/The_Elements_of_Programming_Style 8
Which is the most Spanish beautiful landscape?
Aigüestortes
National Park Beach of
Menorca
Mediterranean Guadalquivir
oak wood marshland
GAMS Code
Search, compare and if you find something better use it
v. 24.8.4
v. 0.5.1
v. 5.0.1
OpenSolver (https://opensolver.org/)
Solver from FrontlineSolvers
(http://www.solver.com/premium-
solver-platform)
Good Optimization Modeling Practices with GAMS. July 2016 18
DCLF in Microsoft Excel
Matlab AIMMS
Python Pyomo
Julia JuMP
MatLab
Solver Studio
aaa.gpr
• Documentation
• GAMS Documentation Center https://www.gams.com/help/index.jsp
• GAMS Support Wiki http://support.gams.com/
• Bruce McCarl's GAMS Newsletter https://www.gams.com/maillist/newsletter.htm
• Users guide Help > GAMS Users Guide
• Solvers guide Help > Expanded GAMS Guide
• Model: FileName.gms
• Results: FileName.lst
• Process log: FileName.log
Good Optimization Modeling Practices with GAMS. July 2016 24
My first minimalist optimization model
positive variables x1, x2
variable z
max = 3 +5
,
≤4
2 ≤ 12
3 +2 ≤ 18
, ≥0
Good Optimization Modeling Practices with GAMS. July 2016 25
Blocks in a GAMS model
• Mandatory
variables
equations
model
solve
• Optional
sets: (alias)
• alias (i,j) i and j can be used indistinctly
• Checking of domain indexes
data: scalars, parameters, table
parameters ≤ ∀#
pA(i) origin capacity
/ VIGO 350
ALGECIRAS 700 /
≥ $ ∀%
pB(j) destination demand
/ MADRID 400 A. Mizielinska y D. Mizielinski Atlas del mundo: Un insólito viaje por las
BARCELONA 450 ≥0 mil curiosidades y maravillas del mundo Ed. Maeva 2015
VALENCIA 150 /
variables
vX(i,j) units transported
vCost transportation cost
positive variable vX
equations
eCost transportation cost
eCapacity(i) maximum capacity of each origin
eDemand (j) demand supply at destination ;
alias (nd,ni,nf)
parameters
pDemand(w,h,nd ) demand in each node
pFlow (w,h,nd,nd) flow in each line ;
pDemand(sprday,nd ) = uniform(-0.5,0.5) ;
pFlow (sprday,nd,nd) = uniform(-1.0,1.0) ;
• Infeasibility detection
• Soft (elastic) constraints
• Introduce a deficit or surplus variable in each equation and penalize them in
the objective function
• Detect the smallest core of infeasible constraints by the LP solver
(option Irreducible Infeasible Subsets iis)
• Once known, they have to be deleted or modified
E. Klotz, A.M. Newman Practical guidelines for solving difficult linear programs Surveys in Operations
Research and Management Science 18 (1-2), 1-17, Oct 2013 10.1016/j.sorms.2012.12.001
Good Optimization Modeling Practices with GAMS. July 2016 41
Efficiency in GAMS code usage (loop)
75.3 s 0.3 s
74.2 s 72.9 s
Y(i,j,k) = 2 ; Y(i,j,k) = 2 ;
4.5 s 1.3 s
while (condition,
) ;
repeat
until condition;
if (condition,
else
) ;
Detection of sets
nd
ndref (nd)
refnd (nd)
nodes
current reference
subset of reference
node
nodes
/
/
/
node01
node01
node01
* node19 /
/
/
isolated nc
nod
ln
(nd)
(nd)
(nd,nd)
subnet(nd,nd,nd)
current connected
subset of connected
lines
subnetworks
nodes
nodes
/
/
null
null
/
/
subnetworks parameters
pAux1 auxiliary / 0 /
pAux2 auxiliary / 1 /
alias (nd,n1,n2,ni,nf)
if (pAux2 = 0,
* subnetwork of the connected lines to a reference node
subnet(ln(nc,nf),ndref) = yes ;
subnet(ln(nf,nc),ndref) = yes ;
* subnetwork of the connected nodes
nod ( nc ) = yes ;
* subnetwork of the reference nodes
refnd ( ndref) = yes ;
) ;
) ;
display subnet, nod, refnd ;
* disconnect the reference node
ndref(nd) = no ;Good Optimization Modeling Practices with GAMS. July 2016 47
) ;
Inverting a matrix, e.g., PTDF
set i / i1*i3 /
display a, ainv
sets
day day / day01*day10 /
sc scenario / sc01* sc02 /
put console
loop ((day,sc),
putclose 'Day ' day.tl:0 ' Scenario ' sc.tl:0 ' Elapsed Time ' [(jnow-jstart)*86400]:6:3 ' s' sleep(1)
) ;
$OnText
Developed by
Andrés Ramos
Instituto de Investigación Tecnológica
Escuela Técnica Superior de Ingeniería - ICAI
UNIVERSIDAD PONTIFICIA COMILLAS
Alberto Aguilera 23
28015 Madrid, Spain
Andres.Ramos@comillas.edu
$OffText
sets
n hour
n1(n) first hour of the day
sc scenario
g generating unit
t (g) thermal unit
h (g) hydro plant
alias (n,nn)
parameters
pDemand (n) hourly load [GW]
pOperReserve (n) hourly operating reserve [GW]
pOperReserveUp (n) hourly operating reserve up [GW]
pOperReserveDw (n) hourly operating reserve down [GW]
pIntermGen (n,sc) stochastic IG generation [GW]
pScenProb (sc) probability of scenarios [p.u.]
pCommitt ( g,n) commitment of the unit [0-1]
pProduct (sc,g,n) output of the unit [GW]
pSRMC (sc, n) short run marginal cost [ € per MWh]
binary variables
vCommitt ( n,g) commitment of the unit [0-1]
vStartup ( n,g) startup of the unit [0-1]
vShutdown ( n,g) shutdown of the unit [0-1]
positive variables
vProduct (sc,n,g) output of the unit [GW]
vProduct1 (sc,n,g) output of the unit > min load [GW]
vConsump (sc,n,g) consumption of the unit [GW]
vENS (sc,n ) energy not served [GW]
vIG (sc,n ) intermittent generation [GW]
vReserve (sc,n,g) reserve at the end of period [GWh]
vSpillage (sc,n,g) spillage [GWh]
equations
eTotalVCost total system variable cost [M€]
eBalance (sc,n ) load generation balance [GW]
eOpReserve( n ) operating reserve [GW]
eReserveUp(sc,n ) operating reserve upwards [GW]
eReserveDw(sc,n ) operating reserve downwards [GW]
eMaxOutput(sc,n,g) max output of a committed unit [GW]
eMinOutput(sc,n,g) min output of a committed unit [GW]
eTotOutput(sc,n,g) tot output of a committed unit [GW]
eRampUp (sc,n,g) bound on ramp up [GW]
eRampDw (sc,n,g) bound on ramp down [GW]
eUCStrShut( n,g) relation among commitment startup and shutdown
eWtReserve(sc,n,g) water reserve [GWh]
eMinTUp (sc,n,g) minimum up time ( committed)
eMinTDown (sc,n,g) minimum down time (not committed) ;
eBalance (sc,n ) $ pScenProb(sc) .. sum[t, vProduct(sc,n,t)] + sum[h, vProduct(sc,n,h)] - sum[h, vConsump(sc,n,h)/pEffic(h)] + vIG(sc,n) + vENS(sc,n) =e= pDemand(n) ;
eOpReserve( n ) .. sum[t, pMaxProd(t) * vCommitt(n,t)] + sum[h, pMaxProd(h)] =g= pOperReserve (n) + pDemand(n) ;
eReserveUp(sc,n ) $ pScenProb(sc) .. sum[t, pMaxProd(t) * vCommitt(n,t) - vProduct(sc,n,t)] =g= pOperReserveUp(n) ;
eReserveDw(sc,n ) $ pScenProb(sc) .. sum[t, pMinProd(t) * vCommitt(n,t) - vProduct(sc,n,t)] =l= - pOperReserveDw(n) ;
eMinTUp (sc,n,t) $ pScenProb(sc) .. sum[nn $(ord(nn) >= ord(n)+1-pMinTU(t) and ord(nn) <= ord(n)), vStartup (nn,t)] =l= vCommitt(n,t) ;
eMinTDown (sc,n,t) $ pScenProb(sc) .. sum[nn $(ord(nn) >= ord(n)+1-pMinTD(t) and ord(nn) <= ord(n)), vShutdown(nn,t)] =l= 1 - vCommitt(n,t) ;
eWtReserve(sc,n,h) $ pScenProb(sc) .. vReserve(sc,n-1,h) + pIniReserve(h) $n1(n) + pInflows(h,n) - vSpillage(sc,n,h) - vProduct(sc,n,h) + vConsump(sc,n,h) =e=
vReserve(sc,n,h) ;
sets
$include tmp_indices.txt
;
$include tmp_param.txt
parameter pDemand(n) hourly load [MW] /
$include tmp_demand.txt
/
parameter pOperReserve(n) hourly operating reserve [MW] /
$include tmp_oprres.txt
/
parameter pOperReserveUp(n) hourly operating reserve [MW] /
$include tmp_oprresup.txt
/
parameter pOperReserveDw(n) hourly operating reserve [MW] /
$include tmp_oprresdw.txt
/
table pIntermGen(n,sc) stochastic IG generation [MW]
$include tmp_IGgen.txt
table pThermalGen(g,*)
$include tmp_thermalgen.txt
table pHydroGen (g,*)
$include tmp_hydrogen.txt
table pInflows (g,n)
$include tmp_inflows.txt
execute 'del tmp.txt tmp_indices.txt tmp_param.txt tmp_demand.txt tmp_oprres.txt tmp_oprresup.txt tmp_oprresdw.txt tmp_IGgen.txt tmp_thermalgen.txt tmp_hydrogen.txt tmp_inflows.txt' ;
* assignment of thermal units, storage hydro and pumped storage hydro plants
* if the initial output of the unit is above its minimum load then the unit is committed, otherwise it is not committed
pIniUC (g) = 1 $[pIniOut(g) >= pMinProd(g)] ;
vCommitt.up ( n,g) = 1 ;
vStartup.up ( n,g) = 1 ;
vShutdown.up( n,g) = 1 ;
put TMP putclose 'par=pCommitt rdim=1 rng=UC!a1' / 'par=pProduct rdim=2 rng=Output!a1' / 'par=pSRMC rdim=1 rng=SRMC!a1'
execute_unload 'tmp.gdx' pProduct pCommitt pSRMC
execute 'gdxxrw.exe tmp.gdx SQ=n EpsOut=0 O="tmp.xlsx" @tmp.txt'
execute 'del tmp.gdx '
execute 'del tmp.txt'
$OnListing
• Specially useful in large-scale LP problems or NLP problems and/or when willing to use the dual
variables
• The condition number is a measure for the sensitivity of the solution of a system of linear equations
to errors in the data.
• It is the ratio between largest and smallest eigenvalues
• Condition numbers below 106 are good enough. Numerical problems arise for condition numbers
above 1010
• Quality 1 in CPLEX
• Kappa 1 in GUROBI
Good Optimization Modeling Practices with GAMS. July 2016 60
Rule of thumb for selecting an LP algorithm
• Simplex (or dual simplex) method can the best choice for
moderate size (up to 100000 x 100000)
• Interior point method is usually the most efficient for
huge problems
• It can be threaded quite efficiently (compared to simplex)
ModelName.NumDVar Number of discrete variables ModelName.BRatio Basis ration controls the use of previous basis
ModelName.NumNz Number of non zeros ModelName.HoldFixed Fix and eliminate variables
ModelName.IterLim Iteration limit
ModelName.NodLim Node limit
ModelName.OptCA Absolute optimality tolerance
ModelName.OptCR Relative optimality tolerance
ModelName.OptFile Use of an option file
Good Optimization Modeling Practices with GAMS. July 2016 69
ModelName.PriorOpt Use of priority
GAMS Call Options
GAMS Options
Suppress Suppress echo of the code listing
PW Page width
PS Page size
Charset Allows international characters
For example, InterfaceName, SolverSelection,
U1..U10 User parameter SkipExcelInput, SkipExcelOutput,
• GUSS (Gather-Update-Solve-Scatter)
• Use of sensitivity analysis for solving many similar problems
equations parameters
eCost transportation cost pA(i) origin capacity
eCapacity(i) maximum capacity of each origin / VIGO 350
eDemand (j) demand supply at destination ; ALGECIRAS 700 /
eCost .. sum[(i,j), pC(i,j) * vX(i,j)] =e= vCost ; pBS(sc,j) stochastic destination demand
eCapacity(i) .. sum[j, vX(i,j)] =l= pA(i) ;
/ sc000 . MADRID 400
eDemand (j) .. sum[i, vX(i,j)] =g= pB(j) ;
sc000 . BARCELONA 450
model mTransport / all / sc000 . VALENCIA 150 / ;
* lazy input, feeding data for all the scenarios with random demand
pBS(sc,j) = pBS('sc000',j) * [1+uniform(-0.05,0.05)] ;
$set language 1
$ifthene %language% == 0
$ include ModelEs.gms
$elseif %language% == 1
$ include ModelEn.gms
$elseif %language% == 2
$ include ModelFr.gms
$endif
* File ModelEs.gms
$setglobal Lema Más sabe el diablo por viejo que por diablo
* File ModelEn.gms
$setglobal Lema The devil knows many things because he is old
* File ModelFr.gms
$setglobal Lema Le diable sait beaucoup parce qu'il est vieux
Good Optimization Modeling Practices with GAMS. July 2016 77
Introducing flexibility
$SetGlobal x 100
parameter pDimension / %x% /
set u / unit1*unit%x% /
display pDimension, u
set i / 1 * 10000000 /
parameter pp(i) ;
pp(i) = 33 ;
≤ ∀#
table pC(i,j) per unit transportation cost
MADRID BARCELONA VALENCIA
VIGO 0.06 0.12 0.09
ALGECIRAS 0.05 0.15 0.11
variables
≥ $ ∀%
vX(i,j) units transported
vCost transportation cost ≥0
positive variable vX
equations
eCost transportation cost
eCapacity(i) maximum capacity of each origin
eDemand (j) demand supply at destination ;
• 700
675
Perc 95 %
• Matlab (gdxmrw)
Cota [m]
RMn
500 RMx
Vertido
• R (gdxrrw) 400
665
TunelInf
TunelSup
TnlMn
• APIs
300
660 TnlMx
Cota
200
• .Net 100
655
• Java 0 650
s0840
s0843
s0846
s0849
s0852
s0903
s0906
s0909
s0912
s0915
s0918
s0921
s0924
s0927
s0930
s0933
s0936
s0939
s0942
s0945
s0948
s0951
s1002
s1005
s1008
s1011
s1014
s1017
s1020
s1023
s1026
s1029
s1032
s1035
s1038
• Python
• Input/output data and simple graphs (Excel, Access)
• Advanced graphs (GNUPlot, Matlab)
• Input
• Use simple input formats. Make input easy to prepare and adapted to the user.
• Check input for validity and plausibility. This can be done in input/output interface
(e.g., using conditional formatting in Excel) or in GAMS
• Identify bad input, recover if possible, if not abort the execution giving
information to the user
• Document your data layouts
• Definitions with physical dimensions
• References to the data origin
• Output
• It is difficult to determine the infeasibility of an optimization program. It is better
to check bad data.
• Produce self-explanatory output.
Mathematical Formulation
Reformulation in MIP problems
• Most MIP problems can be formulated in different ways
• In MIP problems, a good formulation is crucial to solve
the model
• How good is a MIP formulation?
• Integrality gap difference between the objective function of the MIP and
LP relaxation solutions
• Given two equivalent MIP formulations, one is stronger
(tighter/better) than the other, if the feasible region of
the linear relaxation is strictly contained in the feasible
region of the other one. Integrality gap is lower.
Good Optimization Modeling Practices with GAMS. July 2016 90
Warehouse location problem (no limits) (i)
• Choose where to locate warehouses among a set of
locations and assign clients to the warehouses minimizing
the total cost. No limits means that there is no limit in the
number of clients assigned to a warehouse.
• Data j locations
i clients
c j localization cost in j
hij cost of satisfying the demand of client i from j
• Variables y =
1 warehouse located in j
j
0 otherwise
xij fraction of demand of client i met from j
∑x ij = 1 ∀i ∑x
j
ij = 1 ∀i
j
xij ≤ y j ∀ij ∑x
i
ij ≤ My j ∀j
st −1 + xt = dt + st ∀t
Number of constraints: 2T
xt ≤ Myt ∀t
Number of variables: 3T
s0 = sT = 0
xt , st ≥ 0, yt ∈{0,1}
• Formulation #2
T t T
min ∑∑( pi + hi + hi+1 + ⋯+ ht −1 ) qit + ∑ct yt
t =1 i =1 t =1
t Number of constraints: T+T2/2
∑q
i =1
it = dt ∀t
Number of variables: T+T2/2
qit ≤ dt yi ∀it
qit ≥ 0, yt ∈{0,1}
• Formulation #2 is better. However, it has a greater
number of constraints and variables.
∑x j =1 ∑x
j =1
j =1
n
j =i +1
j =1
n n ∑x j =1
∑r x
j =1
j j = r0 ∑r x
j =1
j j = r0 j =1
n
∑r x
j =1
j j = r0
• Formulation #2 is better than the #1. The evaluation of the objective function in #1
requires 2n2/2 multiplications. In #2 only n+n2/2
• Formulation #3 has essentially the same number of multiplications but they appear in
linear constraints. Number of constraints is bigger but all of them are linear. Linear
algebra is much more efficient. Formulation #3 is the most efficient
Good Optimization Modeling Practices with GAMS. July 2016 97
Reformulation of an NLP problem
0
*#+
1
+) 0= +)
*#+
∑
1=
1≥2
• Formulation #1 has a lot of nonlinear variables and it not protected against division by
zero
• Formulation #2 has only 2 nonlinear variables, the remaining ones appear in linear
equations, and the denominator is lower bounded to avoid division by zero. Model is
easier to solve and more robust
) =( + )/2
) =( − )/2
7 ≤ ≤0
7 ≤ ≤0
(7 + 7 ) ≤ ) ≤ (0 + 0 )
(7 − 0 ) ≤ ) ≤ (0 − 7 )
AIMMS Modeling Guide - Integer Programming Tricks
Good Optimization Modeling Practices with GAMS. July 2016 99
Solving real problems
• MIP
• Solve with a sensible relative optimality tolerance
• Provide an initial solution based on specific knowledge of the model
or use the solution from a previous solve
• NLP
• Introduce sensible bounds on variables AND
• Provide a good enough starting point AND
• Scale the problem
Advanced Algorithms
Transportation problem solved as MCP
sets
I origins / VIGO, ALGECIRAS / min sets
J destinations / MADRID, BARCELONA, VALENCIA / I origins / VIGO, ALGECIRAS /
J destinations / MADRID, BARCELONA, VALENCIA /
parameters
pA(i) origin capacity ≤ ∀# parameters
pA(i) origin capacity
/ VIGO 350
ALGECIRAS 700 / / VIGO 350
ALGECIRAS 700 /
pB(j) destination demand ≥ $ ∀% pB(j) destination demand
/ MADRID 400 / MADRID 400
BARCELONA 450
≥0
BARCELONA 450
VALENCIA 150 / VALENCIA 150 /
table pC(i,j) per unit transportation cost table pC(i,j) per unit transportation cost
MADRID BARCELONA VALENCIA MADRID BARCELONA VALENCIA
VIGO 0.06 0.12 0.09 VIGO 0.06 0.12 0.09
ALGECIRAS 0.05 0.15 0.11 ℒ = +9 − +: $ − ALGECIRAS 0.05 0.15 0.11
variables variables
vX(i,j) units transported vX(i,j) units transported
vCost transportation cost vA(i ) Lagrange multiplier of capacity constraint
vB( j) Lagrange multiplier of demand constraint
=> +9 ≥ : : ∀#%
→
positive variable vX
positive variables vX, vA, vB
equations =?@A equations
eCost transportation cost
eCapacity(i) maximum capacity of each origin − ≥− :9 ∀# eProfit(i,j) marginal cost >= marginal profit
eCapacity(i) maximum capacity of each origin
eDemand (j) demand supply at destination ; eDemand (j) demand supply at destination ;
≥$ :: ∀%
eCost .. sum[(i,j), pC(i,j) * vX(i,j)] =e= vCost ; eProfit(i,j) .. vA(i) + pC(i,j) =g= vB(j) ;
eCapacity(i) .. sum[ j , vX(i,j)] =l= pA(i) ; eCapacity(i) .. -sum[j, vX(i,j)] =g= -pA(i) ;
eDemand (j) .. sum[ i , vX(i,j)] =g= pB(j) ; eDemand (j) .. sum[i, vX(i,j)] =g= pB(j) ;
≤ ( )E ∀#% : DE
≥0
Good Optimization Modeling Practices with GAMS. July 2016 105
FCTP solved by Benders decomposition (i)
$title Fixed-charge transportation problem (FCTP) solved by Benders' decomposition
* relative optimality tolerance in solving MIP problems
option OptcR = 0
sets
L iterations / l1 * l20 /
LL(l) iterations subset
I origins / i1 * i4 /
J destinations / j1 * j3 /
* Begin problem data
parameters
A(i) product offer
/ i1 20, i2 30, i3 40, i4 20 /
B(j) product demand
/ j1 20, j2 50, j3 30 /
table C(i,j) per unit variable transportation cost
j1 j2 j3
i1 1 2 3
i2 3 2 1
i3 2 3 4
i4 4 3 2
table F(i,j) fixed transportation cost
j1 j2 j3
i1 10 20 30
i2 20 30 40
i3 30 40 50
i4 40 50 60
* End problem data
abort $(sum[i, A(i)] < sum[j, B(j)]) 'Infeasible problem'
parameters
BdTol relative Benders' tolerance / 1e-6 /
Z_Lower lower bound / -inf /
Z_Upper upper bound / inf /
Y_L (l,i,j) first stage variables values in iteration l
PI_L (l,i,j) dual variables of second stage constraints in iteration l
Delta(l) cut type (feasibility 0 optimality 1) in iteration l
Z2_L (l) subproblem objective function value in iteration l
positive variable
X(i,j) arc flow
binary variable
Y(i,j) arc investment decision
variables
Z1 first stage objective function
Z2 second stage objective function
Theta recourse function
Good Optimization Modeling Practices with GAMS. July 2016 106
FCTP solved by Benders decomposition (ii)
equations
EQ_Z1 first stage objective function
EQ_Z2 second stage objective function
EQ_OBJ complete problem objective function
Offer (i ) offer at origin
Demand ( j) demand at destination
FlowLimit(i,j) arc flow limit
Bd_Cuts (l) Benders' cuts ;
X.up(i,j) = min[A(i),B(j)]
Subproblem_Bd.OptFile = 1 ;
LL (l) = no ;
Theta.fx = 0 ;
Delta (l) = 0 ;
Z2_L (l) = 0 ;
PI_L(l,i,j) = 0 ;
Y_L (l,i,j) = 0 ;
* solving subproblem
solve Subproblem_Bd using RMIP minimizing Z2 ;
Delta(l) = 1 ;
Z2_L (l) = Subproblem_Bd.ObjVal ;
) ;
PI_L(l,i,j) = FlowLimit.m(i,j) ;
Y.lo( i,j) = 0 ;
Y.up( i,j) = 1 ;
Theta.lo = -inf ;
Theta.up = inf ;
solve Complete using MIP minimizing Z1 Good Optimization Modeling Practices with GAMS. July 2016 108
Stochastic FCTP
Flows
(second stage) Complete problem
Investment decisions
(first stage)
L
min
N
C ) +
,B
L
Capacity of each
L
origin ≤ ∀#M
Demand of each
L
≤$ ∀%M
destination
option OptcR = 0
sets
$title Deterministic fixed-charge transportation problem (DFCTP)
I origins / i1 * i4 /
* relative optimality tolerance in solving MIP problems J destinations / j1 * j3 /
option OptcR = 0 S scenarios / s1 * s3 /
sets parameters
I origins / i1 * i4 / A(i) product offer / i1 20, i2 30, i3 40, i4 20 /
J destinations / j1 * j3 / P(s) scenario probability / s1 0.5, s2 0.3, s3 0.2 /
table B(s,j) product demand
parameters j1 j2 j3
A(i) product offer
/ i1 20, i2 30, i3 40, i4 20 / s1 21 51 31
B(j) product demand s2 32 22 52
/ j1 20, j2 50, j3 30 / s3 53 33 23
table C(i,j) per unit variable transportation cost
table C(i,j) per unit variable transportation cost j1 j2 j3
j1 j2 j3 i1 1 2 3
i1 1 2 3 i2 3 2 1
i2 3 2 1 i3 2 3 4
i3 2 3 4 i4 4 3 2
i4 4 3 2
table F(i,j) fixed transportation cost
table F(i,j) fixed transportation cost j1 j2 j3
j1 j2 j3 i1 10 20 30
i1 10 20 30 i2 20 30 40
i2 20 30 40 i3 30 40 50
i3 30 40 50 i4 40 50 60
i4 40 50 60
loop (s, abort $(sum[i, A(i)] < sum[j, B(s,j)]) 'Infeasible problem' )
abort $(sum[i, A(i)] < sum[j, B(j)]) 'Infeasible problem'
X.up(i,j) = 100
Good Optimization Modeling Practices with GAMS. July 2016 111
Cournot model: formulation
e Company
• Objective function: profits e* Other companies
Be = p ⋅ qe - C e Be Profits
p Price
• Inverse demand function qe Output
p = f ∑ qe Ce Variable costs
e MCe Marginal cost
• Cournot conjecture: vertical supply MRe Marginal revenue
p-MCe Price mark-up
System of ∂p ∂p ∂(qe + qe * ) ∂q
equations = = p ′ ↔ e* = 0
∂qe ∂q ∂qe ∂qe Own output decision will not
have an effect on the
decisions of the competitors
• Optimality conditions
∂ Be p − MC e (qe )
= 0 → MRe = p + qe p ′ = MC e (qe ) → qe =
∂qe −p ′
Equilibrium under
perfect competition
MC2 = 3; q2 MAX = 5
3
Competitive
MC1 = 2 ; q1 MAX = 5 supply function
2
D = q1 + q2
5 7 10 MW
Good Optimization Modeling Practices with GAMS. July 2016 114
Cournot model: example (II)
• Duopoly ∂ B1
= 0 → p + q1 ⋅ (−1) = 2
• Company 1: MC1 = 2 €/MW Cournot
∂q 1
• Company 2: MC2 = 3 €/MW ∂ B2
= 0 → p + q2 ⋅ (−1) = 3
∂q 2
• IDF: p = 10 - (q1 + q2); p’=-1
€/MW p Solving
10
q1 = 3, q2 = 2
p’=-1
p = 10 - (q1 + q2) = 5
5
3
q2 = 2
2 q1 = 3
D = q1 + q2
5 7 10 MW
Good Optimization Modeling Practices with GAMS. July 2016 115
Cournot Market Equilibrium
$Title Cournot Market Equilibrium Model
$ontext
Developed by
Andrés Ramos
Instituto de Investigación Tecnológica
Escuela Técnica Superior de Ingeniería - ICAI
UNIVERSIDAD PONTIFICIA COMILLAS
Alberto Aguilera 23
28015 Madrid, Spain
Andres.Ramos@comillas.edu
August 5, 2016
$offtext
sets
p periods / p1 /
t thermal units / t1 * t2 /
parameters
pMarginalCost(t) marginal costs [€ per MW] / t1 2, t2 3 /
pMaxThrOutput(t) maximum output [MW] / t1 5, t2 5 /
positive variables
vSMP ( p) system marginal price [€ per MW]
vThrOutput(t,p) thermal unit output [MW]
binary variables
vCommitment(t,p) thermal unit commitment [p.u.]
variable
vTotalCost total investment and operation cost
equations
eTotalCost total investment and operation cost [€]
eLoadBalance(p) balance between generation and load [€ per MW] ;
variable
vSocialWelfare social welfare [€]
equations
eSocialWelfare social welfare [€] ;
vThrOutput.up(t,p) = pMaxThrOutput(t) ;