-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlp_utils.py
172 lines (148 loc) · 5.13 KB
/
lp_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
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
#python3 lp_utils.py
from decimal import *
import numpy as np
def translate(patA,to_lan,paths=None):
"""
Function to translate from the different fromats represent the paths.
to_lan takes either 'to_O' as argument to translate paths of the form EE..NEN.. to the form
001 011 110...; or takes 'to_A' to do the reverse translation.
"""
if to_lan == "to_O":
num_0 =0
num_1 =0
for i in patA:
if i==1 or i=="R" or i=="N":
num_1 += 1
elif i==0 or i=="D" or i=="E":
num_0 += 1
assert num_0 >= num_1
patO = [[0,0,0] for i in range(num_0+num_1)]
for i in range(len(patO)):
if i!= len(patA)-1:
if patA[i] == 0 or patA[i] == "E" or patA[i] == "D":
patO[i][2] = 0
patO[i+1][0] = patO[i][0] + 1
patO[i+1][1] = patO[i][1]
else:
patO[i][2] = 1
patO[i+1][0] = patO[i][0]
patO[i+1][1] = patO[i][1] +1
else:
if patA[i] == 0 or patA[i] == "E" or patA[i] == "D":
patO[i][2] = 0
else:
patO[i][2] = 1
return patO
elif to_lan == "to_A":
assert paths is not None, "must pass the paths as argument"
patO = []
for term in paths[patA]:
patO.append(term[2])
return patO
def nsoftmax(x):
"""
Takes an array or list, the returns a softmax version of the list.
"""
soft = []
for i in x:
soft.append(Decimal(2.7)**Decimal(i))
sumo = sum(soft)
for i in range(len(soft)):
soft[i]/=sumo
return soft
def softmax(x):
"""
Numpy softmax()
"""
exps = np.exp(x)
return exps/np.sum(exps)
def normalize(x):
"""
Normalizes the list x to values between 0 and 1.
"""
sumo=sum(x)
return [i/sumo for i in x]
def elem_wise_mult(va, vb, a, b):
"""
This is actually just the element-wise multiplication of two vectors va and vb,
scaled by a and b respectively.
"""
vc = []
try:
assert len(va) == len(vb)
except:
raise Exception("different list sizes: size(va) = ", len(va), "size(vb) = ", len(vb))
for i in range(len(va)):
vc.append(va[i]*a + vb[i]*b)
return vc
def bubble_sort(pivot, b):
"""
Bubble sort in unison two list using pivot, which must be a quantitative list(float or int),
as the list on which to base the comparisons.
"""
assert len(pivot)==len(b)
for i in range(len(pivot)):
for j in range(len(pivot)-i-1):
if pivot[j]<=pivot[j+1]:
temp = pivot[j]
temp1 = b[j]
pivot[j] = pivot[j+1]
b[j] = b[j+1]
pivot[j+1] = temp
b[j+1]= temp1
return (pivot, b)
def quick_sort(array):
"""
Quicksort a list of Genome objects using their fitnesses.
"""
if (len(array))<2:
return array
else:
pivot_point = int(len(array)/2)
pivot = array[pivot_point]
x =pivot.fitness()[0]
truncated_array = array[:pivot_point] + array[pivot_point+1:]
less = [i for i in truncated_array if i.fitness()[0] < x]
greater = [i for i in truncated_array if i.fitness()[0]>=x]
to_return = quick_sort(greater) + [pivot] + quick_sort(less)
assert(len(to_return)) == len(array)
return to_return
def generate_paths(current_row, current_col, path,m,n,paths = []):
"""
A recursive function to generate all the paths of an m by n lattice.
"""
# check if the current position is the bottommost right corner
if current_row == m and current_col == n:
# if the current position is the bottommost right corner, add the current path to the list of paths
paths.append(path)
return
# check if we can move down
if current_row < m:
# if we can move down, generate the paths by moving down
generate_paths(current_row + 1, current_col, path + "D",m,n,paths)
# check if we can move right
if current_col < n:
# if we can move right, generate the paths by moving right
generate_paths(current_row, current_col + 1, path + "R",m,n, paths)
def generate_all_paths(m,n):
paths = []
real_paths = []
#you can use while loop to make sure all paths are created
max_len = int(combination(m+n,n))
generate_paths(0,0,"", m,n, paths)
for p in paths:
real_paths.append(translate(p,"to_O"))
assert max_len == len(real_paths)
return real_paths
def factorial(n):
"""
Calculates n factorial. n!
"""
if n == 1 or n==0:
return 1
else:
return n * factorial(n-1)
def combination(m,n):
"""Calculates n combinations of m"""
assert m>=n
return factorial(m)/(factorial(n)*factorial(m-n))