-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
58 lines (47 loc) · 1.45 KB
/
utils.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
from math import sqrt
from random import randint, uniform
def Distance(a, b):
return sqrt( (b.x-a.x)*(b.x-a.x) + (b.y-a.y)*(b.y-a.y) )
def SumDistance(points):
s = 0
for i in range(len(points)):
dist = Distance(points[i], points[(i+1) % len(points)])
s += dist
return s
def PickSelection(myList, probabilities):
i = 0
r = uniform(0, 1)
while r > 0:
r -= probabilities[i]
i += 1
i -= 1
return myList[i].copy()
def LexicalOrder(orderList):
x = -1
y = -1
# Step 1 : Find the largest x such that Order[x]<Order[x+1]
# (If there is no such x, Order is the last permutation.)
for i in range(len(orderList)):
if orderList[i] < orderList[(i+1)%len(orderList)]:
x = i
if x == -1:
return orderList
# Step 2 : Find the largest y such that Order[x]<Order[y].
for i in range(len(orderList)):
if orderList[x] < orderList[i]:
y = i
# Step 3 : Swap Order[x] and Order[y].
orderList[x], orderList[y] = orderList[y], orderList[x]
# Step 4 : Reverse Order[x+1 .. n].
RightSidereversed = orderList[x+1:][::-1]
orderList = orderList[:x+1]
orderList.extend(RightSidereversed)
# print(orderList)
return orderList
def translateValue(value, min1, max1, min2, max2):
return min2 + (max2 - min2)* ((value-min1)/(max1-min1))
def Factorial(n):
if n == 1:
return 1
else:
return n * Factorial(n - 1)