-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgerador_test.py~
221 lines (163 loc) · 5.24 KB
/
gerador_test.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
# coding: utf-8
import sys
import math
import random
import matplotlib
import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt
import matplotlib.cm as cmx
from mpl_toolkits.mplot3d import Axes3D
tx = 10
ty = 10
tz = 10
sigmin = 3
sigmax = 6
X = [i for i in range(0,tx)]
Y = [i for i in range(0,ty)]
Z = [i for i in range(0,tz)]
numero_de_clusters = 5
ID = [i for i in range(len(X)*len(Y)*len(Z))]
blocosescolhidos = random.sample(ID, numero_de_clusters)
cluster_mu = []
cluster_sigma = []
for c in range(numero_de_clusters):
cluster_mu.append([random.uniform(0,tx),random.uniform(0,ty),random.uniform(0,tz)])
cluster_sigma.append([tx/random.uniform(sigmin,sigmax),ty/random.uniform(sigmin,sigmax),tz/random.uniform(sigmin,sigmax)])
print cluster_mu
print cluster_sigma
# blocos_coord = [(a,b,c) for a in X for b in Y for c in Z]
# dens = [0 for b in range(len(blocos_coord))]
# temp = []
# for c in range(numero_de_clusters):
# cov_matrix = np.diag(cluster_sigma[c])
# temp.append([dens[b] + st.multivariate_normal.pdf(b,cluster_mu[c],np.diag(cluster_sigma[c])) for b in range(len(blocos_coord))])
blocos_coord = [(a,b,c) for a in X for b in Y for c in Z]
#print blocos_coord
dens = [0 for b in range(len(blocos_coord))]
for c in range(numero_de_clusters):
cov_matrix = np.diag(cluster_sigma[c])
for b in range(len(blocos_coord)):
dens[b] = dens[b] + st.multivariate_normal.pdf(blocos_coord[b],cluster_mu[c],np.diag(cluster_sigma[c]))
def precList(b, Blocos,xmax,ymax,zmax):
prec = []
for dx in (-1,0,1):
if((b[0]+dx>=0) and(b[0]+dx<xmax)):
for dy in (-1,0,1):
if((b[1]+dy>=0) and(b[1]+dy<ymax)):
if(b[2]-1>=0):
prec.append(Blocos.index((b[0]+dx,b[1]+dy,b[2]-1)))
return prec
def criaPrecList(blocos_coord,tx,ty,tz):
PrecList = []
for b in range(len(blocos_coord)):
prec = precList(blocos_coord[b], blocos_coord,tx,ty,tz)
PrecList.append(prec)
return PrecList
def criaArqPrec(nfile,blocos_coord,tx,ty,tz):
with open(nfile, 'w') as prec_file:
for b in range(len(blocos_coord)):
prec = precList(blocos_coord[b], blocos_coord,tx,ty,tz)
prec_file.write('{}\t{}\t'.format(b,len(prec)))
for p in prec:
prec_file.write('{}\t'.format(p))
prec_file.write('\n')
def criaAPP(blocos_coord,dens,mxb,mnb,mxc):
dmin = min(dens)
dmax = max(dens)
benef = random.uniform(mnb,mxb)
benefList = [benef*((dens[b] - dmin)/(dmax-dmin)) for b in range(len(dens))]
ap_process = [-random.uniform(0,mxc)+benefList[b] for b in range(len(benefList))]
return ap_process
def criaArqBloco(nfile,blocos_coord,ap_process):
with open(nfile, 'w') as bfile:
for b in blocos_coord:
bid = blocos_coord.index(b)
bfile.write('{}\t{}\t{}\t{}\t0\t0\t0\t0\t0\t0\t{}\n'.format(bid,b[0],b[1],b[2],ap_process[bid]))
def UPIT(blocos_coord,value):
m = Model()
n = len(blocos_coord) # number of blocks
# Indicator variable for each block
x = {}
for i in range(n):
x[i] = m.addVar(vtype=GRB.BINARY, name="x%d" % i)
m.update()
# Set objective
m.setObjective(quicksum(value[i]*x[i] for i in range(n)), GRB.MAXIMIZE)
# Add constraints
for b in range(n):
u = edge[0]
v = edge[1]
print(u,v)
m.addConstr(x[u] <= x[v])
m.optimize()
approc = criaAPP(blocos_coord,dens,20,10,0.5)
criaArqBloco('test.bloc',blocos_coord,approc)
criaArqPrec('test.prec',blocos_coord,tx,ty,tz)
#print dens
# temp2 = [0 for b in range(len(blocos_coord))]
# for c in range(0, numero_de_clusters):
# for b in range(len(blocos_coord)):
# if(temp[c][b]!=0):
# temp2[b] = temp[c][b]
# print(temp2)
# for i in blocosescolhidos:
# for j in range(numero_de_clusters):
# for k in range(len(blocos_coord)):
# dens[i] = temp[j][k]
# print(blocosescolhidos)
# print(len(dens))
# indice = 0
# indice2 = 0
# for i in range(len(blocosescolhidos)):
# indice = blocosescolhidos[i]
# for k in range(numero_de_clusters):
# for l in range(len(blocos_coord)):
# if(temp[k+indice2][l] != 0):
# dens[indice] = temp[k][l]
# indice = indice + 1
# else:
# break
# indice2 = indice2 + 1
# break
# k = 0
# l = 0
# indice = 0
# for i in range(len(blocosescolhidos)):
# indice = blocosescolhidos[i]
# for v in temp[k]:
# if(v != 0):
# dens[indice] = dens[indice] + v
# indice = indice + 1
# else:
# k = k + 1
# break
# print(dens)
bx = [b[0] for b in blocos_coord]
by = [b[1] for b in blocos_coord]
bz = [b[2] for b in blocos_coord]
# cm = plt.get_cmap("Blues")
# fig = plt.figure()
# ax3d = fig.add_subplot(111, projection = '3d')
# ax3d.scatter(bx,by,bz,c = dens)
# plt.show()
def scatter3d(x,y,z, cs, colorsMap='YlOrRd'):
cm = plt.get_cmap(colorsMap)
cNorm = matplotlib.colors.Normalize(vmin=min(cs), vmax=max(cs))
scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm)
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(x, y, z, c=scalarMap.to_rgba(cs),marker = 'o', edgecolors = 'none')
scalarMap.set_array(cs)
fig.colorbar(scalarMap)
plt.show()
scatter3d(bx,by,bz,dens)
# #%% Create Color Map
# colormap = plt.get_cmap("Blues")
# norm = matplotlib.colors.Normalize(vmin=min(dens), vmax=max(dens))
# #%% 3D Plot
# fig = plt.figure()
# ax3D = fig.add_subplot(111, projection='3d')
# ax3D.scatter(bx, by, bz, c=colormap(norm(dens)), marker='o')
# # ax3D.scatter(bx, by, bz, s=10, c=colormap(dens), marker='o')
# plt.show()