Skip to content

Commit

Permalink
MAINT: Modify to use new random API in benchmarks/sparse_csgraph_matc…
Browse files Browse the repository at this point in the history
…hing.py
  • Loading branch information
charlotte12l committed Jun 12, 2021
1 parent 683c444 commit 70f8f28
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions benchmarks/benchmarks/sparse_csgraph_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def setup(self, n, density):
# Create random sparse matrices. Note that we could use
# scipy.sparse.rand for this purpose, but simply using np.random and
# disregarding duplicates is quite a bit faster.
np.random.seed(42)
d = np.random.randint(0, n, size=(int(n*n*density), 2))
rng = np.random.default_rng(42)
d = rng.integers(0, n, size=(int(n*n*density), 2))
graph = scipy.sparse.csr_matrix((np.ones(len(d)), (d[:, 0], d[:, 1])),
shape=(n, n))
self.graph = graph
Expand All @@ -31,29 +31,29 @@ def time_maximum_bipartite_matching(self, n, density):
# For benchmarking min_weight_full_bipartite_matching, we rely on some of
# the classes defined in Burkard, Dell'Amico, Martello -- Assignment Problems,
# 2009, Section 4.10.1.
def random_uniform(shape):
return scipy.sparse.csr_matrix(np.random.uniform(1, 100, shape))
def random_uniform(shape, rng):
return scipy.sparse.csr_matrix(rng.uniform(1, 100, shape))


def random_uniform_sparse(shape):
return scipy.sparse.random(shape[0], shape[1], density=0.1, format='csr')
def random_uniform_sparse(shape, rng):
return scipy.sparse.random(shape[0], shape[1], density=0.1, format='csr', random_state=rng)


def random_uniform_integer(shape):
return scipy.sparse.csr_matrix(np.random.randint(1, 1000, shape))
def random_uniform_integer(shape, rng):
return scipy.sparse.csr_matrix(rng.integers(1, 1000, shape))


def random_geometric(shape):
P = np.random.randint(1, 1000, size=(shape[0], 2))
Q = np.random.randint(1, 1000, size=(shape[1], 2))
def random_geometric(shape, rng):
P = rng.integers(1, 1000, size=(shape[0], 2))
Q = rng.integers(1, 1000, size=(shape[1], 2))
return scipy.sparse.csr_matrix(cdist(P, Q, 'sqeuclidean'))


def random_two_cost(shape):
return scipy.sparse.csr_matrix(np.random.choice((1, 1000000), shape))
def random_two_cost(shape, rng):
return scipy.sparse.csr_matrix(rng.choice((1, 1000000), shape))


def machol_wien(shape):
def machol_wien(shape, rng):
# Machol--Wien instances being harder than the other examples, we cut
# down the size of the instance by 5.
return scipy.sparse.csr_matrix(
Expand All @@ -71,15 +71,15 @@ class MinWeightFullBipartiteMatching(Benchmark):
]

def setup(self, shape, input_type):
np.random.seed(42)
rng = np.random.default_rng(42)
input_func = {'random_uniform': random_uniform,
'random_uniform_sparse': random_uniform_sparse,
'random_uniform_integer': random_uniform_integer,
'random_geometric': random_geometric,
'random_two_cost': random_two_cost,
'machol_wien': machol_wien}[input_type]

self.biadjacency_matrix = input_func(shape)
self.biadjacency_matrix = input_func(shape, rng)

def time_evaluation(self, *args):
min_weight_full_bipartite_matching(self.biadjacency_matrix)

0 comments on commit 70f8f28

Please sign in to comment.