예제 #1
0
 def test_flow(self):
     n2 = Node(1)
     a = Arc(n2)
     self.assertTrue(a.total_flow() == 0)
     self.assertTrue(a.flow() == 0)
     a.set_flow(3)
     self.assertTrue(a.flow() == 3)
     a.set_flow(3, 'red')
     self.assertTrue(a.flow('red') == 3)
     self.assertEqual(a.total_flow(), 6)
예제 #2
0
 def test_to_node(self):
     n2 = Node(2)
     a = Arc(n2)
     self.assertTrue(a.to_node() == n2)
예제 #3
0
파일: vbmap.py 프로젝트: sh4rkfin/misc
    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)