예제 #1
0
    def setUp(self):
        rng = np.random.RandomState(42)
        Cluster.global_rng = rng
        Genotype.global_rng = rng

        setattr(Cluster, "num_dims", 2)

        clust1 = Cluster(50)
        clust1.mean = np.array([0, 0])
        clust1.cov = np.array([[1, 0], [0, 1]])
        clust2 = Cluster(30)
        clust2.mean = np.array([5, 5])
        clust2.cov = np.array([[5, 0], [0, 10]])
        self.indiv1 = Genotype([clust1, clust2])
        self.indiv1.create_views()
        self.indiv1.resample_values()

        clust3 = Cluster(50)
        clust3.mean = np.array([2, 2])
        clust3.cov = np.array([[2, 0], [0, 2]])
        clust4 = Cluster(30)
        clust4.mean = np.array([10, 10])
        clust4.cov = np.array([[4, 0], [0, 2]])
        self.indiv2 = Genotype([clust3, clust4])
        self.indiv2.create_views()
        self.indiv2.resample_values()
예제 #2
0
    def test_eigenratio_eccentric(self):
        clust1 = Cluster(50)
        clust1.mean = np.array([0, 0])
        clust1.cov = np.array([[10, 0], [0, 1]])

        clust2 = Cluster(80)
        clust2.mean = np.array([0, 0])
        clust2.cov = np.array([[9.9, 0], [0, 1]])

        indiv = Genotype([clust1, clust2])
        eigen_ratio = hawks.constraints.eigenval_ratio(indiv)

        self.assertEqual(eigen_ratio, 10)
예제 #3
0
    def test_overlap_same(self):
        clust1 = Cluster(4000)
        clust1.mean = np.array([0, 0])
        clust1.cov = np.array([[1, 0], [0, 1]])

        clust2 = Cluster(4000)
        clust2.mean = np.array([0, 0])
        clust2.cov = np.array([[1, 0], [0, 1]])

        indiv = Genotype([clust1, clust2])
        indiv.create_views()
        indiv.resample_values()

        overlap = hawks.constraints.overlap(indiv)

        self.assertAlmostEqual(overlap, 0.5, places=1)
예제 #4
0
    def test_overlap_separated(self):
        clust1 = Cluster(50)
        clust1.mean = np.array([0, 0])
        clust1.cov = np.array([[1, 0], [0, 1]])

        clust2 = Cluster(80)
        clust2.mean = np.array([10, 10])
        clust2.cov = np.array([[1, 0], [0, 1]])

        indiv = Genotype([clust1, clust2])
        indiv.create_views()
        indiv.resample_values()

        overlap = hawks.constraints.overlap(indiv)

        self.assertEqual(overlap, 0)
예제 #5
0
파일: ga.py 프로젝트: sea-shunned/hawks
def generate_indiv(icls, dataset_obj):
    # Create the individual
    # Uses the DEAP wrapper around Genotype()
    indiv = icls([Cluster(size) for size in dataset_obj.cluster_sizes])
    # Create the views (each cluster.values is a view into genotype.all_values)
    indiv.create_views()
    # And sample some initial values
    indiv.resample_values()
    return indiv
예제 #6
0
 def setUp(self):
     # Whatever setup is needed
     rng = np.random.RandomState(42)
     Genotype.global_rng = rng
     Cluster.global_rng = rng
     sizes = [190, 20, 30, 110]
     self.indiv = Genotype([Cluster(size) for size in sizes])
     self.indiv.create_views()
     self.indiv.resample_values()
     hawks.objectives.Silhouette.setup_indiv(self.indiv)
예제 #7
0
파일: tests.py 프로젝트: sea-shunned/hawks
 def setUp(self):
     # Whatever setup is needed
     rng = np.random.RandomState(42)
     Genotype.global_rng = rng
     Cluster.global_rng = rng
     sizes = [190, 20, 30, 110]
     setattr(Cluster, "num_dims", 2)
     setattr(Cluster, "initial_mean_upper", 1.0)
     setattr(Cluster, "initial_cov_upper", 0.5)
     self.indiv = Genotype([Cluster(size) for size in sizes])
     self.indiv.create_views()
     self.indiv.resample_values()
     hawks.objectives.Silhouette.setup_indiv(self.indiv)
예제 #8
0
    def test_silhouette_singleton_cluster(self):
        rng = np.random.RandomState(42)
        Genotype.global_rng = rng
        Cluster.global_rng = rng
        sizes = [1, 20, 30, 110]
        self.indiv = Genotype([Cluster(size) for size in sizes])
        self.indiv.create_views()
        self.indiv.resample_values()
        hawks.objectives.Silhouette.setup_indiv(self.indiv)

        hawks.objectives.Silhouette.eval_objective(self.indiv)

        close_to_sk = np.isclose(
            silhouette_score(self.indiv.all_values,
                             self.indiv.labels,
                             metric="sqeuclidean"), self.indiv.silhouette)

        self.assertTrue(close_to_sk)