def massage_equations(Ads, b, verbose=False, corner=None): ''' Subtract equations from each other to generate additional constraints Helps provide additional guidance to solver for realistic delays Ex: given: a >= 10 a + b >= 100 A valid solution is: a = 100 However, a better solution is something like a = 10 b = 90 This creates derived constraints to provide more realistic results Equation pipeline Some operations may generate new equations Simplify after these to avoid unnecessary overhead on redundant constraints Similarly some operations may eliminate equations, potentially eliminating a column (ie variable) Remove these columns as necessary to speed up solving ''' assert len(Ads) == len(b), 'Ads, b length mismatch' def check_cols(): assert len(index_names(Ads)) == cols def debug(what): check_cols() if 1 or verbose: print('') print_eqns(Ads, b, verbose=verbose, label=what, lim=20) col_dist(Ads, what) #check_feasible_d(Ads, b) # Try to (intelligently) subtract equations to generate additional constraints # This helps avoid putting all delay in a single shared variable dstart = len(b) cols = len(index_names(Ads)) # Each iteration one more column is allowed until all columns are included # (and the system is stable) col_lim = 15 di = 0 while True: print n_orig = len(b) print('Loop %d, lim %d' % (di + 1, col_lim)) # Meat of the operation Ads, b = derive_eq_by_row(Ads, b, col_lim=col_lim, cmp_heuristic=True) debug("der_rows") # Run another simplify pass since new equations may have overlap with original Ads, b = simplify_rows(Ads, b, corner=corner) print('Derive row: %d => %d equations' % (n_orig, len(b))) debug("der_rows simp") # derive_cols is mostly degenerate case of derive_rows # however, it will sub out single variables a lot faster if there are a lot of them # linear vs above quadratic, might as well keep it in if 1: n_orig2 = len(b) # Meat of the operation Ads, b = derive_eq_by_col(Ads, b) debug("der_cols") # Run another simplify pass since new equations may have overlap with original Ads, b = simplify_rows(Ads, b, corner=corner) print('Derive col %d: %d => %d equations' % (di + 1, n_orig2, len(b))) debug("der_cols simp") # Doesn't help computation, but helps debugging Ads, b = sort_equations(Ads, b) debug("loop done") col_dist(Ads, 'derive done iter %d, lim %d' % (di, col_lim), lim=12) rows = len(Ads) # possible that a new equation was generated and taken away, but close enough if n_orig == len(b) and col_lim >= cols: break col_lim += col_lim / 5 di += 1 dend = len(b) print('') print('Derive net: %d => %d' % (dstart, dend)) print('') # Was experimentting to see how much the higher order columns really help # Helps debug readability Ads, b = sort_equations(Ads, b) debug("final (sorted)") print('') print('Massage final: %d => %d rows' % (dstart, dend)) cols_end = len(index_names(Ads)) print('Massage final: %d => %d cols' % (cols, cols_end)) assert cols_end == cols return Ads, b
def debug(what): check_cols() if 1 or verbose: print('') print_eqns(Ads, b, verbose=verbose, label=what, lim=20) col_dist(Ads, what)
def debug(what): if verbose: print('') print_eqns(Ads, b, verbose=verbose, label=what, lim=20) col_dist(Ads, what) check_feasible_d(Ads, b)