Пример #1
0
    def __solver__(self, p):
        f = lambda x: max(p.f(x))

        def df(x):
            F = p.f(x)
            ind = argmax(F)
            return p.df(x, ind)

        def iterfcn(*args, **kwargs):
            p2.primalIterFcn(*args, **kwargs)

            p.xk = p2.xk.copy()
            p.fk = p2.fk
            p.rk = p2.rk

            p.istop = p2.istop

            if p.istop and p2.rk <= p2.contol:
                p.msg = p2.msg
            p.iterfcn()

        p2 = NSP(f, p.x0, df=df, xtol = p.xtol, ftol = p.ftol, gtol = p.gtol,\
        A=p.A,  b=p.b,  Aeq=p.Aeq,  beq=p.beq,  lb=p.lb,  ub=p.ub, \
        maxFunEvals = p.maxFunEvals, fEnough = p.fEnough, maxIter=p.maxIter, iprint = -1, \
        maxtime = p.maxTime, maxCPUTime = p.maxCPUTime,  noise = p.noise)

        if p.userProvided.c:
            p2.c, p2.dc = p.c, p.dc
        if p.userProvided.h:
            p2.h, p2.dh = p.h, p.dh

        p2.primalIterFcn, p2.iterfcn = p2.iterfcn, iterfcn

        r2 = p2.solve('ralg')
        xf = r2.xf
        p.xk = p.xf = xf
        p.fk = p.ff = max(p.f(xf))
Пример #2
0
    def __solver__(self, p):
        f = lambda x: max(p.f(x))
        def df(x):
            F = p.f(x)
            ind = argmax(F)
            return p.df(x, ind)

        def iterfcn(*args,  **kwargs):
            p2.primalIterFcn(*args,  **kwargs)

            p.xk = p2.xk.copy()
            p.fk = p2.fk
            p.rk = p2.rk

            p.istop = p2.istop

            if p.istop and p2.rk <= p2.contol:
                p.msg = p2.msg
            p.iterfcn()

        p2 = NSP(f, p.x0, df=df, xtol = p.xtol, ftol = p.ftol, gtol = p.gtol,\
        A=p.A,  b=p.b,  Aeq=p.Aeq,  beq=p.beq,  lb=p.lb,  ub=p.ub, \
        maxFunEvals = p.maxFunEvals, fEnough = p.fEnough, maxIter=p.maxIter, iprint = -1, \
        maxtime = p.maxTime, maxCPUTime = p.maxCPUTime,  noise = p.noise)

        if p.userProvided.c:
            p2.c,  p2.dc = p.c,  p.dc
        if p.userProvided.h:
            p2.h,  p2.dh = p.h,  p.dh


        p2.primalIterFcn,  p2.iterfcn = p2.iterfcn, iterfcn

        r2 = p2.solve('ralg')
        xf = r2.xf
        p.xk = p.xf = xf
        p.fk = p.ff = max(p.f(xf))
Пример #3
0
    def __solver__(self, p):
        if SMALL_DELTA_X in p.kernelIterFuncs.keys(): p.kernelIterFuncs.pop(SMALL_DELTA_X)
        if SMALL_DELTA_F in p.kernelIterFuncs.keys(): p.kernelIterFuncs.pop(SMALL_DELTA_F)

        if self.nspSolver == 'autoselect':
            nspSolver = 'amsg2p' if p.isUC else 'ralg'
        else:
            nspSolver = self.nspSolver
        
#        nspSolver = 'ralg'

        way = 3 if nspSolver == 'ralg' else 2
       
        if way == 1:
            use2 = False
            f = lambda x: sum(abs(p.f(x)))
            def df(x):
                return dot(p.df(x),  sign(p.f(x)))
        elif way == 2:
            use2 = True
            f = lambda x: sum(p.f(x)**2)
            def df(x):
                return 2.0*dot(p.f(x),  p.df(x))
        elif way == 3:
            use2 = False
            f = lambda x: max(abs(p.f(x)))
            def df(x):
                F = p.f(x)
                ind = argmax(abs(F))
                return p.df(x, ind) * sign(F[ind])

        FTOL = p.ftol**2 if use2 else p.ftol
        def iterfcn(*args,  **kwargs):
            p2.primalIterFcn(*args,  **kwargs)
            p.xk = p2.xk.copy()
            Fk = norm(p.f(p.xk), inf)
            p.rk = p.getMaxResidual(p.xk)

            #TODO: ADD p.rk

            if p.nEvals['f'] > p.maxFunEvals:
                p.istop = p2.istop = IS_MAX_FUN_EVALS_REACHED
            elif p2.istop!=0:
                if Fk < FTOL and p.rk < p.contol:
                    p.istop = 15
                    msg_contol = '' if p.isUC else 'and contol '
                    p.msg = 'solution with required ftol ' + msg_contol+ 'has been reached'
                else: p.istop = p2.istop

            p.iterfcn()
            
            return p.istop


        p2 = NSP(f, p.x0, df=df, xtol = p.xtol/1e16, gtol = p.gtol/1e16,\
        A=p.A,  b=p.b,  Aeq=p.Aeq,  beq=p.beq,  lb=p.lb,  ub=p.ub, \
        maxFunEvals = p.maxFunEvals, fEnough = FTOL, maxIter=p.maxIter, iprint = -1, \
        maxtime = p.maxTime, maxCPUTime = p.maxCPUTime,  noise = p.noise, fOpt = 0.0)

        if p.userProvided.c:
            p2.c,  p2.dc = p.c,  p.dc
        if p.userProvided.h:
            p2.h,  p2.dh = p.h,  p.dh

        p2.primalIterFcn,  p2.iterfcn = p2.iterfcn, iterfcn

        if p.debug: p2.iprint = 1
        
        if nspSolver == 'ralg':
            if p.isUC: p2.ftol = p.ftol / 1e16
            else: p2.ftol = 0.0
        else:
            p2.ftol = 0.0
            p2.xtol = 0.0
            p2.gtol = 0.0
        if use2:
            p2.fTol = 0.5*p.ftol ** 2
        else:
            p2.fTol = 0.5*p.ftol
        r2 = p2.solve(nspSolver)
        #xf = fsolve(p.f, p.x0, fprime=p.df, xtol = p.xtol, maxfev = p.maxFunEvals)
        xf = r2.xf
        p.xk = p.xf = xf
        p.fk = p.ff = asfarray(norm(p.f(xf), inf)).flatten()
Пример #4
0
    if tmp < cb.TMP:
#        print 'distance:', tmp, 'itn:', p.iter, 'n_func:', p.nEvals['f'], 'n_grad:', -p.nEvals['df']
        cb.TMP = tmp
        cb.stat['dist'].append(tmp)
        cb.stat['f'].append(p.nEvals['f'])
        cb.stat['df'].append(-p.nEvals['df'])
    return False
asa = lambda x:asarray(x).reshape(-1, 1)
Colors = ['r', 'k','b']
lines = []
R = {}

for i, solver in enumerate(solvers):
    p = NSP(obj, startPoint, fixedVars=(x[0], x[-1]), maxTime = 20, name = 'Rzhevsky7 (nVars: ' + str(n)+')', maxFunEvals=1e7, color = Colors[i])
    p._prepare()
    p.c=None
    #p.fEnough = 2.08983385058799+4e-10
    p.fOpt = obj(T_optPoint)
    p.fTol = 0.5e-15
    cb.TMP = 1000
    cb.stat = {'dist':[], 'f':[], 'df':[]}
    r = p.solve(solver, iprint=10, xtol = 1e-10, ftol = 1e-16, gtol = 1e-10, debug=0, show = solver == solvers[-1], plot = 0, callback = cb)
    print('objective evals: %d   gradient evals: %d ' % (r.evals['f'],r.evals['df']))
    print('distance to f*: %0.1e' % (r.ff-p.fOpt))
    print('distance to x*: %0.1e' % (norm(asarray(X) - hstack((X[0], p.xk, X[-1])))))
    R[solver] = hstack((asa(cb.stat['dist']), asa(cb.stat['f']), asa(cb.stat['df'])))
'''
--------------------------------------------------
solver: gsubg   problem: rjevsky6 (nVars: 30)    type: NSP   goal: minimum
 iter    objFunVal    log10(MaxResidual/ConTol)   
    0  2.954e+01                   -100.00 
Пример #5
0
asa = lambda x: asarray(x).reshape(-1, 1)
Colors = ['r', 'k', 'b']
lines = []
R = {}

for i, solver in enumerate(solvers):
    p = NSP(obj,
            startPoint,
            fixedVars=(x[0], x[-1]),
            maxTime=20,
            name='Rzhevsky7 (nVars: ' + str(n) + ')',
            maxFunEvals=1e7,
            color=Colors[i])
    p._prepare()
    p.c = None
    #p.fEnough = 2.08983385058799+4e-10
    p.fOpt = obj(T_optPoint)
    p.fTol = 0.5e-15
    cb.TMP = 1000
    cb.stat = {'dist': [], 'f': [], 'df': []}
    r = p.solve(solver,
                iprint=10,
                xtol=1e-10,
                ftol=1e-16,
                gtol=1e-10,
                debug=0,
                show=solver == solvers[-1],
                plot=0,
                callback=cb)
    print('objective evals: %d   gradient evals: %d ' %