Skip to content

Commit 8dafb4a

Browse files
committed
Fix and add tests for rounding_algo.py
1 parent fb44c5e commit 8dafb4a

File tree

2 files changed

+469
-9
lines changed

2 files changed

+469
-9
lines changed

src/pownet/optim_model/rounding_algo.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
""" rounding_algo.py: Functions to perform iterative rounding.
2-
"""
1+
"""rounding_algo.py: Functions to perform iterative rounding."""
32

43
import gurobipy as gp
54
import numpy as np
6-
import pandas as pd
5+
6+
import logging
7+
8+
logger = logging.getLogger(__name__)
79

810

911
def get_variables(model: gp.Model, target_varnames: list[str] = None) -> dict:
@@ -25,8 +27,8 @@ def get_variables(model: gp.Model, target_varnames: list[str] = None) -> dict:
2527
filtered_vars = {}
2628
for v in model.getVars():
2729
# Extract the prefix of the variable name (e.g., 'status' from 'status[1,2]')
28-
if v.varname.split("[")[0] in target_varnames:
29-
filtered_vars[v.varname] = v
30+
if v.varName.split("[")[0] in target_varnames:
31+
filtered_vars[v.varName] = v
3032
return filtered_vars
3133

3234

@@ -91,7 +93,7 @@ def check_binary_values(var_dict: dict) -> bool:
9193
for var_name, var in var_dict.items():
9294
var_value = var.X
9395
if not (var_value == 0 or var_value == 1):
94-
print(f"Variable {var_name} has non-binary value: {var_value}")
96+
logger.info(f"Variable {var_name} has non-binary value: {var_value}")
9597
return False
9698
return True
9799

@@ -145,8 +147,9 @@ def optimize_with_rounding(
145147

146148
# Fixing variables can cause infeasibility
147149
if rounding_model.status == 3:
148-
print("\nPowNet: Rounding is infeasible. Use the MIP method.")
149-
break
150+
logger.warning("\nPowNet: Rounding is infeasible. Use the MIP method.")
151+
model.optimize()
152+
return model, None, None
150153
# The model should be feasible, but raise an error if not.
151154
elif rounding_model.status != 2:
152155
raise ValueError(f"Unrecognized model status: {rounding_model.status}")
@@ -167,7 +170,7 @@ def optimize_with_rounding(
167170
rounding_model.update()
168171

169172
# If no integer solution is found after max_rounding_iter
170-
print(
173+
logger.warning(
171174
"\nPowNet: The rounding heuristic has terminated before finding an integer solution."
172175
)
173176
model.optimize()

0 commit comments

Comments
 (0)