-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestAlgorithm.py
78 lines (60 loc) · 2.94 KB
/
testAlgorithm.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
72
73
74
75
76
77
78
from script import *
global trials, iterations, tweak
# trials*iterations is the total number of times the "good" values are refined
trials = 100000
iterations = 50
tweak = 0.005
def runTests(numIterations, lone, ltwo, lthree, count, passedTorque):
#arrays keeping track of lengths, torque, and the y-values associated with the lengths in the 3 positions
resultsLen = []
resultsTorque = []
#position 1 y-values (joints 1 and 2)
resultsYVal1 = []
resultsYVal2 = []
#position 2 y-values (joints 1 and 2)
resultsYVal3 = []
resultsYVal4 = []
#position 3 y-values (joints 1 and 2)
resultsYVal5 = []
resultsYVal6 = []
for i in range(numIterations):
#uniformly selects random values to increment or decrement each respective size
l1 = np.random.uniform(low=-tweak, high=tweak, size=None)
l2 = np.random.uniform(low=-tweak, high=tweak, size=None)
l3 = np.random.uniform(low=-tweak, high=tweak, size=None)
testLengths = [l1+lone, l2+ltwo, l3+lthree]
resultsLen.append(testLengths)
# try and catch to handle bugs sourced from script.py
try:
resultsTorque.append(calculateTorque2(testLengths))
except:
resultsTorque.append(errorConst)
resultsYVal1.append(positionPoints[0][1][1])
resultsYVal2.append(positionPoints[0][1][2])
resultsYVal3.append(positionPoints[1][1][1])
resultsYVal4.append(positionPoints[1][1][2])
resultsYVal5.append(positionPoints[2][1][1])
resultsYVal6.append(positionPoints[2][1][2])
minTorqueIndex = 0
# searches through array of torques and finds the lowest "legal" torque value and its properties (length's)
for i in range(numIterations):
if (resultsYVal1[i] >= 0 and resultsYVal2[i] >= 0 and resultsYVal3[i] >= 0 and resultsYVal4[i] >= 0 and resultsYVal5[i] >= 0 and resultsYVal6[i] >= 0):
if (resultsTorque[i] < resultsTorque[minTorqueIndex]):
minTorqueIndex = i
else:
resultsTorque[i] = errorConst
# output best lengths and torque for this iteration
print(resultsLen[minTorqueIndex])
print(resultsTorque[minTorqueIndex])
# if a torque that is better is found, use the new lengths for the next iteration
if (count<iterations and resultsTorque[minTorqueIndex] < passedTorque):
runMoreTests(resultsLen[minTorqueIndex], count, resultsTorque[minTorqueIndex])
# if not use the previous lengths
elif (count<iterations and resultsTorque[minTorqueIndex] > passedTorque):
runMoreTests(resultsLen[minTorqueIndex], count, passedTorque)
# helper function just to count iterations
def runMoreTests(newLengths, count, passedTorque):
count += 1
runTests(trials,newLengths[0],newLengths[1],newLengths[2], count, passedTorque)
# function call (input the three middle values)
#runTests(trials, 1.066876903993271, 1.0501579075251466, 0.6433084769483223, 0, 50 )