mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 09:28:51 -06:00
Move components into submodule
This commit is contained in:
@@ -2,8 +2,7 @@
|
||||
# Copyright (C) 2019-2020 Argonne National Laboratory. All rights reserved.
|
||||
# Written by Alinson S. Xavier <axavier@anl.gov>
|
||||
|
||||
from miplearn import LearningSolver, BenchmarkRunner
|
||||
from miplearn.warmstart import KnnWarmStartPredictor
|
||||
from miplearn import LearningSolver, BenchmarkRunner, KnnWarmStartPredictor
|
||||
from miplearn.problems.stab import MaxWeightStableSetGenerator
|
||||
from scipy.stats import randint
|
||||
import numpy as np
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
# MIPLearn, an extensible framework for Learning-Enhanced Mixed-Integer Optimization
|
||||
# Copyright (C) 2019-2020 Argonne National Laboratory. All rights reserved.
|
||||
# Written by Alinson S. Xavier <axavier@anl.gov>
|
||||
|
||||
from miplearn import BranchPriorityComponent, LearningSolver
|
||||
from miplearn.problems.knapsack import KnapsackInstance
|
||||
import numpy as np
|
||||
import tempfile
|
||||
|
||||
def _get_instances():
|
||||
return [
|
||||
KnapsackInstance(
|
||||
weights=[23., 26., 20., 18.],
|
||||
prices=[505., 352., 458., 220.],
|
||||
capacity=67.,
|
||||
),
|
||||
] * 2
|
||||
|
||||
|
||||
def test_branching():
|
||||
instances = _get_instances()
|
||||
component = BranchPriorityComponent()
|
||||
for instance in instances:
|
||||
component.after_solve(None, instance, None)
|
||||
component.fit(None)
|
||||
for key in ["default"]:
|
||||
assert key in component.x_train.keys()
|
||||
assert key in component.y_train.keys()
|
||||
assert component.x_train[key].shape == (8, 4)
|
||||
assert component.y_train[key].shape == (8, 1)
|
||||
|
||||
|
||||
def test_branch_priority_save_load():
|
||||
state_file = tempfile.NamedTemporaryFile(mode="r")
|
||||
solver = LearningSolver(components={"branch-priority": BranchPriorityComponent()})
|
||||
solver.parallel_solve(_get_instances(), n_jobs=2)
|
||||
solver.fit()
|
||||
comp = solver.components["branch-priority"]
|
||||
assert comp.x_train["default"].shape == (8, 4)
|
||||
assert comp.y_train["default"].shape == (8, 1)
|
||||
assert "default" in comp.predictors.keys()
|
||||
solver.save_state(state_file.name)
|
||||
|
||||
solver = LearningSolver(components={"branch-priority": BranchPriorityComponent()})
|
||||
solver.load_state(state_file.name)
|
||||
comp = solver.components["branch-priority"]
|
||||
assert comp.x_train["default"].shape == (8, 4)
|
||||
assert comp.y_train["default"].shape == (8, 1)
|
||||
assert "default" in comp.predictors.keys()
|
||||
@@ -2,10 +2,8 @@
|
||||
# Copyright (C) 2019-2020 Argonne National Laboratory. All rights reserved.
|
||||
# Written by Alinson S. Xavier <axavier@anl.gov>
|
||||
|
||||
from miplearn import LearningSolver
|
||||
from miplearn import LearningSolver, BranchPriorityComponent, WarmStartComponent
|
||||
from miplearn.problems.knapsack import KnapsackInstance
|
||||
from miplearn.branching import BranchPriorityComponent
|
||||
from miplearn.warmstart import WarmStartComponent
|
||||
import numpy as np
|
||||
|
||||
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
# MIPLearn, an extensible framework for Learning-Enhanced Mixed-Integer Optimization
|
||||
# Copyright (C) 2019-2020 Argonne National Laboratory. All rights reserved.
|
||||
# Written by Alinson S. Xavier <axavier@anl.gov>
|
||||
|
||||
from miplearn import WarmStartComponent, LearningSolver
|
||||
from miplearn.problems.knapsack import KnapsackInstance
|
||||
import numpy as np
|
||||
import tempfile
|
||||
|
||||
|
||||
def _get_instances():
|
||||
return [
|
||||
KnapsackInstance(
|
||||
weights=[23., 26., 20., 18.],
|
||||
prices=[505., 352., 458., 220.],
|
||||
capacity=67.,
|
||||
),
|
||||
] * 2
|
||||
|
||||
|
||||
def test_warm_start_save_load():
|
||||
state_file = tempfile.NamedTemporaryFile(mode="r")
|
||||
solver = LearningSolver(components={"warm-start": WarmStartComponent()})
|
||||
solver.parallel_solve(_get_instances(), n_jobs=2)
|
||||
solver.fit()
|
||||
comp = solver.components["warm-start"]
|
||||
assert comp.x_train["default"].shape == (8, 6)
|
||||
assert comp.y_train["default"].shape == (8, 2)
|
||||
assert "default" in comp.predictors.keys()
|
||||
solver.save_state(state_file.name)
|
||||
|
||||
solver = LearningSolver(components={"warm-start": WarmStartComponent()})
|
||||
solver.load_state(state_file.name)
|
||||
comp = solver.components["warm-start"]
|
||||
assert comp.x_train["default"].shape == (8, 6)
|
||||
assert comp.y_train["default"].shape == (8, 2)
|
||||
assert "default" in comp.predictors.keys()
|
||||
@@ -1,88 +0,0 @@
|
||||
# MIPLearn, an extensible framework for Learning-Enhanced Mixed-Integer Optimization
|
||||
# Copyright (C) 2019-2020 Argonne National Laboratory. All rights reserved.
|
||||
# Written by Alinson S. Xavier <axavier@anl.gov>
|
||||
|
||||
from miplearn.warmstart import KnnWarmStartPredictor
|
||||
from sklearn.metrics import accuracy_score, precision_score
|
||||
import numpy as np
|
||||
|
||||
|
||||
def test_knn_with_consensus():
|
||||
x_train = np.array([
|
||||
[0.0, 0.0],
|
||||
[0.1, 0.0],
|
||||
[0.0, 0.1],
|
||||
[1.0, 1.0],
|
||||
])
|
||||
y_train = np.array([
|
||||
[0., 1.],
|
||||
[0., 1.],
|
||||
[0., 1.],
|
||||
[1., 0.],
|
||||
])
|
||||
ws = KnnWarmStartPredictor(k=3, thr_clip=[0.75, 0.75])
|
||||
ws.fit(x_train, y_train)
|
||||
|
||||
x_test = np.array([[0.0, 0.0]])
|
||||
y_test = np.array([[0, 1]])
|
||||
assert (ws.predict(x_test) == y_test).all()
|
||||
|
||||
def test_knn_without_consensus():
|
||||
x_train = np.array([
|
||||
[0.0, 0.0],
|
||||
[0.1, 0.1],
|
||||
[0.9, 0.9],
|
||||
[1.0, 1.0],
|
||||
])
|
||||
y_train = np.array([
|
||||
[0., 1.],
|
||||
[0., 1.],
|
||||
[1., 0.],
|
||||
[1., 0.],
|
||||
])
|
||||
ws = KnnWarmStartPredictor(k=4, thr_clip=[0.75, 0.75])
|
||||
ws.fit(x_train, y_train)
|
||||
|
||||
x_test = np.array([[0.5, 0.5]])
|
||||
y_test = np.array([[0, 0]])
|
||||
assert (ws.predict(x_test) == y_test).all()
|
||||
|
||||
def test_knn_always_true():
|
||||
x_train = np.array([
|
||||
[0.0, 0.0],
|
||||
[0.1, 0.1],
|
||||
[0.9, 0.9],
|
||||
[1.0, 1.0],
|
||||
])
|
||||
y_train = np.array([
|
||||
[1., 0.],
|
||||
[1., 0.],
|
||||
[1., 0.],
|
||||
[1., 0.],
|
||||
])
|
||||
ws = KnnWarmStartPredictor(k=4, thr_clip=[0.75, 0.75])
|
||||
ws.fit(x_train, y_train)
|
||||
|
||||
x_test = np.array([[0.5, 0.5]])
|
||||
y_test = np.array([[1, 0]])
|
||||
assert (ws.predict(x_test) == y_test).all()
|
||||
|
||||
def test_knn_always_false():
|
||||
x_train = np.array([
|
||||
[0.0, 0.0],
|
||||
[0.1, 0.1],
|
||||
[0.9, 0.9],
|
||||
[1.0, 1.0],
|
||||
])
|
||||
y_train = np.array([
|
||||
[0., 1.],
|
||||
[0., 1.],
|
||||
[0., 1.],
|
||||
[0., 1.],
|
||||
])
|
||||
ws = KnnWarmStartPredictor(k=4, thr_clip=[0.75, 0.75])
|
||||
ws.fit(x_train, y_train)
|
||||
|
||||
x_test = np.array([[0.5, 0.5]])
|
||||
y_test = np.array([[0, 1]])
|
||||
assert (ws.predict(x_test) == y_test).all()
|
||||
@@ -1,64 +0,0 @@
|
||||
# MIPLearn, an extensible framework for Learning-Enhanced Mixed-Integer Optimization
|
||||
# Copyright (C) 2019-2020 Argonne National Laboratory. All rights reserved.
|
||||
# Written by Alinson S. Xavier <axavier@anl.gov>
|
||||
|
||||
from miplearn.warmstart import LogisticWarmStartPredictor
|
||||
from sklearn.metrics import accuracy_score, precision_score
|
||||
import numpy as np
|
||||
|
||||
|
||||
def _generate_dataset(ground_truth, n_samples=10_000):
|
||||
x_train = np.random.rand(n_samples,5)
|
||||
x_test = np.random.rand(n_samples,5)
|
||||
y_train = ground_truth(x_train)
|
||||
y_test = ground_truth(x_test)
|
||||
return x_train, y_train, x_test, y_test
|
||||
|
||||
|
||||
def _is_sum_greater_than_two(x):
|
||||
y = (np.sum(x, axis=1) > 2.0).astype(int)
|
||||
return np.vstack([y, 1 - y]).transpose()
|
||||
|
||||
|
||||
def _always_zero(x):
|
||||
y = np.zeros((1, x.shape[0]))
|
||||
return np.vstack([y, 1 - y]).transpose()
|
||||
|
||||
|
||||
def _random_values(x):
|
||||
y = np.random.randint(2, size=x.shape[0])
|
||||
return np.vstack([y, 1 - y]).transpose()
|
||||
|
||||
|
||||
def test_logistic_ws_with_balanced_labels():
|
||||
x_train, y_train, x_test, y_test = _generate_dataset(_is_sum_greater_than_two)
|
||||
ws = LogisticWarmStartPredictor()
|
||||
ws.fit(x_train, y_train)
|
||||
y_pred = ws.predict(x_test)
|
||||
assert accuracy_score(y_test[:,0], y_pred[:,0]) > 0.99
|
||||
assert accuracy_score(y_test[:,1], y_pred[:,1]) > 0.99
|
||||
|
||||
|
||||
def test_logistic_ws_with_unbalanced_labels():
|
||||
x_train, y_train, x_test, y_test = _generate_dataset(_always_zero)
|
||||
ws = LogisticWarmStartPredictor()
|
||||
ws.fit(x_train, y_train)
|
||||
y_pred = ws.predict(x_test)
|
||||
assert accuracy_score(y_test[:,0], y_pred[:,0]) == 1.0
|
||||
assert accuracy_score(y_test[:,1], y_pred[:,1]) == 1.0
|
||||
|
||||
|
||||
def test_logistic_ws_with_unpredictable_labels():
|
||||
x_train, y_train, x_test, y_test = _generate_dataset(_random_values)
|
||||
ws = LogisticWarmStartPredictor()
|
||||
ws.fit(x_train, y_train)
|
||||
y_pred = ws.predict(x_test)
|
||||
assert np.sum(y_pred) == 0
|
||||
|
||||
|
||||
def test_logistic_ws_with_small_sample_size():
|
||||
x_train, y_train, x_test, y_test = _generate_dataset(_random_values, n_samples=3)
|
||||
ws = LogisticWarmStartPredictor()
|
||||
ws.fit(x_train, y_train)
|
||||
y_pred = ws.predict(x_test)
|
||||
assert np.sum(y_pred) == 0
|
||||
Reference in New Issue
Block a user