def test_Cluster(): items = [(1, 1), (3, 1), (1, 3), (3, 3), (3, 4)] cluster = cl.ClusterPdV([cl.PdV(i[0], i[1], i[1]+1) for i in items]) errors = [] if not cluster.total_time == sum([i[1]+1 for i in items]): errors.append("error") for i, j in zip(cluster.elements(), sorted(items)): if i.X != j[0] or i.Y != j[1]: errors.append("error") if not cluster.area() == 5: errors.append("error") cluster.push_back(cl.PdV(3, 5, 2)) if not cluster.total_time == sum([i[1]+1 for i in items]) + 2: errors.append("error") if not cluster.size() == 6: errors.append("error") if not cluster.area() == 6: #errors.append("error") pass cluster.remove(cl.PdV(3, 5, 2)) if not cluster.area() == 5: errors.append("error") if not cluster.size() == 5: errors.append("error") if not cluster.centroid == [None, None]: errors.append("error") cluster.setCenter([2, 4]) if not cluster.centroid == [2, 4]: errors.append("error") assert not errors
def test_PdV(): point_int = cl.PdV(3, 4, 85.9) point_float = cl.PdV(3.5, 2.1, 62.5) point_float2 = cl.PdV(3.5, 2.1, 61.5) 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), 62.50]' and point_int.__repr__() == '[(3.00, 4.00), 85.90]'): errors.append('error') if not (point_float.samePlace(point_float2)): errors.append('error') if not (point_float.time_store == 62.5 and point_int.time_store == 85.9): errors.append('error') assert not errors
def test_areaConvexPolygon(): errors = [] items = [(1, 1), (3, 1), (1, 3), (3, 3)] a = [classes.PdV(i[0], i[1], 0) for i in items] a = utils.getConvexHull(a) if not utils.getConvexPolygonArea(a) == 4: errors.append("error") items.append((3, 4)) a = [classes.PdV(i[0], i[1], 0) for i in items] a = utils.getConvexHull(a) if not utils.getConvexPolygonArea(a) == 5: errors.append("error") items = [(-2, -2), (2, -2), (0, 2)] a = [classes.PdV(i[0], i[1], 0) for i in items] a = utils.getConvexHull(a) if not utils.getConvexPolygonArea(a) == 8: errors.append("error") assert not errors
def test_Cluster_exception(): items = [(1, 1), (3, 1), (1, 3), (3, 3), (3, 4)] cluster = cl.ClusterPdV([cl.PdV(i[0], i[1], i[1]+1) for i in items], [None, None]) try: cluster.setCenter(2) cluster.setCenter([1,2,3]) assert False except ValueError: assert True
def test_euclidean(): errors = [] items = [(1, 1), (3, 1), (1, 3)] a = [classes.PdV(i[0], i[1], 0) for i in items] for i in a: for j in a: print i, j print utils.euclidean(i, j) print(i.X - j.X)**2 + (i.Y - j.Y)**2 if utils.euclidean(i, j) != (i.X - j.X)**2 + (i.Y - j.Y)**2: errors.append('error') assert not errors
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