def setUp(self): self.g = Graph(6) self.g.nodeAt(0, 1).capacityIs(16).costIs(100) self.g.nodeAt(0, 2).capacityIs(13).costIs(2) self.g.nodeAt(1, 2).capacityIs(10).costIs(3) self.g.nodeAt(1, 3).capacityIs(12).costIs(100) self.g.nodeAt(2, 1).capacityIs(4).costIs(5) self.g.nodeAt(2, 4).capacityIs(14).costIs(1) self.g.nodeAt(3, 2).capacityIs(9).costIs(7) self.g.nodeAt(3, 5).capacityIs(20).costIs(100) self.g.nodeAt(4, 3).capacityIs(7).costIs(9) self.g.nodeAt(4, 5).capacityIs(4).costIs(1)
def setUp(self): self.g = Graph(6) self.g.nodeAt(0,1).capacityIs(16).costIs(100) self.g.nodeAt(0,2).capacityIs(13).costIs(2) self.g.nodeAt(1,2).capacityIs(10).costIs(3) self.g.nodeAt(1,3).capacityIs(12).costIs(100) self.g.nodeAt(2,1).capacityIs(4).costIs(5) self.g.nodeAt(2,4).capacityIs(14).costIs(1) self.g.nodeAt(3,2).capacityIs(9).costIs(7) self.g.nodeAt(3,5).capacityIs(20).costIs(100) self.g.nodeAt(4,3).capacityIs(7).costIs(9) self.g.nodeAt(4,5).capacityIs(4).costIs(1)
def createBipartiteGraph(self, sellItems, buyItems): graphSize = len(buyItems) + len(sellItems) + 2 graph = Graph(graphSize) # build cap constraints for edges from source to buy Items sourcePos = 0 startOfBuy = 1 startOfSell = 1 + len(buyItems) endPos = 1 + len(buyItems) + len(sellItems) # build cap constraints for eduges from buy items to end for i in range(startOfBuy, startOfSell): graph.nodeAt(0, i).setCapacity(abs(buyItems[i - 1].num)) # build cost edges between buy and sell items for j in range(startOfSell, endPos): graph.nodeAt(j, endPos).setCapacity(abs(sellItems[j - startOfSell].num)) for i in range(startOfBuy, startOfSell): for j in range(startOfSell, endPos): if self.possibleMatch(buyItems[i - 1], sellItems[j - startOfSell]): unitTaxCost = self.getCost(buyItems[i - 1], sellItems[j - startOfSell]) graph.nodeAt(i, j).setCost(unitTaxCost).setCapacity(sys.maxint) return (graph, startOfBuy, startOfSell, endPos)
def test_create(self): g = Graph(5) g.nodeAt(0,0).capacityIs(1) g.nodeAt(4,4).flowIs(3) g.nodeAt(3,2).residualIs(100) g.nodeAt(3,2).costIs(2) print g self.assertEqual(g.nodeAt(0,0).getCapacity(), 1) self.assertEqual(g.nodeAt(4,4).getFlow(), 3) self.assertEqual(g.nodeAt(3,2).getResidual(), 100) self.assertEqual(g.nodeAt(3,2).getCost(), 2)
class TestFF(unittest.TestCase): def setUp(self): self.g = Graph(6) self.g.nodeAt(0,1).capacityIs(16).costIs(100) self.g.nodeAt(0,2).capacityIs(13).costIs(2) self.g.nodeAt(1,2).capacityIs(10).costIs(3) self.g.nodeAt(1,3).capacityIs(12).costIs(100) self.g.nodeAt(2,1).capacityIs(4).costIs(5) self.g.nodeAt(2,4).capacityIs(14).costIs(1) self.g.nodeAt(3,2).capacityIs(9).costIs(7) self.g.nodeAt(3,5).capacityIs(20).costIs(100) self.g.nodeAt(4,3).capacityIs(7).costIs(9) self.g.nodeAt(4,5).capacityIs(4).costIs(1) def test_ff(self): flow = self.g.fordFulkerson(0, 5) print "max possible flow is:" , flow
def test_create(self): g = Graph(5) g.nodeAt(0, 0).capacityIs(1) g.nodeAt(4, 4).flowIs(3) g.nodeAt(3, 2).residualIs(100) g.nodeAt(3, 2).costIs(2) print g self.assertEqual(g.nodeAt(0, 0).getCapacity(), 1) self.assertEqual(g.nodeAt(4, 4).getFlow(), 3) self.assertEqual(g.nodeAt(3, 2).getResidual(), 100) self.assertEqual(g.nodeAt(3, 2).getCost(), 2)
class TestFF(unittest.TestCase): def setUp(self): self.g = Graph(6) self.g.nodeAt(0, 1).capacityIs(16).costIs(100) self.g.nodeAt(0, 2).capacityIs(13).costIs(2) self.g.nodeAt(1, 2).capacityIs(10).costIs(3) self.g.nodeAt(1, 3).capacityIs(12).costIs(100) self.g.nodeAt(2, 1).capacityIs(4).costIs(5) self.g.nodeAt(2, 4).capacityIs(14).costIs(1) self.g.nodeAt(3, 2).capacityIs(9).costIs(7) self.g.nodeAt(3, 5).capacityIs(20).costIs(100) self.g.nodeAt(4, 3).capacityIs(7).costIs(9) self.g.nodeAt(4, 5).capacityIs(4).costIs(1) def test_ff(self): flow = self.g.fordFulkerson(0, 5) print "max possible flow is:", flow