mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 09:28:51 -06:00
simulate_perfect: Do not overwrite original file
This commit is contained in:
@@ -61,7 +61,7 @@ class DynamicLazyConstraintsComponent(Component):
|
||||
|
||||
self.classifiers = {}
|
||||
violation_to_instance_idx = {}
|
||||
for (idx, instance) in enumerate(training_instances):
|
||||
for (idx, instance) in enumerate(InstanceIterator(training_instances)):
|
||||
for v in instance.found_violated_lazy_constraints:
|
||||
if isinstance(v, list):
|
||||
v = tuple(v)
|
||||
|
||||
@@ -69,9 +69,7 @@ class TestInstanceMin(Instance):
|
||||
|
||||
|
||||
def test_convert_tight_infeasibility():
|
||||
comp = ConvertTightIneqsIntoEqsStep(
|
||||
check_converted=True,
|
||||
)
|
||||
comp = ConvertTightIneqsIntoEqsStep()
|
||||
comp.classifiers = {
|
||||
"c1": Mock(spec=Classifier),
|
||||
"c2": Mock(spec=Classifier),
|
||||
@@ -94,9 +92,7 @@ def test_convert_tight_infeasibility():
|
||||
|
||||
|
||||
def test_convert_tight_suboptimality():
|
||||
comp = ConvertTightIneqsIntoEqsStep(
|
||||
check_converted=True,
|
||||
)
|
||||
comp = ConvertTightIneqsIntoEqsStep(check_optimality=True)
|
||||
comp.classifiers = {
|
||||
"c1": Mock(spec=Classifier),
|
||||
"c2": Mock(spec=Classifier),
|
||||
@@ -119,9 +115,7 @@ def test_convert_tight_suboptimality():
|
||||
|
||||
|
||||
def test_convert_tight_optimal():
|
||||
comp = ConvertTightIneqsIntoEqsStep(
|
||||
check_converted=True,
|
||||
)
|
||||
comp = ConvertTightIneqsIntoEqsStep()
|
||||
comp.classifiers = {
|
||||
"c1": Mock(spec=Classifier),
|
||||
"c2": Mock(spec=Classifier),
|
||||
|
||||
@@ -134,7 +134,8 @@ def test_drop_redundant_with_check_dropped():
|
||||
solver, internal, instance, classifiers = _setup()
|
||||
|
||||
component = DropRedundantInequalitiesStep(
|
||||
check_dropped=True, violation_tolerance=1e-3
|
||||
check_feasibility=True,
|
||||
violation_tolerance=1e-3,
|
||||
)
|
||||
component.classifiers = classifiers
|
||||
|
||||
|
||||
@@ -29,12 +29,15 @@ class InstanceIterator:
|
||||
self.current += 1
|
||||
if isinstance(result, str):
|
||||
logger.debug("Read: %s" % result)
|
||||
try:
|
||||
if result.endswith(".gz"):
|
||||
with gzip.GzipFile(result, "rb") as file:
|
||||
result = pickle.load(file)
|
||||
else:
|
||||
with open(result, "rb") as file:
|
||||
result = pickle.load(file)
|
||||
except pickle.UnpicklingError:
|
||||
raise Exception(f"Invalid instance file: {result}")
|
||||
return result
|
||||
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import gzip
|
||||
from copy import deepcopy
|
||||
from typing import Optional, List
|
||||
from p_tqdm import p_map
|
||||
from tempfile import NamedTemporaryFile
|
||||
|
||||
from . import RedirectOutput
|
||||
from .. import (
|
||||
@@ -211,13 +212,16 @@ class LearningSolver:
|
||||
details.
|
||||
"""
|
||||
if self.simulate_perfect:
|
||||
if not isinstance(instance, str):
|
||||
raise Exception("Not implemented")
|
||||
with tempfile.NamedTemporaryFile(suffix=os.path.basename(instance)) as tmp:
|
||||
self._solve(
|
||||
instance=instance,
|
||||
model=model,
|
||||
output=output,
|
||||
output=tmp.name,
|
||||
tee=tee,
|
||||
)
|
||||
self.fit([instance])
|
||||
self.fit([tmp.name])
|
||||
return self._solve(
|
||||
instance=instance,
|
||||
model=model,
|
||||
|
||||
@@ -7,8 +7,11 @@ import pickle
|
||||
import tempfile
|
||||
import os
|
||||
|
||||
from miplearn import DynamicLazyConstraintsComponent
|
||||
from miplearn import LearningSolver
|
||||
from miplearn import (
|
||||
LearningSolver,
|
||||
GurobiSolver,
|
||||
DynamicLazyConstraintsComponent,
|
||||
)
|
||||
|
||||
from . import _get_instance, _get_internal_solvers
|
||||
|
||||
@@ -109,3 +112,18 @@ def test_solve_fit_from_disk():
|
||||
os.remove(filename)
|
||||
for filename in output:
|
||||
os.remove(filename)
|
||||
|
||||
|
||||
def test_simulate_perfect():
|
||||
internal_solver = GurobiSolver()
|
||||
instance = _get_instance(internal_solver)
|
||||
with tempfile.NamedTemporaryFile(suffix=".pkl", delete=False) as tmp:
|
||||
pickle.dump(instance, tmp)
|
||||
tmp.flush()
|
||||
solver = LearningSolver(
|
||||
solver=internal_solver,
|
||||
simulate_perfect=True,
|
||||
)
|
||||
|
||||
stats = solver.solve(tmp.name)
|
||||
assert stats["Lower bound"] == stats["Predicted LB"]
|
||||
|
||||
Reference in New Issue
Block a user