def load(fn_ins, simplify=False, corner=None, rm_zero=False): zero_names = OrderedSet() Ads, b = loadc_Ads_b(fn_ins, corner=corner, ico=True) if rm_zero: zero_names = rm_zero_cols(Ads) if simplify: print('Simplifying corner %s' % (corner, )) Ads, b = simplify_rows(Ads, b, remove_zd=False, corner=corner) return State(Ads, zero_names=zero_names)
def run(fout, fns_in, corner, verbose=0): Ads, b = loadc_Ads_b(fns_in, corner, ico=True) Ads, b = simplify_rows(Ads, b, corner=corner) fout.write('ico,fast_max fast_min slow_max slow_min,rows...\n') for row_b, row_ds in zip(b, Ads): # write in same format, but just stick to this corner out_b = [str(row_b) for _i in range(4)] ico = '1' items = [ico, ' '.join(out_b)] for k, v in sorted(row_ds.items()): items.append('%u %s' % (v, k)) fout.write(','.join(items) + '\n')
def run(fns_in, sub_json=None, verbose=False): assert len(fns_in) > 0 # arbitrary corner...data is thrown away Ads, b = loadc_Ads_b(fns_in, "slow_max") if sub_json: print('Subbing JSON %u rows' % len(Ads)) #pds(Ads, 'Orig') names_old = index_names(Ads) run_sub_json(Ads, sub_json, verbose=verbose) names_new = index_names(Ads) print("Sub: %u => %u names" % (len(names_old), len(names_new))) print(names_new) print('Subbed JSON %u rows' % len(Ads)) names = names_new #pds(Ads, 'Sub') else: names = index_names(Ads) # Squash into a matrix # A_ub2, b_ub2 = Adi2matrix_random(A_ubd, b, names) Amat, _bmat = Ads2matrix_linear(Ads, b) #pmatrix(Amat, 'Matrix') ''' The matrix must be fully ranked to even be considered reasonable Even then, floating point error *possibly* could make it fully ranked, although probably not since we have whole numbers Hence the slogdet check ''' print # https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.linalg.matrix_rank.html rank = np.linalg.matrix_rank(Amat) print('rank: %s / %d col' % (rank, len(names))) # doesn't work on non-square matrices if 0: # https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.linalg.slogdet.html sign, logdet = np.linalg.slogdet(Amat) # If the determinant is zero, then sign will be 0 and logdet will be -Inf if sign == 0 and logdet == float('-inf'): print('slogdet :( : 0') else: print('slogdet :) : %s, %s' % (sign, logdet)) if rank != len(names): raise Exception("Matrix not fully ranked w/ %u / %u" % (rank, len(names)))
def run(fns_in, corner, bounds_csv, verbose=False): print('Loading data') Ads, borig = loadc_Ads_b(fns_in, corner, ico=True) bounds = load_bounds(bounds_csv, corner) # verify is flattened for k in bounds.keys(): assert 'GROUP_' not in k, 'Must operate on flattened bounds' # compute our timing model delay at the given corner bgots = [] for row_ds in Ads: delays = [n * bounds[x] for x, n in row_ds.items()] bgots.append(sum(delays)) ses = (np.asarray(bgots) - np.asarray(borig))**2 mse = (ses).mean(axis=None) print('MSE aggregate: %0.1f' % mse) print('Min SE: %0.1f' % min(ses)) print('Max SE: %0.1f' % max(ses))
def load(fn_ins, simplify=False, corner=None): Ads, b = loadc_Ads_b(fn_ins, corner=corner, ico=True) if simplify: print('Simplifying corner %s' % (corner, )) Ads, b = simplify_rows(Ads, b, remove_zd=False, corner=corner) return State(Ads)
def run(fns_in, corner, run_corner, sub_json=None, bounds_csv=None, dedup=True, massage=False, outfn=None, verbose=False, **kwargs): print('Loading data') Ads, b = loadc_Ads_b(fns_in, corner, ico=True) # Remove duplicate rows # is this necessary? # maybe better to just add them into the matrix directly if dedup: oldn = len(Ads) iold = instances(Ads) Ads, b = simplify_rows(Ads, b, corner=corner) print('Simplify %u => %u rows' % (oldn, len(Ads))) print('Simplify %u => %u instances' % (iold, instances(Ads))) if sub_json: print('Sub: %u rows' % len(Ads)) iold = instances(Ads) names_old = index_names(Ads) run_sub_json(Ads, sub_json, verbose=verbose) names = index_names(Ads) print("Sub: %u => %u names" % (len(names_old), len(names))) print('Sub: %u => %u instances' % (iold, instances(Ads))) else: names = index_names(Ads) ''' Substitution .csv Special .csv containing one variable per line Used primarily for multiple optimization passes, such as different algorithms or additional constraints ''' if bounds_csv: Ads2, b2 = loadc_Ads_b([bounds_csv], corner, ico=True) bounds = Ads2bounds(Ads2, b2) assert len(bounds), 'Failed to load bounds' rows_old = len(Ads) Ads, b = filter_bounds(Ads, b, bounds, corner) print('Filter bounds: %s => %s + %s rows' % (rows_old, len(Ads), len(Ads2))) Ads = Ads + Ads2 b = b + b2 assert len(Ads) or allow_zero_eqns() assert len(Ads) == len(b), 'Ads, b length mismatch' if verbose: print print_eqns(Ads, b, verbose=verbose) #print #col_dist(A_ubd, 'final', names) if massage: try: Ads, b = massage_equations(Ads, b, corner=corner) except SimplifiedToZero: if not allow_zero_eqns(): raise print('WARNING: simplified to zero equations') Ads = [] b = [] print('Converting to numpy...') names, Anp = A_ds2np(Ads) run_corner(Anp, np.asarray(b), names, corner, outfn=outfn, verbose=verbose, **kwargs)