-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutility.py
executable file
·288 lines (212 loc) · 7.42 KB
/
utility.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""
##########################################################
### @Author Joe Krall ###############################
### @copyright see below ###############################
This file is part of JMOO,
Copyright Joe Krall, 2014.
JMOO is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
JMOO is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with JMOO. If not, see <http://www.gnu.org/licenses/>.
### ###############################
##########################################################
"""
"Brief notes"
"Bunch of often used functions"
import math
import numpy
def avg(list):
return (float)(sum(list)) / (float)(len(list))
def sum(list):
sum = 0;
for i in list:
sum += i;
return sum
def median(list):
return getPercentile(list, 50)
def spread(list):
return getPercentile(list, 75) - getPercentile(list, 25)
def mul(list):
sum = list[0]
for i in list[1:]:
sum *= i
return sum
def var(list):
mean = avg(list)
squared_diffs = []
for i in list:
squared_diffs.append((i - mean) ** 2)
return ((float)(sum(squared_diffs)) / (float)(len(list) - 1))
def avg2(list, index):
"return the average of a particular column from an array of arrays"
newlist = []
for i in list:
newlist.append(i[index])
return avg(newlist)
def matrix_avg(matrix):
vals = []
averages = []
# populate vals with empty frames
for i in range(len(matrix[0])):
vals.append([])
# populate vals with matrix Data
for i, row in enumerate(matrix):
for j, col in enumerate(row):
vals[j].append(col)
# compute averages of each column
for i, col in enumerate(vals):
averages.append(avg(col))
return averages
def matrix_var(matrix):
vals = []
variances = []
# populate vals with empty frames
for i in range(len(matrix[0])):
vals.append([])
# populate vals with matrix Data
for i, row in enumerate(matrix):
for j, col in enumerate(row):
vals[j].append(col)
# compute averages of each column
for i, col in enumerate(vals):
variances.append(var(col))
return variances
def getPercentile(list, percentile):
import math
# sort the list
list1 = sorted(list)
k = (len(list1) - 1) * (percentile / 100.0)
f = math.floor(k)
c = math.ceil(k)
if f == c:
val = list1[int(k)]
else:
d0 = list1[int(f)] * (c - k)
d1 = list1[int(c)] * (k - f)
val = d0 + d1
return val
def avg(list):
return (float)(sum(list)) / (float)(len(list))
def sum(list):
sum = 0;
for i in list:
sum += i;
return sum
def var(list):
mean = avg(list)
squared_diffs = []
for i in list:
squared_diffs.append((i - mean) ** 2)
return ((float)(sum(squared_diffs)) / (float)(len(list) - 1))
def dist(A, B):
return sum([(a - b) ** 2 for a, b in zip(A, B)]) ** 0.5
def pretty_print(matrix):
for row in matrix:
s = ""
for val in row:
s += str('%15.3f' % (val)) + ", "
print s
def getFront(problem, population):
# fitnesses = [list(x) for x in set(tuple(x) for x in fitnesses)]
myList = []
for i, ind in enumerate(population):
for d, decision in enumerate(problem.decisions):
decision.value = ind.decisionValues[d]
if True: # not problem.evalConstraints():
myList.append(population[i])
# for i,pop in enumerate(population):
# print pop.decisionValues, sum(problem.evalConstraintOverages(pop.decisionValues)), problem.evalConstraints(pop.decisionValues)
# Sort the list in either ascending or descending order of X
myList = sorted(myList, key=lambda pop: pop.fitness.fitness)
# Start the Pareto frontier with the first value in the sorted list
p_front = []
for pair in myList:
if pair.valid and not problem.evalConstraints(pair.decisionValues):
p_front = [pair]
break
# Loop through the sorted list
for pair in myList[1:]:
if pair.valid and pair.fitness.fitness[1] <= p_front[-1].fitness.fitness[1] and not problem.evalConstraints(
pair.decisionValues):
p_front.append(pair)
area = 1
return p_front, area
def pareto_frontier_multi(myArray):
# fitnesses = [list(x) for x in set(tuple(x) for x in fitnesses)]
# Sort on first dimension
myArray = myArray[myArray[:, 0].argsort()]
# Add first row to pareto_frontier
pareto_frontier = myArray[0:1, :]
# Test next row against the last row in pareto_frontier
for row in myArray[1:, :]:
if sum([row[x] >= pareto_frontier[-1][x]
for x in range(len(row))]) == len(row):
# If it is better on all features add the row to pareto_frontier
pareto_frontier = numpy.concatenate((pareto_frontier, [row]))
return pareto_frontier
def normalize(x, min, max):
tmp = float((x - min)) / \
(max - min + 0.000001)
if tmp > 1:
return 1
elif tmp < 0:
return 0
else:
return tmp
def loss(x1, x2, mins=None, maxs=None):
# normalize if mins and maxs are given
if mins and maxs:
x1 = [normalize(x, mins[i], maxs[i]) for i, x in enumerate(x1)]
x2 = [normalize(x, mins[i], maxs[i]) for i, x in enumerate(x2)]
o = min(len(x1), len(x2)) # len of x1 and x2 should be equal
return sum([math.exp((x2i - x1i) / o) for x1i, x2i in zip(x1, x2)]) / o
def loss2(x1, pop, mins=None, maxs=None, weights=None): # removal of pop-x1 vs pop
k = len(weights)
norms = [(min, max) for min, max in zip(mins, maxs)]
# Calculate the loss in quality of removing fit1 from the population
F = []
for X2 in pop:
if not X2[0] == '?':
F.append(-k / (sum(
[-math.exp(-w * (normalize(p2, n[0], n[1]) - normalize(p1, n[0], n[1])) / k) for w, p1, p2, n in
zip(weights, x1, X2, norms)])))
F1 = sum(F)
return F1
def cdom(x1, x2, mins=None, maxs=None):
return loss(x1, x2, mins, maxs) / loss(x2, x1, mins, maxs)
def spacing(dataset, distance_m):
dim = len(dataset[0])
# d_ = []
# for i, fit_i in enumerate(dataset):
# fma = []
# for j, fit_j in enumerate(dataset):
# if not i == j:
# fma.append(sum([abs(fit_i[k] - fit_j[k]) for k in range(dim)]))
# d_.append(min(fma))
d_bar = avg(distance_m)
ssm = ((1 / float(len(dataset) - 1)) * sum([(d_bar - d_i) ** 2 for d_i in distance_m])) ** 0.5
return ssm
def callrdivdemo(eraCollector):
keylist = eraCollector.keys()
variant = len(keylist)
rdivarray = []
for y in xrange(variant):
temp = eraCollector[keylist[y]]
temp.insert(0, str(keylist[y]))
rdivarray.append(temp)
rdivDemo(rdivarray)
"""
def losstest():
x1 = [0.1, 0.1, 0.1, 0.1]
x2 = [9, 6, 5, 4]
x3 = [0.8, 0.6, 0.5, 0.4]
x4 = [8, 6, 5, 4]
x5 = [0.4, 0.5, 0.4, 0.3]
x6 = [4, 5, 4, 3]
"""