mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 17:38:51 -06:00
DropRedundant: Make x_y parallel
This commit is contained in:
@@ -48,7 +48,7 @@ class ConvertTightIneqsIntoEqsStep(Component):
|
|||||||
|
|
||||||
def before_solve_mip(self, solver, instance, _):
|
def before_solve_mip(self, solver, instance, _):
|
||||||
logger.info("Predicting tight LP constraints...")
|
logger.info("Predicting tight LP constraints...")
|
||||||
x, constraints = DropRedundantInequalitiesStep._x_test(
|
x, constraints = DropRedundantInequalitiesStep.x(
|
||||||
instance,
|
instance,
|
||||||
constraint_ids=solver.internal_solver.get_constraint_ids(),
|
constraint_ids=solver.internal_solver.get_constraint_ids(),
|
||||||
)
|
)
|
||||||
@@ -99,8 +99,29 @@ class ConvertTightIneqsIntoEqsStep(Component):
|
|||||||
self.classifiers[category] = deepcopy(self.classifier_prototype)
|
self.classifiers[category] = deepcopy(self.classifier_prototype)
|
||||||
self.classifiers[category].fit(x[category], y[category])
|
self.classifiers[category].fit(x[category], y[category])
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _x_train(instances):
|
||||||
|
x = {}
|
||||||
|
for instance in tqdm(
|
||||||
|
InstanceIterator(instances),
|
||||||
|
desc="Extract (drop:x)",
|
||||||
|
disable=len(instances) < 5,
|
||||||
|
):
|
||||||
|
for training_data in instance.training_data:
|
||||||
|
cids = training_data["slacks"].keys()
|
||||||
|
for cid in cids:
|
||||||
|
category = instance.get_constraint_category(cid)
|
||||||
|
if category is None:
|
||||||
|
continue
|
||||||
|
if category not in x:
|
||||||
|
x[category] = []
|
||||||
|
x[category] += [instance.get_constraint_features(cid)]
|
||||||
|
for category in x.keys():
|
||||||
|
x[category] = np.array(x[category])
|
||||||
|
return x
|
||||||
|
|
||||||
def x(self, instances):
|
def x(self, instances):
|
||||||
return DropRedundantInequalitiesStep._x_train(instances)
|
return self._x_train(instances)
|
||||||
|
|
||||||
def y(self, instances):
|
def y(self, instances):
|
||||||
y = {}
|
y = {}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from copy import deepcopy
|
|||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
from p_tqdm import p_umap
|
||||||
|
|
||||||
from miplearn.classifiers.counting import CountingClassifier
|
from miplearn.classifiers.counting import CountingClassifier
|
||||||
from miplearn.components import classifier_evaluation_dict
|
from miplearn.components import classifier_evaluation_dict
|
||||||
@@ -54,7 +55,7 @@ class DropRedundantInequalitiesStep(Component):
|
|||||||
self.current_iteration = 0
|
self.current_iteration = 0
|
||||||
|
|
||||||
logger.info("Predicting redundant LP constraints...")
|
logger.info("Predicting redundant LP constraints...")
|
||||||
x, constraints = self._x_test(
|
x, constraints = self.x(
|
||||||
instance,
|
instance,
|
||||||
constraint_ids=solver.internal_solver.get_constraint_ids(),
|
constraint_ids=solver.internal_solver.get_constraint_ids(),
|
||||||
)
|
)
|
||||||
@@ -98,18 +99,15 @@ class DropRedundantInequalitiesStep(Component):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
def fit(self, training_instances):
|
def fit(self, training_instances, n_jobs=1):
|
||||||
logger.debug("Extracting x and y...")
|
x, y = self.x_y(training_instances, n_jobs=n_jobs)
|
||||||
x = self.x(training_instances)
|
|
||||||
y = self.y(training_instances)
|
|
||||||
logger.debug("Fitting...")
|
|
||||||
for category in tqdm(x.keys(), desc="Fit (drop)"):
|
for category in tqdm(x.keys(), desc="Fit (drop)"):
|
||||||
if category not in self.classifiers:
|
if category not in self.classifiers:
|
||||||
self.classifiers[category] = deepcopy(self.classifier_prototype)
|
self.classifiers[category] = deepcopy(self.classifier_prototype)
|
||||||
self.classifiers[category].fit(x[category], np.array(y[category]))
|
self.classifiers[category].fit(x[category], np.array(y[category]))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _x_test(instance, constraint_ids):
|
def x(instance, constraint_ids):
|
||||||
x = {}
|
x = {}
|
||||||
constraints = {}
|
constraints = {}
|
||||||
cids = constraint_ids
|
cids = constraint_ids
|
||||||
@@ -126,49 +124,58 @@ class DropRedundantInequalitiesStep(Component):
|
|||||||
x[category] = np.array(x[category])
|
x[category] = np.array(x[category])
|
||||||
return x, constraints
|
return x, constraints
|
||||||
|
|
||||||
@staticmethod
|
def x_y(self, instances, n_jobs=1):
|
||||||
def _x_train(instances):
|
def _extract(instance):
|
||||||
x = {}
|
x = {}
|
||||||
for instance in tqdm(
|
y = {}
|
||||||
InstanceIterator(instances),
|
for instance in InstanceIterator([instance]):
|
||||||
desc="Extract (drop:x)",
|
for training_data in instance.training_data:
|
||||||
disable=len(instances) < 5,
|
for (cid, slack) in training_data["slacks"].items():
|
||||||
):
|
category = instance.get_constraint_category(cid)
|
||||||
for training_data in instance.training_data:
|
if category is None:
|
||||||
cids = training_data["slacks"].keys()
|
continue
|
||||||
for cid in cids:
|
if category not in x:
|
||||||
category = instance.get_constraint_category(cid)
|
x[category] = []
|
||||||
if category is None:
|
if category not in y:
|
||||||
continue
|
y[category] = []
|
||||||
if category not in x:
|
if slack > self.slack_tolerance:
|
||||||
x[category] = []
|
y[category] += [[False, True]]
|
||||||
x[category] += [instance.get_constraint_features(cid)]
|
else:
|
||||||
for category in x.keys():
|
y[category] += [[True, False]]
|
||||||
x[category] = np.array(x[category])
|
x[category] += [instance.get_constraint_features(cid)]
|
||||||
return x
|
return x, y
|
||||||
|
|
||||||
def x(self, instances):
|
if n_jobs == 1:
|
||||||
return self._x_train(instances)
|
results = [
|
||||||
|
_extract(i)
|
||||||
|
for i in tqdm(
|
||||||
|
instances,
|
||||||
|
desc="Extract (drop 1/3)",
|
||||||
|
)
|
||||||
|
]
|
||||||
|
else:
|
||||||
|
results = p_umap(
|
||||||
|
_extract,
|
||||||
|
instances,
|
||||||
|
num_cpus=n_jobs,
|
||||||
|
desc="Extract (drop 1/3)",
|
||||||
|
)
|
||||||
|
|
||||||
def y(self, instances):
|
x_combined = {}
|
||||||
y = {}
|
y_combined = {}
|
||||||
for instance in tqdm(
|
for (x, y) in tqdm(results, desc="Extract (drop 2/3)"):
|
||||||
InstanceIterator(instances),
|
for category in x.keys():
|
||||||
desc="Extract (drop:y)",
|
if category not in x_combined:
|
||||||
disable=len(instances) < 5,
|
x_combined[category] = []
|
||||||
):
|
y_combined[category] = []
|
||||||
for training_data in instance.training_data:
|
x_combined[category] += x[category]
|
||||||
for (cid, slack) in training_data["slacks"].items():
|
y_combined[category] += y[category]
|
||||||
category = instance.get_constraint_category(cid)
|
|
||||||
if category is None:
|
for category in tqdm(x_combined.keys(), desc="Extract (drop 3/3)"):
|
||||||
continue
|
x_combined[category] = np.array(x_combined[category])
|
||||||
if category not in y:
|
y_combined[category] = np.array(y_combined[category])
|
||||||
y[category] = []
|
|
||||||
if slack > self.slack_tolerance:
|
return x_combined, y_combined
|
||||||
y[category] += [[False, True]]
|
|
||||||
else:
|
|
||||||
y[category] += [[True, False]]
|
|
||||||
return y
|
|
||||||
|
|
||||||
def predict(self, x):
|
def predict(self, x):
|
||||||
y = {}
|
y = {}
|
||||||
@@ -185,9 +192,8 @@ class DropRedundantInequalitiesStep(Component):
|
|||||||
y[category] += [[True, False]]
|
y[category] += [[True, False]]
|
||||||
return y
|
return y
|
||||||
|
|
||||||
def evaluate(self, instance):
|
def evaluate(self, instance, n_jobs=1):
|
||||||
x = self.x([instance])
|
x, y_true = self.x_y([instance], n_jobs=n_jobs)
|
||||||
y_true = self.y([instance])
|
|
||||||
y_pred = self.predict(x)
|
y_pred = self.predict(x)
|
||||||
tp, tn, fp, fn = 0, 0, 0, 0
|
tp, tn, fp, fn = 0, 0, 0, 0
|
||||||
for category in tqdm(
|
for category in tqdm(
|
||||||
|
|||||||
@@ -289,8 +289,7 @@ def test_x_y_fit_predict_evaluate():
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Should build X and Y matrices correctly
|
# Should build X and Y matrices correctly
|
||||||
actual_x = component.x(instances)
|
actual_x, actual_y = component.x_y(instances)
|
||||||
actual_y = component.y(instances)
|
|
||||||
for category in ["type-a", "type-b"]:
|
for category in ["type-a", "type-b"]:
|
||||||
np.testing.assert_array_equal(actual_x[category], expected_x[category])
|
np.testing.assert_array_equal(actual_x[category], expected_x[category])
|
||||||
np.testing.assert_array_equal(actual_y[category], expected_y[category])
|
np.testing.assert_array_equal(actual_y[category], expected_y[category])
|
||||||
@@ -392,9 +391,7 @@ def test_x_multiple_solves():
|
|||||||
|
|
||||||
# Should build X and Y matrices correctly
|
# Should build X and Y matrices correctly
|
||||||
component = DropRedundantInequalitiesStep()
|
component = DropRedundantInequalitiesStep()
|
||||||
actual_x = component.x([instance])
|
actual_x, actual_y = component.x_y([instance])
|
||||||
actual_y = component.y([instance])
|
|
||||||
print(actual_x)
|
|
||||||
for category in ["type-a", "type-b"]:
|
for category in ["type-a", "type-b"]:
|
||||||
np.testing.assert_array_equal(actual_x[category], expected_x[category])
|
np.testing.assert_array_equal(actual_x[category], expected_x[category])
|
||||||
np.testing.assert_array_equal(actual_y[category], expected_y[category])
|
np.testing.assert_array_equal(actual_y[category], expected_y[category])
|
||||||
|
|||||||
Reference in New Issue
Block a user