
Cheat sheet: Custom constraints
Source:vignettes/articles/cheatsheet-constraints.Rmd
cheatsheet-constraints.RmdLinear rules across the model's variables -- caps, shares, targets -- and extra costs in the objective
PDF, A4, one page · energyRt 0.91.5.9015 (dev) · 2026-11-29Anatomy of a constraint
CO2CAP <- newConstraint("CO2CAP",
eq = "<=",
for.each = data.frame(comm = "CO2",
year = c(2030, 2040, 2050)),
ems = list(variable = "vEmsFuelTot"),
rhs = data.frame(year = c(2030, 2050),
rhs = c(500, 100)),
defVal = Inf)
for.each |
one row of the table = one equation; its columns fix those sets |
ems = list(...) |
a left-hand term: a variable summed over every set not fixed |
eq |
"<=", ">=" or "=="
|
rhs |
by the for.each sets; years between are interpolated |
defVal |
the right-hand side where rhs has no row; set it |
What it becomes
For each row of for.each (here: CO2 in 2030, 2040, 2050):
Σ(region, slice) vEmsFuelTot[CO2, y]
<= rhs[y] # 500, 300, 100
Each term is Σ mult × variable, summed over the sets that for.each does not fix and narrowed by for.sum. Several terms add up on the left.
Check it reaches the model -- no solver needed:
scen <- interpolate_model(mod, CO2CAP,
name = "POL", ondisk = FALSE)A term, in full
variable chr the model variable, e.g. "vTechCap", "vTechOut", "vEmsFuelTot"
mult df coefficient: a number, or a table by sets
process_ chr processes by regular expression; a base name selects all its variants
classes chr process classes, e.g. "technology", "storage"
vintage chr vintages of vintaged processes
vintage_ chr vintages, by regular expression
cluster chr clusters of clustered processes
cluster_ chr clusters, by regular expression
timeframe chr take the variable at one time level, e.g. "ANNUAL"
geoframe chr take the variable at one region level, e.g. "nation"
defVal num default coefficient
desc chr description
misc list free list
newConstraint()
newConstraint(name, desc, ..., eq, for.each, rhs, defVal, interpolation)Each
... argument is one left-hand term, a list(); its name is a label.name chr set name
desc chr description
eq chr "==" (default), "<=", ">="
for.each list the sets the constraint is written for, one equation per row
lhs list the terms, built from ...
rhs df right-hand side by the for.each sets
defVal num right-hand side where rhs has no row
interpolation chr "inter" (default), "back.inter.forth", ...: how rhs fills years
misc list free list
A share
RES30 <- newConstraint("RES30",
eq = ">=",
for.each = data.frame(
year = c(2040, 2050)),
re = list(variable = "vTechOut",
for.sum = list(tech = "EWIN",
comm = "ELC")),
all = list(variable = "vTechOut",
for.sum = list(comm = "ELC"),
mult = -0.3),
rhs = 0, defVal = 0)
wind − 0.3 × all ≥ 0: at least 30% of power from wind, per year.A total over the horizon
NEWGAS <- newConstraint("NEWGAS",
eq = "<=",
cap = list(variable = "vTechNewCap",
for.sum = list(tech = "EGAS")),
rhs = 2, defVal = 2)
No
for.each: one equation, summed over everything -- regions and years.Targeting families
SEL <- newConstraint("SEL", eq = "<=",
for.each = data.frame(year = 2050),
cap = list(variable = "vTechCap",
process_ = "^EW",
classes = "technology"),
rhs = 50, defVal = 50)
process_, classes, vintage, cluster: select processes without listing them.scen <- interpolate_model(mod, RES30,
NEWGAS, SEL, name = "POL2",
ondisk = FALSE)
All three accepted by the model.
Extra costs
name chr set name
desc chr description
variable chr the variable charged
subset df set values it is restricted to, e.g. list(tech = "EGAS")
mult df cost per unit of the variable
defVal num default multiplier
misc list free list
FEE <- newCosts("FEE",
variable = "vTechAct",
mult = 2,
subset = list(tech = "EGAS"))Taxes and subsidies instead
For a price on a flow, a capacity or an emission, newTax() and newSubsidy() are shorter than a constraint -- see Model bricks.
CO2TAX <- newTax("CO2TAX", comm = "CO2",
tax = data.frame(bal = 50))
newTax(name, comm, process, classes, tax, …)
Variables to constrain
vEmsFuelTot |
Emissions from fuel combustion | cryt |
vBalance |
Net commodity balance (all sources) | cryt |
vTechNewCap |
New capacity | ry |
vTechCap |
Total capacity of the technology | ry |
vTechAct |
Activity level of technology | ryt |
vTechInp |
Input level | cryt |
vTechOut |
Output of a main commodity | cryt |
vSupOut |
Output of supply | cryt |
vDemInp |
Input to demand | cryt |
vStorageOut |
Storage output | cryt |
vStorageLevel |
Storage level | cryt |
vTradeIr |
Trade flow between regions | crryt |
vExportRow |
Export to the rest of the world | cryt |
vImportRow |
Import from the rest of the world | cryt |
Two sets with the same name (vTradeIr has two regions) are addressed as src, dst in for.each and for.sum.
Pitfalls
- Always set
defVal: without it a missingrhsrow is 0, and a<=cap at zero switches things off. - A
for.sumname that matches nothing is an error, not a silent zero. - A year outside the horizon drops the row, with a warning.
- A variable present at several time levels: pin one with
timeframe =(andgeoframe =for regions), so levels are not summed together. - Use the owning process's own bounds (
cap.up,af.up) where they suffice; constraints are for rules across objects.