예제 #1
0
def tst_prob1(multicore):
    def prob1(x):
        return (
            5.0 * x[0] * x[0]
            + 2.0 * x[1] * x[1]
            + 2.0 * x[2] * x[2]
            + 2.0 * x[0] * x[1]
            + 2.0 * x[1] * x[2]
            - 2.0 * x[0] * x[2]
            - 6.0 * x[2]
        )

    x0 = (0.0, 0.0, 0.0)
    xmin = (-10.0, -10, -10)
    xmax = (10.0, 10, 10)
    maxfev = 4096
    verbose = False
    ftol = numpy.float_(numpy.finfo(numpy.float32).eps)
    step = None
    initsimplex = 0
    finalsimplex = 0
    starttime = time.time()
    print neldermead(
        prob1, x0, xmin, xmax, ftol, maxfev, initsimplex, finalsimplex, step=None, verbose=False, multicore=multicore
    )
    print "%f secs" % (time.time() - starttime)
    solution = (1.0, -2.0, 3.0)
    print prob1(solution)
예제 #2
0
def tst_prob2(multicore):
    def prob2(x):
        return 2.0 * x[0] * x[0] + x[1] * x[1] - x[0] * x[1]

    x0 = (2.0, 2.0)
    xmin = (-10., -10)
    xmax = (10., 10)
    maxfev = 4096
    verbose = False
    ftol = numpy.float_(numpy.finfo(numpy.float32).eps)
    step = None
    initsimplex = 0
    finalsimplex = 0
    starttime = time.time()
    print neldermead(prob2,
                     x0,
                     xmin,
                     xmax,
                     ftol,
                     maxfev,
                     initsimplex,
                     finalsimplex,
                     step=None,
                     verbose=False,
                     multicore=multicore)
    print '%f secs' % (time.time() - starttime)
    solution = (0.0, 0.0)
    print prob2(solution)
예제 #3
0
def tst_rosen(npar, multicore):
    x0 = npar * [-1.2, 1.0]
    xmin = npar * [-10, -10.0]
    xmax = npar * [10, 10]
    maxfev = npar * 4096 * 16
    verbose = False
    ftol = numpy.float_(numpy.finfo(numpy.float32).eps)
    step = None
    initsimplex = 0
    finalsimplex = 0
    starttime = time.time()
    print neldermead(
        rosen, x0, xmin, xmax, ftol, maxfev, initsimplex, finalsimplex, step=step, verbose=verbose, multicore=multicore
    )
    print "%f secs" % (time.time() - starttime)
예제 #4
0
파일: opt.py 프로젝트: anetasie/sherpa-old
def tst_prob2( multicore ):
    def prob2( x ):
        return 2.0 * x[ 0 ] * x[ 0 ] + x[ 1 ] * x[ 1 ] - x[ 0 ] * x[ 1 ]

    x0 = ( 2.0, 2.0 )
    xmin = ( -10., -10 )
    xmax = ( 10., 10 )
    maxfev = 4096
    verbose = False
    ftol = numpy.float_(numpy.finfo(numpy.float32).eps)
    step = None
    initsimplex = 0
    finalsimplex = 0
    starttime = time.time( )
    print neldermead( prob2, x0, xmin, xmax, ftol, maxfev, initsimplex,
                      finalsimplex, step=None, verbose=False,
                      multicore=multicore )
    print '%f secs' % ( time.time() - starttime )
    solution = ( 0.0, 0.0 )
    print prob2( solution )
예제 #5
0
def tst_rosen(npar, multicore):
    x0 = npar * [-1.2, 1.0]
    xmin = npar * [-10, -10.]
    xmax = npar * [10, 10]
    maxfev = npar * 4096 * 16
    verbose = False
    ftol = numpy.float_(numpy.finfo(numpy.float32).eps)
    step = None
    initsimplex = 0
    finalsimplex = 0
    starttime = time.time()
    print neldermead(rosen,
                     x0,
                     xmin,
                     xmax,
                     ftol,
                     maxfev,
                     initsimplex,
                     finalsimplex,
                     step=step,
                     verbose=verbose,
                     multicore=multicore)
    print '%f secs' % (time.time() - starttime)
예제 #6
0
    model = ngauss(x, pars[0], pars[1], pars[2])
    delta = model - y
    statval = (delta * delta).sum()
    # Keep a record of the parameters we've visited
    store.add_row([statval] + list(pars))
    return statval, delta


start = [y.max(), (x[0] + x[-1]) / 2, (x[-1] - x[0]) / 10]
lows = [0, x[0], 0]
his = [1e4, x[-1], x[-1] - x[0]]

store = Table(names=['stat', 'ampl', 'pos', 'fwhm'])
flag, bestfit, statval, msg, opts = optfcts.neldermead(cb,
                                                       start,
                                                       lows,
                                                       his,
                                                       ftol=1e-4)
print(flag)
print(bestfit)
print(statval)
print(opts)
print(len(store))

plt.clf()
plt.plot(x, y, label='Data', alpha=0.5)
plt.plot(x, ngauss(x, *start), label='Start')
plt.plot(x, ngauss(x, *bestfit), label='Best fit', c='k')
plt.legend()

savefig('normgauss1d-example.png')