BasicCollector: Do not crash on exception

dev
Alinson S. Xavier 2 years ago
parent 8a02e22a35
commit 0534d50af3
Signed by: isoron
GPG Key ID: 0DA8E4B9E1109DCA

@ -9,6 +9,7 @@ import sys
from io import StringIO from io import StringIO
from os.path import exists from os.path import exists
from typing import Callable, List, Any from typing import Callable, List, Any
import traceback
from ..h5 import H5File from ..h5 import H5File
from ..io import _RedirectOutput, gzip, _to_h5_filename from ..io import _RedirectOutput, gzip, _to_h5_filename
@ -29,52 +30,57 @@ class BasicCollector:
verbose: bool = False, verbose: bool = False,
) -> None: ) -> None:
def _collect(data_filename: str) -> None: def _collect(data_filename: str) -> None:
h5_filename = _to_h5_filename(data_filename) try:
mps_filename = h5_filename.replace(".h5", ".mps") h5_filename = _to_h5_filename(data_filename)
mps_filename = h5_filename.replace(".h5", ".mps")
if exists(h5_filename):
# Try to read optimal solution if exists(h5_filename):
mip_var_values = None # Try to read optimal solution
try: mip_var_values = None
with H5File(h5_filename, "r") as h5: try:
mip_var_values = h5.get_array("mip_var_values") with H5File(h5_filename, "r") as h5:
except: mip_var_values = h5.get_array("mip_var_values")
pass except:
pass
if mip_var_values is None:
print(f"Removing empty/corrupted h5 file: {h5_filename}") if mip_var_values is None:
os.remove(h5_filename) print(f"Removing empty/corrupted h5 file: {h5_filename}")
else: os.remove(h5_filename)
return else:
return
with H5File(h5_filename, "w") as h5:
streams: List[Any] = [StringIO()] with H5File(h5_filename, "w") as h5:
if verbose: streams: List[Any] = [StringIO()]
streams += [sys.stdout] if verbose:
with _RedirectOutput(streams): streams += [sys.stdout]
# Load and extract static features with _RedirectOutput(streams):
model = build_model(data_filename) # Load and extract static features
model.extract_after_load(h5) model = build_model(data_filename)
model.extract_after_load(h5)
if not self.skip_lp:
# Solve LP relaxation if not self.skip_lp:
relaxed = model.relax() # Solve LP relaxation
relaxed.optimize() relaxed = model.relax()
relaxed.extract_after_lp(h5) relaxed.optimize()
relaxed.extract_after_lp(h5)
# Solve MIP
model.optimize() # Solve MIP
model.extract_after_mip(h5) model.optimize()
model.extract_after_mip(h5)
if self.write_mps:
# Add lazy constraints to model if self.write_mps:
model._lazy_enforce_collected() # Add lazy constraints to model
model._lazy_enforce_collected()
# Save MPS file
model.write(mps_filename) # Save MPS file
gzip(mps_filename) model.write(mps_filename)
gzip(mps_filename)
h5.put_scalar("mip_log", streams[0].getvalue())
h5.put_scalar("mip_log", streams[0].getvalue())
except:
print(f"Error processing: data_filename")
traceback.print_exc()
if n_jobs > 1: if n_jobs > 1:
p_umap( p_umap(

@ -87,7 +87,10 @@ def read_pkl_gz(filename: str) -> Any:
def _to_h5_filename(data_filename: str) -> str: def _to_h5_filename(data_filename: str) -> str:
output = f"{data_filename}.h5" output = f"{data_filename}.h5"
output = output.replace(".gz.h5", ".h5") output = output.replace(".gz.h5", ".h5")
output = output.replace(".csv.h5", ".h5")
output = output.replace(".jld2.h5", ".h5")
output = output.replace(".json.h5", ".h5") output = output.replace(".json.h5", ".h5")
output = output.replace(".lp.h5", ".h5")
output = output.replace(".mps.h5", ".h5")
output = output.replace(".pkl.h5", ".h5") output = output.replace(".pkl.h5", ".h5")
output = output.replace(".jld2.h5", ".h5")
return output return output

Loading…
Cancel
Save