- if arc.origin.rank < arc.target.rank:

+ if arc.origin.rank.value < arc.target.rank.value:

- positions = simplex(A, B, C)

+ positions = simplex_fast(A, B, C)

for idx in range(len(positions)):

- return order_~~median~~(graph)

+ return order_hybrid(graph)

def order_by_default(graph):

+def order_hybrid(graph):

+ ensure(graph.readonly(WithLayers), "without layers", graph)

+ ensure(not graph.readable(WithBeadOrder), "already with bead orders", graph)

+ graph.upgrade(WithBeadOrder)

+ layer = graph.layers[0]

+ for next_layer in graph.layers[1:]:

+ for bead in layer.beads:

+ for next_bead in next_layer.beads:

+ for arc in bead.node.incoming+bead.node.outgoing:

+ if next_bead.is_node and next_bead.node in [arc.origin, arc.target]:

+ elif next_bead.is_arc and next_bead.arc is arc:

+ if next_bead.is_node and bead.arc in next_bead.node.incoming+next_bead.node.outgoing:

+ elif next_bead.is_arc and bead.arc is next_bead.arc:

+ weight += arc.weight.value

+ weights[bead, next_bead] = weight

+ weights[next_bead, bead] = weight

+ next_order_by_layer = {}

+ for layer in graph.layers:

+ next_order_by_layer[layer] = 0

+ stack.extend(reversed(graph.beads))

+ if bead in order_by_bead:

+ value = next_order_by_layer[bead.layer]

+ order_by_bead[bead] = value

+ next_order_by_layer[bead.layer] = value+1

+ stack.extend(bead.neighbors)

+ layer = graph.layers[0]

+ for next_layer in graph.layers[1:]:

+ for lbead in layer.beads:

+ lorder = order_by_bead[lbead]

+ for rbead in layer.beads:

+ rorder = order_by_bead[rbead]

+ for next_lbead in next_layer.beads:

+ if not weights[lbead, next_lbead]:

+ next_lorder = order_by_bead[next_lbead]

+ for next_rbead in next_layer.beads:

+ if not weights[rbead, next_rbead]:

+ next_rorder = order_by_bead[next_rbead]

+ if next_rorder >= next_lorder:

+ total += weights[lbead, next_lbead]*weights[rbead, next_rbead]

+ best_crossings = crossings()

+ best_order_by_bead = order_by_bead.copy()

+ next_layouts = zip(graph.layers[1:], graph.layers[:-1])

+ current_layouts = zip(graph.layers[:-1][::-1], graph.layers[1:][::-1])

+ for count in range(len(graph.layers)):

+ current_layouts, next_layouts = next_layouts, current_layouts

+ for layer, opposite_layer in current_layouts:

+ beads = sorted(layer.beads, key=(lambda bead: order_by_bead[bead]))

+ neighbors = sorted(order_by_bead[neighbor]

+ for neighbor in opposite_layer.beads if weights[bead,neighbor] > 0.0)

+ value = order_by_bead[bead]

+ length = len(neighbors)

+ value = neighbors[length/2]

+ value = 0.5*sum(neighbors)

+ l = neighbors[length/2-1] - neighbors[0]

+ r = neighbors[-1] - neighbors[length/2]

+ value = 1.0*(l*neighbors[length/2-1]+r*neighbors[length/2])/(l+r)

+ beads = sorted(beads, key=(lambda bead: medians[bead]))

+ for idx, bead in enumerate(beads):

+ order_by_bead[bead] = idx

+ for prev_layer, layer, next_layer in zip([None]+graph.layers[:-1],

+ graph.layers[1:]+[None])[::-1]:

+ beads = sorted(layer.beads, key=(lambda bead: order_by_bead[bead]))

+ while 0 <= idx < len(beads)-1:

+ for opposite_layer in [prev_layer, next_layer]:

+ if opposite_layer is None:

+ for left_neighbor in opposite_layer.beads:

+ left_weight = weights[left_neighbor, left]

+ for right_neighbor in opposite_layer.beads:

+ right_weight = weights[right_neighbor, right]

+ if order_by_bead[left_neighbor] > order_by_bead[right_neighbor]:

+ direct += left_weight*right_weight

+ if order_by_bead[left_neighbor] < order_by_bead[right_neighbor]:

+ reverse += left_weight*right_weight

+ order_by_bead[left] = idx+1

+ order_by_bead[right] = idx

+ curr_crossings = crossings()

+ if curr_crossings < best_crossings:

+ best_crossings = curr_crossings

+ best_order_by_bead = order_by_bead.copy()

+ #print "NEW LAYOUT:", best_crossings

+ for bead in graph.beads:

+ value = best_order_by_bead[bead]

+ graph.freeze(WithBeadOrder)

return place_simplex(graph)

if A[i,j0] > eps and B[i] > -eps:

- if B[i]/A[i,j0] <B[i0]/A[i0,j0]:

+ if B[i]/A[i,j0] < B[i0]/A[i0,j0]:

assert i0 is not None and j0 is not None

#print "solution:", solution

+def simplex_fast(A, B, C):

+ A = [[float(A[i,j]) for j in range(N)] for i in range(M)]

+ B = [float(b) for b in B]

+ C = [float(-c) for c in C]

+ X = [i for i in range(M)]

+ Y = [None for j in range(N)]

+ while not (all(c >= -eps for c in C) and all(b >= -eps for b in B)):

+ if all(b >= -eps for b in B):

+ if p0 is None or p < p0:

+ if A[i][j0] > eps and B[i] > -eps:

+ assert i0 is not None and j0 is not None

+ irange = range(i0)+range(i0+1, M)

+ jrange = range(j0)+range(j0+1, N)

+ BB[i] = B[i]-A[i][j0]*p

+ CC[j] = C[j]-A[i0][j]*q

+ X[i0], Y[j0] = Y[j0], X[i0]

+ #print "solution:", solution

def place_simplex(graph):

ensure(graph.readonly(WithLayers), "without layers", graph)

- centers = simplex(A, B, C)

+ centers = simplex_fast(A, B, C)