#NEWLINE# def assign_module_permutation_to_each_layer( self , preserve_order = False ) : #NEWLINE# #INDENT# wf = self . wf #NEWLINE# layers = Layers ( ) #NEWLINE# for module in wf . modules : #NEWLINE# #INDENT# layers . addModule ( module , module . layout_layer_number ) #NEWLINE# #UNINDENT# for layer in layers . layers : #NEWLINE# #INDENT# layer . modules . sort ( ( lambda a , b : ( a . layout_layer_index - b . layout_layer_index ) ) ) #NEWLINE# for i in xrange ( len ( layer . modules ) ) : #NEWLINE# #INDENT# layer . modules [ i ] . layout_layer_index = i #NEWLINE# #UNINDENT# #UNINDENT# num_layers = len ( layers . layers ) #NEWLINE# lastModified = [ ( - 1 ) for i in xrange ( num_layers ) ] #NEWLINE# iteration = 0 #NEWLINE# updates = len ( wf . modules ) #NEWLINE# while ( ( updates > 0 ) and ( iteration < 100 ) ) : #NEWLINE# #INDENT# updates = 0 #NEWLINE# ( DOWN , UP ) = ( 1 , ( - 1 ) ) #NEWLINE# for direction in [ DOWN , UP ] : #NEWLINE# #INDENT# delta = ( 1 if ( direction == DOWN ) else ( - 1 ) ) #NEWLINE# ( i0 , i1 ) = ( ( 0 , num_layers ) if ( direction == DOWN ) else ( ( num_layers - 1 ) , ( - 1 ) ) ) #NEWLINE# for i in xrange ( ( i0 + delta ) , i1 , direction ) : #NEWLINE# #INDENT# i_prev = ( i - delta ) #NEWLINE# layer = layers . layers [ i ] #NEWLINE# num_modules = len ( layer . modules ) #NEWLINE# if ( num_modules == 1 ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( ( ( iteration - lastModified [ i_prev ] ) > 1 ) and ( ( iteration - lastModified [ i ] ) > 1 ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# barycenters = ( [ ( - 1 ) ] * num_modules ) #NEWLINE# for j in xrange ( num_modules ) : #NEWLINE# #INDENT# module = layer . modules [ j ] #NEWLINE# connections = ( module . cached_connections_pred if ( direction == DOWN ) else module . cached_connections_succ ) #NEWLINE# value = 0.0 #NEWLINE# for c in connections : #NEWLINE# #INDENT# opposite_port = ( c . source_port if ( direction == DOWN ) else c . target_port ) #NEWLINE# value += ( opposite_port . module . layout_layer_index + ( opposite_port . index / 100.0 ) ) #NEWLINE# #UNINDENT# if ( len ( connections ) > 0 ) : #NEWLINE# #INDENT# value /= ( 1.0 * len ( connections ) ) #NEWLINE# barycenters [ j ] = value #NEWLINE# #UNINDENT# #UNINDENT# for j in xrange ( 1 , num_modules ) : #NEWLINE# #INDENT# if ( barycenters [ j ] < 0 ) : #NEWLINE# #INDENT# barycenters [ j ] = ( barycenters [ ( j - 1 ) ] + 1e-05 ) #NEWLINE# #UNINDENT# #UNINDENT# new_order = [ ( barycenters [ j ] , layer . modules [ j ] ) for j in xrange ( num_modules ) ] #NEWLINE# new_order . sort ( ) #NEWLINE# for j in xrange ( num_modules ) : #NEWLINE# #INDENT# module = new_order [ j ] [ 1 ] #NEWLINE# if ( module . layout_layer_index != j ) : #NEWLINE# #INDENT# lastModified [ i ] = iteration #NEWLINE# updates += 1 #NEWLINE# #UNINDENT# #UNINDENT# if ( lastModified [ i ] == iteration ) : #NEWLINE# #INDENT# for j in xrange ( num_modules ) : #NEWLINE# #INDENT# module = new_order [ j ] [ 1 ] #NEWLINE# module . layout_layer_index = j #NEWLINE# #UNINDENT# #UNINDENT# layer . modules . sort ( ( lambda a , b : ( a . layout_layer_index - b . layout_layer_index ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# iteration += 1 #NEWLINE# break #NEWLINE# #UNINDENT# if preserve_order : #NEWLINE# #INDENT# for layer in layers . layers : #NEWLINE# #INDENT# layer . modules . sort ( key = ( lambda x : x . layout_layer_index ) ) #NEWLINE# temp = [ ] #NEWLINE# for i in reversed ( range ( len ( layer . modules ) ) ) : #NEWLINE# #INDENT# if ( layer . modules [ i ] . prev_x is None ) : #NEWLINE# #INDENT# temp . append ( ( i , layer . modules . pop ( i ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# layer . modules . sort ( key = ( lambda x : x . prev_x ) ) #NEWLINE# for item in reversed ( temp ) : #NEWLINE# #INDENT# layer . modules . insert ( item [ 0 ] , item [ 1 ] ) #NEWLINE# #UNINDENT# for i in range ( len ( layer . modules ) ) : #NEWLINE# #INDENT# layer . modules [ i ] . layout_layer_index = i