1616
1717try :
1818 import tsam .timeseriesaggregation as tsam
19+
1920 TSAM_AVAILABLE = True
2021except ImportError :
2122 TSAM_AVAILABLE = False
@@ -52,7 +53,6 @@ def __init__(
5253 time_series_for_high_peaks : List [str ] = None ,
5354 time_series_for_low_peaks : List [str ] = None ,
5455 ):
55-
5656 """
5757 Args:
5858 original_data: The original data to aggregate
@@ -64,8 +64,9 @@ def __init__(
6464 time_series_for_low_peaks: List of time series to use for explicitly selecting periods with low values.
6565 """
6666 if not TSAM_AVAILABLE :
67- raise ImportError ("The 'tsam' package is required for clustering functionality. "
68- "Install it with 'pip install tsam'." )
67+ raise ImportError (
68+ "The 'tsam' package is required for clustering functionality. Install it with 'pip install tsam'."
69+ )
6970 self .original_data = copy .deepcopy (original_data )
7071 self .hours_per_time_step = hours_per_time_step
7172 self .hours_per_period = hours_per_period
@@ -140,12 +141,7 @@ def describe_clusters(self) -> str:
140141 def use_extreme_periods (self ):
141142 return self .time_series_for_high_peaks or self .time_series_for_low_peaks
142143
143- def plot (
144- self ,
145- colormap : str = 'viridis' ,
146- show : bool = True ,
147- save : Optional [pathlib .Path ] = None
148- ) -> 'go.Figure' :
144+ def plot (self , colormap : str = 'viridis' , show : bool = True , save : Optional [pathlib .Path ] = None ) -> 'go.Figure' :
149145 from . import plotting
150146
151147 df_org = self .original_data .copy ().rename (
@@ -339,33 +335,44 @@ def do_modeling(self):
339335 penalty = self .aggregation_parameters .penalty_of_period_freedom
340336 if (self .aggregation_parameters .percentage_of_period_freedom > 0 ) and penalty != 0 :
341337 for variable in self .variables_direct .values ():
342- self ._model .effects .add_share_to_penalty (
343- 'Aggregation' ,
344- variable * penalty
345- )
338+ self ._model .effects .add_share_to_penalty ('Aggregation' , variable * penalty )
346339
347340 def _equate_indices (self , variable : linopy .Variable , indices : Tuple [np .ndarray , np .ndarray ]) -> None :
348341 assert len (indices [0 ]) == len (indices [1 ]), 'The length of the indices must match!!'
349342 length = len (indices [0 ])
350343
351344 # Gleichung:
352345 # eq1: x(p1,t) - x(p3,t) = 0 # wobei p1 und p3 im gleichen Cluster sind und t = 0..N_p
353- con = self .add (self ._model .add_constraints (
354- variable .isel (time = indices [0 ]) - variable .isel (time = indices [1 ]) == 0 ,
355- name = f'{ self .label_full } |equate_indices|{ variable .name } ' ),
356- f'equate_indices|{ variable .name } ' )
346+ con = self .add (
347+ self ._model .add_constraints (
348+ variable .isel (time = indices [0 ]) - variable .isel (time = indices [1 ]) == 0 ,
349+ name = f'{ self .label_full } |equate_indices|{ variable .name } ' ,
350+ ),
351+ f'equate_indices|{ variable .name } ' ,
352+ )
357353
358354 # Korrektur: (bisher nur für Binärvariablen:)
359- if variable .name in self ._model .variables .binaries and self .aggregation_parameters .percentage_of_period_freedom > 0 :
360- var_k1 = self .add (self ._model .add_variables (
361- binary = True ,
362- coords = {'time' : variable .isel (time = indices [0 ]).indexes ['time' ]},
363- name = f'{ self .label_full } |correction1|{ variable .name } ' ), f'correction1|{ variable .name } ' )
355+ if (
356+ variable .name in self ._model .variables .binaries
357+ and self .aggregation_parameters .percentage_of_period_freedom > 0
358+ ):
359+ var_k1 = self .add (
360+ self ._model .add_variables (
361+ binary = True ,
362+ coords = {'time' : variable .isel (time = indices [0 ]).indexes ['time' ]},
363+ name = f'{ self .label_full } |correction1|{ variable .name } ' ,
364+ ),
365+ f'correction1|{ variable .name } ' ,
366+ )
364367
365- var_k0 = self .add (self ._model .add_variables (
366- binary = True ,
367- coords = {'time' : variable .isel (time = indices [0 ]).indexes ['time' ]},
368- name = f'{ self .label_full } |correction0|{ variable .name } ' ), f'correction0|{ variable .name } ' )
368+ var_k0 = self .add (
369+ self ._model .add_variables (
370+ binary = True ,
371+ coords = {'time' : variable .isel (time = indices [0 ]).indexes ['time' ]},
372+ name = f'{ self .label_full } |correction0|{ variable .name } ' ,
373+ ),
374+ f'correction0|{ variable .name } ' ,
375+ )
369376
370377 # equation extends ...
371378 # --> On(p3) can be 0/1 independent of On(p1,t)!
@@ -377,16 +384,20 @@ def _equate_indices(self, variable: linopy.Variable, indices: Tuple[np.ndarray,
377384
378385 # interlock var_k1 and var_K2:
379386 # eq: var_k0(t)+var_k1(t) <= 1.1
380- self .add (self ._model .add_constraints (
381- var_k0 + var_k1 <= 1.1 ,
382- name = f'{ self .label_full } |lock_k0_and_k1|{ variable .name } ' ),
383- f'lock_k0_and_k1|{ variable .name } '
387+ self .add (
388+ self ._model .add_constraints (
389+ var_k0 + var_k1 <= 1.1 , name = f'{ self .label_full } |lock_k0_and_k1|{ variable .name } '
390+ ),
391+ f'lock_k0_and_k1|{ variable .name } ' ,
384392 )
385393
386394 # Begrenzung der Korrektur-Anzahl:
387395 # eq: sum(K) <= n_Corr_max
388- self .add (self ._model .add_constraints (
389- sum (var_k0 ) + sum (var_k1 ) <= round (self .aggregation_parameters .percentage_of_period_freedom / 100 * length ),
390- name = f'{ self .label_full } |limit_corrections|{ variable .name } ' ),
391- f'limit_corrections|{ variable .name } '
396+ self .add (
397+ self ._model .add_constraints (
398+ sum (var_k0 ) + sum (var_k1 )
399+ <= round (self .aggregation_parameters .percentage_of_period_freedom / 100 * length ),
400+ name = f'{ self .label_full } |limit_corrections|{ variable .name } ' ,
401+ ),
402+ f'limit_corrections|{ variable .name } ' ,
392403 )
0 commit comments