Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ The maximum is a single value out of the horizon, which leaves one gap: a load s
<img alt="Charging plan with attenuate_grid_peaks: the battery refill spread over the solar window and the feed-in held to a flat 1 kW plateau" src="docs/img/example-peak-light.png">
</picture>

## Hedging against forecast errors

A schedule on the forecast alone is a bet that the forecast holds. When the solar falls short or the demand runs higher, a battery drained to its minimum overnight leaves the evening to the grid at the evening price. The optional `uncertainty` block makes that risk part of the cost.

```json
"uncertainty": {"ft_dev": 0.3, "gt_dev": 0.2, "budget": 8}
```

Every step may deliver `ft_dev` less solar and `gt_dev` more demand than forecast, but at most `budget` steps do so within a day. That is the uncertainty budget of Bertsimas and Sim: a budget of 0 trusts the forecast, a budget of the full horizon assumes every step goes wrong, which hardly ever happens and buys a lot of insurance. Something like a quarter of a day covers the usual bad afternoon. The budget may be fractional.

The schedule itself stays as planned. What the model prices is the recourse. The deviations come from the forecast alone, so the worst case up to each step, the sum of the largest deviations of the day before it within the budget, is known before the solve. It looks back one day only: the schedule is replanned long before that, and counted from the start of the horizon the reserve would grow with every day planned. A shortfall up to a step drains the batteries below the plan: the reserve they hold above their minimum covers it and is priced at the export price, the least that energy would have earned, and whatever is left is imported, at the price of the step where the plan would have discharged or where the shortfall arrives. Every step is priced, so an evening no reserve can cover does not leave the other days free to drain. A cheap step can also refill the shortfall from the grid with the charge power the plan leaves unused, at the round trip, so the plan has no reason to discharge into a cheap step and charge it back. On a flat tariff a reserve buys nothing over importing the shortfall when it happens, so the hedge does not charge one from the grid. Solar surplus above what all batteries can take in a step is exported or curtailed whatever the plan, so a shortfall up to it is not counted. Export the plan only chooses is not known before the solve, so a shortfall it would absorb still counts, which leans the hedge to the safe side on sunny steps. The hedge adds three variables and six rows per step, no binaries.

The reported `objective_value` is the plan's worth on the forecast as sent. A hedged plan scores a little worse there, that is the premium paid for the cover.

## How a request is solved

One request is up to five solver runs under one wall clock, `OPTIMIZER_TIME_LIMIT` (10 s in production). The stage clocks are shares of it, so a slower host raises the limit and every stage scales with it. Each stage keeps what the previous one found unless it can improve on it without spending money. The request log carries where the clock went (`stages`), which path was taken (`path`), what the tie break (`preferences`) and the continuity pass (`continuity`) did, and what the cost stage found (`cost_stage_value`) against what it could not rule out (`cost_stage_gap`, currency, zero when proven).
Expand Down
133 changes: 127 additions & 6 deletions client/client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,35 @@ components:
description: Grid export remuneration per Wh at each time step (currency units/Wh)
example: [0.15, 0.12, 0.10, 0.11, 0.14, 0.16]

Uncertainty:
type: object
description: >-
Forecast uncertainty the schedule hedges against. Every step may deliver ft_dev less solar and
gt_dev more demand than forecast, but at most budget steps do so within a day. The schedule then
carries the extra import the worst such case forces as a cost. Omitted, or a budget of 0,
plans on the forecast as sent.
properties:
ft_dev:
type: number
minimum: 0
maximum: 1
default: 0
description: Share of the solar forecast each step may fall short of
example: 0.3
gt_dev:
type: number
minimum: 0
maximum: 1
default: 0
description: Share of the demand forecast each step may exceed
example: 0.2
budget:
type: number
minimum: 0
default: 0
description: Steps that deviate adversely within a day, may be fractional
example: 8

OptimizationInput:
type: object
required:
Expand All @@ -418,6 +447,8 @@ components:
type: object
$ref: "#/components/schemas/OptimizerStrategy"
description: Strategy preferences
uncertainty:
$ref: "#/components/schemas/Uncertainty"
grid:
type: object
$ref: "#/components/schemas/GridConfig"
Expand Down
34 changes: 32 additions & 2 deletions src/optimizer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@
from werkzeug.exceptions import BadRequest

from . import explain, feedback
from .optimizer import CHARGING_STRATEGIES, DISCHARGING_STRATEGIES, BatteryConfig, GridConfig, OptimizationStrategy, Optimizer, TimeSeriesData
from .optimizer import (
CHARGING_STRATEGIES,
DISCHARGING_STRATEGIES,
BatteryConfig,
GridConfig,
OptimizationStrategy,
Optimizer,
TimeSeriesData,
UncertaintyConfig,
)
from .settings import OptimizerSettings

app = Flask(__name__)
Expand Down Expand Up @@ -199,11 +208,19 @@ def handle_validation_error(error):
'p_E': fields.List(fields.Float, required=True, description='Remuneration per Wh fed into grid at each time step'),
})

uncertainty_model = api.model('Uncertainty', {
'ft_dev': fields.Float(required=False, min=0, max=1, description='Share of the solar forecast each step may fall short of, 0 to 1'),
'gt_dev': fields.Float(required=False, min=0, max=1, description='Share of the demand forecast each step may exceed, 0 to 1'),
'budget': fields.Float(required=False, min=0, description='Steps that deviate adversely within a day. 0 disables the hedge'),
})

optimization_input_model = api.model('OptimizationInput', {
'strategy': fields.Nested(strategy_model, required=False, description='Optimization strategy'),
'grid': fields.Nested(grid_model, required=False, description='Grid import and export configuration'),
'batteries': fields.List(fields.Nested(battery_config_model), required=True, description='Battery configurations'),
'time_series': fields.Nested(time_series_model, required=True, description='Time series data'),
'uncertainty': fields.Nested(uncertainty_model, required=False,
description='Forecast uncertainty the schedule hedges against, see README'),
'eta_c': fields.Float(required=False, default=0.95, description='Charging efficiency'),
'eta_d': fields.Float(required=False, default=0.95, description='Discharging efficiency'),
})
Expand Down Expand Up @@ -293,6 +310,18 @@ def post(self):
p_E=data['time_series']['p_E'],
)

uncertainty_data = data.get('uncertainty') or {}
uncertainty = UncertaintyConfig(
ft_dev=uncertainty_data.get('ft_dev', 0.),
gt_dev=uncertainty_data.get('gt_dev', 0.),
budget=uncertainty_data.get('budget', 0.),
)
for key, limit in (('ft_dev', 1), ('gt_dev', 1), ('budget', None)):
value = getattr(uncertainty, key)
if value < 0 or (limit is not None and value > limit):
api.abort(400, "Uncertainty must be a share between 0 and 1 and a budget of steps",
details={f'uncertainty.{key}': value})

# Validate time series lengths. dt included: the model indexes every series by it,
# so a short dt was an IndexError and a 500 rather than a 400 naming the series.
lengths = {
Expand Down Expand Up @@ -333,7 +362,8 @@ def post(self):
time_series=time_series,
eta_c=data.get('eta_c', 0.95),
eta_d=data.get('eta_d', 0.95),
M=1e6
M=1e6,
uncertainty=uncertainty,
)

started = time.perf_counter()
Expand Down
Loading
Loading