Skip to content

Commit

Permalink
Converted all print statements to logging statements or made the func…
Browse files Browse the repository at this point in the history
…tion return a str instead of printing
  • Loading branch information
FBumann committed Jul 26, 2024
1 parent 90fffd9 commit a437a69
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 84 deletions.
2 changes: 1 addition & 1 deletion examples/Ex03_full_seg_agg/Model_and_solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@

for aResult in listOfCalcs:
print(aResult.label + ':')
aResult.listOfModbox[0].printNoEqsAndVars()
aResult.listOfModbox[0].describe()
# print(aResult.infos)
# print(aResult.duration)
print('costs: ' + str(aResult.results_struct.global_comp.costs.all.sum))
Expand Down
10 changes: 5 additions & 5 deletions flixOpt/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def cluster(self) -> None:
self.results = self.aggregation.predictOriginalData()

self.time_for_clustering = timeit.default_timer() - start_time # Zeit messen:
print(self.describe_clusters())
logger.info(self.describe_clusters())

@property
def results_original_index(self):
Expand Down Expand Up @@ -444,11 +444,11 @@ def _checkPeak_TSraw(self, aTSrawlist):
raise Exception('addPeak_max/min must be list of TimeSeriesRaw-objects!')

def print(self):
print('used ' + str(len(self.time_series_list)) + ' TS for aggregation:')
logger.info('used ' + str(len(self.time_series_list)) + ' TS for aggregation:')
for TS in self.time_series_list:
aStr = ' ->' + TS.label_full + ' (weight: {:.4f}; agg_group: ' + str(self._get_agg_type(TS)) + ')'
print(aStr.format(self._getWeight(TS)))
logger.info(aStr.format(self._getWeight(TS)))
if len(self.agg_type_count.keys()) > 0:
print('agg_types: ' + str(list(self.agg_type_count.keys())))
logger.info('agg_types: ' + str(list(self.agg_type_count.keys())))
else:
print('Warning!: no agg_types defined, i.e. all TS have weigth 1 (or explicit given weight)!')
logger.warning('Warning!: no agg_types defined, i.e. all TS have weigth 1 (or explicit given weight)!')
60 changes: 29 additions & 31 deletions flixOpt/calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ def _save_solve_infos(self):
yaml.dump(self.infos, f, width=1000, # Verhinderung Zeilenumbruch für lange equations
allow_unicode=True, sort_keys=False)

aStr = f'# saved calculation {self.name} #'
print('#' * len(aStr))
print(aStr)
print('#' * len(aStr))
message = f'# Saved Calculation {self.name} #'
logger.info(len(message)*'#')
logger.info(message)
logger.info(len(message)*'#')


class FullCalculation(Calculation):
Expand Down Expand Up @@ -262,8 +262,8 @@ def do_modeling(self, periodLengthInHours, nr_of_typical_periods, use_extreme_pe
raise Exception('!!! Achtung Aggregation geht nicht, da unterschiedliche delta_t von ' + str(
min(dt_in_hours)) + ' bis ' + str(max(dt_in_hours)) + ' h')

print('#########################')
print('## TS for aggregation ###')
logger.info('#########################')
logger.info('## TS for aggregation ###')

## Daten für Aggregation vorbereiten:
# TSlist and TScollection ohne Skalare:
Expand Down Expand Up @@ -323,21 +323,21 @@ def do_modeling(self, periodLengthInHours, nr_of_typical_periods, use_extreme_pe

# ### Some infos as print ###

print('TS Aggregation:')
logger.info('TS Aggregation:')
for i in range(len(self.time_series_for_aggregation)):
aLabel = self.time_series_for_aggregation[i].label_full
print('TS ' + str(aLabel))
print(' max_agg:' + str(max(dataAgg.results[aLabel])))
print(' max_orig:' + str(max(df_OriginalData[aLabel])))
print(' min_agg:' + str(min(dataAgg.results[aLabel])))
print(' min_orig:' + str(min(df_OriginalData[aLabel])))
print(' sum_agg:' + str(sum(dataAgg.results[aLabel])))
print(' sum_orig:' + str(sum(df_OriginalData[aLabel])))

print('addpeakmax:')
print(self.time_series_collection.addPeak_Max_labels)
print('addpeakmin:')
print(self.time_series_collection.addPeak_Min_labels)
logger.info('TS ' + str(aLabel))
logger.info(' max_agg:' + str(max(dataAgg.results[aLabel])))
logger.info(' max_orig:' + str(max(df_OriginalData[aLabel])))
logger.info(' min_agg:' + str(min(dataAgg.results[aLabel])))
logger.info(' min_orig:' + str(min(df_OriginalData[aLabel])))
logger.info(' sum_agg:' + str(sum(dataAgg.results[aLabel])))
logger.info(' sum_orig:' + str(sum(df_OriginalData[aLabel])))

logger.info('addpeakmax:')
logger.info(self.time_series_collection.addPeak_Max_labels)
logger.info('addpeakmin:')
logger.info(self.time_series_collection.addPeak_Min_labels)

# ################
# ### Modeling ###
Expand Down Expand Up @@ -434,8 +434,8 @@ def solve(self, solverProps, segment_length: int, nr_of_used_steps: int, path='r
"""
self.check_if_already_modeled()
self._infos['segmented_properties'] = {'segment_length': segment_length, 'nr_of_used_steps': nr_of_used_steps}
print('##############################################################')
print('#################### segmented Solving #######################')
logger.info('##############################################################')
logger.info('#################### segmented Solving #######################')

t_start = timeit.default_timer()
self.flow_system.finalize() # flow_system finalisieren:
Expand All @@ -447,10 +447,10 @@ def solve(self, solverProps, segment_length: int, nr_of_used_steps: int, path='r
# Anzahl = Letzte Simulation bis zum Ende plus die davor mit Überlappung:
nr_of_segments = math.ceil((len(self.time_series)) / nr_of_used_steps)
self._infos['segmented_properties']['nr_of_segments'] = nr_of_segments
print(f'Indices : {self.time_indices[0]}...{self.time_indices[-1]}')
print(f'Segment Length: {segment_length}')
print(f'Used Steps : {nr_of_used_steps}')
print(f'Number of Segments: {nr_of_segments}\n')
logger.info(f'Indices : {self.time_indices[0]}...{self.time_indices[-1]}')
logger.info(f'Segment Length: {segment_length}')
logger.info(f'Used Steps : {nr_of_used_steps}')
logger.info(f'Number of Segments: {nr_of_segments}\n')

self._define_path_names(path, save_results=True, nr_of_system_models=nr_of_segments)

Expand All @@ -466,7 +466,7 @@ def solve(self, solverProps, segment_length: int, nr_of_used_steps: int, path='r
if i == max(range(nr_of_segments)): # if last Segment:
nr_of_used_steps = end_index_of_segment - start_index_of_segment + 1

print(f'{i}. Segment (flow_system indices {start_index_global}...{end_index_global}):')
logger.info(f'{i}. Segment (flow_system indices {start_index_global}...{end_index_global}):')


# Modellierungsbox / TimePeriod-Box bauen:
Expand All @@ -476,9 +476,9 @@ def solve(self, solverProps, segment_length: int, nr_of_used_steps: int, path='r
# Startwerte übergeben von Vorgänger-system_model:
if i > 0:
system_model_of_segment.before_values = self.get_before_values_for_next_segment(nr_of_used_steps - 1)
print('### before_values: ###')
pp(system_model_of_segment.before_values)
print('#######################') # transferStartValues(segment, segmentBefore)
logger.info('### before_values: ###')
logger.info(f'{system_model_of_segment.before_values}')
logger.info('#######################') # transferStartValues(segment, segmentBefore)

# model in Energiesystem aktivieren:
self.flow_system.activate_model(system_model_of_segment)
Expand Down Expand Up @@ -517,8 +517,6 @@ def append_new_results_to_dict_values(result: Dict, result_to_append: Dict, resu
firstFill = False

for key, val in result_to_append.items():
# print(key)

# Wenn val ein Wert ist:
if isinstance(val, np.ndarray) or isinstance(val, np.float64) or np.isscalar(val):

Expand Down
4 changes: 2 additions & 2 deletions flixOpt/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def declare_vars_and_eqs(self, system_model) -> None:
self.all.declare_vars_and_eqs(system_model)

def do_modeling(self, system_model, time_indices: Union[List[int], range]) -> None:
print('modeling ' + self.label)
logger.debug('modeling ' + self.label)
super().declare_vars_and_eqs(system_model)
self.operation.do_modeling(system_model, time_indices)
self.invest.do_modeling(system_model, time_indices)
Expand Down Expand Up @@ -970,7 +970,7 @@ def finalize(self) -> None:
super().finalize()

def declare_vars_and_eqs(self, system_model: SystemModel) -> None:
print('declare_vars_and_eqs ' + self.label)
logger.debug('declare_vars_and_eqs ' + self.label)
super().declare_vars_and_eqs(system_model)

self.featureOn.declare_vars_and_eqs(system_model) # TODO: rekursiv aufrufen für sub_elements
Expand Down
7 changes: 3 additions & 4 deletions flixOpt/flow_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ def __str__(self):

def add_effects(self, *args: Effect) -> None:
for new_effect in list(args):
print('Register new effect ' + new_effect.label)
logger.info('Register new effect ' + new_effect.label)
self.effect_collection.add_effect(new_effect)

def add_components(self, *args: Component) -> None:
# Komponenten registrieren:
new_components = list(args)
for new_component in new_components:
print('Register new Component ' + new_component.label)
logger.info('Register new Component ' + new_component.label)
self._check_if_element_is_unique(new_component) # check if already exists:
new_component.register_component_in_flows() # Komponente in Flow registrieren
new_component.register_flows_in_bus() # Flows in Bus registrieren:
Expand Down Expand Up @@ -215,13 +215,12 @@ def error_str(effect_label: str, shareEffect_label: str):

# Finalisieren aller ModelingElemente (dabei werden teilweise auch noch sub_elements erzeugt!)
def finalize(self) -> None:
print('finalize all Elements...')
logger.debug('finalize all Elements...')
self._plausibility_checks()
# nur EINMAL ausführen: Finalisieren der Elements:
if not self._finalized:
# finalize Elements for modeling:
for element in self.all_first_level_elements_with_flows:
print(element.label) #TODO: Remove this print??
element.finalize() # inklusive sub_elements!
self._finalized = True

Expand Down
4 changes: 2 additions & 2 deletions flixOpt/math_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ def parse_infos(self):
self.presolved_continuous = self.presolved_cols - self.presolved_integer

elif self.solver_name == 'glpk':
print('######################################################')
print('### No solver-log parsing implemented for glpk yet! ###')
logger.warning('######################################################')
logger.warning('### No solver-log parsing implemented for glpk yet! ###')
else:
raise Exception('SolverLog.parse_infos() is not defined for solver ' + self.solver_name)
1 change: 0 additions & 1 deletion flixOpt/postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ def postObjOfStr(self, aStr):
def isGreaterMinFlowHours(aFlowValues, dt_in_hours, minFlowHours):
# absolute Summe, damit auch negative Werte gezählt werden:
absFlowHours = sum(abs(aFlowValues * dt_in_hours))
# print(absFlowHours)
return absFlowHours > minFlowHours

def getLoadFactorOfComp(self, aComp):
Expand Down
63 changes: 30 additions & 33 deletions flixOpt/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,11 @@ def solve(self,
raise Exception(
'no allowed arguments for kwargs: ' + str(key) + '(all arguments:' + str(kwargs) + ')')

print('')
print('##############################################################')
print('##################### solving ################################')
print('')

self.printNoEqsAndVars()
logger.info('')
logger.info('##############################################################')
logger.info('##################### solving ################################')
logger.info('')
logger.info(self.describe())

super().solve(mip_gap, time_limit_seconds, solver_name, solver_output_to_console, logfile_name, **kwargs)

Expand All @@ -118,48 +117,48 @@ def solve(self,
termination_message = self.solver_results['Solver'][0]['Status']
else:
termination_message = f'not implemented for solver "{solver_name}" yet'
print(f'Termination message: "{termination_message}"')
logger.info(f'Termination message: "{termination_message}"')

print('')
logger.info('')
# Variablen-Ergebnisse abspeichern:
# 1. dict:
(self.results, self.results_var) = self.flow_system.get_results_after_solve()
# 2. struct:
self.results_struct = utils.createStructFromDictInDict(self.results)

print('##############################################################')
print('################### finished #################################')
print('')
logger.info('##############################################################')
logger.info('################### finished #################################')
logger.info('')
for aEffect in self.flow_system.effect_collection.effects:
print(aEffect.label + ' in ' + aEffect.unit + ':')
print(' operation: ' + str(aEffect.operation.model.variables['sum'].result))
print(' invest : ' + str(aEffect.invest.model.variables['sum'].result))
print(' sum : ' + str(aEffect.all.model.variables['sum'].result))

print('SUM : ' + '...todo...')
print('penaltyCosts : ' + str(self.flow_system.effect_collection.penalty.model.variables['sum'].result))
print('––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––')
print('Result of Obj : ' + str(self.objective_result))
logger.info(aEffect.label + ' in ' + aEffect.unit + ':')
logger.info(' operation: ' + str(aEffect.operation.model.variables['sum'].result))
logger.info(' invest : ' + str(aEffect.invest.model.variables['sum'].result))
logger.info(' sum : ' + str(aEffect.all.model.variables['sum'].result))

logger.info('SUM : ' + '...todo...')
logger.info('penaltyCosts : ' + str(self.flow_system.effect_collection.penalty.model.variables['sum'].result))
logger.info('––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––')
logger.info('Result of Obj : ' + str(self.objective_result))
try:
print('lower bound : ' + str(self.solver_results['Problem'][0]['Lower bound']))
logger.info('lower bound : ' + str(self.solver_results['Problem'][0]['Lower bound']))
except:
print
print('')
logger.info
logger.info('')
for aBus in self.flow_system.all_buses:
if aBus.with_excess:
if any(self.results[aBus.label]['excess_input'] > 1e-6) or any(
self.results[aBus.label]['excess_output'] > 1e-6):
# if any(aBus.excess_input.result > 0) or any(aBus.excess_output.result > 0):
print('!!!!! Attention !!!!!')
print('!!!!! Exzess.Value in Bus ' + aBus.label + '!!!!!')
logger.warning('!!!!! Attention !!!!!')
logger.warning('!!!!! Exzess.Value in Bus ' + aBus.label + '!!!!!')

# if penalties exist
if self.flow_system.effect_collection.penalty.model.variables['sum'].result > 10:
print('Take care: -> high penalty makes the used mip_gap quite high')
print(' -> real costs are not optimized to mip_gap')
logger.warning('Take care: -> high penalty makes the used mip_gap quite high')
logger.warning(' -> real costs are not optimized to mip_gap')

print('')
print('##############################################################')
logger.info('')
logger.info('##############################################################')

# str description of results:
# nested fct:
Expand Down Expand Up @@ -207,7 +206,7 @@ def _getMainResultsAsStr():
return main_results_str

self.main_results_str = _getMainResultsAsStr()
utils.printDictAndList(self.main_results_str)
logger.info(utils.printDictAndList(self.main_results_str))

@property
def all_variables(self) -> List[Variable]:
Expand Down Expand Up @@ -379,7 +378,7 @@ def finalize(self) -> None:

# 2.
def create_new_model_and_activate_system_model(self, system_model: SystemModel) -> None:
# print('new model for ' + self.label)
logger.debug('new model for ' + self.label)
# subElemente ebenso:
element: Element
for element in self.sub_elements:
Expand Down Expand Up @@ -414,7 +413,6 @@ def get_results(self) -> Tuple[Dict, Dict]:
# 2. Variablenwerte ablegen:
aVar: Variable
for aVar in self.model.variables.values():
# print(aVar.label)
aData[aVar.label] = aVar.result
aVars[aVar.label] = aVar # link zur Variable
if aVar.is_binary and aVar.length > 1:
Expand All @@ -426,7 +424,6 @@ def get_results(self) -> Tuple[Dict, Dict]:
# 3. Alle TS übergeben
aTS: TimeSeries
for aTS in self.TS_list:
# print(aVar.label)
aData[aTS.label] = aTS.data
aVars[aTS.label] = aTS # link zur Variable

Expand Down
10 changes: 5 additions & 5 deletions flixOpt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def check_name_for_conformity(label: str):
ord('-'): '_'}
new_label = label.translate(char_map)
if new_label != label:
print(f'{label=} doesnt allign with name restrictions and is changed to {new_label=}')
logger.warning(f'{label=} doesnt allign with name restrictions and is changed to {new_label=}')

# check, ob jetzt valid variable name: (für Verwendung in results_struct notwendig)
import re
Expand Down Expand Up @@ -187,18 +187,18 @@ def check_time_series(label: str,

# unterschiedliche dt:
if np.max(dt_in_hours) - np.min(dt_in_hours) != 0:
print(label + ': !! Achtung !! unterschiedliche delta_t von ' + str(min(dt)) + 'h bis ' + str(max(dt)) + ' h')
logger.warning(f'{label}: !! Achtung !! unterschiedliche delta_t von {min(dt)} h bis {max(dt)} h')
# negative dt:
if np.min(dt_in_hours) < 0:
raise Exception(label + ': Zeitreihe besitzt Zurücksprünge - vermutlich Zeitumstellung nicht beseitigt!')


def printDictAndList(aDictOrList):
def printDictAndList(aDictOrList) -> str:
import yaml
print(yaml.dump(aDictOrList,
return yaml.dump(aDictOrList,
default_flow_style=False,
width=1000, # verhindern von zusätzlichen Zeilenumbrüchen
allow_unicode=True))
allow_unicode=True)

def max_args(*args):
# max from num-lists and skalars
Expand Down

0 comments on commit a437a69

Please sign in to comment.