-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapr_2D_SS_py3_ver10.py
220 lines (168 loc) · 5.3 KB
/
apr_2D_SS_py3_ver10.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
import math
import operator
import time
import random
import sys
import bisect
from aux_functions import *
hit_sum=0
L_sum=0
## trim
def trim_2D_10(inL, mu):
"""
inL must have the format:
column 0, x coord
column 1, y coord
column 2, length
column 3, angle
column 4, list of indexes
"""
t = time.process_time()
#sort according to the vectors length
sorted_L=inL
#we suppose the list is already sorted
N=len(inL)
N2=N
R=[]
#count the hits, how many vectors are removed
max_j=0
sum_j=0
hit_cnt=0
max_win=0
i=0
A = []
#leave out very short vectors that do not create a radius more than 1
x=sorted_L[0]
while i<N-1 and x[2]<1/mu:
if R==[]:
R.append(x)
else:
#add x only if the other vectors in R are different from x
r=len(R)-1
add_x_flag= True
while r>=0 and R[r][2]==x[2]:
if R[r][0]==x[0] and R[r][1]==x[1]:
add_x_flag = False
break
else:
r=r-1
if add_x_flag==True:
R.append(x)
x = sorted_L[i]
i=i+1
#^^ while
i=i-1
if i<0:
i=0
j=i
r=0
if R==[]:
x=sorted_L[i]
R.append[x]
i=1
while i<N:
x=sorted_L[i]
r=bin_search_2D(R, int(x[2]/(1+mu)), 2, r)
if len(R)-r>max_win:
max_win=len(R)-r
#at this point, r indicates the the first vector v (the shortest)
#in whose zone x belongs
add_x_flag=True
#check the vector in R.All these vector have the proper length.
#If one has also the rigth angle, do not add x.
#print(r,len(R))
for j in range(r,len(R)):
if R[j][3]-mu <x[3]< R[j][3]+mu:
add_x_flag=False
break
if add_x_flag==True:
R.append(x)
i=i+1
#^while i<N
#hit_percent = round(N2/(len ) ,2)
#print("\nin trim:original size=",N2 ," hits=",N2-len(R), "len(ret)=",len(R))
#print("percentage of deleted items=",round((N2-len(R))/N2*100,2),"%")
print("maximum window is:", max_win,"len(L_i)=",len(R))
elap_t = time.process_time() - t
print("time elapsed=",elap_t)
#print(R)
return R
#
#---------------------------------------------------------------
#---------------------------------------------------------------
#
## SS_aprox
def D2_SS_approx( inList,
#s=0, #target value
c=0.2 #the error 0<c<1
):
x_list=[] #x coordinate
y_list=[] #y coordinate
len_list=[] #length
p_list=[]
max_len=0
cnt=0
#points_list: every x in p_list is 5 numbers, its x and y coordinate,
# its length,its angle in radians and its index
index=0
for i in inList:
i_len = (i[0]**2+i[1]**2)**(0.5)
i_angle = angle_2D([i[0],i[1]])
max_len = max_len+i_len
#len_list.append( i_len )
p_list.append([i[0],i[1],i_len,i_angle,cnt])
cnt=cnt+1
#print(p_list,'<>')
#print(y_list,'<>',sum(y_list))
#print(len_list,'<>',sum(len_list))
N = len(inList) # number of values
M = max([max(abs(x[0]),abs(x[1])) for x in inList])
#print("\nin approx_Subset sum: target=",s ,", error=",c,"and number of elements=", N)
#print("also, sum of input is=" , sum(x_list),'\n%%\t%%\t%%\t%%\n')
#t=time.process_time()
p_list_2= sort_by_col(p_list,2)
max_len_hits=0
max_len_i=0
S = [(0,0,0,0, [])]
for x in p_list_2:
t = time.process_time()
T = []
len_x=x[2]
max_boundary = max_len-max_len_i+c*M/N
for Sx, Sy, Sl, Sa, tmp_list in S:
Tx = x[0]+Sx
Ty = x[1]+Sy
Tl = (Tx**2+Ty**2)**(0.5)
Ta = angle_2D([Tx,Ty])
if Tl<max_len/2:
if Tl<max_boundary:
T.append( (Tx, Ty, Tl, Ta, sorted(tmp_list + [x[4]])) )
else:
T.append( (Tx, Ty, Tl, Ta, sorted(tmp_list + [x[4]])) )
max_len_hits=max_len_hits+1
else:
T.append( (Tx, Ty, Tl, Ta, sorted(tmp_list + [x[4]])) )
max_len_hits=max_len_hits +1
elap_t = time.process_time() - t
U = T + S
#print(index,"::")
index=index+1
#print("max_len_hits=",max_len_hits)
#print("\tlen(U) before trimming:\t",len(U))
max_len_hits=0
U = sort_by_col(U, 2)
#print("largest length in U",U[len(U)-1][2])
#print("difference =",max_boundary-U[len(U)-1][2])
#trim the list U
S = trim_2D_10(U, c/(float(N)**2))
#position, res[1], it has the numbers that achieve this result as an array
#res = sort_by_col(S, 2)
res = S
i=0;
for x in res:
#print(i,":",x)
i=i+1
if i>20:
break
return S
############################################################