parent
3644c59101
commit
3c9b1e2f44
@ -0,0 +1,45 @@
|
||||
# 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 LearningSolver
|
||||
from miplearn.problems.stab import MaxWeightStableSetInstance
|
||||
from miplearn.problems.stab import MaxWeightStableSetGenerator
|
||||
import networkx as nx
|
||||
import numpy as np
|
||||
from scipy.stats import uniform, randint
|
||||
|
||||
|
||||
def test_stab():
|
||||
graph = nx.cycle_graph(5)
|
||||
weights = [1., 2., 3., 4., 5.]
|
||||
instance = MaxWeightStableSetInstance(graph, weights)
|
||||
solver = LearningSolver()
|
||||
solver.solve(instance)
|
||||
assert instance.model.OBJ() == 8.
|
||||
|
||||
|
||||
def test_stab_generator_fixed_graph():
|
||||
from miplearn.problems.stab import MaxWeightStableSetGenerator
|
||||
gen = MaxWeightStableSetGenerator(w=uniform(loc=50., scale=10.),
|
||||
n=randint(low=10, high=11),
|
||||
density=uniform(loc=0.05, scale=0.),
|
||||
fix_graph=True)
|
||||
instances = gen.generate(1_000)
|
||||
weights = np.array([instance.weights for instance in instances])
|
||||
weights_avg_actual = np.round(np.average(weights, axis=0))
|
||||
weights_avg_expected = [55.0] * 10
|
||||
assert list(weights_avg_actual) == weights_avg_expected
|
||||
|
||||
|
||||
def test_stab_generator_random_graph():
|
||||
from miplearn.problems.stab import MaxWeightStableSetGenerator
|
||||
gen = MaxWeightStableSetGenerator(w=uniform(loc=50., scale=10.),
|
||||
n=randint(low=30, high=41),
|
||||
density=uniform(loc=0.5, scale=0.),
|
||||
fix_graph=False)
|
||||
instances = gen.generate(1_000)
|
||||
n_nodes = [instance.graph.number_of_nodes() for instance in instances]
|
||||
n_edges = [instance.graph.number_of_edges() for instance in instances]
|
||||
assert np.round(np.mean(n_nodes)) == 35.
|
||||
assert np.round(np.mean(n_edges), -1) == 300.
|
@ -1,31 +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 LearningSolver
|
||||
from miplearn.problems.stab import MaxStableSetInstance, MaxStableSetGenerator
|
||||
import networkx as nx
|
||||
import numpy as np
|
||||
|
||||
|
||||
def test_stab():
|
||||
graph = nx.cycle_graph(5)
|
||||
weights = [1.0, 2.0, 3.0, 4.0, 5.0]
|
||||
instance = MaxStableSetInstance(graph, weights)
|
||||
solver = LearningSolver()
|
||||
solver.solve(instance)
|
||||
assert instance.model.OBJ() == 8.0
|
||||
|
||||
|
||||
def test_stab_generator():
|
||||
graph = nx.cycle_graph(5)
|
||||
base_weights = [1.0, 2.0, 3.0, 4.0, 5.0]
|
||||
instances = MaxStableSetGenerator(graph=graph,
|
||||
base_weights=base_weights,
|
||||
perturbation_scale=1.0,
|
||||
).generate(100_000)
|
||||
weights = np.array([instance.weights for instance in instances])
|
||||
weights_avg = np.round(np.average(weights, axis=0), 2)
|
||||
weights_std = np.round(np.std(weights, axis=0), 2)
|
||||
assert list(weights_avg) == [1.50, 2.50, 3.50, 4.50, 5.50]
|
||||
assert list(weights_std) == [0.29] * 5
|
Loading…
Reference in new issue