def LJ(coord, sig=1, eps=1, pbc=False): vectors = distances.vectors(coord, pbc) dist = distances.distances(vectors) dist[dist != 0] = 1 / dist[dist != 0] D_att = 6 * sig**6 * dist**8 D_rep = -12 * sig**12 * dist**14 D = 4 * (eps * (D_att + D_rep))[:, :, None] * vectors return np.sum(D, axis=-2)
def coulomb(coord, q, eps0=1, pbc=False): vectors = distances.vectors(coord, pbc) dist = distances.distances(vectors) if dist.ndim != 0: dist[dist != 0] = 1 / dist[dist != 0] else: dist = 1 / dist return 1 / (4 * np.pi * eps0) * np.dot(dist, q)
def LJ(coord, eps=1, sig=1, pbc=False): vectors = distances.vectors(coord, pbc) dist = distances.distances(vectors) if dist.ndim != 0: dist[dist != 0] = 1 / dist[dist != 0] else: dist = 1 / dist pot_rep = np.dot(eps * sig**12, dist**12) pot_atr = np.dot(eps * sig**6, dist**6) pot = pot_rep - pot_atr return np.sum(pot, axis=-1)
def using_distance_to_original(features=all_features): f_distances = distances.distances(features, distance_metric) remix_distances = [] for feature in features: release = feature[0] if not is_remix(release): # Original version. Do nothing. continue original = str(int(float(release))) r_i = release_indices[release] o_i = release_indices[original] remix_distances += [(release, f_distances[r_i][o_i])] return sorted(remix_distances, key=lambda x: -1 * x[1])
def using_num_closer_than_original(features=all_features): f_distances = distances.distances(features, distance_metric) num_nearer_neighbors = [] for feature in features: release = feature[0] if not is_remix(release): # Original version. Do nothing. (Original has no base for comparison against.) continue original = str(int(float(release))) r_i = release_indices[release] o_i = release_indices[original] distance_to_original = f_distances[r_i][o_i] num_closer = 0 for i in range(len(f_distances[r_i])): if i == r_i or is_remix(features[i][0]): continue if f_distances[r_i][i] < distance_to_original: num_closer += 1 num_nearer_neighbors += [(release, num_closer)] return sorted(num_nearer_neighbors, key=lambda x: -1 * x[1])
def njt(list_of_sequences): tree = [] iteration = 0 stack = [] D_matrices = [] J_matrices = [] D_matrix = distances(list_of_sequences) D_matrices.append(D_matrix.copy()) while D_matrix.size > 4: J_matrix = njm(D_matrix).round(3) J_matrices.append(J_matrix) i, j = np.unravel_index(np.argmin(J_matrix, axis=None), J_matrix.shape) Dij = D_matrix[i, j] diu = (((1 / 2) * Dij) + ( (1 / 2) * (1 / (len(D_matrix) - 2)) * (np.sum(D_matrix[i, :]) - np.sum(D_matrix[j, :])))).__round__(3) dju = (Dij - diu).__round__(3) stack.append([i, j, diu, dju]) for k in range(len(D_matrix)): Dik = D_matrices[iteration][i, k] Djk = D_matrices[iteration][j, k] D_matrix[i, k] = ((1 / 2) * (Dik + Djk - Dij)).__round__(3) D_matrix = np.delete(D_matrix, j, axis=0) D_matrix = np.delete(D_matrix, j, axis=1) D_matrix[:, i] = D_matrix[i, :].T D_matrices.append(D_matrix.copy()) if D_matrix.size == 4: indices = [0, 1, 2] if i > j: indices.pop(i) indices.pop(j) else: indices.pop(j) indices.pop(i) Dlast = (D_matrices[iteration][j, indices[0]] - diu).__round__(3) stack.append([1, Dlast]) iteration += 1 return stack
how='outer', left_on=['Model #', 'Brand / Appliance', 'MSRP'], right_on=[str(fields_m[0]), str(fields_m[1]), str(fields_m[2])]) #outer merge of datasets if df_dw.empty == False: drop_cols = [col for col in df_dw.columns if 'Planogram' in col] #drop empty rows df_dw_master = df_dw.dropna(subset=drop_cols, how='all').reset_index(drop=True) #MASTER DATAFRAME df_dw_master["Brand / Appliance"] = df_dw_master[ "Brand / Appliance"].str.strip() df_dw_master2 = distances(df_dw_master, file_count, files2, fields1, fields2, fields3) correlation(df_dw_master2, file_count, files2, fields_corr, path_plot) df_dw_master3 = qmodel(df_dw_master2, file_count, files2, fields_q, path_plot, fields_pred) df_dw_master3.to_excel(outfile, 'Dishwashers', index=False) #outfile.save(); #%% # Refrigerators df0 = pd.read_excel(files2.loc[0, 'Filename'], sheet_name="Refrigerators") m_check = [col for col in df0.columns if 'MRSP' in col] if m_check: df0.rename(columns={m_check[0]: "MSRP"}, inplace=True) df_ref = df0[fields0]
def acs(clk, rst, a, b, y1, y2, z, selStateL, selDistL, selState, stateDist, weight, q=8, l=20, n=4, r=5): """ Add-Compare-Select top level. q -- accumulated distance width l -- first trellis length n -- received decoder signal width r -- extrinsic information width clk, rst -- in : clock and negative reset a, b, y1, y2 -- in : received decoder signals z -- in : extrinsic information selStateL -- in : selected state at t = L selDistL -- in : selected transition at selStateL selState -- out : selected state stateDist -- out : selected accumulated distances (per state) weight -- out : four weights sorted by transition code """ from2to = [ 0, 25, 6, 31, 8, 17, 14, 23, 20, 13, 18, 11, 28, 5, 26, 3, 4, 29, 2, 27, 12, 21, 10, 19, 16, 9, 22, 15, 24, 1, 30, 7 ] distance16 = [ Signal(intbv(0, 0, 4 * (2**(n - 1)) + (2**r))) for i in range(16) ] accDist8 = [Signal(intbv(0, 0, 2**q)) for i in range(8)] accDist32 = [Signal(intbv(0, 0, 2**q)) for i in range(32)] accDistDel32 = [[Signal(intbv(0, 0, 2**q)) for i in range(4)] for j in range(8)] accDistDel4 = [Signal(intbv(0, 0, 2**q)) for i in range(4)] selAccDistL = Signal(intbv(0, 0, 2**q)) delayer_i = [None for i in range(32)] distances_i0 = distances(a, b, y1, y2, z, distance16) accDist_i0 = accDist(clk, rst, accDist8, distance16, accDist32, q) for i in range(8): for j in range(4): delayer_i[i * 4 + j] = delayer(clk, rst, accDist32[from2to[i * 4 + j]], accDistDel32[i][j], l - 1, 0, 2**(q + 1)) mux8_i0 = mux8(accDistDel32[0], accDistDel32[1], accDistDel32[2], accDistDel32[3], accDistDel32[4], accDistDel32[5], accDistDel32[6], accDistDel32[7], selStateL, accDistDel4) mux4_i0 = mux4(accDistDel4[0], accDistDel4[1], accDistDel4[2], accDistDel4[3], selDistL, selAccDistL) sub_i0 = sub(accDistDel4[0], selAccDistL, weight[0]) sub_i1 = sub(accDistDel4[1], selAccDistL, weight[1]) sub_i2 = sub(accDistDel4[2], selAccDistL, weight[2]) sub_i3 = sub(accDistDel4[3], selAccDistL, weight[3]) accDistSel_i0 = accDistSel(accDist32, stateDist, accDist8, q) stateSel_i0 = stateSel(accDist8, selState, q) return distances_i0, accDist_i0, mux8_i0, mux4_i0, sub_i0, sub_i1, sub_i2, sub_i3, accDistSel_i0, stateSel_i0, delayer_i
def gen_paths(index_threats_list=random.sample(range(1, 3000), 50)): #Definição de variaveis peso_distancia = 1 peso_tempo = 0 vel_med_drone = 100 #km/h max_flight_time = 1 num_pontos = 8 #locais a serem visitados tempo_prep = 30 #min num_drone = 5 num_cluster = 10 max_carga_drone = 3000 #g peso_inseticida = 10 #g por ponto param_sec = 0.75 max_pontos = 15 #max_carga_drone/peso_inseticida if vel_med_drone != "" and max_flight_time != "": distancia = vel_med_drone * max_flight_time else: distancia = 100 #Get threat lat lon positions df = pd.read_pickle("positions.pkl") df = df.loc[index_threats_list, :] matriz_pontos = np.asmatrix(df) optimal = False k = 4 drone_paths = [] # colors = ['black', 'bo', 'co', 'go', 'yo', 'mo', 'ko'] while (not optimal): k = k + 1 #Clusterização labels = clusterization(matriz_pontos, k) df['labels'] = labels color = ['r', 'b', 'c', 'g', 'y', 'm', 'k'] unique_labels = unique(labels) plt.axis([-50, -75, 10, -15]) for i in unique_labels: plt.plot(df[df['labels'] == i][0], df[df['labels'] == i][1], color[i % len(color)] + 'o') plt.show() #Se tivermos mais que max_pontos if any([x > max_pontos for x in count_itens(df, unique_labels)]): continue # while any([x < 3 for x in count_itens(df, unique_labels)]): # labels = clusterization (matriz_pontos, k) # unique_labels = unique(labels) # plt.axis([-50, -75, 10, -15]) # for i in unique_labels: # plt.plot(df[df['labels'] == i][0], df[df['labels'] == i][1], color[i%len(color)]+ 'o') # plt.show() #Calcula distancias entre os pontos matrix_distances = distances(matriz_pontos) #para cada cluster for i in unique_labels: matrix_distances = distances((df[df['labels'] == i]).as_matrix()) #Se a distancia entre quaisquer dois pontos for maior que a maxima do drone, não continua if np.any([[(x > distancia and x != float('inf')) for x in y] for y in matrix_distances]): continue #starter_point = [round(random()) for x in range(num_pontos)] #matriz_conexoes = [[random() for x in range(num_pontos)] for y in range(num_pontos)] from random import randint colors = [] lp_list = [] for i in range(30): colors.append('%06X' % randint(0, 0xFFFFFF)) unique_labels.sort() size_clusters = count_itens(df, unique_labels) for label in unique_labels: if size_clusters[label] < 3: continue lp_list.append(label) base_distance = 1 cluster_points = (df[df['labels'] == label]).as_matrix() matrix_distances = distances(cluster_points) file = open('testfile' + str(label) + '.lp', 'w') #Função objetivo file.write('min: ' + str(peso_distancia) + 'dist_total; \n\n\n' ) #+ str(peso_tempo) + 'tempo_total \n\n\n') #minimizacao da distancia file.write('//Implementa a funcao objetivo \n') str_lp = 'dist_total >= ' + str(base_distance) for i in range(0, len(matrix_distances)): for j in range(0, len(matrix_distances)): if i != j: str_lp = str_lp + ' + ' + str( matrix_distances[i][j]) + 'X[' + str( i) + '][' + str(j) + ']' #str_lp = str_lp + ' + ' + str(9999999999) + 'X[' + str(i) + '][' + str(j) + ']' #else: # str_lp = str_lp + ' + ' + str(matrix_distances[i][j]) + 'X[' + str(i) + '][' + str(j) + ']' file.write(str_lp + ";\n") #Restrição de visitar cada coisa uma unica vez file.write('//Visitar cada ponto pelo menos uma vez \n') str_lp = '' for i in range(0, len(matrix_distances)): str_lp_total_neg = '1 >= ' str_lp_total_pos = '' for j in range(0, len(matrix_distances)): # str_lp = str_lp + ' + X[' + i + '][' + j + ']' if i != j: str_lp_total_pos = str( str_lp_total_pos) + ' + X[' + str(i) + '][' + str( j) + ']' str_lp_total_neg = str( str_lp_total_neg) + ' + X[' + str(i) + '][' + str( j) + ']' str_lp_total_pos = str_lp_total_pos + '>= 1' str_lp_total_neg = str_lp_total_neg file.write(str_lp_total_pos + ';\n') file.write(str_lp_total_neg + ";\n\n") file.write('\n') # file.write('//Visitar cada ponto uma unica vez \n') str_lp = '' for j in range(0, len(matrix_distances)): str_lp_total_neg = '1 >=' str_lp_total_pos = '' for i in range(0, len(matrix_distances)): if i != j: str_lp_total_pos = str( str_lp_total_pos) + ' + X[' + str(i) + '][' + str( j) + ']' str_lp_total_neg = str( str_lp_total_neg) + ' + X[' + str(i) + '][' + str( j) + ']' str_lp_total_pos = str_lp_total_pos + '>= 1' str_lp_total_neg = str_lp_total_neg file.write(str_lp_total_pos + ';\n') file.write(str_lp_total_neg + ";\n\n") file.write(str_lp + '\n') # # #str_lp_total_pos = str_lp_total_pos + '>=' + str(num_pontos) #str_lp_total_neg = str_lp_total_neg + '>= -' + str(num_pontos) # file.write('//Visitar exatamente um ponto duas vezes \n') # for j in range(0, len(matrix_distances)): for i in range(0, len(matrix_distances)): str_lp = '1 >= + X[' + str(j) + '][' + str( i) + '] + X[' + str(i) + '][' + str(j) + ']' file.write(str_lp + ';\n') for j in range(0, len(matrix_distances)): for i in range(0, len(matrix_distances)): str_lp = 'X[' + str(i) + '][' + str(j) + '] >= 0' file.write(str_lp + ';\n') pontoInicial = 0 # Seta ponto inicial file.write('V[' + str(pontoInicial) + '] = 1;\n') file.write('-1 >= -V[' + str(pontoInicial) + '];\n') #impede que haja subcaminhos que nao passam pela origem \n'); str_lp = '' for i in range(0, len(matrix_distances)): for j in range(0, len(matrix_distances)): if i != j: if j != pontoInicial: file.write('1 - X[' + str(i) + '][' + str(j) + '] + u[' + str(i) + '][' + str(j) + '] >= 1;\n') file.write( str(len(matrix_distances) - 2) + 'u[' + str(i) + '][' + str(j) + '] >= -1 - V[' + str(i) + '] + V[' + str(j) + '];\n') file.write( str(len(matrix_distances)) + ' - ' + str(len(matrix_distances)) + 'u[' + str(i) + '][' + str(j) + '] >= V[' + str(i) + '] - V[' + str(j) + '] + 1;\n') #Restricoes da capacidade do drone str_lp = str(10000000) + '>= dist_total' #1000 file.write(str_lp + ';\n') for i in range(0, len(matrix_distances)): for j in range(0, len(matrix_distances)): file.write('int X[' + str(i) + '][' + str(j) + '];\n') file.write('bin u[' + str(i) + '][' + str(j) + '];\n') file.close() # command = []; #[status,cmdout] = system(command) processes = set() max_processes = 10 tempo = 120 import os import time import shutil # import subprocess if os.path.isdir('output'): shutil.rmtree('output') os.makedirs('output') for label in lp_list: # status = os.system("lp_solve -s -timeout " + str(tempo) + " testfile" + str(label) + ".lp > saida" + str(label) + ".txt") processes.add( os.system("lp_solve -s -timeout " + str(tempo) + " testfile" + str(label) + ".lp > output/saida" + str(label) + ".txt")) if len(processes) >= max_processes: print("oi\n") time.sleep(.1) processes.difference_update( [p for p in processes if p.poll() is not None]) while (len([name for name in os.listdir('output')]) < len(lp_list)): print(len(processes)) drone_paths = [] for label in lp_list: cor = 'rgb(' + str(random.randint(0, 255)) + ',' + str( random.randint(0, 255)) + ',' + str(random.randint(0, 255)) + ')' cluster_points = (df[df['labels'] == label]).as_matrix() path_data = read_output("output/saida" + str(label) + ".txt") if path_data == -1: continue for i in range(len(path_data)): drone_paths.append( dict( type='scattermapbox', name='Cluster ' + str(label), lon=[ cluster_points[i][0], cluster_points[path_data[i].index(1)][0] ], lat=[ cluster_points[i][1], cluster_points[path_data[i].index(1)][1] ], mode='lines', line=dict( width=1, color=cor #colors[label%len(colors)], ), # opacity = float(cluster_points['cnt'][i])/float(df_flight_paths['cnt'].max()), )) optimal = 1 # layout = dict( # title = 'Optimal path between treats', # showlegend = False, # geo = dict( # projection=dict( type='azimuthal equal area' ), # showland = True, # landcolor = 'rgb(243, 243, 243)', # countrycolor = 'rgb(204, 204, 204)', # ), # ) #fig = dict( data=drone_paths , layout=layout ) #import plotly for label in unique_labels: if size_clusters[label] == 2: cor = 'rgb(' + str(random.randint(0, 255)) + ',' + str( random.randint(0, 255)) + ',' + str(random.randint( 0, 255)) + ')' cluster_points = (df[df['labels'] == label]).as_matrix() drone_paths.append( dict( name='Cluster ' + str(label), type='scattermapbox', lon=[cluster_points[0][0], cluster_points[1][0]], lat=[cluster_points[0][1], cluster_points[1][1]], mode='lines', line=dict( width=1, color=cor, ), # opacity = float(cluster_points['cnt'][i])/float(df_flight_paths['cnt'].max()), )) if size_clusters[label] == 1: cor = 'rgb(' + str(random.randint(0, 255)) + ',' + str( random.randint(0, 255)) + ',' + str(random.randint( 0, 255)) + ')' cluster_points = (df[df['labels'] == label]).as_matrix() drone_paths.append( dict(name='Cluster ' + str(label), type='scattermapbox', lon=cluster_points[0][0], lat=cluster_points[0][1], hoverinfo='text', text=str(cluster_points[0][0]) + "_" + str(cluster_points[0][1]), marker=dict(size=3, color=cor))) # from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot # plot( fig, filename='d3-path-brazil_1.html') result = [] result.append(drone_paths) result.append(len(size_clusters)) return result
def coulomb(coord, q, eps0=1, pbc=False): vectors = distances.vectors(coord, pbc) dist = distances.distances(vectors) dist[dist != 0] = 1 / dist[dist != 0]**3 D = dist[:, :, None] * vectors return q[:, None] * np.einsum("ijk, j", D, q)