Ejemplo n.º 1
0
def solve_dual(cap_array, fx, dfx, gm, Budg):
    """
    Input:capacity array (e.g. vertices of U) or its nec.measure component, functions, gradients, global maximums of capacities
    Finds the robust capacity, i.e. the solution of dual problem
    Capacities assumed to be 2-MONOTONE
    Output: v^r
    see max_dual() for scipy version
    """
    x0 = array([1. / shape(cap_array)[0] for i in range(shape(cap_array)[0])])
    #    print x0
    Aeq = ones(shape(cap_array)[0])
    beq = Budg
    A = diag(-ones(shape(cap_array)[0]))
    b = zeros(shape(cap_array)[0])
    vmax = gm

    # vmax = array([chq.Choquet(array(chq.max_choquet(v,Budg)),v) for v in cap_array])

    def fprime(x, cap_array, Budg):
        cap_int = dot(cap_array.T, x)
        x_int = array(max_choquet(cap_int, fx, dfx, Budg))
        ch_xr = array([Choquet(x_int, v, fx) for v in cap_array])
        ch_diffs = ch_xr - vmax
        return ch_diffs

    def objfunc(x, cap_array, Budg):
        cap_int = dot(cap_array.T, x)
        x_int = array(max_choquet(cap_int, fx, dfx, Budg))
        ch_xr = array([Choquet(x_int, v, fx) for v in cap_array])
        ch_diffs = ch_xr - vmax
        #        print  dot(x, ch_diffs)
        return dot(x, ch_diffs)

    objfunc_w = lambda x: objfunc(x, cap_array, Budg)
    fprime_w = lambda x: fprime(x, cap_array, Budg)
    p = NSP(objfunc_w,
            x0,
            df=fprime_w,
            A=A,
            b=b,
            Aeq=Aeq,
            beq=beq,
            iprint=25,
            maxIter=300,
            maxFunEvals=1e7,
            diffint=1e-10,
            xtol=1e-9,
            ftol=1e-7,
            gtol=1e-5)
    r = p.solve('ralg')
    print r.xf, r.ff
    cap_mix = dot(cap_array.T, array(r.xf))
    return r.ff, cap_mix
def solve_dual(cap_array,fx,dfx,gm,Budg):
    """
    Input:capacity array (e.g. vertices of U) or its nec.measure component, functions, gradients, global maximums of capacities
    Finds the robust capacity, i.e. the solution of dual problem
    Capacities assumed to be 2-MONOTONE
    Output: v^r
    see max_dual() for scipy version
    """
    x0 = array([1./shape(cap_array)[0] for i in range(shape(cap_array)[0])])
#    print x0
    Aeq = ones(shape(cap_array)[0])
    beq = Budg
    A = diag(-ones(shape(cap_array)[0]))
    b = zeros(shape(cap_array)[0])
    vmax=gm
    # vmax = array([chq.Choquet(array(chq.max_choquet(v,Budg)),v) for v in cap_array])
    
    def fprime(x,cap_array,Budg):
        cap_int = dot(cap_array.T,x)
        x_int = array(max_choquet(cap_int,fx,dfx,Budg))
        ch_xr = array([Choquet(x_int,v,fx) for v in cap_array])
        ch_diffs = ch_xr - vmax
        return ch_diffs

    def objfunc(x,cap_array,Budg):
        cap_int = dot(cap_array.T,x)
        x_int = array(max_choquet(cap_int,fx,dfx,Budg))
        ch_xr = array([Choquet(x_int,v,fx) for v in cap_array])
        ch_diffs = ch_xr - vmax
#        print  dot(x, ch_diffs)
        return dot(x, ch_diffs)
    
    objfunc_w = lambda x: objfunc(x,cap_array,Budg)
    fprime_w = lambda x: fprime(x,cap_array,Budg)
    p = NSP(objfunc_w, x0, df=fprime_w,  A=A,  b=b,  Aeq=Aeq,  beq=beq, iprint = 25, maxIter = 300, maxFunEvals = 1e7, diffint=1e-10, xtol=1e-9,ftol=1e-7,gtol=1e-5)
    r = p.solve('ralg')
    print r.xf, r.ff                      
    cap_mix = dot(cap_array.T, array(r.xf))
    return r.ff,cap_mix                      
Ejemplo n.º 3
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))
Ejemplo n.º 4
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))
Ejemplo n.º 5
0
from openopt import NSP
from numpy import cos, asfarray, arange, sign

N = 75
f = lambda x: sum(1.2 ** arange(len(x)) * abs(x))
df = lambda x: 1.2 ** arange(len(x)) * sign(x)
x0 = cos(1 + asfarray(range(N)))

# non-linear constraint c(x) <= 0:
c = lambda x: abs(x[4] - 0.8) + abs(x[5] - 1.5) - 0.015

p = NSP(f, x0, df=df, c=c, callback=MyIterFcn, contol=1e-5, maxIter=1e4, iprint=100, xtol=1e-8, ftol=1e-8)

# optional:
# p.plot = 1
r = p.solve("ralg")
print r.xf[:8]

"""
-----------------------------------------------------
solver: ralg   problem: unnamed   goal: minimum
 iter    objFunVal    log10(maxResidual)
    0  2.825e+06               0.02
--= non-feasible ralg iter =--
itn: 0
curr f: [ 2824966.83813157]
max constraint value 1.04116752789
--= non-feasible ralg iter =--
itn: 1
curr f: [ 2824973.2896607]
max constraint value 1.75725959686
Ejemplo n.º 6
0
x, y = oovars('x y')

N = 75
koeffs = arange(1, N + 1)**1.2  # 1, 1.2, 1.44, ..., 1.2^m, ..., 1.2^N

objective = sum(abs(x) * koeffs) + abs(y - 15) + abs(y + 15) + y**2
constraints = [(y - 1)**2 < 1,
               abs(y) < 0.5,
               abs(x[0]) < 1e-5,
               abs(x[N - 1]) < 1e-5]
constraints.append(
    (x - 0.01 * arange(N))**2 < 0.1 * arange(1, N + 1)
)  # (x_0-0)**2 < 0.1, (x_1-0.01)**2 < 0.2, (x_2-0.02)**2 < 0.2,...
startPoint = {x: cos(1 + arange(N)), y: 80}

p = NSP(objective, startPoint, maxIter=1e5, constraints=constraints)

r = p.solve('ralg')
x_opt, y_opt = x(r), y(r)
print(max(abs(x_opt)), y_opt)
'''
expected output:
[...]
  876  3.004e+01            -100.00 
istop: 4 (|| F[k] - F[k-1] || < ftol)
Solver:   Time Elapsed = 7.98 	CPU Time Elapsed = 7.97
objFunValue: 30.042539 (feasible, MaxResidual = 0)
(6.6277698279489041e-06, 0.20306221768582972)
'''
Ejemplo n.º 7
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()
Ejemplo n.º 8
0
    def __solver__(self, p):
        n = p.n
        x0 = copy(p.x0)
        xPrev = x0.copy()
        xf = x0.copy()
        xk = x0.copy()
        p.xk = x0.copy()

        f0 = p.f(x0)

        fk = f0
        ff = f0
        p.fk = fk

        df0 = p.df(x0)

        #####################################################################

        ##        #handling box-bounded problems
        ##        if p.__isNoMoreThanBoxBounded__():
        ##            for k in range(int(p.maxIter)):
        ##
        ##        #end of handling box-bounded problems
        isBB = p.__isNoMoreThanBoxBounded__()
        ##        isBB = 0
        H = diag(ones(p.n))
        if not p.userProvided.c:
            p.c = lambda x: array([])
            p.dc = lambda x: array([]).reshape(0, p.n)
        if not p.userProvided.h:
            p.h = lambda x: array([])
            p.dh = lambda x: array([]).reshape(0, p.n)

        p.use_subproblem = 'QP'

        #p.use_subproblem = 'LLSP'

        for k in range(p.maxIter + 4):
            if isBB:
                f0 = p.f(xk)
                df = p.df(xk)
                direction = -df
                f1 = p.f(xk + direction)
                ind_l = direction <= p.lb - xk
                direction[ind_l] = (p.lb - xk)[ind_l]
                ind_u = direction >= p.ub - xk
                direction[ind_u] = (p.ub - xk)[ind_u]
                ff = p.f(xk + direction)
##                print 'f0', f0, 'f1', f1, 'ff', ff
            else:

                mr = p.getMaxResidual(xk)
                if mr > p.contol: mr_grad = p.getMaxConstrGradient(xk)
                lb = p.lb - xk  #- p.contol/2
                ub = p.ub - xk  #+ p.contol/2
                c, dc, h, dh, df = p.c(xk), p.dc(xk), p.h(xk), p.dh(xk), p.df(
                    xk)
                A, Aeq = vstack((dc, p.A)), vstack((dh, p.Aeq))
                b = concatenate((-c, p.b - p.matmult(p.A, xk)))  #+ p.contol/2
                beq = concatenate((-h, p.beq - p.matmult(p.Aeq, xk)))

                if b.size != 0:
                    isFinite = isfinite(b)
                    ind = where(isFinite)[0]
                    A, b = A[ind], b[ind]
                if beq.size != 0:
                    isFinite = isfinite(beq)
                    ind = where(isFinite)[0]
                    Aeq, beq = Aeq[ind], beq[ind]

                if p.use_subproblem == 'LP':  #linear
                    linprob = LP(df, A=A, Aeq=Aeq, b=b, beq=beq, lb=lb, ub=ub)
                    linprob.iprint = -1
                    r2 = linprob.solve(
                        'cvxopt_glpk')  # TODO: replace lpSolve by autoselect
                    if r2.istop <= 0:
                        p.istop = -12
                        p.msg = "failed to solve LP subproblem"
                        return
                elif p.use_subproblem == 'QP':  #quadratic
                    qp = QP(H=H,
                            f=df,
                            A=A,
                            Aeq=Aeq,
                            b=b,
                            beq=beq,
                            lb=lb,
                            ub=ub)
                    qp.iprint = -1
                    r2 = qp.solve(
                        'cvxopt_qp')  # TODO: replace solver by autoselect
                    #r2 = qp.solve('qld') # TODO: replace solver by autoselect
                    if r2.istop <= 0:
                        for i in range(4):
                            if p.debug:
                                p.warn("iter " + str(k) + ": attempt Num " +
                                       str(i) +
                                       " to solve QP subproblem has failed")
                            #qp.f += 2*N*sum(qp.A,0)
                            A2 = vstack((A, Aeq, -Aeq))
                            b2 = concatenate(
                                (b, beq, -beq)) + pow(10, i) * p.contol
                            qp = QP(H=H, f=df, A=A2, b=b2, iprint=-5)
                            qp.lb = lb - pow(10, i) * p.contol
                            qp.ub = ub + pow(10, i) * p.contol
                            # I guess lb and ub don't matter here
                            try:
                                r2 = qp.solve(
                                    'cvxopt_qp'
                                )  # TODO: replace solver by autoselect
                            except:
                                r2.istop = -11
                            if r2.istop > 0: break
                        if r2.istop <= 0:
                            p.istop = -11
                            p.msg = "failed to solve QP subproblem"
                            return
                elif p.use_subproblem == 'LLSP':
                    direction_c = getConstrDirection(p,
                                                     xk,
                                                     regularization=1e-7)
                else:
                    p.err('incorrect or unknown subproblem')

            if isBB:
                X0 = xk.copy()
                N = 0
                result, newX = chLineSearch(p, X0, direction, N, isBB)
            elif p.use_subproblem != 'LLSP':
                duals = r2.duals
                N = 1.05 * abs(duals).sum()
                direction = r2.xf
                X0 = xk.copy()
                result, newX = chLineSearch(p, X0, direction, N, isBB)
            else:  # case LLSP
                direction_f = -df
                p2 = NSP(LLSsubprobF, [0.8, 0.8],
                         ftol=0,
                         gtol=0,
                         xtol=1e-5,
                         iprint=-1)
                p2.args.f = (xk, direction_f, direction_c, p, 1e20)
                r_subprob = p2.solve('ralg')
                alpha = r_subprob.xf
                newX = xk + alpha[0] * direction_f + alpha[1] * direction_c

                #                dw = (direction_f * direction_c).sum()
                #                cos_phi = dw/p.norm(direction_f)/p.norm(direction_c)
                #                res_0, res_1 = p.getMaxResidual(xk), p.getMaxResidual(xk+1e-1*direction_c)
                #                print cos_phi, res_0-res_1

                #                res_0 = p.getMaxResidual(xk)
                #                optimConstrPoint = getDirectionOptimPoint(p, p.getMaxResidual, xk, direction_c)
                #                res_1 = p.getMaxResidual(optimConstrPoint)
                #
                #                maxConstrLimit = p.contol

                #xk = getDirectionOptimPoint(p, p.f, optimConstrPoint, -optimConstrPoint+xk+direction_f, maxConstrLimit = maxConstrLimit)
                #print 'res_0', res_0, 'res_1', res_1, 'res_2', p.getMaxResidual(xk)
                #xk = getDirectionOptimPoint(p, p.f, xk, direction_f, maxConstrLimit)
                #newX = xk.copy()
                result = 0


#                x_0 = X0.copy()
#                N = j = 0
#                while p.getMaxResidual(x_0) > Residual0 + 0.1*p.contol:
#                    j += 1
#                    x_0 = xk + 0.75**j * (X0-xk)
#                X0 = x_0
#                result, newX = 0, X0
#                print 'newIterResidual = ', p.getMaxResidual(x_0)

            if result != 0:
                p.istop = result
                p.xf = newX
                return

            xk = newX.copy()
            fk = p.f(xk)

            p.xk, p.fk = copy(xk), copy(fk)
            #p._df = p.df(xk)
            ####################
            p.iterfcn()

            if p.istop:
                p.xf = xk
                p.ff = fk
                #p._df = g FIXME: implement me
                return
Ejemplo n.º 9
0
        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)

R = {}
for i, solver in enumerate(solvers):
    p = NSP(obj, startPoint, maxIter = 17000, name = 'Rzhevsky2 (nVars: ' + str(n)+')', maxTime = 300, maxFunEvals=1e7, color = Colors[i])
    p.fTol = 0.5e-12
    p.maxTime = 100
    #p.fEnough = -0.841408334595
    p.fOpt = -0.841408334596
    cb.TMP = 1000
    cb.stat = {'dist':[], 'f':[], 'df':[]}
    r = p.solve(solver, iprint=10, ftol = 1e-15, xtol = 1e-12, show = solver == solvers[-1], plot = 0, callback = cb)
    R[solver] = hstack((asa(cb.stat['dist']), asa(cb.stat['f']), asa(cb.stat['df'])))
'''
--------------------------------------------------
solver: gsubg   problem: rjevsky2 (nVars: 10)    type: NSP   goal: minimum
 iter    objFunVal   
    0  5.337e+03 
   10  -5.386e-01 
   20  -8.399e-01 
   30  -8.414e-01 
   40  -8.414e-01 
   50  -8.414e-01 
   53  -8.414e-01 
istop: 16 (optimal solution wrt required fTol has been obtained)
Solver:   Time Elapsed = 4.55 	CPU Time Elapsed = 4.54
objFunValue: -0.84138193
Ejemplo n.º 10
0
from numpy import cos, arange
from FuncDesigner import *
from openopt import NSP

x, y = oovars('x y')

N = 75
koeffs = arange(1, N+1) ** 1.2 # 1, 1.2, 1.44, ..., 1.2^m, ..., 1.2^N

objective = sum(abs(x) * koeffs) + abs(y-15) + abs(y+15) + y**2
constraints = [(y-1)**2<1, abs(y) < 0.5, abs(x[0]) < 1e-5, abs(x[N-1]) < 1e-5]
constraints.append((x - 0.01*arange(N))**2 < 0.1*arange(1, N+1)) # (x_0-0)**2 < 0.1, (x_1-0.01)**2 < 0.2, (x_2-0.02)**2 < 0.2,...
startPoint = {x: cos(1+arange(N)), y:80}

p = NSP(objective, startPoint, maxIter = 1e5, constraints = constraints)

r = p.solve('ralg')
x_opt, y_opt = x(r), y(r)
print(max(abs(x_opt)), y_opt)

'''
expected output:
[...]
  876  3.004e+01            -100.00 
istop: 4 (|| F[k] - F[k-1] || < ftol)
Solver:   Time Elapsed = 7.98 	CPU Time Elapsed = 7.97
objFunValue: 30.042539 (feasible, MaxResidual = 0)
(6.6277698279489041e-06, 0.20306221768582972)
'''
Ejemplo n.º 11
0

print 'start point: f1 = %e   f2 = %e' % (f1(startPoint), f2(startPoint))
#print "start point: norm(f1') = %e   norm(f2') = %e" % (norm(f1.D(startPoint, y)), norm(f2.D(startPoint, x)))

ralg = oosolver('ralg')
gsubg = oosolver('gsubg', addASG = False)

solvers = [ralg]

#solvers = [oosolver('gsubg', zhurb = 20, dual=False)]
solvers = ['ipopt', gsubg, 'scipy_cg']
solvers = [gsubg]
#solvers = ['ipopt']
#solvers = ['slmvm2']
#solvers = ['slmvm1']
#solvers = ['mma']
Colors = ['r', 'k','b']

lines = []
for i, solver in enumerate(solvers):
    p = NSP(f, startPoint, maxIter = 300, name = 'ns' + str(N+K), maxTime = 15000, maxFunEvals=1e7, color = Colors[i])
    #p.constraints = y>-100
    p.fEnough = 1.0e-1#e-1
    p.fTol = 0.5e-1
    p.debug = 1
    #p.constraints = (y > 5)(tol=1e-4) #x>1e-1 #[2*y<sin(arange(N))]
    #r = p.manage(solver, iprint=10, xtol = 1e-9, ftol = 1e-9, show = solver == solvers[-1], maxIter = 10000)
    r = p.solve(solver, iprint=10, xtol = 1e-6, ftol = 1e-6, debug=0, show = solver == solvers[-1], plot = 0)
    print 'end point: f1 = %e   f2 = %e' % (f1(r.xf), f2(r.xf))
Ejemplo n.º 12
0
f_opt = 0
"""

from numpy import *
from openopt import NSP

N = 75
objFun = lambda x: sum(1.2 ** arange(len(x)) * abs(x))

x0 = cos(1+asfarray(range(N)))

p = NSP(objFun, x0, maxFunEvals = 1e7, xtol = 1e-8)
#These assignments are also correct:
#p = NLP(objFun, x0=x0)
#p = NLP(f=objFun, x0=x0)
#p = NLP(ftol = 1e-5, contol = 1e-5, f=objFun, x0=x0)


p.maxIter = 5000

#optional (requires matplotlib installed)
#p.plot = 1
#p.graphics.xlabel = 'cputime'#default: time, case-unsensetive; also here maybe 'cputime', 'niter'

#OPTIONAL: user-supplied gradient/subgradient
p.df = lambda x: 1.2 ** arange(len(x)) * sign(x)

r = p.solve('ralg') # ralg is name of a solver
print('x_opt: %s' % r.xf)
print('f_opt: %f' % r.ff)  # should print small positive number like 0.00056
Ejemplo n.º 13
0
    return False
asa = lambda x:asarray(x).reshape(-1, 1)
solvers = ['ralg', 'amsg2p', 'gsubg']
solvers = [oosolver('amsg2p', gamma = 2.0)]
Colors = ['r', 'k','b']
lines = []
R = {}
for Tol_p in range(-10, -31, -1):
    #print('Tol = 10^%d' % Tol_p)
    for i, solver in enumerate(solvers):
        p = NSP(obj, startPoint, maxIter = 1700, name = 'Rzhevsky6 (nVars: ' + str(n)+')', maxTime = 300, maxFunEvals=1e7, color = Colors[i])
        p.fOpt = 0.0
        p.fTol = 10**Tol_p
        cb.TMP = 1000
        cb.stat = {'dist':[], 'f':[], 'df':[]}
        r = p.solve(solver, iprint=-1, xtol = 0, ftol = 0, callback = cb)
        R[solver] = hstack((asa(cb.stat['dist']), asa(cb.stat['f']), asa(cb.stat['df'])))
        print('itn df dx   %d   %0.1e   %0.1e' % (-r.evals['df'], r.ff-p.fOpt, norm(p.xk-1.0)))
#        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(p.xk-1.0))
'''
--------------------------------------------------
solver: gsubg   problem: rjevsky6 (nVars: 15)    type: NSP   goal: minimum
 iter    objFunVal   
    0  2.408e+02 
    1  9.712e+01 
    2  5.315e+01 
    3  3.569e+01 
    4  1.432e+01 
    5  8.967e+00 
Ejemplo n.º 14
0
if __name__ == '__main__':
    import sys, os.path as pth
    sys.path.insert(0,pth.split(pth.split(pth.split(pth.split(pth.realpath(pth.dirname(__file__)))[0])[0])[0])[0])
    from openopt import NSP

    from numpy import ones, arange
    N = 4
    p = NSP(lambda x: abs(x).sum(), ones([N,1]), maxFunEvals = 1e6, plot = False)
    p.r0 = N
    r = p.solve('ShorEllipsoid')

from numpy import diag, ones, inf, any, copy, sqrt
from openopt.kernel.baseSolver import baseSolver
class ShorEllipsoid(baseSolver):
    __name__ = 'ShorEllipsoid'
    __license__ = "BSD"
    __authors__ = "Dmitrey"
    __alg__ = "Naum Z. Shor modificated method of ellipsoids"
    iterfcnConnected = True
    #__optionalDataThatCanBeHandled__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h']

    def __init__(self): pass

    def __solver__(self, p):

        #if p.useScaling: p.err('ShorEllipsoid + scaling isn''t implemented yet')


        n = p.n

        B = diag(ones(n))
Ejemplo n.º 15
0
for i, solver in enumerate(solvers):
    p = NSP(
        obj,
        startPoint,
        maxIter=1700,
        name="Rzhevsky5 (nVars: " + str(n) + ")",
        maxTime=300,
        maxFunEvals=1e7,
        color=Colors[i],
    )
    p.fTol = 0.5e-10
    cb.TMP = 1000
    cb.stat = {"dist": [], "f": [], "df": []}
    p.fOpt = -34.408608965509742
    p.maxTime = 30
    r = p.solve(solver, iprint=1, xtol=1e-15, ftol=1e-15, gtol=1e-15, show=solver == solvers[-1], plot=0, callback=cb)
    R[solver] = hstack((asa(cb.stat["dist"]), asa(cb.stat["f"]), asa(cb.stat["df"])))

"""
solver: gsubg   problem: rjevsky5 (nVars: 50)    type: NSP   goal: minimum
 iter    objFunVal   
    0  -2.241e+00 
    1  -3.056e+01 
    2  -3.387e+01 
    3  -3.427e+01 
    4  -3.434e+01 
    5  -3.436e+01 
    6  -3.439e+01 
    7  -3.439e+01 
    8  -3.439e+01 
    9  -3.440e+01 
Ejemplo n.º 16
0
    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 
OpenOpt Warning: Handling of constraints is not implemented properly for the solver gsubg yet
   10  4.002e+00                   -100.00 
   20  2.232e+00                   -100.00 
   30  1.975e+00                   -100.00 
   40  1.943e+00                   -100.00 
   50  1.927e+00                   -100.00 
Ejemplo n.º 17
0
for i, solver in enumerate(solvers):
    p = NSP(obj,
            startPoint,
            maxIter=1700,
            name='Rzhevsky4 (nVars: ' + str(n) + ')',
            maxTime=300,
            maxFunEvals=1e7,
            color=Colors[i])
    p.fTol = 0.5e-20
    p.fOpt = 0.0
    cb.TMP = 1000
    cb.stat = {'dist': [], 'f': [], 'df': []}
    r = p.solve(solver,
                iprint=10,
                xtol=1e-20,
                ftol=1e-20,
                debug=0,
                show=solver == solvers[-1],
                plot=0,
                callback=cb)
    R[solver] = hstack(
        (asa(cb.stat['dist']), asa(cb.stat['f']), asa(cb.stat['df'])))
'''
--------------------------------------------------
solver: gsubg   problem: rjevsky3 (nVars: 10)    type: NSP   goal: minimum
 iter    objFunVal   
    0  9.789e-01 
   10  5.155e-03 
   12  2.168e-03 
istop: 16 (optimal solution wrt required fTol has been obtained)
Solver:   Time Elapsed = 1.1 	CPU Time Elapsed = 1.1
objFunValue: 0.002167903
Ejemplo n.º 18
0
    def __solver__(self, p):
        n = p.n
        x0 = copy(p.x0)
        xPrev = x0.copy()
        xf = x0.copy()
        xk = x0.copy()
        p.xk = x0.copy()

        f0 = p.f(x0)

        fk = f0
        ff = f0
        p.fk = fk

        df0 = p.df(x0)

        #####################################################################


##        #handling box-bounded problems
##        if p.__isNoMoreThanBoxBounded__():
##            for k in range(int(p.maxIter)):
##
##        #end of handling box-bounded problems
        isBB = p.__isNoMoreThanBoxBounded__()
##        isBB = 0
        H = diag(ones(p.n))
        if not p.userProvided.c:
            p.c = lambda x : array([])
            p.dc = lambda x : array([]).reshape(0, p.n)
        if not p.userProvided.h:
            p.h = lambda x : array([])
            p.dh = lambda x : array([]).reshape(0, p.n)

        p.use_subproblem = 'QP'

        #p.use_subproblem = 'LLSP'

        for k in range(p.maxIter+4):
            if isBB:
                f0 = p.f(xk)
                df = p.df(xk)
                direction = -df
                f1 = p.f(xk+direction)
                ind_l = direction<=p.lb-xk
                direction[ind_l] = (p.lb-xk)[ind_l]
                ind_u = direction>=p.ub-xk
                direction[ind_u] = (p.ub-xk)[ind_u]
                ff = p.f(xk + direction)
##                print 'f0', f0, 'f1', f1, 'ff', ff
            else:

                mr = p.getMaxResidual(xk)
                if mr > p.contol: mr_grad = p.getMaxConstrGradient(xk)
                lb = p.lb - xk #- p.contol/2
                ub = p.ub - xk #+ p.contol/2
                c, dc, h, dh, df = p.c(xk), p.dc(xk), p.h(xk), p.dh(xk), p.df(xk)
                A, Aeq = vstack((dc, p.A)), vstack((dh, p.Aeq))
                b = concatenate((-c, p.b-p.matmult(p.A,xk))) #+ p.contol/2
                beq = concatenate((-h, p.beq-p.matmult(p.Aeq,xk)))

                if b.size != 0:
                    isFinite = isfinite(b)
                    ind = where(isFinite)[0]
                    A, b = A[ind], b[ind]
                if beq.size != 0:
                    isFinite = isfinite(beq)
                    ind = where(isFinite)[0]
                    Aeq, beq = Aeq[ind], beq[ind]


                if p.use_subproblem == 'LP': #linear
                    linprob = LP(df, A=A, Aeq=Aeq, b=b, beq=beq, lb=lb, ub=ub)
                    linprob.iprint = -1
                    r2 = linprob.solve('cvxopt_glpk') # TODO: replace lpSolve by autoselect
                    if r2.istop <= 0:
                        p.istop = -12
                        p.msg = "failed to solve LP subproblem"
                        return
                elif p.use_subproblem == 'QP': #quadratic
                    qp = QP(H=H,f=df, A=A, Aeq=Aeq, b=b, beq=beq, lb=lb, ub = ub)
                    qp.iprint = -1
                    r2 = qp.solve('cvxopt_qp') # TODO: replace solver by autoselect
                    #r2 = qp.solve('qld') # TODO: replace solver by autoselect
                    if r2.istop <= 0:
                        for i in range(4):
                            if p.debug: p.warn("iter " + str(k) + ": attempt Num " + str(i) + " to solve QP subproblem has failed")
                            #qp.f += 2*N*sum(qp.A,0)
                            A2 = vstack((A, Aeq, -Aeq))
                            b2 = concatenate((b, beq, -beq)) + pow(10,i)*p.contol
                            qp = QP(H=H,f=df, A=A2, b=b2, iprint = -5)
                            qp.lb = lb - pow(10,i)*p.contol
                            qp.ub = ub + pow(10,i)*p.contol
                            # I guess lb and ub don't matter here
                            try:
                                r2 = qp.solve('cvxopt_qp') # TODO: replace solver by autoselect
                            except:
                                r2.istop = -11
                            if r2.istop > 0: break
                        if r2.istop <= 0:
                            p.istop = -11
                            p.msg = "failed to solve QP subproblem"
                            return
                elif p.use_subproblem == 'LLSP':
                    direction_c = getConstrDirection(p,  xk, regularization = 1e-7)
                else: p.err('incorrect or unknown subproblem')


            if isBB:
                X0 = xk.copy()
                N = 0
                result, newX = chLineSearch(p, X0, direction, N, isBB)
            elif p.use_subproblem != 'LLSP':
                duals = r2.duals
                N = 1.05*abs(duals).sum()
                direction = r2.xf
                X0 = xk.copy()
                result, newX = chLineSearch(p, X0, direction, N, isBB)
            else: # case LLSP
                direction_f = -df
                p2 = NSP(LLSsubprobF, [0.8, 0.8], ftol=0, gtol=0, xtol = 1e-5, iprint = -1)
                p2.args.f =  (xk, direction_f, direction_c, p, 1e20)
                r_subprob = p2.solve('ralg')
                alpha = r_subprob.xf
                newX = xk + alpha[0]*direction_f + alpha[1]*direction_c

#                dw = (direction_f * direction_c).sum()
#                cos_phi = dw/p.norm(direction_f)/p.norm(direction_c)
#                res_0, res_1 = p.getMaxResidual(xk), p.getMaxResidual(xk+1e-1*direction_c)
#                print cos_phi, res_0-res_1

#                res_0 = p.getMaxResidual(xk)
#                optimConstrPoint = getDirectionOptimPoint(p, p.getMaxResidual, xk, direction_c)
#                res_1 = p.getMaxResidual(optimConstrPoint)
#
#                maxConstrLimit = p.contol



                #xk = getDirectionOptimPoint(p, p.f, optimConstrPoint, -optimConstrPoint+xk+direction_f, maxConstrLimit = maxConstrLimit)
                #print 'res_0', res_0, 'res_1', res_1, 'res_2', p.getMaxResidual(xk)
                #xk = getDirectionOptimPoint(p, p.f, xk, direction_f, maxConstrLimit)
                #newX = xk.copy()
                result = 0
#                x_0 = X0.copy()
#                N = j = 0
#                while p.getMaxResidual(x_0) > Residual0 + 0.1*p.contol:
#                    j += 1
#                    x_0 = xk + 0.75**j * (X0-xk)
#                X0 = x_0
#                result, newX = 0, X0
#                print 'newIterResidual = ', p.getMaxResidual(x_0)

            if result != 0:
                p.istop = result
                p.xf = newX
                return

            xk = newX.copy()
            fk = p.f(xk)

            p.xk, p.fk = copy(xk), copy(fk)
            #p._df = p.df(xk)
            ####################
            p.iterfcn()

            if p.istop:
                p.xf = xk
                p.ff = fk
                #p._df = g FIXME: implement me
                return
Ejemplo n.º 19
0
from numpy import arange
from numpy.linalg import norm
from openopt import NSP, oosolver
from FuncDesigner import *

N = 300
x = oovar('x')
startPoint = {x: 1 + 1.0 / arange(1, N)}
S = 1e4 ** (1.0/arange(1, N))

#f = abs(x[0]) + S * abs(x[1]) + S**2 * abs(x[2])
f = sum(abs(x)*S)


solvers = [oosolver('ralg')]
solvers = [oosolver('gsubg', addASG = True)]
#solvers = [oosolver('gsubg', zhurb = 20, dual=False)]
#solvers = ['ipopt']
#solvers = ['slmvm2']
#solvers = ['mma']
for solver in solvers:
    p = NSP(f, startPoint, maxIter = 10000, maxTime = 15000, maxFunEvals=1e7)
    p.fEnough = 1.5e1
    p.fTol = 1.0e1
    #p.constraints = (y > 5)(tol=1e-4) #x>1e-1 #[2*y<sin(arange(N))]
    #r = p.solve(solver, iprint=10, xtol = 1e-36, ftol = 1e-16, show = solver == solvers[-1])
    r = p.solve(solver, iprint=10, xtol = 1e-16, ftol = 1e-6, show = solver == solvers[-1])
    

Ejemplo n.º 20
0
R = {}
for Tol_p in range(-10, -31, -1):
    #print('Tol = 10^%d' % Tol_p)
    for i, solver in enumerate(solvers):
        p = NSP(obj,
                startPoint,
                maxIter=1700,
                name='Rzhevsky6 (nVars: ' + str(n) + ')',
                maxTime=300,
                maxFunEvals=1e7,
                color=Colors[i])
        p.fOpt = 0.0
        p.fTol = 10**Tol_p
        cb.TMP = 1000
        cb.stat = {'dist': [], 'f': [], 'df': []}
        r = p.solve(solver, iprint=-1, xtol=0, ftol=0, callback=cb)
        R[solver] = hstack(
            (asa(cb.stat['dist']), asa(cb.stat['f']), asa(cb.stat['df'])))
        print('itn df dx   %d   %0.1e   %0.1e' %
              (-r.evals['df'], r.ff - p.fOpt, norm(p.xk - 1.0)))
#        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(p.xk-1.0))
'''
--------------------------------------------------
solver: gsubg   problem: rjevsky6 (nVars: 15)    type: NSP   goal: minimum
 iter    objFunVal   
    0  2.408e+02 
    1  9.712e+01 
    2  5.315e+01 
    3  3.569e+01 
Ejemplo n.º 21
0
df = lambda x: [sign(x[0]), sign(x[1])*K, sign(x[2]) * K**2]


#p.df = lambda x: 2*x
    
#p.plot = 0
#p.xlim = (inf,  5)
#p.ylim = (0, 5000000)
#p.checkdf()
solvers = ['r2', 'ipopt', 'algencan','ralg']
solvers = ['r2', 'algencan','ralg']
#solvers = ['ralg', 'r2']
solvers = ['r2', 'lincher']
solvers = ['ralg']
solvers = ['r2']
#solvers = ['scipy_slsqp']
#solvers = ['algencan']
#solvers = ['ipopt']
colors = ['r', 'b', 'k', 'g']
maxIter = 1000
for i, solver in enumerate(solvers):
    p = NSP(f, x0, df=df, xtol = 1e-11, ftol=1e-10,  maxIter = maxIter, maxTime=150)
    #p.checkdf()
    r = p.solve(solver, maxVectorNum=4, iprint=1, showLS=0, plot=0, color=colors[i], show=solver==solvers[-1]) # ralg is name of a solver
#for i, solver in enumerate(solvers):
#    p2 = NSP(f, r.xf, df=df, xtol = 1e-6, maxIter = 1200, maxTime=150, ftol=1e-6)
#    #p.checkdf()
#    r2 = p2.solve(solver, maxVectorNum=15, iprint=1, showLS=1, plot=0, color=colors[i], show=solver==solvers[-1]) # ralg is name of a solver
#print 'x_opt:\n', r.xf
print 'f_opt:', r.ff  # should print small positive number like 0.00056
Ejemplo n.º 22
0
if __name__ == '__main__':
    import sys, os.path as pth
    sys.path.insert(
        0,
        pth.split(
            pth.split(
                pth.split(pth.split(pth.realpath(
                    pth.dirname(__file__)))[0])[0])[0])[0])
    from openopt import NSP

    from numpy import ones, arange
    N = 4
    p = NSP(lambda x: abs(x).sum(), ones([N, 1]), maxFunEvals=1e6, plot=False)
    p.r0 = N
    r = p.solve('ShorEllipsoid')

from numpy import diag, ones, inf, any, copy, sqrt
from openopt.kernel.baseSolver import baseSolver


class ShorEllipsoid(baseSolver):
    __name__ = 'ShorEllipsoid'
    __license__ = "BSD"
    __authors__ = "Dmitrey"
    __alg__ = "Naum Z. Shor modificated method of ellipsoids"
    iterfcnConnected = True

    #__optionalDataThatCanBeHandled__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h']

    def __init__(self):
        pass