math_spec.piecewise
Expand piecewise: blocks into plain variables and constraints.
A piecewise: block becomes ordinary affine declarations before anything
reads the model. The λ convex-combination method needs only the breakpoint
parameters themselves, no derived data. For a block
piecewise:
curve:
over: bp
links:
- [power, power_bp]
- [fuel * eff, fuel_bp, "<="]
with F = the union of the links' dims, it emits:
variables:
curve_lam(F, bp) in [0, 1]
curve_seg(F, bp) binary (method: adjacency)
constraints:
curve_convexity(F): sum(curve_lam, over=bp) == 1
curve_pick(F): sum(curve_seg, over=bp) == 1 (method: adjacency)
curve_adjacency(F, bp): curve_lam <= curve_seg + shift(curve_seg, over=bp, offset=1, edge=0)
curve_link0(F): (power) == sum(curve_lam * power_bp, over=bp)
curve_link1(F): (fuel * eff) <= sum(curve_lam * fuel_bp, over=bp)
Only the restriction on λ varies (:data:~math_spec.model.PIECEWISE_METHODS);
lp emits no weights at all. A link expression is judged before expansion,
so p * p is refused against the link the user wrote rather than
curve_link0.
declaration_of(expanded)
#
The facts of one expanded block, as a program carries them.
A curve has an x-axis only where two links tie it, so the increasing
condition — and the shape it is checked with — exist only there; lp
alone needs a segment to state a line for; a mask must be one run.
Source code in src/math_spec/piecewise.py
derivations_of(block, expanded)
#
How each parameter block's expansion emitted is filled, by name.
Everything emitted hangs off the mask, so a block masking nothing emits nothing for the caller to be told about.
Source code in src/math_spec/piecewise.py
expand_piecewise(schema)
#
Return schema as a :class:_ExpandedSpec — every piecewise: block expanded away.
The adjacency row shifts with edge=0: a bare shift would drop the
first breakpoint's row and leave its weight unconstrained, a wrong MILP
with no error (#289). points: masks the weights and the segment
binaries and no constraint — every emitted row reduces over the breakpoint
axis or carries a masked weight. The result is memoised on schema, and a
:class:_ExpandedSpec comes straight back; a model with no piecewise: is
retyped with model_construct, its validation already done on the way in.
| RAISES | DESCRIPTION |
|---|---|
PiecewiseExpansionError
|
A block naming something that does not exist, or emitting a name the file already declares. |
Source code in src/math_spec/piecewise.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |