def test_Point2D(): point_int = cl.Point2D(3, 4) point_float = cl.Point2D(3.5, 2.1) errors = [] if not (point_int.X == 3 and point_int.Y == 4): errors.append('error') if not (point_float.X == 3.5 and point_float.Y == 2.1): errors.append('error') if not (point_float.__repr__() == '(3.50, 2.10)' and point_int.__repr__() == '(3.00, 4.00)'): errors.append('error') if not point_int + point_float == cl.Point2D(6.5, 6.1): errors.append("error") if not point_float - point_int == cl.Point2D(0.5, -1.9): errors.append("error") if not point_float * point_int == cl.Point2D(10.5, 8.4): errors.append("error") point_int += point_float if not point_int == cl.Point2D(6.5, 6.1): errors.append("error") point_int -= point_float if point_int != cl.Point2D(3, 4): pass #errors.append("error") point_int *= point_float if not point_int == cl.Point2D(10.5, 8.4): pass #errors.append("error") assert not errors
def test_turnLeft(): points_set = [ classes.Point2D(1, 1), classes.Point2D(2, 2), classes.Point2D(2.5, 3), classes.Point2D(3, 2.5), classes.Point2D(3, 3) ] errors = [] if not utils.turnLeft(points_set[:2], points_set[2]): errors.append("error") if utils.turnLeft(points_set[:2], points_set[3]): errors.append("error") if utils.turnLeft(points_set[:2], points_set[4]): errors.append("error") assert not errors
def move(self, W, C1, C2, Gb): R1, R2 = ranf(size=2) for i in xrange(len(self.centroids)): self.vel[i] = (W * self.vel[i]) + (C1 * R1 * (utils.euclidean(self.best_centroids[i], self.centroids[i])))\ + (C2 * R2 * (utils.euclidean(Gb[i] , self.centroids[i]))) self.centroids[i] += classes.Point2D(*self.vel[i]) self.recluster() if self.use_variance: self.fit_by_variance() else: self.fit_by_area() if self.fitness > self.best_fitness: self.best_fitness = self.fitness self.best_centroids = self.centroids[:]
def generate_population(self, n_clusters=13): population = [] for _ in tqdm(xrange(self.n_particles), desc='Generando Poblacion inicial', unit=' Particle'): individuo = [] cluster = KMeans(n_clusters=n_clusters, random_state=self.seed) cluster_labels = cluster.fit_predict(self.data[:, 0:2]) individuo_df = pd.DataFrame({ 'x': self.data[:, 0], 'y': self.data[:, 1], 'time_store': self.data[:, 2], 'cluster': cluster_labels }) for i in xrange(n_clusters): individuo.append( classes.ClusterPdV([ classes.PdV(*point[:-1]) for point in individuo_df[ individuo_df.cluster == i].values ], classes.Point2D(*cluster.cluster_centers_[i]))) population.append(Particle(individuo, self.use_var)) return population
def test_getConvexHull(): errors = [] points_set = [ classes.Point2D(2.5, 0), classes.Point2D(3.5, 0.5), classes.Point2D(1, 1), classes.Point2D(2, 2), classes.Point2D(3, 2.5), classes.Point2D(4, 3), classes.Point2D(2.5, 3), classes.Point2D(3, 3) ] convex_hull = utils.getConvexHull(points_set) if not convex_hull == [ classes.Point2D(1, 1), classes.Point2D(2.5, 3), classes.Point2D(4, 3), classes.Point2D(3.5, 0.5), classes.Point2D(2.5, 0) ]: errors.append("error") if not points_set != [ classes.Point2D(2.5, 0), classes.Point2D(3.5, 0.5), classes.Point2D(1, 1), classes.Point2D(2, 2), classes.Point2D(3, 2.5), classes.Point2D(4, 3), classes.Point2D(2.5, 3), classes.Point2D(3, 3) ]: errors.append("error") assert not errors
def test_sortByX(): a = [ classes.Point2D(1, 1), classes.Point2D(2, 2), classes.Point2D(3, 3), classes.Point2D(3, 2.5), classes.Point2D(2.5, 3) ] errors = [] random.shuffle(a) utils.sortByX(a) if not a == [ classes.Point2D(1, 1), classes.Point2D(2, 2), classes.Point2D(2.5, 3), classes.Point2D(3, 2.5), classes.Point2D(3, 3) ]: errors.append('error') random.shuffle(a) utils.sortByX(a, reverse=True) if not a == [ classes.Point2D(3, 3), classes.Point2D(3, 2.5), classes.Point2D(2.5, 3), classes.Point2D(2, 2), classes.Point2D(1, 1) ]: errors.append('error') assert not errors