Skip to content

Commit

Permalink
changed solver_to_df, breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ddceruti committed Jan 30, 2025
1 parent c02f443 commit 10aea45
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 29 deletions.
2 changes: 1 addition & 1 deletion examples/one-producer/run_mts.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def main(filepath, outputpath, plots=True, solver='gurobi', mode='forced'):
dfres.to_csv(os.path.join(outputpath, 'results.csv'), sep=';')

# save solver results
dfsol = tt.utils.solver_to_df(result, model, solver=solver)
dfsol = tt.utils.solver_to_df(result, model)
dfsol.to_csv(os.path.join(outputpath, 'solver.csv'), sep=';')

opt_mats = tt.postprocessing.mts(model=model,
Expand Down
2 changes: 1 addition & 1 deletion examples/one-producer/run_sts.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def main(filepath, outputpath, plots=True, solver='gurobi', mode='economic'):
dfres.to_csv(os.path.join(outputpath, 'results.csv'), sep=';')

# save solver results
dfsol = tt.utils.solver_to_df(result, model, solver=solver)
dfsol = tt.utils.solver_to_df(result, model)
dfsol.to_csv(os.path.join(outputpath, 'solver.csv'), sep=';')

opt_mats = tt.postprocessing.sts(
Expand Down
2 changes: 1 addition & 1 deletion examples/synthetic-network/run_sts.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def main(filepath, outputpath, plots=True, solver='gurobi', mode='forced'):
dfres.to_csv(os.path.join(outputpath, 'results.csv'), sep=';')

# save solver results
dfsol = tt.utils.solver_to_df(result, model, solver=solver)
dfsol = tt.utils.solver_to_df(result, model)
dfsol.to_csv(os.path.join(outputpath, 'solver.csv'), sep=';')

opt_mats = tt.postprocessing.sts(
Expand Down
2 changes: 1 addition & 1 deletion examples/two-producers/run_sts_two_producers.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def main(filepath, outputpath, plots=True, solver='gurobi', mode='forced'):
dfres.to_csv(os.path.join(outputpath, 'results.csv'), sep=';')

# save solver results
dfsol = tt.utils.solver_to_df(result, model, solver=solver)
dfsol = tt.utils.solver_to_df(result, model)
dfsol.to_csv(os.path.join(outputpath, 'solver.csv'), sep=';')

# Postprocessing of the optimization results
Expand Down
35 changes: 10 additions & 25 deletions topotherm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def create_dir(path: str) -> None:
return


def solver_to_df(result, model, solver):
def solver_to_df(result, model):
"""Returns solver results in a dataframe. This needs to be adapted to the
solver output (gurobi vs cplex have different naming conventions)"""

Expand All @@ -35,33 +35,18 @@ def solver_to_df(result, model, solver):

dfslvr = pd.DataFrame()
slvr_res = result['Solver'][0]
if solver == "cplex":
try:
dfslvr.loc['Termination condition', 0] = slvr_res['Termination condition']
dfslvr.loc['Termination condition', 'unit'] = '-'
dfslvr.loc['Time', 0] = slvr_res['User time']
dfslvr.loc['Time', 'unit'] = 's'
dfslvr.loc['Objective', 0] = result['Solution'][0]['Objective']['obj']['Value']
dfslvr.loc['Objective', 'unit'] = '-'
elif solver == "gurobi":
dfslvr.loc['Termination condition', 0] = slvr_res['Termination condition']
dfslvr.loc['Termination condition', 'unit'] = '-'
dfslvr.loc['Wall time', 0] = slvr_res['Wall time']
dfslvr.loc['Wall time', 'unit'] = 's'
dfslvr.loc['Time', 0] = slvr_res['Wall time']
dfslvr.loc['Time', 'unit'] = 's'
dfslvr.loc['Objective', 0] = pyo.value(model.obj)
dfslvr.loc['Objective', 'unit'] = '-'
elif solver == "scip":
dfslvr.loc['Termination condition', 0] = slvr_res['Termination condition']
dfslvr.loc['Termination condition', 'unit'] = '-'
dfslvr.loc['Termination messag', 0] = slvr_res['Termination condition']
dfslvr.loc['Termination messag', 'unit'] = '-'
dfslvr.loc['Time', 0] = slvr_res['Wallclock time']
dfslvr.loc['Time', 'unit'] = 's'
dfslvr.loc['User Time', 0] = slvr_res['User time']
dfslvr.loc['User Time', 'unit'] = 's'
dfslvr.loc['Wall Time', 0] = slvr_res['Wall time']
dfslvr.loc['Wall Time', 'unit'] = 's'
dfslvr.loc['Objective', 0] = pyo.value(model.obj)
dfslvr.loc['Objective', 'unit'] = '-'
else:
raise NotImplementedError(f"Solver {solver} not implemented")
dfslvr.loc['Objective', 'unit'] = 'eur/y'
except KeyError:
print('Solver output not as expected. Check the solver output.')
return slvr_res
return dfslvr


Expand Down

0 comments on commit 10aea45

Please # to comment.