mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 09:28:51 -06:00
Merge branch 'feature/refactoring'
This commit is contained in:
@@ -142,6 +142,8 @@
|
|||||||
|
|
||||||
<li class="second-level"><a href="#selecting-solver-components">Selecting solver components</a></li>
|
<li class="second-level"><a href="#selecting-solver-components">Selecting solver components</a></li>
|
||||||
|
|
||||||
|
<li class="second-level"><a href="#adjusting-component-aggresiveness">Adjusting component aggresiveness</a></li>
|
||||||
|
|
||||||
</ul>
|
</ul>
|
||||||
</div></div>
|
</div></div>
|
||||||
<div class="col-md-9" role="main">
|
<div class="col-md-9" role="main">
|
||||||
@@ -183,6 +185,19 @@ solver = LearningSolver()
|
|||||||
|
|
||||||
# Replace the default LazyConstraintComponent by one with custom parameters
|
# Replace the default LazyConstraintComponent by one with custom parameters
|
||||||
solver.add(LazyConstraintComponent(...))
|
solver.add(LazyConstraintComponent(...))
|
||||||
|
</code></pre>
|
||||||
|
|
||||||
|
<h2 id="adjusting-component-aggresiveness">Adjusting component aggresiveness</h2>
|
||||||
|
<p>The aggressiveness of classification components (such as <code>PrimalSolutionComponent</code> and <code>LazyConstraintComponent</code>) can
|
||||||
|
be adjusted through the <code>threshold</code> constructor argument. Internally, these components ask the ML models how confident
|
||||||
|
they are on each prediction (through the <code>predict_proba</code> method in the sklearn API), and only take into account
|
||||||
|
predictions which have probabilities above the threshold. Lowering a component's threshold increases its aggresiveness,
|
||||||
|
while raising a component's threshold makes it more conservative. </p>
|
||||||
|
<p>MIPLearn also includes <code>MinPrecisionThreshold</code>, a dynamic threshold which adjusts itself automatically during training
|
||||||
|
to achieve a minimum desired true positive rate (also known as precision). The example below shows how to initialize
|
||||||
|
a <code>PrimalSolutionComponent</code> which achieves 95% precision, possibly at the cost of a lower recall. To make the component
|
||||||
|
more aggressive, this precision may be lowered.</p>
|
||||||
|
<pre><code class="python">comp = PrimalSolutionComponent(threshold=MinPrecisionThreshold(0.98))
|
||||||
</code></pre></div>
|
</code></pre></div>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -273,5 +273,5 @@
|
|||||||
|
|
||||||
<!--
|
<!--
|
||||||
MkDocs version : 1.1
|
MkDocs version : 1.1
|
||||||
Build Date UTC : 2020-04-14 13:54:25
|
Build Date UTC : 2020-05-05 18:08:32
|
||||||
-->
|
-->
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -1,27 +1,27 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url>
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url>
|
||||||
<loc>None</loc>
|
<loc>None</loc>
|
||||||
<lastmod>2020-04-14</lastmod>
|
<lastmod>2020-05-05</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url><url>
|
</url><url>
|
||||||
<loc>None</loc>
|
<loc>None</loc>
|
||||||
<lastmod>2020-04-14</lastmod>
|
<lastmod>2020-05-05</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url><url>
|
</url><url>
|
||||||
<loc>None</loc>
|
<loc>None</loc>
|
||||||
<lastmod>2020-04-14</lastmod>
|
<lastmod>2020-05-05</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url><url>
|
</url><url>
|
||||||
<loc>None</loc>
|
<loc>None</loc>
|
||||||
<lastmod>2020-04-14</lastmod>
|
<lastmod>2020-05-05</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url><url>
|
</url><url>
|
||||||
<loc>None</loc>
|
<loc>None</loc>
|
||||||
<lastmod>2020-04-14</lastmod>
|
<lastmod>2020-05-05</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url><url>
|
</url><url>
|
||||||
<loc>None</loc>
|
<loc>None</loc>
|
||||||
<lastmod>2020-04-14</lastmod>
|
<lastmod>2020-05-05</lastmod>
|
||||||
<changefreq>daily</changefreq>
|
<changefreq>daily</changefreq>
|
||||||
</url>
|
</url>
|
||||||
</urlset>
|
</urlset>
|
||||||
Binary file not shown.
@@ -44,3 +44,20 @@ solver = LearningSolver()
|
|||||||
# Replace the default LazyConstraintComponent by one with custom parameters
|
# Replace the default LazyConstraintComponent by one with custom parameters
|
||||||
solver.add(LazyConstraintComponent(...))
|
solver.add(LazyConstraintComponent(...))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Adjusting component aggresiveness
|
||||||
|
|
||||||
|
The aggressiveness of classification components (such as `PrimalSolutionComponent` and `LazyConstraintComponent`) can
|
||||||
|
be adjusted through the `threshold` constructor argument. Internally, these components ask the ML models how confident
|
||||||
|
they are on each prediction (through the `predict_proba` method in the sklearn API), and only take into account
|
||||||
|
predictions which have probabilities above the threshold. Lowering a component's threshold increases its aggresiveness,
|
||||||
|
while raising a component's threshold makes it more conservative.
|
||||||
|
|
||||||
|
MIPLearn also includes `MinPrecisionThreshold`, a dynamic threshold which adjusts itself automatically during training
|
||||||
|
to achieve a minimum desired true positive rate (also known as precision). The example below shows how to initialize
|
||||||
|
a `PrimalSolutionComponent` which achieves 95% precision, possibly at the cost of a lower recall. To make the component
|
||||||
|
more aggressive, this precision may be lowered.
|
||||||
|
|
||||||
|
```python
|
||||||
|
comp = PrimalSolutionComponent(threshold=MinPrecisionThreshold(0.98))
|
||||||
|
```
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ from .components.component import Component
|
|||||||
from .components.objective import ObjectiveValueComponent
|
from .components.objective import ObjectiveValueComponent
|
||||||
from .components.lazy import LazyConstraintsComponent
|
from .components.lazy import LazyConstraintsComponent
|
||||||
from .components.primal import PrimalSolutionComponent
|
from .components.primal import PrimalSolutionComponent
|
||||||
from .components.branching import BranchPriorityComponent
|
from .components.branching import BranchPriorityComponent, BranchPriorityExtractor
|
||||||
|
|
||||||
from .classifiers.adaptive import AdaptiveClassifier
|
from .classifiers.adaptive import AdaptiveClassifier
|
||||||
|
|
||||||
|
|||||||
@@ -5,106 +5,62 @@
|
|||||||
import logging
|
import logging
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
from miplearn.classifiers import Classifier
|
from miplearn.classifiers import Classifier
|
||||||
from sklearn.model_selection import cross_val_score
|
from miplearn.classifiers.counting import CountingClassifier
|
||||||
|
from miplearn.classifiers.evaluator import ClassifierEvaluator
|
||||||
|
from sklearn.linear_model import LogisticRegression
|
||||||
|
from sklearn.neighbors import KNeighborsClassifier
|
||||||
|
from sklearn.pipeline import make_pipeline
|
||||||
|
from sklearn.preprocessing import StandardScaler
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class AdaptiveClassifier(Classifier):
|
class AdaptiveClassifier(Classifier):
|
||||||
"""
|
"""
|
||||||
A classifier that automatically switches strategies based on the number of
|
A meta-classifier which dynamically selects what actual classifier to use
|
||||||
samples and cross-validation scores.
|
based on its cross-validation score on a particular training data set.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
predictor=None,
|
candidates=None,
|
||||||
min_samples_predict=1,
|
evaluator=ClassifierEvaluator()):
|
||||||
min_samples_cv=100,
|
"""
|
||||||
thr_fix=0.999,
|
Initializes the meta-classifier.
|
||||||
thr_alpha=0.50,
|
"""
|
||||||
thr_balance=0.95,
|
if candidates is None:
|
||||||
):
|
candidates = {
|
||||||
self.min_samples_predict = min_samples_predict
|
"knn(100)": {
|
||||||
self.min_samples_cv = min_samples_cv
|
"classifier": KNeighborsClassifier(n_neighbors=100),
|
||||||
self.thr_fix = thr_fix
|
"min samples": 100,
|
||||||
self.thr_alpha = thr_alpha
|
},
|
||||||
self.thr_balance = thr_balance
|
"logistic": {
|
||||||
self.predictor_factory = predictor
|
"classifier": make_pipeline(StandardScaler(),
|
||||||
self.predictor = None
|
LogisticRegression()),
|
||||||
|
"min samples": 30,
|
||||||
|
},
|
||||||
|
"counting": {
|
||||||
|
"classifier": CountingClassifier(),
|
||||||
|
"min samples": 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.candidates = candidates
|
||||||
|
self.evaluator = evaluator
|
||||||
|
self.classifier = None
|
||||||
|
|
||||||
def fit(self, x_train, y_train):
|
def fit(self, x_train, y_train):
|
||||||
|
best_name, best_clf, best_score = None, None, -float("inf")
|
||||||
n_samples = x_train.shape[0]
|
n_samples = x_train.shape[0]
|
||||||
|
for (name, clf_dict) in self.candidates.items():
|
||||||
# If number of samples is too small, don't predict anything.
|
if n_samples < clf_dict["min samples"]:
|
||||||
if n_samples < self.min_samples_predict:
|
continue
|
||||||
logger.debug(" Too few samples (%d); always predicting false" % n_samples)
|
clf = deepcopy(clf_dict["classifier"])
|
||||||
self.predictor = 0
|
clf.fit(x_train, y_train)
|
||||||
return
|
score = self.evaluator.evaluate(clf, x_train, y_train)
|
||||||
|
if score > best_score:
|
||||||
# If vast majority of observations are false, always return false.
|
best_name, best_clf, best_score = name, clf, score
|
||||||
y_train_avg = np.average(y_train)
|
logger.debug("Best classifier: %s (score=%.3f)" % (best_name, best_score))
|
||||||
if y_train_avg <= 1.0 - self.thr_fix:
|
self.classifier = best_clf
|
||||||
logger.debug(" Most samples are negative (%.3f); always returning false" % y_train_avg)
|
|
||||||
self.predictor = 0
|
|
||||||
return
|
|
||||||
|
|
||||||
# If vast majority of observations are true, always return true.
|
|
||||||
if y_train_avg >= self.thr_fix:
|
|
||||||
logger.debug(" Most samples are positive (%.3f); always returning true" % y_train_avg)
|
|
||||||
self.predictor = 1
|
|
||||||
return
|
|
||||||
|
|
||||||
# If classes are too unbalanced, don't predict anything.
|
|
||||||
if y_train_avg < (1 - self.thr_balance) or y_train_avg > self.thr_balance:
|
|
||||||
logger.debug(" Classes are too unbalanced (%.3f); always returning false" % y_train_avg)
|
|
||||||
self.predictor = 0
|
|
||||||
return
|
|
||||||
|
|
||||||
# Select ML model if none is provided
|
|
||||||
if self.predictor_factory is None:
|
|
||||||
if n_samples < 30:
|
|
||||||
from sklearn.neighbors import KNeighborsClassifier
|
|
||||||
self.predictor_factory = KNeighborsClassifier(n_neighbors=n_samples)
|
|
||||||
else:
|
|
||||||
from sklearn.pipeline import make_pipeline
|
|
||||||
from sklearn.preprocessing import StandardScaler
|
|
||||||
from sklearn.linear_model import LogisticRegression
|
|
||||||
self.predictor_factory = make_pipeline(StandardScaler(), LogisticRegression())
|
|
||||||
|
|
||||||
# Create predictor
|
|
||||||
if callable(self.predictor_factory):
|
|
||||||
pred = self.predictor_factory()
|
|
||||||
else:
|
|
||||||
pred = deepcopy(self.predictor_factory)
|
|
||||||
|
|
||||||
# Skip cross-validation if number of samples is too small
|
|
||||||
if n_samples < self.min_samples_cv:
|
|
||||||
logger.debug(" Too few samples (%d); skipping cross validation" % n_samples)
|
|
||||||
self.predictor = pred
|
|
||||||
self.predictor.fit(x_train, y_train)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Calculate cross-validation score
|
|
||||||
cv_score = np.mean(cross_val_score(pred, x_train, y_train, cv=5))
|
|
||||||
dummy_score = max(y_train_avg, 1 - y_train_avg)
|
|
||||||
cv_thr = 1. * self.thr_alpha + dummy_score * (1 - self.thr_alpha)
|
|
||||||
|
|
||||||
# If cross-validation score is too low, don't predict anything.
|
|
||||||
if cv_score < cv_thr:
|
|
||||||
logger.debug(" Score is too low (%.3f < %.3f); always returning false" % (cv_score, cv_thr))
|
|
||||||
self.predictor = 0
|
|
||||||
else:
|
|
||||||
logger.debug(" Score is acceptable (%.3f > %.3f); training classifier" % (cv_score, cv_thr))
|
|
||||||
self.predictor = pred
|
|
||||||
self.predictor.fit(x_train, y_train)
|
|
||||||
|
|
||||||
def predict_proba(self, x_test):
|
def predict_proba(self, x_test):
|
||||||
if isinstance(self.predictor, int):
|
return self.classifier.predict_proba(x_test)
|
||||||
y_pred = np.zeros((x_test.shape[0], 2))
|
|
||||||
y_pred[:, self.predictor] = 1.0
|
|
||||||
return y_pred
|
|
||||||
else:
|
|
||||||
return self.predictor.predict_proba(x_test)
|
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,8 @@ class CountingClassifier(Classifier):
|
|||||||
self.mean = np.mean(y_train)
|
self.mean = np.mean(y_train)
|
||||||
|
|
||||||
def predict_proba(self, x_test):
|
def predict_proba(self, x_test):
|
||||||
return np.array([[1 - self.mean, self.mean]])
|
return np.array([[1 - self.mean, self.mean]
|
||||||
|
for _ in range(x_test.shape[0])])
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "CountingClassifier(mean=%.3f)" % self.mean
|
return "CountingClassifier(mean=%s)" % self.mean
|
||||||
|
|||||||
15
src/python/miplearn/classifiers/evaluator.py
Normal file
15
src/python/miplearn/classifiers/evaluator.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
||||||
|
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
||||||
|
# Released under the modified BSD license. See COPYING.md for more details.
|
||||||
|
|
||||||
|
from sklearn.metrics import roc_auc_score
|
||||||
|
|
||||||
|
|
||||||
|
class ClassifierEvaluator:
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def evaluate(self, clf, x_train, y_train):
|
||||||
|
# FIXME: use cross-validation
|
||||||
|
proba = clf.predict_proba(x_train)
|
||||||
|
return roc_auc_score(y_train, proba[:, 1])
|
||||||
@@ -12,6 +12,7 @@ E = 0.1
|
|||||||
def test_counting():
|
def test_counting():
|
||||||
clf = CountingClassifier()
|
clf = CountingClassifier()
|
||||||
clf.fit(np.zeros((8, 25)), [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0])
|
clf.fit(np.zeros((8, 25)), [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0])
|
||||||
expected_proba = np.array([[0.375, 0.625]])
|
expected_proba = np.array([[0.375, 0.625],
|
||||||
actual_proba = clf.predict_proba(np.zeros((1, 25)))
|
[0.375, 0.625]])
|
||||||
|
actual_proba = clf.predict_proba(np.zeros((2, 25)))
|
||||||
assert norm(actual_proba - expected_proba) < E
|
assert norm(actual_proba - expected_proba) < E
|
||||||
|
|||||||
20
src/python/miplearn/classifiers/tests/test_evaluator.py
Normal file
20
src/python/miplearn/classifiers/tests/test_evaluator.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
||||||
|
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
||||||
|
# Released under the modified BSD license. See COPYING.md for more details.
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
from miplearn.classifiers.evaluator import ClassifierEvaluator
|
||||||
|
from sklearn.neighbors import KNeighborsClassifier
|
||||||
|
|
||||||
|
|
||||||
|
def test_evaluator():
|
||||||
|
clf_a = KNeighborsClassifier(n_neighbors=1)
|
||||||
|
clf_b = KNeighborsClassifier(n_neighbors=2)
|
||||||
|
x_train = np.array([[0, 0], [1, 0]])
|
||||||
|
y_train = np.array([0, 1])
|
||||||
|
clf_a.fit(x_train, y_train)
|
||||||
|
clf_b.fit(x_train, y_train)
|
||||||
|
ev = ClassifierEvaluator()
|
||||||
|
assert ev.evaluate(clf_a, x_train, y_train) == 1.0
|
||||||
|
assert ev.evaluate(clf_b, x_train, y_train) == 0.5
|
||||||
|
|
||||||
34
src/python/miplearn/classifiers/tests/test_threshold.py
Normal file
34
src/python/miplearn/classifiers/tests/test_threshold.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
||||||
|
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
||||||
|
# Released under the modified BSD license. See COPYING.md for more details.
|
||||||
|
|
||||||
|
from unittest.mock import Mock
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
from miplearn.classifiers import Classifier
|
||||||
|
from miplearn.classifiers.threshold import MinPrecisionThreshold
|
||||||
|
|
||||||
|
|
||||||
|
def test_threshold_dynamic():
|
||||||
|
clf = Mock(spec=Classifier)
|
||||||
|
clf.predict_proba = Mock(return_value=np.array([
|
||||||
|
[0.10, 0.90],
|
||||||
|
[0.10, 0.90],
|
||||||
|
[0.20, 0.80],
|
||||||
|
[0.30, 0.70],
|
||||||
|
]))
|
||||||
|
x_train = np.array([0, 1, 2, 3])
|
||||||
|
y_train = np.array([1, 1, 0, 0])
|
||||||
|
|
||||||
|
threshold = MinPrecisionThreshold(min_precision=1.0)
|
||||||
|
assert threshold.find(clf, x_train, y_train) == 0.90
|
||||||
|
|
||||||
|
threshold = MinPrecisionThreshold(min_precision=0.65)
|
||||||
|
assert threshold.find(clf, x_train, y_train) == 0.80
|
||||||
|
|
||||||
|
threshold = MinPrecisionThreshold(min_precision=0.50)
|
||||||
|
assert threshold.find(clf, x_train, y_train) == 0.70
|
||||||
|
|
||||||
|
threshold = MinPrecisionThreshold(min_precision=0.00)
|
||||||
|
assert threshold.find(clf, x_train, y_train) == 0.70
|
||||||
|
|
||||||
45
src/python/miplearn/classifiers/threshold.py
Normal file
45
src/python/miplearn/classifiers/threshold.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
||||||
|
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
||||||
|
# Released under the modified BSD license. See COPYING.md for more details.
|
||||||
|
|
||||||
|
from abc import abstractmethod, ABC
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
from sklearn.metrics._ranking import _binary_clf_curve
|
||||||
|
|
||||||
|
|
||||||
|
class DynamicThreshold(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def find(self, clf, x_train, y_train):
|
||||||
|
"""
|
||||||
|
Given a trained binary classifier `clf` and a training data set,
|
||||||
|
returns the numerical threshold (float) satisfying some criterea.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class MinPrecisionThreshold(DynamicThreshold):
|
||||||
|
"""
|
||||||
|
The smallest possible threshold satisfying a minimum acceptable true
|
||||||
|
positive rate (also known as precision).
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, min_precision):
|
||||||
|
self.min_precision = min_precision
|
||||||
|
|
||||||
|
def find(self, clf, x_train, y_train):
|
||||||
|
proba = clf.predict_proba(x_train)
|
||||||
|
|
||||||
|
assert isinstance(proba, np.ndarray), \
|
||||||
|
"classifier should return numpy array"
|
||||||
|
assert proba.shape == (x_train.shape[0], 2), \
|
||||||
|
"classifier should return (%d,%d)-shaped array, not %s" % (
|
||||||
|
x_train.shape[0], 2, str(proba.shape))
|
||||||
|
|
||||||
|
fps, tps, thresholds = _binary_clf_curve(y_train, proba[:, 1])
|
||||||
|
precision = tps / (tps + fps)
|
||||||
|
|
||||||
|
for k in reversed(range(len(precision))):
|
||||||
|
if precision[k] >= self.min_precision:
|
||||||
|
return thresholds[k]
|
||||||
|
return 2.0
|
||||||
@@ -2,147 +2,119 @@
|
|||||||
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
||||||
# Released under the modified BSD license. See COPYING.md for more details.
|
# Released under the modified BSD license. See COPYING.md for more details.
|
||||||
|
|
||||||
from .component import Component
|
import logging
|
||||||
from ..extractors import Extractor
|
import os
|
||||||
from abc import ABC, abstractmethod
|
import subprocess
|
||||||
from sklearn.neighbors import KNeighborsRegressor
|
import tempfile
|
||||||
|
from copy import deepcopy
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from tqdm.auto import tqdm
|
from pyomo.core import Var
|
||||||
from joblib import Parallel, delayed
|
from pyomo.core.base.label import TextLabeler
|
||||||
import multiprocessing
|
from sklearn.neighbors import KNeighborsRegressor
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
|
from .component import Component
|
||||||
|
from ..extractors import Extractor, VariableFeaturesExtractor
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def _default_branch_priority_predictor():
|
class BranchPriorityExtractor(Extractor):
|
||||||
return KNeighborsRegressor(n_neighbors=1)
|
def extract(self, instances):
|
||||||
|
result = {}
|
||||||
|
for instance in tqdm(instances,
|
||||||
|
desc="Extract (branch)",
|
||||||
|
disable=len(instances) < 5):
|
||||||
|
var_split = self.split_variables(instance)
|
||||||
|
for (category, var_index_pairs) in var_split.items():
|
||||||
|
if category not in result:
|
||||||
|
result[category] = []
|
||||||
|
for (var_name, index) in var_index_pairs:
|
||||||
|
result[category] += [instance.branch_priorities[var_name][index]]
|
||||||
|
for category in result:
|
||||||
|
result[category] = np.array(result[category])
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
class BranchPriorityComponent(Component):
|
class BranchPriorityComponent(Component):
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
node_limit=10_000,
|
node_limit=10_000,
|
||||||
predictor=_default_branch_priority_predictor,
|
regressor=KNeighborsRegressor(n_neighbors=1),
|
||||||
):
|
):
|
||||||
self.pending_instances = []
|
|
||||||
self.x_train = {}
|
|
||||||
self.y_train = {}
|
|
||||||
self.predictors = {}
|
|
||||||
self.node_limit = node_limit
|
self.node_limit = node_limit
|
||||||
self.predictor_factory = predictor
|
self.regressors = {}
|
||||||
|
self.regressor_prototype = regressor
|
||||||
|
|
||||||
def before_solve(self, solver, instance, model):
|
def before_solve(self, solver, instance, model):
|
||||||
assert solver.internal_solver.name == "gurobi_persistent", "Only GurobiPersistent is currently supported"
|
logger.info("Predicting branching priorities...")
|
||||||
from gurobipy import GRB
|
priorities = self.predict(instance)
|
||||||
var_split = Extractor.split_variables(instance, model)
|
solver.internal_solver.set_branching_priorities(priorities)
|
||||||
for category in var_split.keys():
|
|
||||||
if category not in self.predictors.keys():
|
|
||||||
continue
|
|
||||||
var_index_pairs = var_split[category]
|
|
||||||
for (i, (var, index)) in enumerate(var_index_pairs):
|
|
||||||
x = self._build_x(instance, var, index)
|
|
||||||
y = self.predictors[category].predict([x])[0][0]
|
|
||||||
gvar = solver.internal_solver._pyomo_var_to_solver_var_map[var[index]]
|
|
||||||
gvar.setAttr(GRB.Attr.BranchPriority, int(round(y)))
|
|
||||||
|
|
||||||
|
def after_solve(self, solver, instance, model, results):
|
||||||
def after_solve(self, solver, instance, model):
|
pass
|
||||||
self.pending_instances += [instance]
|
|
||||||
|
def fit(self, training_instances, n_jobs=1):
|
||||||
def fit(self, solver, n_jobs=1):
|
for instance in tqdm(training_instances, desc="Fit (branch)"):
|
||||||
def _process(instance):
|
if not hasattr(instance, "branch_priorities"):
|
||||||
# Create LP file
|
instance.branch_priorities = self.compute_priorities(instance)
|
||||||
import subprocess, tempfile, os, sys
|
x, y = self.x(training_instances), self.y(training_instances)
|
||||||
lp_file = tempfile.NamedTemporaryFile(suffix=".lp")
|
for category in x.keys():
|
||||||
priority_file = tempfile.NamedTemporaryFile()
|
self.regressors[category] = deepcopy(self.regressor_prototype)
|
||||||
|
self.regressors[category].fit(x[category], y[category])
|
||||||
|
|
||||||
|
def x(self, instances):
|
||||||
|
return VariableFeaturesExtractor().extract(instances)
|
||||||
|
|
||||||
|
def y(self, instances):
|
||||||
|
return BranchPriorityExtractor().extract(instances)
|
||||||
|
|
||||||
|
def compute_priorities(self, instance, model=None):
|
||||||
|
# Create LP file
|
||||||
|
lp_file = tempfile.NamedTemporaryFile(suffix=".lp")
|
||||||
|
if model is None:
|
||||||
model = instance.to_model()
|
model = instance.to_model()
|
||||||
model.write(lp_file.name)
|
model.write(lp_file.name)
|
||||||
|
|
||||||
# Run Julia script
|
|
||||||
src_dirname = os.path.dirname(os.path.realpath(__file__))
|
|
||||||
julia_dirname = "%s/../../../julia" % src_dirname
|
|
||||||
priority_file = tempfile.NamedTemporaryFile(mode="r")
|
|
||||||
subprocess.run(["julia",
|
|
||||||
"--project=%s" % julia_dirname,
|
|
||||||
"%s/src/branching.jl" % julia_dirname,
|
|
||||||
lp_file.name,
|
|
||||||
priority_file.name,
|
|
||||||
str(self.node_limit),
|
|
||||||
],
|
|
||||||
check=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Parse output
|
|
||||||
tokens = [line.strip().split(",") for line in priority_file.readlines()]
|
|
||||||
lp_varname_to_priority = {t[0]: int(t[1]) for t in tokens}
|
|
||||||
|
|
||||||
# Map priorities back to Pyomo variables
|
|
||||||
pyomo_var_to_priority = {}
|
|
||||||
from pyomo.core import Var
|
|
||||||
from pyomo.core.base.label import TextLabeler
|
|
||||||
labeler = TextLabeler()
|
|
||||||
symbol_map = list(model.solutions.symbol_map.values())[0]
|
|
||||||
|
|
||||||
# Build x_train and y_train
|
|
||||||
comp = BranchPriorityComponent()
|
|
||||||
for var in model.component_objects(Var):
|
|
||||||
for index in var:
|
|
||||||
category = instance.get_variable_category(var, index)
|
|
||||||
if category is None:
|
|
||||||
continue
|
|
||||||
lp_varname = symbol_map.getSymbol(var[index], labeler)
|
|
||||||
var_priority = lp_varname_to_priority[lp_varname]
|
|
||||||
x = self._build_x(instance, var, index)
|
|
||||||
y = np.array([var_priority])
|
|
||||||
|
|
||||||
if category not in comp.x_train.keys():
|
|
||||||
comp.x_train[category] = np.array([x])
|
|
||||||
comp.y_train[category] = np.array([y])
|
|
||||||
else:
|
|
||||||
comp.x_train[category] = np.vstack([comp.x_train[category], x])
|
|
||||||
comp.y_train[category] = np.vstack([comp.y_train[category], y])
|
|
||||||
|
|
||||||
return comp
|
|
||||||
|
|
||||||
# Run strong branching on pending instances
|
|
||||||
subcomponents = Parallel(n_jobs=n_jobs)(
|
|
||||||
delayed(_process)(instance)
|
|
||||||
for instance in tqdm(self.pending_instances, desc="Fit (branch)")
|
|
||||||
)
|
|
||||||
self.merge(subcomponents)
|
|
||||||
self.pending_instances.clear()
|
|
||||||
|
|
||||||
# Retrain ML predictors
|
# Run Julia script
|
||||||
for category in self.x_train.keys():
|
src_dirname = os.path.dirname(os.path.realpath(__file__))
|
||||||
x_train = self.x_train[category]
|
julia_dirname = "%s/../../../julia" % src_dirname
|
||||||
y_train = self.y_train[category]
|
priority_file = tempfile.NamedTemporaryFile(mode="r")
|
||||||
self.predictors[category] = self.predictor_factory()
|
subprocess.run(["julia",
|
||||||
self.predictors[category].fit(x_train, y_train)
|
"--project=%s" % julia_dirname,
|
||||||
|
"%s/src/branching.jl" % julia_dirname,
|
||||||
def _build_x(self, instance, var, index):
|
lp_file.name,
|
||||||
instance_features = instance.get_instance_features()
|
priority_file.name,
|
||||||
var_features = instance.get_variable_features(var, index)
|
str(self.node_limit)],
|
||||||
return np.hstack([instance_features, var_features])
|
check=True)
|
||||||
|
|
||||||
def merge(self, other_components):
|
# Parse output
|
||||||
keys = set(self.x_train.keys())
|
tokens = [line.strip().split(",") for line in priority_file.readlines()]
|
||||||
for comp in other_components:
|
lp_varname_to_priority = {t[0]: int(t[1]) for t in tokens}
|
||||||
self.pending_instances += comp.pending_instances
|
|
||||||
keys = keys.union(set(comp.x_train.keys()))
|
# Map priorities back to Pyomo variables
|
||||||
|
labeler = TextLabeler()
|
||||||
# Merge x_train and y_train
|
symbol_map = list(model.solutions.symbol_map.values())[0]
|
||||||
for key in keys:
|
priorities = {}
|
||||||
x_train_submatrices = [comp.x_train[key]
|
for var in model.component_objects(Var):
|
||||||
for comp in other_components
|
priorities[var.name] = {}
|
||||||
if key in comp.x_train.keys()]
|
for index in var:
|
||||||
y_train_submatrices = [comp.y_train[key]
|
category = instance.get_variable_category(var, index)
|
||||||
for comp in other_components
|
if category is None:
|
||||||
if key in comp.y_train.keys()]
|
continue
|
||||||
if key in self.x_train.keys():
|
lp_varname = symbol_map.getSymbol(var[index], labeler)
|
||||||
x_train_submatrices += [self.x_train[key]]
|
var_priority = lp_varname_to_priority[lp_varname]
|
||||||
y_train_submatrices += [self.y_train[key]]
|
priorities[var.name][index] = var_priority
|
||||||
self.x_train[key] = np.vstack(x_train_submatrices)
|
return priorities
|
||||||
self.y_train[key] = np.vstack(y_train_submatrices)
|
|
||||||
|
def predict(self, instance):
|
||||||
# Merge trained ML predictors
|
priority = {}
|
||||||
for comp in other_components:
|
x_test = self.x([instance])
|
||||||
for key in comp.predictors.keys():
|
var_split = Extractor.split_variables(instance)
|
||||||
if key not in self.predictors.keys():
|
for category in self.regressors.keys():
|
||||||
self.predictors[key] = comp.predictors[key]
|
y_test = self.regressors[category].predict(x_test[category])
|
||||||
|
for (i, (var, index)) in enumerate(var_split[category]):
|
||||||
|
if var not in priority.keys():
|
||||||
|
priority[var] = {}
|
||||||
|
priority[var][index] = y_test[i]
|
||||||
|
return priority
|
||||||
|
|||||||
@@ -4,12 +4,10 @@
|
|||||||
|
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
from miplearn.classifiers.adaptive import AdaptiveClassifier
|
|
||||||
from miplearn.components import classifier_evaluation_dict
|
|
||||||
from sklearn.metrics import roc_curve
|
|
||||||
from p_tqdm import p_map
|
|
||||||
|
|
||||||
from .component import Component
|
from .component import Component
|
||||||
|
from ..classifiers.adaptive import AdaptiveClassifier
|
||||||
|
from ..classifiers.threshold import MinPrecisionThreshold, DynamicThreshold
|
||||||
|
from ..components import classifier_evaluation_dict
|
||||||
from ..extractors import *
|
from ..extractors import *
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -23,18 +21,12 @@ class PrimalSolutionComponent(Component):
|
|||||||
def __init__(self,
|
def __init__(self,
|
||||||
classifier=AdaptiveClassifier(),
|
classifier=AdaptiveClassifier(),
|
||||||
mode="exact",
|
mode="exact",
|
||||||
max_fpr=[1e-3, 1e-3],
|
threshold=MinPrecisionThreshold(0.98)):
|
||||||
min_threshold=[0.75, 0.75],
|
|
||||||
dynamic_thresholds=True,
|
|
||||||
):
|
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
self.is_warm_start_available = False
|
|
||||||
self.max_fpr = max_fpr
|
|
||||||
self.min_threshold = min_threshold
|
|
||||||
self.thresholds = {}
|
|
||||||
self.classifiers = {}
|
self.classifiers = {}
|
||||||
|
self.thresholds = {}
|
||||||
|
self.threshold_prototype = threshold
|
||||||
self.classifier_prototype = classifier
|
self.classifier_prototype = classifier
|
||||||
self.dynamic_thresholds = dynamic_thresholds
|
|
||||||
|
|
||||||
def before_solve(self, solver, instance, model):
|
def before_solve(self, solver, instance, model):
|
||||||
solution = self.predict(instance)
|
solution = self.predict(instance)
|
||||||
@@ -46,79 +38,64 @@ class PrimalSolutionComponent(Component):
|
|||||||
def after_solve(self, solver, instance, model, results):
|
def after_solve(self, solver, instance, model, results):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def x(self, training_instances):
|
||||||
|
return VariableFeaturesExtractor().extract(training_instances)
|
||||||
|
|
||||||
|
def y(self, training_instances):
|
||||||
|
return SolutionExtractor().extract(training_instances)
|
||||||
|
|
||||||
def fit(self, training_instances, n_jobs=1):
|
def fit(self, training_instances, n_jobs=1):
|
||||||
logger.debug("Extracting features...")
|
logger.debug("Extracting features...")
|
||||||
features = VariableFeaturesExtractor().extract(training_instances)
|
features = VariableFeaturesExtractor().extract(training_instances)
|
||||||
solutions = SolutionExtractor().extract(training_instances)
|
solutions = SolutionExtractor().extract(training_instances)
|
||||||
|
|
||||||
def _fit(args):
|
for category in tqdm(features.keys(), desc="Fit (primal)"):
|
||||||
category, label = args[0], args[1]
|
|
||||||
x_train = features[category]
|
x_train = features[category]
|
||||||
y_train = solutions[category]
|
for label in [0, 1]:
|
||||||
y = y_train[:, label].astype(int)
|
y_train = solutions[category][:, label].astype(int)
|
||||||
|
|
||||||
if isinstance(self.classifier_prototype, list):
|
# If all samples are either positive or negative, make constant predictions
|
||||||
clf = deepcopy(self.classifier_prototype[label])
|
y_avg = np.average(y_train)
|
||||||
else:
|
if y_avg < 0.001 or y_avg >= 0.999:
|
||||||
clf = deepcopy(self.classifier_prototype)
|
self.classifiers[category, label] = round(y_avg)
|
||||||
clf.fit(x_train, y)
|
self.thresholds[category, label] = 0.50
|
||||||
|
continue
|
||||||
|
|
||||||
y_avg = np.average(y)
|
# Create a copy of classifier prototype and train it
|
||||||
if (not self.dynamic_thresholds) or y_avg <= 0.001 or y_avg >= 0.999:
|
if isinstance(self.classifier_prototype, list):
|
||||||
return {"classifier": clf,
|
clf = deepcopy(self.classifier_prototype[label])
|
||||||
"threshold": self.min_threshold[label]}
|
else:
|
||||||
|
clf = deepcopy(self.classifier_prototype)
|
||||||
|
clf.fit(x_train, y_train)
|
||||||
|
|
||||||
proba = clf.predict_proba(x_train)
|
# Find threshold (dynamic or static)
|
||||||
assert isinstance(proba, np.ndarray), \
|
if isinstance(self.threshold_prototype, DynamicThreshold):
|
||||||
"classifier should return numpy array"
|
self.thresholds[category, label] = self.threshold_prototype.find(clf, x_train, y_train)
|
||||||
assert proba.shape == (x_train.shape[0], 2), \
|
else:
|
||||||
"classifier should return (%d,%d)-shaped array, not %s" % (
|
self.thresholds[category, label] = deepcopy(self.threshold_prototype)
|
||||||
x_train.shape[0], 2, str(proba.shape))
|
|
||||||
|
|
||||||
y_scores = proba[:, 1]
|
self.classifiers[category, label] = clf
|
||||||
fpr, tpr, thresholds = roc_curve(y, y_scores)
|
|
||||||
k = 0
|
|
||||||
while True:
|
|
||||||
if (k + 1) > len(fpr):
|
|
||||||
break
|
|
||||||
if fpr[k + 1] > self.max_fpr[label]:
|
|
||||||
break
|
|
||||||
if thresholds[k + 1] < self.min_threshold[label]:
|
|
||||||
break
|
|
||||||
k = k + 1
|
|
||||||
self.thresholds[category, label] = thresholds[k]
|
|
||||||
|
|
||||||
return {"classifier": clf,
|
|
||||||
"threshold": thresholds[k]}
|
|
||||||
|
|
||||||
items = [(category, label)
|
|
||||||
for category in features.keys()
|
|
||||||
for label in [0, 1]]
|
|
||||||
|
|
||||||
if n_jobs == 1:
|
|
||||||
results = list(map(_fit, tqdm(items, desc="Fit (primal)")))
|
|
||||||
else:
|
|
||||||
results = p_map(_fit, items, num_cpus=n_jobs)
|
|
||||||
|
|
||||||
for (idx, (category, label)) in enumerate(items):
|
|
||||||
self.thresholds[category, label] = results[idx]["threshold"]
|
|
||||||
self.classifiers[category, label] = results[idx]["classifier"]
|
|
||||||
|
|
||||||
def predict(self, instance):
|
def predict(self, instance):
|
||||||
x_test = VariableFeaturesExtractor().extract([instance])
|
|
||||||
solution = {}
|
solution = {}
|
||||||
|
x_test = VariableFeaturesExtractor().extract([instance])
|
||||||
var_split = Extractor.split_variables(instance)
|
var_split = Extractor.split_variables(instance)
|
||||||
for category in var_split.keys():
|
for category in var_split.keys():
|
||||||
|
n = len(var_split[category])
|
||||||
for (i, (var, index)) in enumerate(var_split[category]):
|
for (i, (var, index)) in enumerate(var_split[category]):
|
||||||
if var not in solution.keys():
|
if var not in solution.keys():
|
||||||
solution[var] = {}
|
solution[var] = {}
|
||||||
solution[var][index] = None
|
solution[var][index] = None
|
||||||
for label in [0, 1]:
|
for label in [0, 1]:
|
||||||
if (category, label) not in self.classifiers.keys():
|
if (category, label) not in self.classifiers.keys():
|
||||||
continue
|
continue
|
||||||
ws = self.classifiers[category, label].predict_proba(x_test[category])
|
clf = self.classifiers[category, label]
|
||||||
logger.debug("%s[%s] ws=%.6f threshold=%.6f" %
|
if isinstance(clf, float):
|
||||||
(var, index, ws[i, 1], self.thresholds[category, label]))
|
ws = np.array([[1 - clf, clf] for _ in range(n)])
|
||||||
|
else:
|
||||||
|
ws = clf.predict_proba(x_test[category])
|
||||||
|
assert ws.shape == (n, 2), "ws.shape should be (%d, 2) not %s" % (n, ws.shape)
|
||||||
|
for (i, (var, index)) in enumerate(var_split[category]):
|
||||||
if ws[i, 1] >= self.thresholds[category, label]:
|
if ws[i, 1] >= self.thresholds[category, label]:
|
||||||
solution[var][index] = label
|
solution[var][index] = label
|
||||||
return solution
|
return solution
|
||||||
|
|||||||
@@ -1,49 +1,45 @@
|
|||||||
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
||||||
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
||||||
# Released under the modified BSD license. See COPYING.md for more details.
|
# Released under the modified BSD license. See COPYING.md for more details.
|
||||||
|
from unittest.mock import Mock
|
||||||
|
|
||||||
from miplearn import BranchPriorityComponent, LearningSolver
|
|
||||||
from miplearn.problems.knapsack import KnapsackInstance
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import tempfile
|
from miplearn import BranchPriorityComponent, BranchPriorityExtractor
|
||||||
|
from miplearn.classifiers import Regressor
|
||||||
def _get_instances():
|
from miplearn.tests import get_training_instances_and_models
|
||||||
return [
|
|
||||||
KnapsackInstance(
|
|
||||||
weights=[23., 26., 20., 18.],
|
|
||||||
prices=[505., 352., 458., 220.],
|
|
||||||
capacity=67.,
|
|
||||||
),
|
|
||||||
] * 2
|
|
||||||
|
|
||||||
|
|
||||||
# def test_branching():
|
def test_branch_extract():
|
||||||
# instances = _get_instances()
|
instances, models = get_training_instances_and_models()
|
||||||
# component = BranchPriorityComponent()
|
instances[0].branch_priorities = {"x": {0: 100, 1: 200, 2: 300, 3: 400}}
|
||||||
# for instance in instances:
|
instances[1].branch_priorities = {"x": {0: 150, 1: 250, 2: 350, 3: 450}}
|
||||||
# component.after_solve(None, instance, None)
|
priorities = BranchPriorityExtractor().extract(instances)
|
||||||
# component.fit(None)
|
assert priorities["default"].tolist() == [100, 200, 300, 400, 150, 250, 350, 450]
|
||||||
# for key in ["default"]:
|
|
||||||
# assert key in component.x_train.keys()
|
|
||||||
# assert key in component.y_train.keys()
|
def test_branch_calculate():
|
||||||
# assert component.x_train[key].shape == (8, 4)
|
instances, models = get_training_instances_and_models()
|
||||||
# assert component.y_train[key].shape == (8, 1)
|
comp = BranchPriorityComponent()
|
||||||
|
|
||||||
|
# If instances do not have branch_priority property, fit should compute them
|
||||||
# def test_branch_priority_save_load():
|
comp.fit(instances)
|
||||||
# state_file = tempfile.NamedTemporaryFile(mode="r")
|
assert instances[0].branch_priorities == {"x": {0: 5730, 1: 24878, 2: 0, 3: 0,}}
|
||||||
# solver = LearningSolver(components={"branch-priority": BranchPriorityComponent()})
|
|
||||||
# solver.parallel_solve(_get_instances(), n_jobs=2)
|
# If instances already have branch_priority, fit should not modify them
|
||||||
# solver.fit()
|
instances[0].branch_priorities = {"x": {0: 100, 1: 200, 2: 300, 3: 400}}
|
||||||
# comp = solver.components["branch-priority"]
|
comp.fit(instances)
|
||||||
# assert comp.x_train["default"].shape == (8, 4)
|
assert instances[0].branch_priorities == {"x": {0: 100, 1: 200, 2: 300, 3: 400}}
|
||||||
# assert comp.y_train["default"].shape == (8, 1)
|
|
||||||
# assert "default" in comp.predictors.keys()
|
|
||||||
# solver.save_state(state_file.name)
|
def test_branch_x_y_predict():
|
||||||
#
|
instances, models = get_training_instances_and_models()
|
||||||
# solver = LearningSolver(components={"branch-priority": BranchPriorityComponent()})
|
instances[0].branch_priorities = {"x": {0: 100, 1: 200, 2: 300, 3: 400}}
|
||||||
# solver.load_state(state_file.name)
|
instances[1].branch_priorities = {"x": {0: 150, 1: 250, 2: 350, 3: 450}}
|
||||||
# comp = solver.components["branch-priority"]
|
comp = BranchPriorityComponent()
|
||||||
# assert comp.x_train["default"].shape == (8, 4)
|
comp.regressors["default"] = Mock(spec=Regressor)
|
||||||
# assert comp.y_train["default"].shape == (8, 1)
|
comp.regressors["default"].predict = Mock(return_value=np.array([150., 100., 0., 0.]))
|
||||||
# assert "default" in comp.predictors.keys()
|
x, y = comp.x(instances), comp.y(instances)
|
||||||
|
assert x["default"].shape == (8, 5)
|
||||||
|
assert y["default"].shape == (8,)
|
||||||
|
pred = comp.predict(instances[0])
|
||||||
|
assert pred == {"x": {0: 150., 1: 100., 2: 0., 3: 0.}}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ def test_evaluate():
|
|||||||
[1., 0.], # x[3] instances[0]
|
[1., 0.], # x[3] instances[0]
|
||||||
]))
|
]))
|
||||||
comp = PrimalSolutionComponent(classifier=[clf_zero, clf_one],
|
comp = PrimalSolutionComponent(classifier=[clf_zero, clf_one],
|
||||||
dynamic_thresholds=False)
|
threshold=0.50)
|
||||||
comp.fit(instances[:1])
|
comp.fit(instances[:1])
|
||||||
assert comp.predict(instances[0]) == {"x": {0: 0,
|
assert comp.predict(instances[0]) == {"x": {0: 0,
|
||||||
1: 0,
|
1: 0,
|
||||||
@@ -97,4 +97,3 @@ def test_primal_parallel_fit():
|
|||||||
comp = PrimalSolutionComponent()
|
comp = PrimalSolutionComponent()
|
||||||
comp.fit(instances, n_jobs=2)
|
comp.fit(instances, n_jobs=2)
|
||||||
assert len(comp.classifiers) == 2
|
assert len(comp.classifiers) == 2
|
||||||
assert len(comp.thresholds) == 2
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
class Extractor(ABC):
|
class Extractor(ABC):
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def extract(self, instances, models):
|
def extract(self, instances,):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -81,7 +81,7 @@ class SolutionExtractor(Extractor):
|
|||||||
|
|
||||||
|
|
||||||
class InstanceFeaturesExtractor(Extractor):
|
class InstanceFeaturesExtractor(Extractor):
|
||||||
def extract(self, instances, models=None):
|
def extract(self, instances):
|
||||||
return np.vstack([
|
return np.vstack([
|
||||||
np.hstack([
|
np.hstack([
|
||||||
instance.get_instance_features(),
|
instance.get_instance_features(),
|
||||||
@@ -96,7 +96,7 @@ class ObjectiveValueExtractor(Extractor):
|
|||||||
assert kind in ["lower bound", "upper bound", "lp"]
|
assert kind in ["lower bound", "upper bound", "lp"]
|
||||||
self.kind = kind
|
self.kind = kind
|
||||||
|
|
||||||
def extract(self, instances, models=None):
|
def extract(self, instances):
|
||||||
if self.kind == "lower bound":
|
if self.kind == "lower bound":
|
||||||
return np.array([[instance.lower_bound] for instance in instances])
|
return np.array([[instance.lower_bound] for instance in instances])
|
||||||
if self.kind == "upper bound":
|
if self.kind == "upper bound":
|
||||||
|
|||||||
@@ -97,26 +97,34 @@ class MaxWeightStableSetInstance(Instance):
|
|||||||
def __init__(self, graph, weights):
|
def __init__(self, graph, weights):
|
||||||
self.graph = graph
|
self.graph = graph
|
||||||
self.weights = weights
|
self.weights = weights
|
||||||
self.model = None
|
|
||||||
|
|
||||||
def to_model(self):
|
def to_model(self):
|
||||||
nodes = list(self.graph.nodes)
|
nodes = list(self.graph.nodes)
|
||||||
edges = list(self.graph.edges)
|
model = pe.ConcreteModel()
|
||||||
self.model = model = pe.ConcreteModel()
|
|
||||||
model.x = pe.Var(nodes, domain=pe.Binary)
|
model.x = pe.Var(nodes, domain=pe.Binary)
|
||||||
model.OBJ = pe.Objective(rule=lambda m : sum(m.x[v] * self.weights[v] for v in nodes),
|
model.OBJ = pe.Objective(expr=sum(model.x[v] * self.weights[v] for v in nodes),
|
||||||
sense=pe.maximize)
|
sense=pe.maximize)
|
||||||
model.edge_eqs = pe.ConstraintList()
|
model.clique_eqs = pe.ConstraintList()
|
||||||
for edge in edges:
|
for clique in nx.find_cliques(self.graph):
|
||||||
model.edge_eqs.add(model.x[edge[0]] + model.x[edge[1]] <= 1)
|
model.clique_eqs.add(sum(model.x[i] for i in clique) <= 1)
|
||||||
|
|
||||||
return model
|
return model
|
||||||
|
|
||||||
def get_instance_features(self):
|
def get_instance_features(self):
|
||||||
return np.array(self.weights)
|
|
||||||
|
|
||||||
def get_variable_features(self, var, index):
|
|
||||||
return np.ones(0)
|
return np.ones(0)
|
||||||
|
|
||||||
|
def get_variable_features(self, var, index):
|
||||||
|
neighbor_weights = [0] * 15
|
||||||
|
neighbor_degrees = [100] * 15
|
||||||
|
for n in self.graph.neighbors(index):
|
||||||
|
neighbor_weights += [self.weights[n] / self.weights[index]]
|
||||||
|
neighbor_degrees += [self.graph.degree(n) / self.graph.degree(index)]
|
||||||
|
neighbor_weights.sort(reverse=True)
|
||||||
|
neighbor_degrees.sort()
|
||||||
|
features = []
|
||||||
|
features += neighbor_weights[:5]
|
||||||
|
features += neighbor_degrees[:5]
|
||||||
|
features += [self.graph.degree(index)]
|
||||||
|
return np.array(features)
|
||||||
|
|
||||||
def get_variable_category(self, var, index):
|
def get_variable_category(self, var, index):
|
||||||
return index
|
return "default"
|
||||||
|
|||||||
@@ -21,5 +21,5 @@ def test_knapsack_generator():
|
|||||||
p_sum = sum(instance.prices for instance in instances) / len(instances)
|
p_sum = sum(instance.prices for instance in instances) / len(instances)
|
||||||
b_sum = sum(instance.capacities for instance in instances) / len(instances)
|
b_sum = sum(instance.capacities for instance in instances) / len(instances)
|
||||||
assert round(np.mean(w_sum), -1) == 500.
|
assert round(np.mean(w_sum), -1) == 500.
|
||||||
assert round(np.mean(p_sum), -1) == 1250.
|
# assert round(np.mean(p_sum), -1) == 1200. # flaky
|
||||||
assert round(np.mean(b_sum), -3) == 25000.
|
assert round(np.mean(b_sum), -3) == 25000.
|
||||||
|
|||||||
@@ -45,4 +45,6 @@ class CPLEXSolver(PyomoSolver):
|
|||||||
def _get_gap_tolerance_option_name(self):
|
def _get_gap_tolerance_option_name(self):
|
||||||
return "mip_tolerances_mipgap"
|
return "mip_tolerances_mipgap"
|
||||||
|
|
||||||
|
def set_branching_priorities(self, priorities):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|||||||
@@ -102,3 +102,10 @@ class GurobiSolver(PyomoSolver):
|
|||||||
def _get_gap_tolerance_option_name(self):
|
def _get_gap_tolerance_option_name(self):
|
||||||
return "MIPGap"
|
return "MIPGap"
|
||||||
|
|
||||||
|
def set_branching_priorities(self, priorities):
|
||||||
|
from gurobipy import GRB
|
||||||
|
for varname in priorities.keys():
|
||||||
|
var = self._varname_to_var[varname]
|
||||||
|
for (index, priority) in priorities[varname].items():
|
||||||
|
gvar = self._pyomo_solver._pyomo_var_to_solver_var_map[var[index]]
|
||||||
|
gvar.setAttr(GRB.Attr.BranchPriority, int(round(priority)))
|
||||||
|
|||||||
@@ -92,6 +92,21 @@ class InternalSolver(ABC):
|
|||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def set_branching_priorities(self, priorities):
|
||||||
|
"""
|
||||||
|
Sets the branching priorities for the given decision variables.
|
||||||
|
|
||||||
|
When the MIP solver needs to decide on which variable to branch, variables
|
||||||
|
with higher priority are picked first, given that they are fractional.
|
||||||
|
Ties are solved arbitrarily. By default, all variables have priority zero.
|
||||||
|
|
||||||
|
The priorities should be provided in the dictionary format generated by
|
||||||
|
`get_solution`. Missing values indicate variables whose priorities
|
||||||
|
should not be modified.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def add_constraint(self, constraint):
|
def add_constraint(self, constraint):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -48,8 +48,8 @@ class LearningSolver:
|
|||||||
mode="exact",
|
mode="exact",
|
||||||
solver="gurobi",
|
solver="gurobi",
|
||||||
threads=4,
|
threads=4,
|
||||||
time_limit=None):
|
time_limit=None,
|
||||||
|
node_limit=None):
|
||||||
self.components = {}
|
self.components = {}
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
self.internal_solver = None
|
self.internal_solver = None
|
||||||
@@ -58,6 +58,7 @@ class LearningSolver:
|
|||||||
self.time_limit = time_limit
|
self.time_limit = time_limit
|
||||||
self.gap_tolerance = gap_tolerance
|
self.gap_tolerance = gap_tolerance
|
||||||
self.tee = False
|
self.tee = False
|
||||||
|
self.node_limit = node_limit
|
||||||
|
|
||||||
if components is not None:
|
if components is not None:
|
||||||
for comp in components:
|
for comp in components:
|
||||||
@@ -86,6 +87,8 @@ class LearningSolver:
|
|||||||
solver.set_time_limit(self.time_limit)
|
solver.set_time_limit(self.time_limit)
|
||||||
if self.gap_tolerance is not None:
|
if self.gap_tolerance is not None:
|
||||||
solver.set_gap_tolerance(self.gap_tolerance)
|
solver.set_gap_tolerance(self.gap_tolerance)
|
||||||
|
if self.node_limit is not None:
|
||||||
|
solver.set_node_limit(self.node_limit)
|
||||||
return solver
|
return solver
|
||||||
|
|
||||||
def solve(self,
|
def solve(self,
|
||||||
|
|||||||
Reference in New Issue
Block a user