Open a production line only if it pays.
Chapter: V — Modelling tricks · lesson id fixed-cost · practice lesson
A common pattern: opening a facility (or production line, or contract) costs a fixed amount, and then each unit produced has its own variable cost or revenue. You only want to pay the fixed cost if the line is actually used.
Model it with a binary y (line open?) and a continuous x (units produced). Tie them together with x ≤ M · y — if y = 0, x is forced to 0; if y = 1, x is free up to M.
Here we earn 2 per unit, pay a setup cost of 30 to open, and cap output at 50. Should the line open?
Setup cost 30, revenue 2/unit, max 50 units. Does the line open?
model fixed_cost
var 0 <= x # units produced
bin y # 1 if production line is open
constr x <= 50 * y # only produce if the line is open (M = 50)
max 2*x - 30*y
end
import elytica
def main():
elytica.init_model("fixed_cost")
elytica.run_model("fixed_cost")
print("x =", elytica.get_variable_value("fixed_cost", "x"))
print("y =", elytica.get_variable_value("fixed_cost", "y"))
print("obj =", elytica.get_best_primal_bound("fixed_cost"))
return 0
Profit with line open: 2·50 − 30 = 70. With line closed: 0. The solver will pick open.
no-error — Model solvesnumeric-match — Line opens (y = 1)numeric-match — Run at full capacity (x = 50)numeric-match — Profit = 2·50 − 30 = 70code-contains — Reads the objective from the solver (no hardcoding)code-contains — Reads variable values from the solver (no hardcoding)Higher setup cost but better unit margin. Worth opening the line?
model fixed_cost_v
var 0 <= x # units produced
bin y # 1 if production line is open
constr x <= 30 * y # only produce if the line is open (M = 30)
max 5*x - 40*y
end
import elytica
def main():
elytica.init_model("fixed_cost_v")
elytica.run_model("fixed_cost_v")
print("x =", elytica.get_variable_value("fixed_cost_v", "x"))
print("y =", elytica.get_variable_value("fixed_cost_v", "y"))
print("obj =", elytica.get_best_primal_bound("fixed_cost_v"))
return 0
Read this through MCP: read with path: "lessons/fixed-cost", or resource hlpl://lessons/fixed-cost.