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 = {}
|
self.classifiers = {}
|
||||||
violation_to_instance_idx = {}
|
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:
|
for v in instance.found_violated_lazy_constraints:
|
||||||
if isinstance(v, list):
|
if isinstance(v, list):
|
||||||
v = tuple(v)
|
v = tuple(v)
|
||||||
|
|||||||
@@ -69,9 +69,7 @@ class TestInstanceMin(Instance):
|
|||||||
|
|
||||||
|
|
||||||
def test_convert_tight_infeasibility():
|
def test_convert_tight_infeasibility():
|
||||||
comp = ConvertTightIneqsIntoEqsStep(
|
comp = ConvertTightIneqsIntoEqsStep()
|
||||||
check_converted=True,
|
|
||||||
)
|
|
||||||
comp.classifiers = {
|
comp.classifiers = {
|
||||||
"c1": Mock(spec=Classifier),
|
"c1": Mock(spec=Classifier),
|
||||||
"c2": Mock(spec=Classifier),
|
"c2": Mock(spec=Classifier),
|
||||||
@@ -94,9 +92,7 @@ def test_convert_tight_infeasibility():
|
|||||||
|
|
||||||
|
|
||||||
def test_convert_tight_suboptimality():
|
def test_convert_tight_suboptimality():
|
||||||
comp = ConvertTightIneqsIntoEqsStep(
|
comp = ConvertTightIneqsIntoEqsStep(check_optimality=True)
|
||||||
check_converted=True,
|
|
||||||
)
|
|
||||||
comp.classifiers = {
|
comp.classifiers = {
|
||||||
"c1": Mock(spec=Classifier),
|
"c1": Mock(spec=Classifier),
|
||||||
"c2": Mock(spec=Classifier),
|
"c2": Mock(spec=Classifier),
|
||||||
@@ -119,9 +115,7 @@ def test_convert_tight_suboptimality():
|
|||||||
|
|
||||||
|
|
||||||
def test_convert_tight_optimal():
|
def test_convert_tight_optimal():
|
||||||
comp = ConvertTightIneqsIntoEqsStep(
|
comp = ConvertTightIneqsIntoEqsStep()
|
||||||
check_converted=True,
|
|
||||||
)
|
|
||||||
comp.classifiers = {
|
comp.classifiers = {
|
||||||
"c1": Mock(spec=Classifier),
|
"c1": Mock(spec=Classifier),
|
||||||
"c2": Mock(spec=Classifier),
|
"c2": Mock(spec=Classifier),
|
||||||
|
|||||||
@@ -134,7 +134,8 @@ def test_drop_redundant_with_check_dropped():
|
|||||||
solver, internal, instance, classifiers = _setup()
|
solver, internal, instance, classifiers = _setup()
|
||||||
|
|
||||||
component = DropRedundantInequalitiesStep(
|
component = DropRedundantInequalitiesStep(
|
||||||
check_dropped=True, violation_tolerance=1e-3
|
check_feasibility=True,
|
||||||
|
violation_tolerance=1e-3,
|
||||||
)
|
)
|
||||||
component.classifiers = classifiers
|
component.classifiers = classifiers
|
||||||
|
|
||||||
|
|||||||
@@ -29,12 +29,15 @@ class InstanceIterator:
|
|||||||
self.current += 1
|
self.current += 1
|
||||||
if isinstance(result, str):
|
if isinstance(result, str):
|
||||||
logger.debug("Read: %s" % result)
|
logger.debug("Read: %s" % result)
|
||||||
if result.endswith(".gz"):
|
try:
|
||||||
with gzip.GzipFile(result, "rb") as file:
|
if result.endswith(".gz"):
|
||||||
result = pickle.load(file)
|
with gzip.GzipFile(result, "rb") as file:
|
||||||
else:
|
result = pickle.load(file)
|
||||||
with open(result, "rb") as file:
|
else:
|
||||||
result = pickle.load(file)
|
with open(result, "rb") as file:
|
||||||
|
result = pickle.load(file)
|
||||||
|
except pickle.UnpicklingError:
|
||||||
|
raise Exception(f"Invalid instance file: {result}")
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import gzip
|
|||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
from p_tqdm import p_map
|
from p_tqdm import p_map
|
||||||
|
from tempfile import NamedTemporaryFile
|
||||||
|
|
||||||
from . import RedirectOutput
|
from . import RedirectOutput
|
||||||
from .. import (
|
from .. import (
|
||||||
@@ -211,13 +212,16 @@ class LearningSolver:
|
|||||||
details.
|
details.
|
||||||
"""
|
"""
|
||||||
if self.simulate_perfect:
|
if self.simulate_perfect:
|
||||||
self._solve(
|
if not isinstance(instance, str):
|
||||||
instance=instance,
|
raise Exception("Not implemented")
|
||||||
model=model,
|
with tempfile.NamedTemporaryFile(suffix=os.path.basename(instance)) as tmp:
|
||||||
output=output,
|
self._solve(
|
||||||
tee=tee,
|
instance=instance,
|
||||||
)
|
model=model,
|
||||||
self.fit([instance])
|
output=tmp.name,
|
||||||
|
tee=tee,
|
||||||
|
)
|
||||||
|
self.fit([tmp.name])
|
||||||
return self._solve(
|
return self._solve(
|
||||||
instance=instance,
|
instance=instance,
|
||||||
model=model,
|
model=model,
|
||||||
|
|||||||
@@ -7,8 +7,11 @@ import pickle
|
|||||||
import tempfile
|
import tempfile
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from miplearn import DynamicLazyConstraintsComponent
|
from miplearn import (
|
||||||
from miplearn import LearningSolver
|
LearningSolver,
|
||||||
|
GurobiSolver,
|
||||||
|
DynamicLazyConstraintsComponent,
|
||||||
|
)
|
||||||
|
|
||||||
from . import _get_instance, _get_internal_solvers
|
from . import _get_instance, _get_internal_solvers
|
||||||
|
|
||||||
@@ -109,3 +112,18 @@ def test_solve_fit_from_disk():
|
|||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
for filename in output:
|
for filename in output:
|
||||||
os.remove(filename)
|
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