-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup_testing_function.py
More file actions
77 lines (61 loc) · 2.61 KB
/
group_testing_function.py
File metadata and controls
77 lines (61 loc) · 2.61 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import numpy as np
import numpy.random as nrndm
import random as rndm
# this file contains the functions needed to generate an instance of a
# group testing decoding problem
# generate_input function generate the vector x, called input vector,
# which is at the same time the input to the Group Testing problem
# and the solution to be recovered
# n is the number of variables that need to be generate
# p_i is the probability that an item is faulty
def generate_input(n, p_i):
# generate a sparse input to recover
x = rndm.sample(range(1, n + 1), nrndm.binomial(n, p_i))
# counting number of faulty items
k = len(x)
return x, k
# generate_input_k function generate the vector x, called input vector,
# which is at the same time the input to the Group Testing problem
# and the solution to be recovered
# n is the number of variables that need to be generate
# k is the fixed number of faulty item
def generate_input_k(n, k, verbose=False):
if(verbose):
print("\ngenerating a random item vector with ", n ,"items and ", k ," defective items")
# generate a sparse input to recover
x = rndm.sample(range(1, n + 1), k)
return x
# generate_pool_matrix generate the recovery matrix, following a binomial model
# where each item has a probability to belong to a group
# the probability here is considered with respect to k number of faulty,
# this parameter should be known
# n is the number of elements of the input vector
# k is the number of faulty
# t is the number of groups (number of tests to be performed)
def generate_pool_matrix(n, k, t):
a = []
for i in range(t):
a.append(rndm.sample(range(1, n + 1), nrndm.binomial(n, (np.log(2) / k))))
# print("matrix generated, getting result")
return a
# get_results considering the input x, the pool matrix a return
# the results for the every group tested (a vector y)
# n is the number of elements of the input vector
# a is the recovery matrix
# t is the number of groups (number of tests to be performed)
# noiseless is a boolean, True if the problem is noiseless, False otherwise
# noise_probability is the probability that a test result is turned, considered if NOT noiseless
def get_results(t, a, x, noiseless, noise_probability):
# vector of the tests
y = [0] * t
# get result of test
if noiseless:
for i in range(t):
if any(x_i in a[i] for x_i in x):
y[i] = 1
else:
for i in range(t):
if any(x_i in a[i] for x_i in x):
y[i] = 1
y[i] = abs(y[i] - nrndm.choice(range(0, 2), p=[1 - noise_probability, noise_probability]))
return y