Esempio n. 1
0
 def test_dont_lose_nodes(self):
     n1 = Node(1)
     n2 = Node(2)
     n1.add_arc(Arc(n2))
     nw = Network([n1])
     self.assertEqual(nw.find_node(2), n2)
     n3 = Node(3)
     n2.add_arc(Arc(n3))
     self.assertEqual(nw.find_node(3), n3)
Esempio n. 2
0
 def create_non_zero_flow_network(self, color):
     if not self.is_colored_vbmap_problem():
         raise ValueError("VbMapProblem instance is not a colored map problem")
     network = Network()
     za = self.get_active_vbucket_moves()[color]
     for i, a in enumerate(self.prev_avb()[color]):
         if a > 0:
             node = Node(("prev_avb", i), a)
             network.add_node(node)
             for j, x in enumerate(za[i]):
                 if x > 0:
                     to_node = network.find_or_create_node(('avb', j))
                     node.add_arc(Arc(to_node, x))
     avb = self.get_colored_avb()[color]
     # print "avb: ", avb
     x = self.get_colored_replication_map()[color]
     # for i in range(len(x)):
     #     print "x[{0}]: ".format(i), x[i]
     for i, a in enumerate(avb):
         if a > 0:
             node = network.find_or_create_node(("avb", i))
             for j, y in enumerate(x[i]):
                 if y > 0:
                     to_node = network.find_or_create_node(('rvb', j))
                     node.add_arc(Arc(to_node, y))
     zr = self.get_replica_vbucket_moves()[color]
     for i, a in enumerate(self.prev_rvb()[color]):
         if a > 0:
             node = network.find_or_create_node(("prev_rvb", i))
             node.set_source(-a)
             for j in range(len(zr)):
                 y = zr[j][i]
                 if y > 0:
                     from_node = network.find_node(('rvb', j))
                     from_node.add_arc(Arc(node, y))
     return network
Esempio n. 3
0
 def test_has_arc(self):
     n1 = Node(1)
     n2 = Node(2)
     a = Arc(n2)
     n1.add_arc(a)
     self.assertTrue(n1.has_arc(a))
Esempio n. 4
0
    def add_to_network(self, color, network):
        prev_avb = self.prev_avb()[color]
        for i, a in enumerate(prev_avb):
            if a > 0:
                n1 = network.find_or_create_node(('prev_avb', i, color))
                n1.set_source(a, color)
                for j in range(self.node_count):
                    n2 = network.find_or_create_node(('avb', j))
                    a = Arc(n2)
                    n1.add_arc(a)
                    a.set_cost(1 if i != j else 0)
        avbs = network.find_nodes_satisfying(lambda x: x.key()[0] == 'avb')
        for n1 in avbs:
            key = ('avbp', n1.key()[1])
            n2 = network.find_node(key)
            if n2 is None:
                n2 = Node(key)
                network.add_node(n2)
                arc = Arc(n2)
                arc.set_capacity(math.floor(1024.0 / len(avbs)))
                arc.set_cost(-100000)
                n1.add_arc(arc)
                n1.add_arc(Arc(n2))

        replica_network = self._replica_networks[0]
        for i, connections in enumerate(replica_network):
            for j, connection in enumerate(connections):
                if connection != 0:
                    n1 = network.find_or_create_node(('avbp', i))
                    n2 = network.find_or_create_node(('rvbp', j))
                    a = n1.get_arc(n2)
                    if not a:
                        a = Arc(n2)
                        n1.add_arc(a)
                        capacity = math.ceil(1024.0 / (self.node_count * self.slave_factor))
                        print "cap: ", capacity
                        a.set_capacity(capacity)

        rvbps = network.find_nodes_satisfying(lambda x: x.key()[0] == 'rvbp')
        for n1 in rvbps:
            key = ('rvb', n1.key()[1])
            n2 = network.find_node(key)
            if n2 is None:
                n2 = Node(key)
                network.add_node(n2)
                arc = Arc(n2)
                arc.set_capacity(math.floor(1024.0 / len(avbs)))
                arc.set_cost(-100000)
                n1.add_arc(arc)
                n1.add_arc(Arc(n2))

        prev_rvb = self.prev_rvb()[color]
        for i, r in enumerate(prev_rvb):
            if r > 0:
                n1 = network.find_or_create_node(('prev_rvb', i, color))
                for j in range(self.node_count):
                    n2 = network.find_or_create_node(('rvb', j))
                    a = Arc(n1)
                    n2.add_arc(a)
                    a.set_cost(1 if i != j else 0)
                    n1.set_source(-r, color)