-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_instance.py
49 lines (43 loc) · 1.74 KB
/
generate_instance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import numpy as np
import random
from symmetric_matrix import SymmetricMatrix
def generate_random_instance(n, max_demand, sparsity, integer=False, seed=None):
"""
Generates a random instance of ring loading of the given size n.
:param sparsity: probability in [0, 1] that any demand is zero
:param max_demand: maximum possible demand, not necessarily assumed
:param n: ring size, i.e. number of nodes
:param integer: whether the instance should consist of integer numbers only
:return: SymmetricMatrix containing generated demands
"""
assert 0 <= sparsity <= 1
np.random.seed(seed)
a = np.random.rand(n, n) * max_demand
a = (a + a.T) / 2
np.fill_diagonal(a, 0)
if integer:
a = a.astype(np.int)
sym_matrix = SymmetricMatrix(n, initial_values=a)
rng = np.random.default_rng()
zero_indices = rng.choice(n*(n-1)//2, int(sparsity * n*(n-1) / 2), replace=False)
triu_indices = np.asarray([(i, j) for i in range(n) for j in range(i+1, n)])
zero_indices = triu_indices[zero_indices]
sym_matrix[zero_indices[:, 0], zero_indices[:, 1]] = 0
return sym_matrix
def demands_to_list(n, demands, seed=None):
"""
Converts the demands in a SymmetricMatrix into a list of containing tuples of type (i, j, d_{ij}).
The list only contains the non-zero demands.
:param seed: seed used for shuffling demand list
:param n: ring size
:param demands: SymmetricMatrix containing demands
:return: list of non-zero demands
"""
demands_list = []
for i in range(n):
for j in range(i + 1, n):
if demands[i, j] != 0:
demands_list.append((i, j, demands[i, j]))
random.seed(seed)
random.shuffle(demands_list)
return demands_list