timfuz: leastsq reasonable delays

Signed-off-by: John McMaster <johndmcmaster@gmail.com>
This commit is contained in:
John McMaster 2018-08-29 13:31:53 -07:00
parent 41cf8469b5
commit 9e63d9ee55
1 changed files with 33 additions and 14 deletions

View File

@ -19,6 +19,17 @@ import numpy
import scipy.optimize as optimize import scipy.optimize as optimize
from scipy.optimize import least_squares from scipy.optimize import least_squares
def mkestimate(Anp, b):
cols = len(Anp[0])
x0 = np.array([1e3 for _x in range(cols)])
for row_np, row_b in zip(Anp, b):
for coli, val in enumerate(row_np):
if val:
ub = row_b / val
if ub >= 0:
x0[coli] = min(x0[coli], ub)
return x0
def run_corner(Anp, b, names, verbose=False, opts={}, meta={}, outfn=None): def run_corner(Anp, b, names, verbose=False, opts={}, meta={}, outfn=None):
# Given timing scores for above delays (-ps) # Given timing scores for above delays (-ps)
assert type(Anp[0]) is np.ndarray, type(Anp[0]) assert type(Anp[0]) is np.ndarray, type(Anp[0])
@ -54,21 +65,31 @@ def run_corner(Anp, b, names, verbose=False, opts={}, meta={}, outfn=None):
if rows < cols: if rows < cols:
raise Exception("rows must be >= cols") raise Exception("rows must be >= cols")
def func(params, xdata, ydata): def func(params):
return (ydata - np.dot(xdata, params)) return (b - np.dot(Anp, params))
print('') print('')
# Now find smallest values for delay constants # Now find smallest values for delay constants
# Due to input bounds (ex: column limit), some delay elements may get eliminated entirely # Due to input bounds (ex: column limit), some delay elements may get eliminated entirely
print('Running leastsq w/ %d r, %d c (%d name)' % (rows, cols, len(names))) print('Running leastsq w/ %d r, %d c (%d name)' % (rows, cols, len(names)))
x0 = np.array([0.0 for _x in range(cols)]) # starting at 0 completes quicky, but gives a solution near 0 with terrible results
x, cov_x, infodict, mesg, ier = optimize.leastsq(func, x0, args=(Anp, b), full_output=True) # maybe give a starting estimate to the smallest net delay with the indicated variable
print('x', x) #x0 = np.array([1000.0 for _x in range(cols)])
print('cov_x', cov_x) x0 = mkestimate(Anp, b)
print('infodictx', infodict) #print('x0', x0)
print('mesg', mesg) if 0:
print('ier', ier) x, cov_x, infodict, mesg, ier = optimize.leastsq(func, x0, args=(), full_output=True)
print(' Solution found: %s' % (ier in (1, 2, 3, 4))) print('x', x)
print('cov_x', cov_x)
print('infodictx', infodict)
print('mesg', mesg)
print('ier', ier)
print(' Solution found: %s' % (ier in (1, 2, 3, 4)))
else:
res = least_squares(func, x0, bounds=(0, float('inf')))
print(res)
print('')
print(res.x)
def main(): def main():
import argparse import argparse
@ -240,8 +261,6 @@ def test5():
print(res) print(res)
def test6(): def test6():
from scipy.optimize import least_squares
def func(params): def func(params):
return (ydata - np.dot(xdata, params)) return (ydata - np.dot(xdata, params))
@ -262,11 +281,11 @@ def test6():
print(res) print(res)
if __name__ == '__main__': if __name__ == '__main__':
#main() main()
#test1() #test1()
#test2() #test2()
#test3() #test3()
#test4() #test4()
#test5() #test5()
test6() #test6()