def test_reviseCostMatrix (self):
  
    costMatrix =self.costMatrix

    a, b, costMatrix2, partialGraphMatrix = HungarianAssignment.HA_Step1_ConstructInitialPartialGraph ( costMatrix ) 

    supplyLabels = [0,'*',-1,-1]
    demandLabels = [1,-1,-1,-1]

    delta = HungarianAssignment._findLowestEdgeCost (supplyLabels, demandLabels, costMatrix2)

    self.failUnless ( delta == 4) # see page 320 of 'Networks and Algorithms'

    revCostMatrix = HungarianAssignment._reviseCostMatrix (delta, costMatrix2, supplyLabels, demandLabels )

    self.failUnless ( revCostMatrix.sum () == 35)
  def testExtractSubMatrix ( self ) : 
  
   numpy2DArray = numpy.arange (16).reshape (4,4) 
   requiredRows  = [0,1]
   requiredCols  = [2,3]

   resultMatrix = HungarianAssignment._extractSubMatrixDeepCopy ( numpy2DArray, requiredRows, requiredCols)

   self.failUnless ( resultMatrix.sum () == 18)
  def test_findLowestEdgeCost (self):

    supplyLabels = [ 10,-1,10 ]
    demandLabels = [ -1,10,10  ]
  
    costMatrix = numpy.arange ( 9 ).reshape ( 3,3)

    lowestCost = HungarianAssignment._findLowestEdgeCost (supplyLabels, demandLabels, costMatrix)

    self.failUnless  ( lowestCost == 6)
  def testLabellingIsComplete (self  ):
    MATCHEDGraphMat = numpy.array ([True, False, False,  False, 
                                    False, True, False,  False,
                                    False, False, True, False,
                                    False, False, False, True] ).reshape(4,4)


    res = HungarianAssignment._labellingIsComplete (MATCHEDGraphMat  )
    
    self.failUnless ( res == True)
  def testfoundBreakThroughTwo (self):
    '''
    This should fail because there NO labelled demand vertices to consider.

    ''' 

    demandLabelsList = [-1] * 4

    MATCHEDGraphBoolMatrix = numpy.array ( [False]*16).reshape(4,4)
    res = HungarianAssignment.findFirstBreakThroughIndex ( demandLabelsList, MATCHEDGraphBoolMatrix ) 
    self.failUnless ( res==None )
  def test_findNewEdgesFromUpdatedCostMatrix (self):

    oldCostMatrix = numpy.array ( [ 0,1,1,0 ] ).reshape(2,2)
    newCostMatrix = numpy.array ( [ 0,1,0,0 ] ).reshape(2,2)


    result =  HungarianAssignment._findNewEdgesFromUpdatedCostMatrix ( oldCostMatrix, newCostMatrix )


    testMat = numpy.arange (4).reshape (2,2)
    testMat [result] = 1000

    self.failUnless ( testMat[1:2, 0:1] == 1000 )
  def testfoundBreakThroughOne (self):
    '''
    This will pass because the last demand node is not attached to any 
    edges in the MATCHED graph : it's a breakthrough...

    ''' 

    demandLabelsList = [-1] * 4
    demandLabelsList [3] = 0

    MATCHEDGraphBoolMatrix = numpy.array ( [False]*16).reshape(4,4)

    res = HungarianAssignment.findFirstBreakThroughIndex ( demandLabelsList, MATCHEDGraphBoolMatrix ) 
    self.failUnless ( res == 3 )
  def testfoundBreakThroughThree (self):
    '''
    This will fail because although there are labelled demand vertices, 
    edges in graph MATCHED do in fact connect to both of them. 

    ''' 

    demandLabelsList = [-1] * 4
    demandLabelsList [0] = 0
    demandLabelsList [3] = 1

    MATCHEDGraphBoolMatrix = numpy.array ( [False]*16).reshape(4,4) 
    MATCHEDGraphBoolMatrix [ 0:1 , 0:1  ] = True 
    MATCHEDGraphBoolMatrix [ 0:1 , 3:4  ] = True 

    res = HungarianAssignment.findFirstBreakThroughIndex ( demandLabelsList, MATCHEDGraphBoolMatrix ) 
    self.failUnless ( res==None )