-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainingArray.py
48 lines (32 loc) · 1.55 KB
/
trainingArray.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
import random
from collections import namedtuple
'''The trainingArray class holds the functionality to create all of the training and testing data that is required'''
trial_run = namedtuple('trial_run', ['inputs', 'solution'])
class trainingArray:
def __init__(self, n, examples):
self.n = n
self.examples = examples
def createTrainingData(self):
''' Loop through the specified number of examples. For each
iteration, generate n random inputs to the Rosenbrock function,
calculate the output, store both in an array, return the array. '''
trainingData = []
for i in range(self.examples):
trainingData.append(self.createInstance())
return trainingData
def createInstance(self):
''' Produce one randomized input and solution to the Rosenbrock
function. Return an tuple of the array of inputs and the solution. '''
functionInputs = []
for i in range(self.n):
functionInputs.append(random.uniform(-1, 1)) #update this based on bounds of function
solution = self.solveRosenbrock(functionInputs)
return trial_run(functionInputs, solution)
def solveRosenbrock(self, inputs):
''' Given an array of inputs, return the solution to the
Rosenbrock function. '''
functionSum = 0
for i in range(len(inputs) - 1):
tempSum = ((1 - inputs[i]) ** 2) + (100 * ((inputs[i + 1] - (inputs[i] ** 2)) ** 2))
functionSum += tempSum
return functionSum