有关该项目的信息,请参阅下文。我已经完成了代码,但绘图失败。似乎我的代码没有创建最小距离列表。 点击这里查看问题描述
import numpy as np
import random
import matplotlib.pyplot as plt
%load_ext line_profiler
def euclideanDistance(point1, point2):
return pow(pow(point1[0] - point2[0], 2) + pow(point1[1] - point2[1], 2), .5)
def main_original(n, delta, T):
"""
n: is the number of uniformly at random generated points in the unit square
delta: a maximal move of a point in one of four random directions: left, right, up, or down
T: number of iterations
return:
lst_of_min_distances: of the minimum distances among all n points over times: t=0, 1, 2, \dots, T - 1,
it is a list of reals of length T"""
points = [(random.random(), random.random()) for _ in range(n)] # Generate random n points
#print(points)
z = np.arange(50) # List of time
#print (z)
time = iter(z) #iteration of time T
for t in time:
points+=t*delta+points
if t==T:
break
#print(points)
distances = []
for i in range(len(points)-1):
for j in range(i+1, len(points)):
distances += [euclideanDistance(points[i],points[j])]
list_min_ditance = (min(distances))
#print(list_min_ditance)
return
n = 1500
delta = 1.0 / n
T = 40
%lprun -f main_original lst_min_dist = main_original(n, delta, T)
# plot the diagram of the minimum distances:
# where we rescale distance with by factor $\sqrt{n}$:
print("len:", len(lst_min_dist))
plt.plot(range(T), np.array(lst_min_dist) / np.sqrt(n))
plt.show()
这是一个笔记本作业,有人知道如何更正吗?感谢帮助。