Skip to content

Commit 6dc3a83

Browse files
committed
Improve Exceptions and add a meaningfull comment in InvestParameters
1 parent 088cd3f commit 6dc3a83

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

flixopt/features.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def _do_modeling(self):
5555
def _create_variables_and_constraints(self):
5656
size_min, size_max = (self.parameters.minimum_or_fixed_size, self.parameters.maximum_or_fixed_size)
5757
if self.parameters.linked_periods is not None:
58+
# Mask size bounds: linked_periods is a binary DataArray that zeros out non-linked periods
5859
size_min = size_min * self.parameters.linked_periods
5960
size_max = size_max * self.parameters.linked_periods
6061

@@ -394,6 +395,11 @@ def __init__(
394395

395396
def _do_modeling(self):
396397
super()._do_modeling()
398+
# Validate all piecewise variables have the same number of segments
399+
segment_counts = [len(pw) for pw in self._piecewise_variables.values()]
400+
if not all(count == segment_counts[0] for count in segment_counts):
401+
raise ValueError(f'All piecewises must have the same number of pieces, got {segment_counts}')
402+
397403
for i in range(len(list(self._piecewise_variables.values())[0])):
398404
new_piece = self.add_submodels(
399405
PieceModel(
@@ -454,9 +460,13 @@ def __init__(
454460
piecewise_shares: dict[str, Piecewise],
455461
zero_point: bool | linopy.Variable | None,
456462
):
457-
assert len(piecewise_origin[1]) == len(list(piecewise_shares.values())[0]), (
458-
'Piece length of variable_segments and share_segments must be equal'
459-
)
463+
origin_count = len(piecewise_origin[1])
464+
share_counts = [len(pw) for pw in piecewise_shares.values()]
465+
if not all(count == origin_count for count in share_counts):
466+
raise ValueError(
467+
f'Piece count mismatch: piecewise_origin has {origin_count} segments, '
468+
f'but piecewise_shares have {share_counts}'
469+
)
460470
self._zero_point = zero_point
461471
self._piecewise_origin = piecewise_origin
462472
self._piecewise_shares = piecewise_shares

0 commit comments

Comments
 (0)