예제 #1
0
 def update(self, current_time):
     self.organism_quad_tree = QuadTree(Rectangle(0, 0, 1, 1))
     for blob in self.organism_list:
         blob.update(current_time)
         if blob.is_dead():
             self.kill_organism(blob, is_in_tree=False)
         else:
             for offspring in blob.produce_offspring(current_time):
                 self.add_blob(offspring)
             self.organism_quad_tree.insert(blob)
예제 #2
0
    def init_clusters(self, nodes):
        node_dicts = []
        
        x_list = [nd[0] for nd in nodes]
        y_list = [nd[1] for nd in nodes]
        x_list.sort()
        y_list.sort()

        sw = (x_list[0], y_list[0])
        ne = (x_list[-1], y_list[-1])

        self.qt = QuadTree(8, sw, ne, get_point, False)

        for i in range(0, len(nodes)):
            node_dict = {}
            node_dict['id'] = i
            node_dict['xy'] = nodes[i]
            self.qt.add(node_dict)
예제 #3
0
class Organisms:
    def __init__(self):
        self.organism_list = []
        self.organism_quad_tree = QuadTree(Rectangle(0, 0, 1, 1))

    def update(self, current_time):
        self.organism_quad_tree = QuadTree(Rectangle(0, 0, 1, 1))
        for blob in self.organism_list:
            blob.update(current_time)
            if blob.is_dead():
                self.kill_organism(blob, is_in_tree=False)
            else:
                for offspring in blob.produce_offspring(current_time):
                    self.add_blob(offspring)
                self.organism_quad_tree.insert(blob)

    def add_blob(self, blob):
        self.organism_list += [blob]
        self.organism_quad_tree.insert(blob)

    def add_random_blobs(self, number_of_new_blobs=1, current_time=0):
        for i in range(number_of_new_blobs):
            new_blob = Blob(time_of_birth=current_time)
            self.add_blob(new_blob)

    def kill_organism(self, organism, is_in_tree=True):
        self.organism_list.remove(organism)
        if is_in_tree:
            self.organism_quad_tree.remove(organism)

    def find_close_organisms(self, domain: Rectangle):
        close_organisms = []
        self.organism_quad_tree.retrieve_close_objects(domain, close_organisms)
        return close_organisms

    def update_extrema_of_organisms(self):
        for organism in self.organism_list:
            organism.restrict_to_extrema()
예제 #4
0
 def __init__(self):
     self.organism_list = []
     self.organism_quad_tree = QuadTree(Rectangle(0, 0, 1, 1))
예제 #5
0
class KMeansHelper(object):
    """Helper class to manage seeding, finding and updating clustered
    node assignment for KMeans"""

    def __init__(self):
        qt = None
        pass

    
    def seed(self, k, nodes):
        """ splits nodes based on x and y means of each of k partitions of
        nodes sorted by x and y """
        x_list = [nd[0] for nd in nodes]
        y_list = [nd[1] for nd in nodes]
        x_list.sort()
        y_list.sort()
    
        parts = range(0, len(nodes), (len(nodes)-1)/k)
        
        x_means = [sum(x_list[i:j])/len(x_list[i:j]) for i, j in zip(parts, parts[1:len(parts)])]
        y_means = [sum(y_list[i:j])/len(y_list[i:j]) for i, j in zip(parts, parts[1:len(parts)])]
        
        k_nodes = zip(x_means, y_means)
            
        return k_nodes

    def init_clusters(self, nodes):
        node_dicts = []
        
        x_list = [nd[0] for nd in nodes]
        y_list = [nd[1] for nd in nodes]
        x_list.sort()
        y_list.sort()

        sw = (x_list[0], y_list[0])
        ne = (x_list[-1], y_list[-1])

        self.qt = QuadTree(8, sw, ne, get_point, False)

        for i in range(0, len(nodes)):
            node_dict = {}
            node_dict['id'] = i
            node_dict['xy'] = nodes[i]
            self.qt.add(node_dict)

        
    def find_nearest(self, node):
        k_node = self.qt.find_nearest(node)
        return k_node['id']

    def update_clusters(self, cluster_assignments):
        """ Returns a new set of cluster nodes based on average of assigned points """
        cluster_list = []
        for k_id in range(0, len(cluster_assignments)):
            points = cluster_assignments[k_id]
            if len(points) > 0:
                x_list = [pt[0] for pt in points]
                y_list = [pt[1] for pt in points]
                x_list.sort()
                y_list.sort()
                x_mean = sum(x_list)/len(x_list)
                y_mean = sum(y_list)/len(y_list)
                cluster_list.append([x_mean, y_mean])
                
        return cluster_list
예제 #6
0
 def setUp(self):
     self.tree = QuadTree(0, 0, 200, 200)
예제 #7
0
class QuadTreeTests(unittest.TestCase):
    def setUp(self):
        self.tree = QuadTree(0, 0, 200, 200)

    def test_insert_should_increase_count(self):
        items = [BoundableObject(50, 50, 60, 60)] * 3
        for item in items:
            self.tree.add_object(item)

        self.assertEqual(len(self.tree), len(items))

    def test_report_empty_quadrant_should_return_empty(self):
        items = [
            BoundableObject(10, 0, 20, 10),
            BoundableObject(110, 0, 120, 10),
            BoundableObject(10, 110, 20, 120),
            BoundableObject(50, 0, 60, 10)
        ]
        for item in items:
            self.tree.add_object(item)
        # after 4 inserts our root splits into 4 and the third quadrant is empty
        first_quadrant = self.tree.get_subquandrant(1)
        elements = self.tree.report(
            first_quadrant
        )  # get everthing that intersects with our third quadrant
        self.assertEqual(len(elements), 0)

    def test_report_from_non_empty_quadrants_should_return_elements(self):
        expected_quadrant_items = [
            [BoundableObject(110, 101, 120, 110)],  # first quadrant
            [BoundableObject(10, 110, 20, 120)],  # second quadrant
            [BoundableObject(10, 0, 20, 10),
             BoundableObject(50, 0, 60, 10)],  # third quadrant
            []  # fourth quadrant
        ]
        # add the objects to the tree
        for obj in [
                obj for quadrant in expected_quadrant_items for obj in quadrant
        ]:
            self.tree.add_object(obj)

        first_quadrant = self.tree.get_subquandrant(1)
        f_q_elements = self.tree.report(first_quadrant)
        self.assertEqual(f_q_elements, expected_quadrant_items[0])

        second_quadrant = self.tree.get_subquandrant(2)
        s_q_elements = self.tree.report(second_quadrant)
        self.assertEqual(s_q_elements, expected_quadrant_items[1])

        third_quadrant = self.tree.get_subquandrant(3)
        t_q_elements = self.tree.report(third_quadrant)
        self.assertEqual(t_q_elements, expected_quadrant_items[2])

        fourth_quadrant = self.tree.get_subquandrant(4)
        fourth_q_elements = self.tree.report(fourth_quadrant)
        self.assertEqual(fourth_q_elements, expected_quadrant_items[3])

    def test_foreach_dfs_should_fuck_you(self):
        items = [BoundableObject(2, 2, 3, 3)] * 100
        for item in items:
            self.tree.add_object(item)

        def assert_function(_, __, depth):
            self.assertEqual(self.tree.max_depth, depth)

        self.tree.foreach_dfs(assert_function)

    def test_report_many_random_elements_should_return_all_colliders(self):
        object_count = 10000
        shepherd = BoundableObject(10, 20, 20, 30)
        items = []
        self.tree.add_object(shepherd)
        for _ in range(object_count):
            x = random.randint(0, self.tree.x2 - 11)
            x2 = x + 10
            y = random.randint(0, self.tree.y2 - 11)
            y2 = y + 10
            obj = BoundableObject(x, y, x2, y2)
            items.append(obj)
            self.tree.add_object(obj)

        expected_collisions = self.search_for_collisions_in_a_list(
            shepherd, items)
        result_collisions = self.search_for_collisions_in_a_tree(shepherd)

        self.assertEqual(len(expected_collisions), len(result_collisions))
        print(expected_collisions)
        self.assertCountEqual(expected_collisions, result_collisions)

    def search_for_collisions_in_a_tree(self, shepherd):
        result = []
        collision_candidates = self.tree.report(shepherd)
        for col_candidate in collision_candidates:
            if col_candidate.intersects(
                    shepherd) and col_candidate != shepherd:
                result.append(col_candidate)

        return result

    def search_for_collisions_in_a_list(self, shepherd, objs):
        result = []

        for obj in objs:
            if shepherd.intersects(obj) and shepherd != obj:
                result.append(obj)

        return result
예제 #8
0
 def init_quad_tree(self, cam_img_dim, min_cell_size):
     qt = QuadTree(cam_img_dim[0], cam_img_dim[1], min_cell_size)
     qt.create_tree(self.cb_split_node)
     return qt