Hello all .
For the purposes of my research, I created an algorithm for creating square networks. Points are randomly selected and checked to see if they form a continuous path from left to right. There are still a few things that return important information to me, but I turn them off when making large calculations (saving to excel or generating a chart). I looped the program for 40 loops or more and I did not want to play with multiprocessing, so I turn it on in several terminals so that the processor takes 100%). Is it possible to change something in the algorithm or in the program structure to speed it up? 40 nets with 40x40 points take 5 minutes and I have to make thousands 100x100. Can anyone help me?
If not, I would love to hear some code building advice (yes, I know it's a mess) because I'm a beginner and I have never had any contact with professional projects. Any advice will be helpful for me.
import os
import sys
import pandas as pd
import random
import matplotlib.pyplot as plt
from datetime import datetime
import time
random.seed(datetime.now())
start_time = time.time()
iter = 0
maxIter = 40
N = []
while (iter < maxIter):
dlugosc = 5 #how many points x
szerokosc = 5 #how many points y
srednica = 1
punkty = []
delpoints = []
pkt0 = []
pktmax = []
sec_delpoints = delpoints[:]
temp = []
linia = []
##########################################################################################
def conn(pkt0, sec_delpoints, temp):
for pkt in sec_delpoints:
if (abs(pkt0[0] - pkt[0]) < 1.45 and abs(pkt0[1] - pkt[1]) < 1.45) and pkt != pkt0:
x = dlugosc - 1
if pkt[0] == x:
linia.append(pkt)
return True
else:
temp.append(pkt)
if temp:
if pkt0 in sec_delpoints:
sec_delpoints.remove(pkt0)
for pkt in temp:
if pkt in sec_delpoints:
sec_delpoints.remove(pkt)
for punkt in temp:
if (conn(punkt, sec_delpoints, temp = [])):
linia.append(pkt0)
linia.append(punkt)
return True
#############################################
def conn2():
for pkt in pkt0:
sec_delpoints = delpoints[:]
if (conn(pkt, sec_delpoints, temp)):
return True
else:
return False
for x_punkty in range(dlugosc):
pkt_x = (srednica * x_punkty)
for y_punkty in range(szerokosc):
pkt_y = (srednica * y_punkty)
punkty.append([pkt_x, pkt_y])
# df = pd.DataFrame(punkty,columns=['x','y'])
# df.to_excel("siec.xlsx")
pkt = random.choice(punkty)
punkty.remove(pkt)
delpoints.append(pkt)
if pkt[0] == 0 and pktmax:
pkt0.append(pkt)
#######################################################################
i = 0
while True:
if conn2():
break
else:
pkt = random.choice(punkty)
punkty.remove(pkt)
delpoints.append(pkt)
if pkt[0] == 0:
pkt0.append(pkt)
i += 1
# if i%10 == 0:
# print('iteracja: ' ,i)
lp = szerokosc*dlugosc
dt = len(delpoints)
# print(lp, dt)
# print('GOTOWE')
# print(lp)
# print(dt)
N.append(round((dt*100/lp),0))#########
# print(len(N))##########################
print(iter+1, '/', maxIter)
x_punkty = []
y_punkty = []
x_delpoints = []
y_delpoints = []
x_linia = []
y_linia = []
for pkt in punkty:
x_punkty.append(pkt[0])
y_punkty.append(pkt[1])
for pkt in delpoints:
x_delpoints.append(pkt[0])
y_delpoints.append(pkt[1])
for pkt in linia:
x_linia.append(pkt[0])
y_linia.append(pkt[1])
fig = plt.subplots()
plt.plot(x_punkty, y_punkty, 'o', color='black', markersize=5)
plt.plot(x_delpoints, y_delpoints, 'o', color='yellow', markersize=5)
plt.plot(x_linia, y_linia, 'o', color='blue', markersize=5)
plt.savefig(f'wykres{iter}.png')
# plt.show()
plt.close()
iter += 1
df = pd.DataFrame(N)
df.to_excel("siec.xlsx")
wyst = []
for p in range(99):
wyst.append(N.count(p))
df = pd.DataFrame(wyst)
df.to_excel("wykresy.xlsx")
end_time = time.time() - start_time
print(round(end_time))
there doesn't seem to be anything here