-
Notifications
You must be signed in to change notification settings - Fork 2
/
random_allocation_function.py
276 lines (247 loc) · 8.27 KB
/
random_allocation_function.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
import numpy as np
import math
from math import sqrt, sin, cos, pi, floor, ceil
import matplotlib.pyplot as plt
import random
def random_point_allocation(data):
#Calling Event ID API
def get_event_id():
from datetime import datetime
import requests
dd=datetime.now()
time=dd.strftime("%d:%m:%Y,%H:%M:%S")
url="https://100003.pythonanywhere.com/event_creation"
data={
"platformcode":"FB" ,
"citycode":"101",
"daycode":"0",
"dbcode":"pfm" ,
"ip_address":"192.168.0.41",
"login_id":"lav",
"session_id":"new",
"processcode":"1",
"regional_time":time,
"dowell_time":time,
"location":"22446576",
"objectcode":"1",
"instancecode":"100051",
"context":"afdafa ",
"document_id":"3004",
"rules":"some rules",
"status":"work",
"data_type": "learn",
"purpose_of_usage": "add",
"colour":"color value",
"hashtags":"hash tag alue",
"mentions":"mentions value",
"emojis":"emojis",
}
r=requests.post(url,json=data)
return r.text
#Calling Dowell Connection
def dowell_connection(field):
import json
import requests
url = "http://100002.pythonanywhere.com/"
payload = {
"cluster": "license",
"database": "license",
"collection": "licenses",
"document": "licenses",
"team_member_ID": "689044433",
"function_ID": "ABCDE",
"command":"insert",
"field":field,
"update_field": {
"order_nos": 21
},
"platform": "bangalore"
}
headers = {
'Content-Type': 'application/json'
}
response = requests.post( url, headers=headers, json=payload)
#Random Allocation Function
selected_type=data['selected_type']
rows=data['rows']
columns=data['columns']
selection=data['selection']
def FieldRP(columns, rows, selection, data):
N=data['N']
length= columns
width= rows
area=length*width #calculate area of rectangle
area_of_circle=area/N #calculate area of circle
radius=sqrt(area_of_circle/pi) #calculate the radius of circle
diameter=2*radius
coor=[[0,0],[-10,10],[-10,0],[-10,-10],[0,-10],[10,-10],[10,0],[10,10],[0,10]]
data_coor = np.array([coor])
x_coor, y_coor = data_coor.T
n_coor=list(range(1,10))
fig_coor, ax_coor = plt.subplots()
ax_coor.scatter(x_coor, y_coor)
for i, txt in enumerate(n_coor):
ax_coor.annotate(txt, (x_coor[i], y_coor[i]),fontsize=15)
#1st co-ordinate
if(selection==1):
list1=[[0,0]]
if(selection==2):
list1=[[-length/2,width/2]]
if(selection==3):
list1=[[-length/2,0]]
if(selection==4):
list1=[[-length/2,-width/2]]
if(selection==5):
list1=[[0,-width/2]]
if(selection==6):
list1=[[length/2,-width/2]]
if(selection==7):
list1=[[length/2,0]]
if(selection==8):
list1=[[length/2,width/2]]
if(selection==9):
list1=[[0,width/2]]
L=[] #List for collecting 12 points on circumference
i=0.0
while i<=2*pi:
[a,b]=[diameter*cos(i),diameter*sin(i)]
select1= [round(num) for num in [a,b]]
L.append(select1)
i+=pi/6
#Removing duplicate points
X = []
for i in L:
if i not in X:
X.append(i)
select=list1[0]
#Getting remaining N-1 co-ordinates
while len(list1)<N: #Loop will stop when it has N random unique co-ordinates
X1=[]
for i in range(len(X)):
X1.append([x + y for x, y in zip(X[i], select)])
X2=[] #Applying range & removing outliers
for [a,b] in X1:
if a >=-length/2 and b >=-width/2 and a<=length/2 and b<=width/2:
X2.append([a,b])
X1 = X2
select=random.choice(X1) #Choosing N random points, one loop at a time
list1.append(select)
Z = [] #Removing duplicates
for i in list1:
if i not in Z:
Z.append(i)
list1 = Z
# print("After",N,"th run, the list of points is:",list1)
#Plotting points
plt.figure(figsize=(6,6))
data = np.array([list1])
x, y = data.T
plt.scatter(x,y) #Co-ordinates
plt.plot(x,y) #Joins Co-ordinates using a line
# plt.show()
plt.savefig('static/images/new_plot.png')
#Annotation
n=list(range(1,N+1))
fig, ax = plt.subplots()
ax.scatter(x, y)
for i, txt in enumerate(n):
ax.annotate(txt, (x[i], y[i]))
return(list1)
def ExcelRP(columns, rows, selection):
length= columns
width= rows
# selection= selection
N=length
area=length*width #calculate area of rectangle
area_of_circle=area/N #calculate area of circle
radius=sqrt(area_of_circle/pi) #calculate the radius of circle
diameter=2*radius
# Getting co-ordinates of points on the larger circle
list1=[]
X = []
if(selection==0):
i=1
else:
i=selection
for j in np.arange(1,(width)+1):
X.append([i,j])
select=random.choice(X)
select= [round(num) for num in select] #Choosing 1st co=ordinate randomly
list1.append(select) #1st Co-ordinate
i=0.0
L=[] #List for collecting 360 points on circle
while i<=2*pi:
[a,b]=[diameter*cos(i),diameter*sin(i)]
select1=[round(a),ceil(b)]
select2=[round(a),floor(b)]
L.append(select1)
L.append(select2)
i+=pi/180
#Removing duplicate points
X = []
for i in L:
if i not in X:
X.append(i)
#Getting remaining N-1 co-ordinates
if(selection==0): #For ordered selection
for k in range(N-1): #For N random unique co-ordinates
X1=[]
c1=[]
for i in range(len(X)):
X1.append([x + y for x, y in zip(X[i], select)])
a1=X1[i][0]
if(a1==k+2):
c1.append(X1[i])
X2=[] #Applying range & removing outliers
for [a,b] in c1:
if a >=1 and b >=1 and a<=length and b<=width:
X2.append([a,b])
select=random.choice(X2) #Choosing N random points, one loop at a time
list1.append(select)
# print(list1)
else: #For random selection
column_nos=[selection]
columns=[item for item in range(1, N+1)]
select_init=select
while (column_nos!=columns):
X1=[]
for i in range(len(X)):
X1.append([x+y for x,y in zip(X[i],select_init)])
X2=[] #Applying range & removing outliers
for [a,b] in X1:
if a >=1 and b >=1 and a<=length and b<=width:
X2.append([a,b])
X1=X2
select_init=random.choice(X1)
if select_init[0] not in column_nos:
column_nos.append(select_init[0])
select=select_init
list1.append(select)
column_nos.sort()
# print(list1)
#Plotting points
plt.figure(figsize=(6,6))
data = np.array([list1])
x, y = data.T
plt.scatter(x,y) #Co-ordinates
plt.plot(x,y) #Joins Co-ordinates using a line
# plt.show()
plt.savefig('static/images/new_plot.png')
#Annotation
n=list(range(1,N+1))
fig, ax = plt.subplots()
ax.scatter(x, y)
for i, txt in enumerate(n):
ax.annotate(txt, (x[i], y[i]))
return(list1)
if (selected_type=='fieldrp'):
list1=FieldRP(columns, rows, selection,data)
if (selected_type=='excelrp'):
list1=ExcelRP(columns, rows, selection)
random_allocation_data={
'eventId':get_event_id(),
'type':selected_type,
'listOfPoints':list1
}
dowell_connection(random_allocation_data)
return (random_allocation_data)