-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplicit_sample_generator.py
71 lines (52 loc) · 1.81 KB
/
explicit_sample_generator.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import sys
import numpy.random as rd
class CalculatorFunction:
def __init__(self, function):
self.function = function
def calculate(self, variables):
return self.function(variables)
def generate_variable(lower, upper):
return lower + (upper - lower) * rd.random()
def pithagora(variables):
a = variables[0]
b = variables[1]
return a*a + b*b
def generate_sample_line(sample_domain, sample_codomain):
string = ' '.join([str(x) for x in sample_domain])
string += " " + str(sample_codomain)
return string
def filesave_domain_codomain(domain, codomain, filename):
file = open(filename, 'w')
size = len(domain)
sample_size = len(domain)
var_size = len(domain[0])
file.write("{0} {1}\n".format(sample_size, var_size))
for i in range(size):
sample_domain = domain[i]
sample_line = sample_domain
if codomain is not None:
sample_codomain = codomain[i]
sample_line = generate_sample_line(sample_domain, sample_codomain)
file.write(sample_line + "\n")
file.close()
def sample_generator(nSamples, nVars, lower, upper, calculator):
domain = []
codomain = []
function = CalculatorFunction(calculator)
for i in range(0, nSamples):
x = []
for j in range(nVars):
variable = generate_variable(lower, upper)
x.append(variable)
y = function.calculate(x)
domain.append(x)
codomain.append(y)
return domain, codomain
if __name__ == '__main__':
filename = sys.argv[1]
nSamples = int(sys.argv[2])
numberOfVars = int(sys.argv[3])
lower = int(sys.argv[4])
upper = int(sys.argv[5])
domain, codomain = sample_generator(nSamples, numberOfVars, lower, upper, pithagora)
filesave_domain_codomain(domain, codomain, filename)