mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 09:28:51 -06:00
ConvertTight: Use x function from DropRedundant
This commit is contained in:
@@ -7,11 +7,13 @@ from copy import deepcopy
|
|||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
import random
|
||||||
|
|
||||||
from miplearn import Component
|
from ... import Component
|
||||||
from miplearn.classifiers.counting import CountingClassifier
|
from ...classifiers.counting import CountingClassifier
|
||||||
from miplearn.components import classifier_evaluation_dict
|
from ...components import classifier_evaluation_dict
|
||||||
from miplearn.extractors import InstanceIterator
|
from ...extractors import InstanceIterator
|
||||||
|
from .drop_redundant import DropRedundantInequalitiesStep
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -46,11 +48,9 @@ class ConvertTightIneqsIntoEqsStep(Component):
|
|||||||
|
|
||||||
def before_solve(self, solver, instance, _):
|
def before_solve(self, solver, instance, _):
|
||||||
logger.info("Predicting tight LP constraints...")
|
logger.info("Predicting tight LP constraints...")
|
||||||
cids = solver.internal_solver.get_constraint_ids()
|
x, constraints = DropRedundantInequalitiesStep._x_test(
|
||||||
x, constraints = self.x(
|
instance,
|
||||||
[instance],
|
constraint_ids=solver.internal_solver.get_constraint_ids(),
|
||||||
constraint_ids=cids,
|
|
||||||
return_constraints=True,
|
|
||||||
)
|
)
|
||||||
y = self.predict(x)
|
y = self.predict(x)
|
||||||
|
|
||||||
@@ -68,7 +68,6 @@ class ConvertTightIneqsIntoEqsStep(Component):
|
|||||||
solver.internal_solver.set_constraint_sense(cid, "=")
|
solver.internal_solver.set_constraint_sense(cid, "=")
|
||||||
self.converted += [cid]
|
self.converted += [cid]
|
||||||
self.n_converted += 1
|
self.n_converted += 1
|
||||||
print(cid)
|
|
||||||
else:
|
else:
|
||||||
self.n_kept += 1
|
self.n_kept += 1
|
||||||
|
|
||||||
@@ -100,36 +99,8 @@ 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])
|
||||||
|
|
||||||
def x(
|
def x(self, instances):
|
||||||
self,
|
return DropRedundantInequalitiesStep._x_train(instances)
|
||||||
instances,
|
|
||||||
constraint_ids=None,
|
|
||||||
return_constraints=False,
|
|
||||||
):
|
|
||||||
x = {}
|
|
||||||
constraints = {}
|
|
||||||
for instance in tqdm(
|
|
||||||
InstanceIterator(instances),
|
|
||||||
desc="Extract (rlx:conv_ineqs:x)",
|
|
||||||
disable=len(instances) < 5,
|
|
||||||
):
|
|
||||||
if constraint_ids is not None:
|
|
||||||
cids = constraint_ids
|
|
||||||
else:
|
|
||||||
cids = instance.training_data[0]["slacks"].keys()
|
|
||||||
for cid in cids:
|
|
||||||
category = instance.get_constraint_category(cid)
|
|
||||||
if category is None:
|
|
||||||
continue
|
|
||||||
if category not in x:
|
|
||||||
x[category] = []
|
|
||||||
constraints[category] = []
|
|
||||||
x[category] += [instance.get_constraint_features(cid)]
|
|
||||||
constraints[category] += [cid]
|
|
||||||
if return_constraints:
|
|
||||||
return x, constraints
|
|
||||||
else:
|
|
||||||
return x
|
|
||||||
|
|
||||||
def y(self, instances):
|
def y(self, instances):
|
||||||
y = {}
|
y = {}
|
||||||
@@ -215,13 +186,18 @@ class ConvertTightIneqsIntoEqsStep(Component):
|
|||||||
is_infeasible = True
|
is_infeasible = True
|
||||||
restore(cid)
|
restore(cid)
|
||||||
elif self.check_optimality:
|
elif self.check_optimality:
|
||||||
|
random.shuffle(self.converted)
|
||||||
|
n_restored = 0
|
||||||
for cid in self.converted:
|
for cid in self.converted:
|
||||||
|
if n_restored >= 100:
|
||||||
|
break
|
||||||
pi = solver.internal_solver.get_dual(cid)
|
pi = solver.internal_solver.get_dual(cid)
|
||||||
csense = self.original_sense[cid]
|
csense = self.original_sense[cid]
|
||||||
msense = solver.internal_solver.get_sense()
|
msense = solver.internal_solver.get_sense()
|
||||||
if not check_pi(msense, csense, pi):
|
if not check_pi(msense, csense, pi):
|
||||||
is_suboptimal = True
|
is_suboptimal = True
|
||||||
restore(cid)
|
restore(cid)
|
||||||
|
n_restored += 1
|
||||||
|
|
||||||
for cid in restored:
|
for cid in restored:
|
||||||
self.converted.remove(cid)
|
self.converted.remove(cid)
|
||||||
|
|||||||
@@ -103,7 +103,8 @@ class DropRedundantInequalitiesStep(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])
|
||||||
|
|
||||||
def _x_test(self, instance, constraint_ids):
|
@staticmethod
|
||||||
|
def _x_test(instance, constraint_ids):
|
||||||
x = {}
|
x = {}
|
||||||
constraints = {}
|
constraints = {}
|
||||||
cids = constraint_ids
|
cids = constraint_ids
|
||||||
@@ -120,7 +121,8 @@ class DropRedundantInequalitiesStep(Component):
|
|||||||
x[category] = np.array(x[category])
|
x[category] = np.array(x[category])
|
||||||
return x, constraints
|
return x, constraints
|
||||||
|
|
||||||
def _x_train(self, instances):
|
@staticmethod
|
||||||
|
def _x_train(instances):
|
||||||
x = {}
|
x = {}
|
||||||
for instance in tqdm(
|
for instance in tqdm(
|
||||||
InstanceIterator(instances),
|
InstanceIterator(instances),
|
||||||
|
|||||||
Reference in New Issue
Block a user