Post · 6 min read· pp. 285–289

Numerical Scaling

Solvers work best when the coefficients, bounds and right-hand sides of a problem sit in a narrow band around 1. Energy system models often mix units that break this, e.g. CO2 buses in tonnes next to electricity buses in MWh, global constraints with right-hand sides of 1e8 or objective coefficients spanning many orders of magnitude. Badly scaled models solve slower and trigger solver warnings such as Gurobi's "Matrix range" messages.

PyPSA lets you declare the solver unit of each bus, each global constraint and the objective. The factors only change the numbers handed to the solver. linopy transforms solutions, shadow prices and the objective back to original units, so a scaled and an unscaled run give the same result up to solver tolerance.

Scaling factors

FactorSet viaActs on
Bus column factor sbs_bn.buses.static.scalingDispatch, capacity and storage columns of all one-ports at bus bb and of all branches with bus0 =b= b (solver column holds sb⋅xs_b \cdot x), plus all rows of these components and the energy balance row of bus bb (Bus-nodal_balance)
Global constraint row factorn.global_constraints.static.scalingCoefficients and right-hand side of the GlobalConstraint-{name} row
Objective factorn.optimize(objective_scaling=...)All objective coefficients, plus the cost-valued objective_constant column, the columns and rows of CVaR (CVaR, CVaR-a, CVaR-theta, CVaR-excess-*, CVaR-def) and the cost column and output rows of piecewise cost curves

A bus factor s = 1e-3 means the solver works in GW instead of MW at that bus. Since the factor applies to the columns of a component and to all of its internal rows, these rows are unchanged in the solver. A Link coefficient in the energy balance of bus1 reads η⋅sbus1/sbus0\eta \cdot s_{\text{bus1}} / s_{\text{bus0}} in the solver. With the same factor at both buses, the coefficients of the energy balance stay at their unscaled values.

Binary and integer columns (unit commitment status, modular capacities, maintenance), angles (transformer phase shifts), and the normalised weights and binaries of piecewise formulations are never scaled. The output column of a piecewise curve (e.g. the converted flow of a Link) and its linking, chord and domain rows take the factor of the component, so breakpoints are expressed in solver units. Piecewise cost curves (marginal_cost, capital_cost) are an exception: their cost column and output rows take the objective factor, their domain rows the factor of the component. The rows of the Kirchhoff voltage law take the factor of their sub-network, which keeps their solver coefficients independent of the bus factor. Their coefficients keep the fixed factor of 1e5 on the impedances. Since these rows mix the flows of all branches of a sub-network, all buses of one AC or DC sub-network must share the same factor whenever the model contains Kirchhoff voltage law rows. Radial networks without cycles may mix factors. The growth limit rows of a carrier (max_growth) take the geometric mean of the smallest and largest factor of the assets they sum, which equals the shared factor when all assets of the carrier use one factor.

Recipe

The following factors are a starting point that worked on PyPSA-Eur and the carbon management example, i.e. networks in MW, MWh and currency units (see the benchmarks). Tune them for your model.

text
>>> n = pypsa.examples.ac_dc_meshed()
>>> n.buses.static["scaling"] = 1e-4
>>> n.global_constraints.static["scaling"] = 1e-6
>>> n.optimize(objective_scaling=2e-3, log_to_console=False)
('ok', 'optimal')
>>> round(n.objective / 1e6, 2)
-3.47

The objective and all other results are reported in original units. For buses in different units, map factors by carrier:

text
>>> n.buses.static["scaling"] = n.buses.static.carrier.map({"AC": 1e-4, "DC": 1e-4, "co2": 1e-7})
>>> n.buses.static.scaling.unique()
array([0.0001])

Factors must be finite and positive; all default to 1. Results such as n.generators.p_nom_opt, n.buses_t.marginal_price or n.global_constraints.mu and the duals in n.model are reported in original units. LP files and n.model.matrices show solver units. Use n.model.matrices (A, b, c, lb, ub) to check the four ranges a solver judges.

Constraints added after n.optimize.create_model(), e.g. in extra_functionality, are not scaled by PyPSA. Set their factor with linopy, e.g. n.model.add_constraints(..., scaling=1e-3) or n.model.constraints["my-constraint"].scaling = 1e-3, and take the factors PyPSA used from the model, e.g. n.model.variables["Generator-p"].scaling. Replacing the objective in extra_functionality via n.model.objective = ... resets the objective factor to 1. Use n.model.add_objective(expr, overwrite=True, scaling=n.model.objective.scaling) to keep it. optimize_security_constrained, optimize_mga, optimize_mga_in_direction and optimize_mga_in_multiple_directions accept objective_scaling as well. In MGA, the budget constraint takes the factor, and the alternative objective keeps the factor of the cost objective although its units differ (e.g. MW for capacity weights). Choose the factor with both objectives in mind.

Choosing factors

Three rules follow from how column, row and objective scaling act:

  • Only column scaling narrows a spread inside a row or inside the objective. Row and objective factors change the unit of a whole row, so they only move its band.
  • Column scaling moves bounds and coefficients in opposite directions. A column that is too small in both its bounds and its coefficients cannot be fixed in both places.
  • Column scaling never touches the right-hand side, and row scaling never touches the bounds. After changing the unit of a bus, re-check the rows its columns appear in.

Scale units, not asset sizes

Scaling targets mismatched units, e.g. a tonne-based CO2 bus connected to MWh buses by links with efficiencies around 1e-3, huge global constraint rows and the objective band. It does not help with large single assets: column-scaling the bus of a 1e8 MWh biomass store only moves the spread from its bounds into the coefficients of the links and rows it shares with other assets. A large spread of asset sizes at one bus is real and no choice of units hides it.

Do not scale units that already match

On a PyPSA-Eur sector network with 39 nodes, scaling the CO2 buses (tonnes) by 1e-3 relative to the energy buses made things worse: HiGHS failed to solve, Gurobi was unaffected. CO2 coefficients of 0.013 to 1.8 t/MWh are already of the same order as the energy flows, so the factor only pushed them away from 1. Use different bus factors only for a genuine unit mismatch, e.g. kt next to MWh or link efficiencies around 1e-3 or 1e3. Large right-hand sides from annual potentials, e.g. EU-wide biomass, are real magnitudes that no scaling removes.

Scaling also shifts what solver tolerances mean, since a solver checks feasibility and optimality on the scaled problem. Results of scaled and unscaled runs therefore agree only up to solver tolerance.

diagram 2026-11-05

diagram 2026-11-05