def findWeightedCentroids(seeds, stepsize, sigma, heighpar, bnd, X, Y, plot): aimax = 30 ai = aimax stepsize = 10 decFactor = 0.5 print("Start finding weighted centroids") stdevs = [maxsize] sumdists = [maxsize] centroids = seeds minseeds = seeds while True: seeds = allMoveSafeTowards(seeds, centroids, stepsize, bnd) cells = voronoi(seeds, bnd) areas = poly_areas(cells) stdev = np.round(np.std(areas), 4) heights = gauss_heights(areas, heighpar) phi = init_phi(X, Y, seeds, heights, sigma) centroids = wCentroids(cells, phi, X, Y) sumdist = sumDist(seeds, centroids) # plot the result if (plot): plot_voronoi(cells, seeds, centroids, X, Y, phi) if sumdist >= min(sumdists): ai -= 1 else: ai = aimax minseeds = seeds if ai == 0: seeds = minseeds centroids = minseeds stdevs.append(min(stdevs)) sumdists.append(min(sumdists)) stepsize = decFactor * stepsize ai = aimax continue if np.array_equal(np.round(seeds, 2), np.round(centroids, 2)): break stdevs.append(stdev) sumdists.append(sumdist) return stdevs[1:]
def eaStepsizeControl(seeds, sigma, heighpar, bnd, X, Y, plot): aimax = 10 ai = aimax stepsize = 10 decFactor = 0.5 minStepsize = 0.5 print("Start equalizing areas with stepsize control") stdevs = [maxsize] centroids = seeds minseeds = seeds while True: if stepsize < minStepsize: break seeds = allMoveSafeTowards(seeds, centroids, stepsize, bnd) cells = voronoi(seeds, bnd) areas = poly_areas(cells) stdev = np.round(np.std(areas), 4) heights = gauss_heights(areas, heighpar) phi = init_phi(X, Y, seeds, heights, sigma) centroids = wCentroids(cells, phi, X, Y) # plot the result if (plot): plot_voronoi(cells, seeds, centroids, X, Y, phi) if stdev >= min(stdevs): ai -= 1 else: ai = aimax minseeds = seeds if ai == 0: seeds = minseeds centroids = minseeds stdevs.append(min(stdevs)) stepsize = decFactor * stepsize ai = aimax continue stdevs.append(stdev) return stdevs[1:]
def lloyd(seeds,stepsize, bnd, X, Y, plot): print("Start Lloyd algorithm") stdevs = [maxsize] centroids = seeds while True: seeds = allMoveSafeTowards(seeds, centroids, stepsize, bnd) cells = voronoi(seeds,bnd) areas = poly_areas(cells) centroids = uCentroids(cells) # plot the result if(plot): plot_voronoi(cells, seeds, centroids, X, Y, phi) stdev = np.round(np.std(areas),4) if np.array_equal(np.round(seeds,3),np.round(centroids,3)): break if len(stdevs) > 6 and len(set(stdevs[-6:])) < 3: break stdevs.append(stdev) return stdevs[1:]
# ----------------------------------------------------------------------------------- # Algorithm # ----------------------------------------------------------------------------------- stdevs = [] stdevs.append(maxsize) centroids = seeds # Moves seeds towards the weighted centroids until the voronoi tesselation yields # a higher standard deviation of the areas while True: # iteration seeds = allMoveSafeTowards(seeds, centroids, stepsize, bnd) cells = voronoi(seeds, bnd) areas = poly_areas(cells) heights = gauss_heights(areas, heighPar) phi = init_phi(X, Y, seeds, heights, sigma) centroids = wCentroids(cells, phi, X, Y) # plot the result # plot_voronoi(cells, seeds,centroids, X, Y, phi) stdev = np.round(np.std(areas), 4) print(stdev) if stdev >= stdevs[-1]: break stdevs.append(stdev) plt.clf() plt.ioff()