Mathematical Modeling¶
HAEO uses linear programming (LP) to optimize energy flows across your network. This section explains the mathematical formulation behind HAEO's optimization engine.
Linear Programming Overview¶
Linear programming finds optimal values for decision variables that minimize (or maximize) an objective function subject to linear constraints.
Standard LP form:
Where:
- \(x\): Decision variables (what the optimizer chooses)
- \(c\): Objective coefficients (costs/prices)
- \(A\), \(b\): Inequality constraint matrices
- \(A_{\text{eq}}\), \(b_{\text{eq}}\): Equality constraint matrices
- \(l\), \(u\): Variable bounds
Linearity requirement: All relationships must be linear. No quadratic terms (\(x^2\)), products (\(xy\)), or nonlinear functions (\(\sin(x)\), etc.).
Why linear programming?¶
LP fits home energy systems because power balances, costs, and most device constraints are linear. It delivers a global optimum when a feasible solution exists and scales well as you extend the horizon or add elements.
HAEO Uses LP, Not MILP
HAEO uses pure linear programming, not Mixed-Integer Linear Programming (MILP). This avoids binary variables for on/off decisions, keeping solve times fast. Energy systems with continuous power flows don't need MILP's discrete decision capabilities.
HAEO's Optimization Problem¶
HAEO formulates energy system optimization as a linear program:
Where:
- \(t\): Time step index (0 to \(T-1\))
- \(T\): Number of time steps in optimization horizon
Feasibility and Optimality¶
- Feasible solution: Satisfies all constraints (power balance, limits)
- Optimal solution: Feasible solution with minimum cost
- Infeasible: No solution exists (load exceeds supply capacity)
Network Structure¶
HAEO models energy systems as directed graphs:
- Nodes: Elements (battery, grid, solar, load) and nodes (balance points)
- Edges: Connections with power flow variables
- Direction: Source → Target defines positive power flow direction
graph LR
Grid((Grid)) <-->|P_grid| Node((Node))
Solar((Solar)) -->|P_solar| Node
Battery((Battery)) <-->|P_batt| Node
Node -->|P_load| Load((Load))
style Net fill:#90EE90
Power Balance Constraint¶
At each node and every time step:
Where:
- \(\mathcal{C}_{\text{in}}\): Connections with node as target (inflows)
- \(\mathcal{C}_{\text{out}}\): Connections with node as source (outflows)
- \(P_c(t)\): Power flow through connection \(c\) at time \(t\)
This enforces Kirchhoff's current law: power in equals power out.
Time Discretization¶
The optimization horizon is divided into discrete periods:
- Horizon: Total optimization period (hours)
- Period: Time step duration (minutes)
- Time steps: \(T = \text{Horizon} / \text{Period}\)
Each decision variable exists for every time step.
Decision Variables¶
HAEO's LP solver determines optimal values for these variables:
| Variable | Symbol | Units | Description | Count |
|---|---|---|---|---|
| Connection power | \(P_c(t)\) | kW | Power flow through connection \(c\) | \(N_c \times T\) |
| Battery energy | \(E_b(t)\) | kWh | Stored energy in battery \(b\) | \(N_b \times T\) |
| Grid import | \(P_{\text{import}}(t)\) | kW | Power imported from grid | \(N_g \times T\) |
| Grid export | \(P_{\text{export}}(t)\) | kW | Power exported to grid | \(N_g \times T\) |
| Solar curtailment | \(P_{\text{curtail}}(t)\) | kW | Curtailed solar generation | \(N_s \times T\) |
Where:
- \(N_c\): Number of connections
- \(N_b\): Number of batteries
- \(N_g\): Number of grids
- \(N_s\): Number of solar arrays with curtailment enabled
- \(T\): Number of time steps
Element Models¶
Each element type has specific constraints and variables:
- Battery: Energy storage with SOC dynamics and charge/discharge limits
- Grid: Bidirectional power flow with import/export pricing
- Photovoltaics: Generation following forecast with optional curtailment
- Loads: Constant or forecast-based power consumption
- Connections: Power flow with directional limits
- Node: Virtual balance points enforcing power conservation
Objective Function¶
HAEO minimizes total system cost over the optimization horizon:
Where \(\Delta t\) is the period duration in hours.
Cost Components¶
Grid costs (import minus export revenue):
Battery costs (artificial costs for optimization):
Solar curtailment costs:
All prices/costs are in $/kWh, powers in kW, giving costs in $ per time step.
Constraints¶
HAEO enforces multiple constraint types:
Equality Constraints¶
- Power balance: At each net, inflow equals outflow
- Energy dynamics: Battery energy evolution over time
Inequality Constraints¶
- Power limits: Connection flows, charge/discharge rates, import/export limits
- Energy limits: Battery SOC minimum and maximum
- Non-negativity: Power flows, curtailment ≥ 0
Solver¶
HAEO uses HiGHS as the default LP solver:
- Open source: MIT licensed
- Fast: State-of-the-art performance
- Python integration: Via PuLP library
- Actively maintained: Continuous improvements
Example: Grid-Battery-Load System¶
Simple system with grid, battery, and load:
graph LR
Grid((Grid)) <--> Node((Node))
Battery((Battery)) <--> Node
Node --> Load((Load))
style Node fill:#90EE90
Decision variables:
- \(P_{\text{import}}(t)\), \(P_{\text{export}}(t)\): Grid power flows
- \(P_{\text{charge}}(t)\), \(P_{\text{discharge}}(t)\): Battery power flows
- \(E(t)\): Battery energy state
Objective:
Constraints:
Power balance at net:
Battery energy dynamics:
Battery limits:
This demonstrates all core HAEO modeling concepts.
Units¶
HAEO uses consistent units for numerical stability:
- Power: kilowatts (kW)
- Energy: kilowatt-hours (kWh)
- Time: hours (h)
- Cost: dollars ($)
This keeps values in similar numerical ranges, improving solver performance. See units documentation for details.
Element-Specific Models¶
Each element type has detailed mathematical formulation:
-
Energy storage with SOC dynamics, efficiency, and charge/discharge limits.
-
Bidirectional power flow with time-varying import/export pricing.
-
Solar generation following forecasts with optional curtailment.
-
Constant power consumption or forecast-based demand profiles.
-
Power flow constraints between elements with directional limits.
-
Virtual balance points enforcing power conservation (Kirchhoff's law).
Related Documentation¶
- User Guide - Configuring HAEO networks
- Units Guide - Unit conventions