forked from cynliu98/Pixel_Array_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution_analysis.py
More file actions
141 lines (115 loc) · 4.14 KB
/
Copy pathsolution_analysis.py
File metadata and controls
141 lines (115 loc) · 4.14 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# UROP Summer 2017 - Spring 2018
# July 13, 17; June 4, 18
# A file for miscellaneous forms of solution analysis
import math
def readSolutions(fname):
f = open(fname, 'r') # open the file of solutions
sols = []; bcs = []
for l in f:
# print (l)
bcraw = l[(l.index('(')+1):(l.index(')'))]
bcraw = bcraw.split(',')
bc = []
for ele in bcraw:
ele.strip()
bc.append(float(ele))
if (bc not in bcs):
# print (bc)
bcs.append(bc)
unparsedSols = l[(l.index(':')+4):-2] # remove all brackets etc.
# print (unparsedSols)
bcsol = []
try:
while True:
i = unparsedSols.index(')') # while there are more solutions
unparsedSol = unparsedSols[:i]
solNums = unparsedSol.split(',')
sol = []
for n in solNums:
n.strip()
sol.append(float(n))
bcsol.append(sol)
unparsedSols = unparsedSols[(unparsedSols.index('(')+1):]
except:
sols.append(bcsol)
return sols, bcs
# Detect whether there are solutions indistinguishable
# from another, fixed solution
def detectSimilar(sol, dim, template):
assert len(sol) == len(template)
dif = dim[1] - dim[0]
maxdev = .5 * (len(sol)) * math.pow(dif, 2)
dev = 0
for j in range(len(sol)):
dev += math.pow(sol[j] - template[j], 2)
dev *= .5
if (dev <= maxdev): # not a unique solution
print (dev)
return True
return False
# Intersection of 2D arrays
def intersect(arr1, arr2):
tortn = []
for a in arr1:
if a in arr2:
tortn.append(a)
return tortn
# Returns all solutions that are shared among
# Two sets of solutions with the same bins
# Assume solutions have been read
def returnOverlap(sols1, bcs1, sols2, bcs2):
assert (len(sols1) == len(bcs1)) # solution is 2D array, each element with solutions corresponds to a boundary
bcs = intersect(bcs1, bcs2)
overlaps = []
for i in range(len(bcs)):
bc = bcs[i]
ind1 = bcs1.index(bc); ind2 = bcs2.index(bc)
overlaps.append(intersect(sols1[ind1], sols2[ind2]))
return overlaps, bcs
# "L2 norm" calculator
# Adjusted for the nature of PA
def L2(sol,a,b,m):
ideal = [((m+1-i)*a+i*b)/(m+1) for i in range (1,m+1,1)]
L2 = 0
for i in range(len(sol)):
L2 += (sol[i] - ideal[i])**2
return math.pow(L2,.5)
def main():
''' # detecting whether solutions were similar to 0
stuff = readSolutions('solutions_negative_bins.txt')
template = [0]*20
dim = [-1.0 + i*.05 for i in range(41)]
for s in stuff:
if detectSimilar(s, dim, template):
print ("We found a similar solution")
print (s)
return
'''
''' # do both types of rounding, return overlapping (shared) solutions
sols1, bcs1 = readSolutions('Round_1_Testing.txt')
sols2, bcs2 = readSolutions('Round_1_Testing_Down.txt')
overlaps, bcs = returnOverlap(sols1, bcs1, sols2, bcs2)
numfulfilled = 0
for i in range(len(bcs1)):
print ("Value for bc's (" + str(bcs[i][0]) + ", " + str(bcs[i][1]) + "): ")
+ str(overlaps[i]))
if (len(overlaps[i])):
numfulfilled += 1
print ("The number of fulfilled boundary conditions was: " + str(numfulfilled))
'''
worstL = [-1,-1,-1] # the worst 3 L2 values
worstsols = [[],[],[]] # the worst 3 solutions
sols, bcs = readSolutions('sample2_for_analysis.txt')
for i in range(len(bcs)):
for j in range(len(sols[i])):
val = L2(sols[i][j],bcs[i][0],bcs[i][1],8)
if val > worstL[2] and not(val in worstL):
worstL.append(val); worstsols.append(sols[i][j])
worstL.sort(reverse=True); worstsols.sort(reverse=True)
print (worstsols)
worstL = worstL[0:3]; worstsols = worstsols[0:3]
print ("The worst 3 L2 norms were: " + str(worstL))
print ("The worst 3 solutions corresponding to those norms were: ")
for sol in worstsols:
print (sol)
main()