Ejemplo n.º 1
0
def adjustr4WithDiscreteVariables(wr4, p):
    for i in p._discreteVarsNumList:
        v = p._freeVarsList[i]
        
        if v.domain is bool or v.domain is 'bool':
            wr4[:, i] = where(wr4[:, i]<0.5, 0, 1)
        else:
            tmp = wr4[:, i]
            d = v.domain 
            if isPyPy:
                d2 = d.tolist()
                ind = atleast_1d([bisect_left(d2, val) for val in tmp])
                ind2 = atleast_1d([bisect_right(d2, val) for val in tmp])
            else:
                ind = searchsorted(d, tmp, side='left')
                ind2 = searchsorted(d, tmp, side='right')
            ind3 = where(ind!=ind2)[0]
            Tmp = tmp[ind3].copy()
            
            ind[ind==d.size] -= 1 # may be due to roundoff errors
            ind[ind==1] = 0
            ind2[ind2==d.size] -=1
            ind2[ind2==0] = 1 # may be due to roundoff errors
            tmp1 = asarray(d[ind], p.solver.dataType)
            tmp2 = asarray(d[ind2], p.solver.dataType)
            if Tmp.size!=0:
                if str(tmp1.dtype).startswith('int'):
                    Tmp = asarray(Tmp, p.solver.dataType)
                tmp2[ind3] = Tmp.copy()
                tmp1[ind3] = Tmp.copy()
            tmp = where(abs(tmp-tmp1)<abs(tmp-tmp2), tmp1, tmp2)
            #print max(abs(tmp-tmp1)), max(abs(tmp-tmp2))
            wr4[:, i] = tmp
Ejemplo n.º 2
0
def adjustr4WithDiscreteVariables(wr4, p):
    for i in p._discreteVarsNumList:
        v = p._freeVarsList[i]

        if v.domain is bool or v.domain is 'bool':
            wr4[:, i] = where(wr4[:, i] < 0.5, 0, 1)
        else:
            tmp = wr4[:, i]
            d = v.domain
            if isPyPy:
                d2 = d.tolist()
                ind = atleast_1d([bisect_left(d2, val) for val in tmp])
                ind2 = atleast_1d([bisect_right(d2, val) for val in tmp])
            else:
                ind = searchsorted(d, tmp, side='left')
                ind2 = searchsorted(d, tmp, side='right')
            ind3 = where(ind != ind2)[0]
            Tmp = tmp[ind3].copy()

            ind[ind == d.size] -= 1  # may be due to roundoff errors
            ind[ind == 1] = 0
            ind2[ind2 == d.size] -= 1
            ind2[ind2 == 0] = 1  # may be due to roundoff errors
            tmp1 = asarray(d[ind], p.solver.dataType)
            tmp2 = asarray(d[ind2], p.solver.dataType)
            if Tmp.size != 0:
                if str(tmp1.dtype).startswith('int'):
                    Tmp = asarray(Tmp, p.solver.dataType)
                tmp2[ind3] = Tmp.copy()
                tmp1[ind3] = Tmp.copy()
            tmp = where(abs(tmp - tmp1) < abs(tmp - tmp2), tmp1, tmp2)
            #print max(abs(tmp-tmp1)), max(abs(tmp-tmp2))
            wr4[:, i] = tmp
Ejemplo n.º 3
0
def _correct_box_constraints(lb, ub, pop):
    diff_lb = pop - lb
    check_lb = diff_lb < 0.0
    scale = 1.0
    while np.any(check_lb):
        check_lb = check_lb*1
        pop = pop - (1+scale)*check_lb*diff_lb
        diff_lb = pop - lb
        check_lb = diff_lb < 0.0 
        scale /= 2

    diff_ub = pop - ub
    check_ub = diff_ub > 0.0 
    scale = 1.0
    while np.any(check_ub):
        check_ub = check_ub*1
        pop = pop - (1+scale)*check_ub*diff_ub
        diff_ub = pop - ub
        check_ub = diff_ub > 0.0 
        scale /= 2

    # temporary fix
    ind = where(pop<lb)
    if ind[0].size:
        pop[ind] = lb[ind[1]]
    ind = where(pop>ub)
    if ind[0].size:
        pop[ind] = ub[ind[1]]

    return pop
Ejemplo n.º 4
0
def func9(an, fo, g, p):

    #ind = searchsorted(ar, fo, side='right')
    if p.probType in ('NLSP', 'SNLE') and p.maxSolutions != 1:
        mino = atleast_1d([node.key for node in an])
        ind = mino > 0
        if not any(ind):
            return an, g
        else:
            g = nanmin((g, nanmin(mino[ind])))
            ind2 = where(logical_not(ind))[0]
            #an = take(an, ind2, axis=0, out=an[:ind2.size])
            #an = asarray(an[ind2])
            an = [an[i] for i in ind2]
            return an, g

    elif p.solver.dataHandling == 'sorted':
        #OLD
        mino = [node.key for node in an]
        ind = bisect_right(mino, fo)
        if ind == len(mino):
            return an, g
        else:
            g = nanmin((g, nanmin(atleast_1d(mino[ind]))))
            return an[:ind], g
    elif p.solver.dataHandling == 'raw':

        #NEW
        mino = atleast_1d([node.key for node in an])
        r10 = mino > fo
        if not any(r10):
            return an, g
        else:
            ind = where(r10)[0]
            g = nanmin((g, nanmin(atleast_1d(mino)[ind])))
            #an = asarray(an)
            ind2 = where(logical_not(r10))[0]
            #an = take(an, ind2, axis=0, out=an[:ind2.size])
            an = [an[i] for i in ind2]
            return an, g

        # NEW 2


#        curr_tnlh = [node.tnlh_curr for node in an]
#        import warnings
#        warnings.warn('! fix g')

        return an, g

    else:
        assert 0, 'incorrect nodes remove approach'
Ejemplo n.º 5
0
def func9(an, fo, g, p):
    
    #ind = searchsorted(ar, fo, side='right')
    if p.probType in ('NLSP', 'SNLE') and p.maxSolutions != 1:
        mino = atleast_1d([node.key for node in an])
        ind = mino > 0
        if not any(ind):
            return an, g
        else:
            g = nanmin((g, nanmin(mino[ind])))
            ind2 = where(logical_not(ind))[0]
            #an = take(an, ind2, axis=0, out=an[:ind2.size])
            #an = asarray(an[ind2])
            an = [an[i] for i in ind2]
            return an, g
            
        
    elif p.solver.dataHandling == 'sorted':
        #OLD
        mino = [node.key for node in an]
        ind = bisect_right(mino, fo)
        if ind == len(mino):
            return an, g
        else:
            g = nanmin((g, nanmin(atleast_1d(mino[ind]))))
            return an[:ind], g
    elif p.solver.dataHandling == 'raw':
        
        #NEW
        mino = atleast_1d([node.key for node in an])
        r10 = mino > fo
        if not any(r10):
            return an, g
        else:
            ind = where(r10)[0]
            g = nanmin((g, nanmin(atleast_1d(mino)[ind])))
            #an = asarray(an)
            ind2 = where(logical_not(r10))[0]
            #an = take(an, ind2, axis=0, out=an[:ind2.size])
            an = [an[i] for i in ind2]
            return an, g

        # NEW 2
#        curr_tnlh = [node.tnlh_curr for node in an]
#        import warnings
#        warnings.warn('! fix g')
        
        return an, g
        
    else:
        assert 0, 'incorrect nodes remove approach'
Ejemplo n.º 6
0
def func13(o, a):
    m, n = o.shape
    n /= 2
    #    if case == 1:
    #        U1, U2 = a[:, :n].copy(), a[:, n:]
    #        #TODO: mb use nanmax(concatenate((U1,U2),3),3) instead?
    #        U1 = where(logical_or(U1<U2, isnan(U1)),  U2, U1)
    #        return nanmin(U1, 1)

    L1, L2, U1, U2 = o[:, :n], o[:, n:], a[:, :n], a[:, n:]
    #    if case == 2:
    U = where(logical_or(U1 < U2, isnan(U1)), U2, U1)
    L = where(logical_or(L2 < L1, isnan(L1)), L2, L1)
    return nanmax(U - L, 1)
Ejemplo n.º 7
0
def func13(o, a): 
    m, n = o.shape
    n /= 2
#    if case == 1:
#        U1, U2 = a[:, :n].copy(), a[:, n:] 
#        #TODO: mb use nanmax(concatenate((U1,U2),3),3) instead?
#        U1 = where(logical_or(U1<U2, isnan(U1)),  U2, U1)
#        return nanmin(U1, 1)
        
    L1, L2, U1, U2 = o[:, :n], o[:, n:], a[:, :n], a[:, n:] 
#    if case == 2:
    U = where(logical_or(U1<U2, isnan(U1)),  U2, U1)
    L = where(logical_or(L2<L1, isnan(L1)), L2, L1)
    return nanmax(U-L, 1)
Ejemplo n.º 8
0
    def getPrimevalDilationMatrixWRTlinEqConstraints(self, p):
        n, Aeq, beq = p.n, p.Aeq, p.beq
        nLinEq = len(p.beq)
        ind_fixed = where(p.lb == p.ub)[0]
        arr = ones(n, dtype=self.T)
        arr[ind_fixed] = 0
        b = diag(arr)

        if hasattr(Aeq, 'tocsc'): Aeq = Aeq.tocsc()

        for i in range(nLinEq):
            vec = Aeq[i]
            #raise 0
            if hasattr(vec, 'toarray'): vec = vec.toarray().flatten()
            g = economyMult(b.T, vec)
            if not any(g): continue
            #ind_nnz = nonzero(g)[0]
            ng = norm(g)
            g = (g / ng).reshape(-1, 1)

            vec1 = p.matmult(b, g)  # TODO: remove economyMult, use dot?
            vec2 = -g.T

            b += p.matmult(vec1, vec2)

#            if len(ind_nnz) > 0.7 * g.size:
#                b += p.matmult(vec1, vec2)
#            else:
#                ind_nnz1 = nonzero(vec1)[0]
#                ind_nnz2 = nonzero(vec2)[1]
#                r = dot(vec1[ind_nnz1, :], vec2[:, ind_nnz2])
#                if p.debug:
#                    assert abs(norm(p.matmult(vec1, vec2).flatten()) - norm(r.flatten())) < 1e-5
#                b[ix_(ind_nnz1, ind_nnz2)] += r
        return b
Ejemplo n.º 9
0
def finalTextOutput(p, r):
    if type(p.msg) == ndarray:  # from fmincon etc
        msg = asarray(p.msg.flatten(), int).tolist()
        p.msg = ''.join(chr(c) for c in msg)
    if p.iprint >= 0:
        if len(p.msg):
            p.disp("istop: " + str(r.istop) + ' (' + p.msg + ')')
        else:
            p.disp("istop: " + str(r.istop))

        p.disp('Solver:   Time Elapsed = ' + str(r.elapsed['solver_time']) +
               ' \tCPU Time Elapsed = ' + str(r.elapsed['solver_cputime']))
        if p.plot:
            p.disp('Plotting: Time Elapsed = ' + str(r.elapsed['plot_time']) +
                   ' \tCPU Time Elapsed = ' + str(r.elapsed['plot_cputime']))

        if p.probType == 'MOP':
            msg = '%d solutions have been obtained' % len(p.solutions.coords)
            p.disp(msg)
            return

        # TODO: add output of NaNs number in constraints (if presernt)
        if p.useScaledResidualOutput:
            rMsg = 'max(residuals/requiredTolerances) = %g' % (r.rf / p.contol)
        else:
            rMsg = 'MaxResidual = %g' % r.rf
        if not p.isFeasible:
            nNaNs = (len(where(isnan(p.c(p.xf)))[0]) if p.c is not None and type(p.c)!=list else 0) \
            + (len(where(isnan(p.h(p.xf)))[0]) if p.h is not None and type(p.h)!=list else 0)
            if nNaNs == 0:
                nNaNsMsg = ''
            elif nNaNs == 1:
                nNaNsMsg = '1 constraint is equal to NaN, '
            else:
                nNaNsMsg = ('%d constraints are equal to NaN, ' % nNaNs)
            p.disp(
                'NO FEASIBLE SOLUTION has been obtained (%s%s, objFunc = %0.8g)'
                % (nNaNsMsg, rMsg, r.ff))
        else:
            if p.maxSolutions == 1:
                msg = "objFunValue: " + (p.finalObjFunTextFormat % r.ff)
                if not p.isUC: msg += ' (feasible, %s)' % rMsg
            else:
                msg = '%d solutions have been obtained' % len(p.solutions)
            p.disp(msg)
Ejemplo n.º 10
0
def _eval_pop(pop, p):
    
    NP = pop.shape[0]
    
    constr_vals = np.zeros(NP)
    vals = p.f(pop).flatten()
    
    if vals.size == 1:
        vals = np.array([vals]*NP)
    vals[np.isnan(vals)] = np.inf

    if p.__isNoMoreThanBoxBounded__():
        best_i = vals.argmin()        
        best = (vals[best_i], 0, pop[best_i])
    else:
#        new = 1
#        
#        if new:# and p.isFDmodel:
            # TODO: handle nanPenalty * newPoint.nNaNs()
            #vals = np.empty(pop.shape[0])
            #vals.fill(np.nan)
        
        P = p.point(pop)

        constr_vals = P.mr(checkBoxBounds = False) + nanPenalty * P.nNaNs()

        ind = constr_vals < p.contol

        if not np.any(ind):
            j = np.argmin(constr_vals)
            bestPoint = p.point(pop[j])
            #bestPoint.i = j
        else:
            IND = where(ind)[0]
            #print(IND, pop.shape)
            try:
                P2 = np.atleast_2d(pop[IND])
                F = np.atleast_1d(vals[IND])
            except: # PyPy
                P2 = np.atleast_2d([pop[j] for j in IND])
                F = np.atleast_1d([vals[j] for j in IND])
            J = np.nanargmin(F)
            bestPoint = p.point(P2[J], f=F[J])# TODO: other fields
            #bestPoint.i = IND[J]
            #bestPoint.i = where(ind)[0]

        best = (bestPoint.f(), bestPoint.mr() + nanPenalty * bestPoint.nNaNs(), bestPoint.x)
#    print(vals, constr_vals)
#    from openopt.kernel.ooMisc import isSolved 
#    raise isSolved

    return best, vals, constr_vals
Ejemplo n.º 11
0
def func2(y, e, t, vv):  #, tnlhf_curr):
    #assert y.size != 0
    if y.size == 0: return y.copy(), e.copy()  # especially for PyPy
    new_y, new_e = y.copy(), e.copy()
    m, n = y.shape
    w = arange(m)

    #!!!! TODO: omit or imporove it for all-float problems
    th = (new_y[w, t] + new_e[w, t]) / 2

    ### !!!!!!!!!!!!!!!!!!!!!
    # TODO: rework it for integer dataType

    BoolVars = [(v.domain is bool or v.domain is 'bool') for v in vv]
    if not str(th.dtype).startswith('float') and any(BoolVars):
        indBool = where(BoolVars)[0]
        if len(indBool) != n:
            #boolCoords = list(set(indBool) & set(t))
            boolCoords = where([t[j] in indBool for j in range(m)])[0]
            new_y[w, t] = th
            new_e[w, t] = th
            new_y[boolCoords, t[boolCoords]] = 1
            new_e[boolCoords, t[boolCoords]] = 0
        else:
            new_y[w, t] = 1
            new_e[w, t] = 0
    else:
        #        print new_y.shape, w.shape, t.shape, th.shape
        new_y[w, t] = th
        new_e[w, t] = th

    new_y = vstack((y, new_y))
    new_e = vstack((new_e, e))

    #    if tnlhf_curr is not None:
    #        tnlhf_curr_local = hstack((tnlhf_curr[w, t], tnlhf_curr[w, n+t]))
    #    else:
    #        tnlhf_curr_local = None
    return new_y, new_e  #, tnlhf_curr_local
Ejemplo n.º 12
0
def func2(y, e, t, vv):#, tnlhf_curr):
    #assert y.size != 0
    if y.size == 0: return y.copy(), e.copy() # especially for PyPy
    new_y, new_e = y.copy(), e.copy()
    m, n = y.shape
    w = arange(m)
    
    #!!!! TODO: omit or imporove it for all-float problems    
    th = (new_y[w, t] + new_e[w, t]) / 2
    
    ### !!!!!!!!!!!!!!!!!!!!!
    # TODO: rework it for integer dataType 
    
    BoolVars = [(v.domain is bool or v.domain is 'bool') for v in vv]
    if not str(th.dtype).startswith('float') and any(BoolVars):
        indBool = where(BoolVars)[0]
        if len(indBool) != n:
            #boolCoords = list(set(indBool) & set(t))
            boolCoords = where([t[j] in indBool for j in range(m)])[0]
            new_y[w, t] = th
            new_e[w, t] = th
            new_y[boolCoords, t[boolCoords]] = 1
            new_e[boolCoords, t[boolCoords]] = 0
        else:
            new_y[w, t] = 1
            new_e[w, t] = 0
    else:
#        print new_y.shape, w.shape, t.shape, th.shape
        new_y[w, t] = th
        new_e[w, t] = th
    
    new_y = vstack((y, new_y))
    new_e = vstack((new_e, e))
    
#    if tnlhf_curr is not None:
#        tnlhf_curr_local = hstack((tnlhf_curr[w, t], tnlhf_curr[w, n+t]))
#    else:
#        tnlhf_curr_local = None
    return new_y, new_e#, tnlhf_curr_local
Ejemplo n.º 13
0
def func7(y, e, o, a, _s, indT, nlhc, residual):
    r10 = logical_and(all(isnan(o), 1), all(isnan(a), 1))
    if any(r10):
        j = where(logical_not(r10))[0]
        lj = j.size
        y = take(y, j, axis=0, out=y[:lj])
        e = take(e, j, axis=0, out=e[:lj])
        o = take(o, j, axis=0, out=o[:lj])
        a = take(a, j, axis=0, out=a[:lj])
        _s = _s[j]
        if indT is not None:
            indT = indT[j]
        if nlhc is not None:
            nlhc = take(nlhc, j, axis=0, out=nlhc[:lj])
        if residual is not None:
            residual = take(residual, j, axis=0, out=residual[:lj])
    return y, e, o, a, _s, indT, nlhc, residual
Ejemplo n.º 14
0
def func7(y, e, o, a, _s, indT, nlhc, residual):
    r10 = logical_and(all(isnan(o), 1), all(isnan(a), 1))
    if any(r10):
        j = where(logical_not(r10))[0]
        lj = j.size
        y = take(y, j, axis=0, out=y[:lj])
        e = take(e, j, axis=0, out=e[:lj])
        o = take(o, j, axis=0, out=o[:lj])
        a = take(a, j, axis=0, out=a[:lj])
        _s = _s[j]
        if indT is not None:
            indT = indT[j]
        if nlhc is not None:
            nlhc = take(nlhc, j, axis=0, out=nlhc[:lj])
        if residual is not None:
            residual = take(residual, j, axis=0, out=residual[:lj])
    return y, e, o, a, _s, indT, nlhc, residual
Ejemplo n.º 15
0
def adjustDiscreteVarBounds(y, e, _s, indT, p):
    #n = p.n
    # TODO: remove the cycle, use vectorization
    for i in p._discreteVarsNumList:
        v = p._freeVarsList[i]
        #y += 100
        adjust_lx_WithDiscreteDomain(y[:, i], v)
        adjust_ux_WithDiscreteDomain(e[:, i], v)

    Ind = any(y > e, 1)

    # TODO:  is it triggered? // updated: can be from MOP or cons
    if any(Ind):
        ind = where(logical_not(Ind))[0]
        s = ind.size
        y = take(y, ind, axis=0, out=y[:s])
        e = take(e, ind, axis=0, out=e[:s])
        _s = _s[ind]
        if indT is not None:
            indT = indT[ind]
    return y, e, _s, indT
Ejemplo n.º 16
0
def adjustDiscreteVarBounds(y, e, _s, indT, p):
    #n = p.n
    # TODO: remove the cycle, use vectorization
    for i in p._discreteVarsNumList:
        v = p._freeVarsList[i]
        #y += 100
        adjust_lx_WithDiscreteDomain(y[:, i], v)
        adjust_ux_WithDiscreteDomain(e[:, i], v)

    Ind = any(y>e, 1)

    # TODO:  is it triggered? // updated: can be from MOP or cons
    if any(Ind):
        ind = where(logical_not(Ind))[0]
        s = ind.size
        y = take(y, ind, axis=0, out=y[:s])
        e = take(e, ind, axis=0, out=e[:s])
        _s = _s[ind]
        if indT is not None:
            indT = indT[ind]
    return y, e, _s, indT
Ejemplo n.º 17
0
def func5(an, nn, g, p):
    m = len(an)
    if m <= nn: return an, g
    
    mino = np.array([node.key for node in an])
    
    if nn == 1: # box-bound probs with exact interval analysis
        ind = argmin(mino)
        assert ind in (0, 1), 'error in interalg engine'
        g = nanmin((mino[1-ind], g))
        an = [an[i] for i in ind]
    elif m > nn:
        if p.solver.dataHandling == 'raw':
            ind = argsort(mino)
            th = mino[ind[nn]]
            ind2 = where(mino < th)[0]
            g = nanmin((th, g))
            #an = take(an, ind2, axis=0, out=an[:ind2.size])
            an = [an[i] for i in ind2]#an[ind2]
        else:
            g = nanmin((mino[nn], g))
            an = an[:nn]
    return an, g
Ejemplo n.º 18
0
def func5(an, nn, g, p):
    m = len(an)
    if m <= nn: return an, g

    mino = np.array([node.key for node in an])

    if nn == 1:  # box-bound probs with exact interval analysis
        ind = argmin(mino)
        assert ind in (0, 1), 'error in interalg engine'
        g = nanmin((mino[1 - ind], g))
        an = [an[i] for i in ind]
    elif m > nn:
        if p.solver.dataHandling == 'raw':
            ind = argsort(mino)
            th = mino[ind[nn]]
            ind2 = where(mino < th)[0]
            g = nanmin((th, g))
            #an = take(an, ind2, axis=0, out=an[:ind2.size])
            an = [an[i] for i in ind2]  #an[ind2]
        else:
            g = nanmin((mino[nn], g))
            an = an[:nn]
    return an, g
Ejemplo n.º 19
0
def func12(an, maxActiveNodes, p, Solutions, vv, varTols, fo):
    solutions, r6 = Solutions.solutions, Solutions.coords
    if len(an) == 0:
        return array([]), array([]), array([]), array([])
    _in = an

    if r6.size != 0:
        r11, r12 = r6 - varTols, r6 + varTols
    y, e, S = [], [], []
    Tnlhf_curr_local = []
    n = p.n
    N = 0
    maxSolutions = p.maxSolutions

    #    new = 1
    #    # new
    #    if new and p.probType in ('MOP', 'SNLE', 'GLP', 'NLP', 'MINLP') and p.maxSolutions == 1:
    #
    #
    #        return y, e, _in, _s

    while True:
        an1Candidates, _in = func3(_in, maxActiveNodes, p.solver.dataHandling)

        #print nanmax(2**(-an1Candidates[0].tnlh_curr)) ,  nanmax(2**(-an1Candidates[-1].tnlh_curr))
        yc, ec, oc, ac, SIc = asarray([t.y for t in an1Candidates]), \
        asarray([t.e for t in an1Candidates]), \
        asarray([t.o for t in an1Candidates]), \
        asarray([t.a for t in an1Candidates]), \
        asarray([t._s for t in an1Candidates])

        if p.probType == 'MOP':
            tnlhf_curr = asarray([t.tnlh_all for t in an1Candidates])
            tnlhf = None
        elif p.solver.dataHandling == 'raw':
            tnlhf = asarray([t.tnlhf for t in an1Candidates])
            tnlhf_curr = asarray([t.tnlh_curr for t in an1Candidates])
        else:
            tnlhf, tnlhf_curr = None, None

        if p.probType != 'IP':
            #nlhc = asarray([t.nlhc for t in an1Candidates])

            #residual = asarray([t.residual for t in an1Candidates])
            residual = None

            indT = func4(p, yc, ec, oc, ac, fo, tnlhf_curr)

            if an1Candidates[0].indtc is not None:
                indtc = asarray([t.indtc for t in an1Candidates])
                indT = logical_or(indT, indtc)
        else:
            residual = None
            indT = None
        t, _s, indD = func1(tnlhf, tnlhf_curr, residual, yc, ec, oc, ac, SIc,
                            p, indT)

        new = 0
        nn = 0
        if new and p.probType in ('MOP', 'SNLE', 'NLSP', 'GLP', 'NLP',
                                  'MINLP') and p.maxSolutions == 1:
            arr = tnlhf_curr if p.solver.dataHandling == 'raw' else oc
            M = arr.shape[0]
            w = arange(M)
            Midles = 0.5 * (yc[w, t] + ec[w, t])
            arr_1, arr2 = arr[w, t], arr[w, n + t]
            Arr = hstack((arr_1, arr2))
            ind = np.argsort(Arr)
            Ind = set(ind[:maxActiveNodes])
            tag_all, tag_1, tag_2 = [], [], []
            sn = []

            # TODO: get rid of the cycles
            for i in range(M):
                cond1, cond2 = i in Ind, (i + M) in Ind
                if cond1:
                    if cond2:
                        tag_all.append(i)
                    else:
                        tag_1.append(i)
                else:
                    if cond2:
                        tag_2.append(i)
                    else:
                        sn.append(an1Candidates[i])

            list_lx, list_ux = [], []

            _s_new = []
            updateTC = an1Candidates[0].indtc is not None
            isRaw = p.solver.dataHandling == 'raw'
            for i in tag_1:
                node = an1Candidates[i]
                I = t[i]
                #                if node.o[n+I] >= node.o[I]:
                #                    print '1'
                #                else:
                #                    print i, I, node.o[n+I] ,  node.o[I], node.key, node.a[n+I] ,  node.a[I], node.nlhc[n+I], node.nlhc[I]
                node.key = node.o[n + I]
                node._s = _s[i]

                if isRaw:
                    node.tnlh_curr[I] = node.tnlh_curr[n + I]
                    node.tnlh_curr_best = nanmin(node.tnlh_curr)

                #assert node.o[n+I] >= node.o[I]
                #lx, ux = node.y, node.e
                lx, ux = yc[i], ec[i]
                if nn:
                    #node.o[I], node.a[I] = node.o[n+I], node.a[n+I]
                    node.o[I], node.a[I] = node.o[n + I], node.a[n + I]
                    node.o[node.o < node.o[n + I]], node.a[
                        node.a > node.a[n + I]] = node.o[n + I], node.a[n + I]
                else:
                    node.o[n + I], node.a[n + I] = node.o[I], node.a[I]
                    node.o[node.o < node.o[I]], node.a[
                        node.a > node.a[I]] = node.o[I], node.a[I]

#                if p.solver.dataHandling == 'raw':
                for Attr in ('nlhf', 'nlhc', 'tnlhf', 'tnlh_curr', 'tnlh_all'):
                    r = getattr(node, Attr, None)
                    if r is not None:
                        if nn: r[I] = r[n + I]
                        else:
                            r[n + I] = r[I]

                mx = ux.copy()
                mx[I] = Midles[i]  #0.5*(lx[I] + ux[I])
                list_lx.append(lx)
                list_ux.append(mx)
                node.y = lx.copy()
                node.y[I] = Midles[i]  #0.5*(lx[I] + ux[I])
                if updateTC:
                    node.indtc = True

                _s_new.append(node._s)
                sn.append(node)

            for i in tag_2:
                node = an1Candidates[i]
                I = t[i]
                node.key = node.o[I]

                node._s = _s[i]

                # for raw only
                if isRaw:
                    node.tnlh_curr[n + I] = node.tnlh_curr[I]
                    node.tnlh_curr_best = nanmin(node.tnlh_curr)

                #assert node.o[I] >= node.o[n+I]
                #lx, ux = node.y, node.e
                lx, ux = yc[i], ec[i]

                if nn:
                    node.o[n + I], node.a[n + I] = node.o[I], node.a[I]
                    node.o[node.o < node.o[I]], node.a[
                        node.a > node.a[I]] = node.o[I], node.a[I]
                else:
                    node.o[I], node.a[I] = node.o[n + I], node.a[n + I]
                    node.o[node.o < node.o[n + I]], node.a[
                        node.a > node.a[n + I]] = node.o[n + I], node.a[n + I]
                for Attr in ('nlhf', 'nlhc', 'tnlhf', 'tnlh_curr', 'tnlh_all'):
                    r = getattr(node, Attr, None)
                    if r is not None:
                        if nn: r[n + I] = r[I]
                        else:
                            r[I] = r[n + I]

                mx = lx.copy()
                mx[I] = Midles[i]  #0.5*(lx[I] + ux[I])
                list_lx.append(mx)
                list_ux.append(ux)
                node.e = ux.copy()
                node.e[I] = Midles[i]  #0.5*(lx[I] + ux[I])
                if updateTC:
                    node.indtc = True

                _s_new.append(node._s)
                sn.append(node)

            for i in tag_all:
                node = an1Candidates[i]
                I = t[i]

                #lx, ux = node.y, node.e
                lx, ux = yc[i], ec[i]
                mx = ux.copy()
                mx[I] = Midles[i]  #0.5 * (lx[I] + ux[I])

                list_lx.append(lx)
                list_ux.append(mx)

                mx = lx.copy()
                mx[I] = Midles[i]  #0.5 * (lx[I] + ux[I])
                #mx[n+ t] = 0.5 * (lx[n + t] + ux[n + t])
                list_lx.append(mx)
                list_ux.append(ux)

                #_s_new += [_s[i]] * 2
                _s_new.append(_s[i])
                _s_new.append(_s[i])

#            print 'y_new:', vstack(list_lx)
#            print 'e_new:', vstack(list_ux)
#            print '_s_new:', hstack(_s)
            _in = sn + _in.tolist()
            if p.solver.dataHandling == 'sorted':
                _in.sort(key=lambda obj: obj.key)
            else:
                #pass
                _in.sort(key=lambda obj: obj.tnlh_curr_best)
#            print 'tag 1:', len(tag_1), 'tag 2:', len(tag_2), 'tag all:', len(tag_all)
#            print 'lx:', list_lx
#            print 'sn lx:', [node.y for node in sn]
#            print 'ux:', list_ux
#            print 'sn ux:', [node.e for node in sn]
#            print '-'*10
#print '!', vstack(list_lx), vstack(list_ux), hstack(_s_new)
            NEW_lx, NEW_ux, NEW__in, NEW__s = \
            vstack(list_lx), vstack(list_ux), array(_in), hstack(_s_new)
            return NEW_lx, NEW_ux, NEW__in, NEW__s

        NewD = 1

        if NewD and indD is not None:
            s4d = _s[indD]
            sf = _s[logical_not(indD)]

            _s = hstack((s4d, s4d, sf))
            yf, ef = yc[logical_not(indD)], ec[logical_not(indD)]
            yc, ec = yc[indD], ec[indD]
            t = t[indD]
        else:
            _s = tile(_s, 2)

        #yc, ec, tnlhf_curr_local = func2(yc, ec, t, vv, tnlhf_curr)
        yc, ec = func2(yc, ec, t, vv)

        if NewD and indD is not None:
            yc = vstack((yc, yf))
            ec = vstack((ec, ef))

        if maxSolutions == 1 or len(solutions) == 0:
            #y, e, Tnlhf_curr_local = yc, ec, tnlhf_curr_local
            y, e = yc, ec
            break

        # TODO: change cycle variable if len(solutions) >> maxActiveNodes
        for i in range(len(solutions)):
            ind = logical_and(all(yc >= r11[i], 1), all(ec <= r12[i], 1))
            if any(ind):
                j = where(logical_not(ind))[0]
                lj = j.size
                yc = take(yc, j, axis=0, out=yc[:lj])
                ec = take(ec, j, axis=0, out=ec[:lj])
                _s = _s[j]
#                if tnlhf_curr_local is not None:
#                    tnlhf_curr_local = tnlhf_curr_local[j]
        y.append(yc)
        e.append(ec)
        S.append(_s)
        #Tnlhf_curr_local.append(tnlhf_curr_local)
        N += yc.shape[0]
        if len(_in) == 0 or N >= maxActiveNodes:
            y, e, _s = vstack(y), vstack(e), hstack(S)
            #Tnlhf_curr_local = hstack(Tnlhf_curr_local)
            break


#    if Tnlhf_curr_local is not None and len(Tnlhf_curr_local) != 0 and Tnlhf_curr_local[0] is not None:
#        #print len(where(isfinite(Tnlhf_curr_local))[0]), Tnlhf_curr_local.size
#        pass

#    print 'y_prev:', y
#    print 'e_prev:', e
#    print '_s_prev:', hstack(_s)
#print 'prev!', y, e, _s

#    from numpy import array_equal
#    if not array_equal(NEW_lx.sort(), y.sort()):
#        pass
#    if not array_equal(NEW_ux.sort(), e.sort()):
#        pass
#    if not array_equal(NEW__s.sort(), _s.sort()):
#        pass

#, NEW_ux, NEW__in, NEW__s
    return y, e, _in, _s
Ejemplo n.º 20
0
def func11(y, e, nlhc, indTC, residual, o, a, _s, p):
    m, n = y.shape
    if p.probType == "IP":
        w = arange(m)
        # TODO: omit recalculation from func1
        ind = nanargmin(a[:, 0:n] - o[:, 0:n] + a[:, n:] - o[:, n:], 1)
        sup_inf_diff = 0.5 * (a[w, ind] - o[w, ind] + a[w, n + ind] -
                              o[w, n + ind])
        diffao = a - o
        minres_ind = nanargmin(diffao, 1)
        minres = diffao[w, minres_ind]
        complementary_minres = diffao[w,
                                      where(minres_ind < n, minres_ind +
                                            n, minres_ind - n)]
        volume = prod(e - y, 1)
        volumeResidual = volume * sup_inf_diff
        F = 0.25 * (a[w, ind] + o[w, ind] + a[w, n + ind] + o[w, n + ind])
        return [
            si(IP_fields, sup_inf_diff[i], minres[i], minres_ind[i],
               complementary_minres[i], y[i], e[i], o[i], a[i], _s[i], F[i],
               volume[i], volumeResidual[i]) for i in range(m)
        ]

    else:
        residual = None

        isSNLE = p.probType in ('SNLE', 'NLSP')
        if 1 or not isSNLE:
            o, a = asarray(o), asarray(a)
            a[a == inf] = 1e300
            o[o == -inf] = -1e300
            tmp = a - o
            tmp[tmp < 1e-300] = 1e-300
            #            ind_uf_inf = where(a==inf)[0]
            #            if ind_uf_inf.size:
            #                Tmp = o[ind_uf_inf]
            #                Tmp[Tmp==-inf] = -1e100
            #                M = nanmax(abs(Tmp))
            #                if M is nan or M == 0.0:
            #                    M = 1.0
            #                tmp[ind_uf_inf] = 1e200 * (1.0 + Tmp/M)
            nlhf = log2(tmp)  #-log2(p.fTol)

#        nlhf[a==inf] = 1e300# to make it not inf and nan
#        nlhf[o==-inf] = 1e300# to make it not inf and nan

        if p.probType == "MOP":
            if nlhf.ndim == 3:  # in MOP
                nlhf = nlhf.sum(axis=1)
            else:
                assert 0, 'bug in interalg'
            # make correct o,a wrt each target
            return [
                si(MOP_Fields, y[i], e[i], nlhf[i],
                   nlhc[i] if nlhc is not None else None,
                   indTC[i] if indTC is not None else None,
                   residual[i] if residual is not None else None,
                   [o[i][k]
                    for k in range(p.nf)], [a[i][k]
                                            for k in range(p.nf)], _s[i])
                for i in range(m)
            ]
        else:
            assert p.probType in ('GLP', 'NLP', 'NSP', 'SNLE', 'NLSP', 'MINLP',
                                  'QP', 'LP', 'MILP')

            if 0 and isSNLE:
                nlhf = Tmp = o = a = [None] * m
            else:
                s, q = o[:, 0:n], o[:, n:2 * n]
                Tmp = nanmax(where(q < s, q, s), 1)

                nlhf[logical_and(isinf(a), isinf(nlhf))] = 1e300

#            residual = None

            return [
                si(Fields, Tmp[i], y[i], e[i], nlhf[i],
                   nlhc[i] if nlhc is not None else None,
                   indTC[i] if indTC is not None else None,
                   residual[i] if residual is not None else None, o[i], a[i],
                   _s[i]) for i in range(m)
            ]
Ejemplo n.º 21
0
def func12(an, maxActiveNodes, p, Solutions, vv, varTols, fo):
    solutions, r6 = Solutions.solutions, Solutions.coords
    if len(an) == 0:
        return array([]), array([]), array([]), array([])
    _in = an
    
    if r6.size != 0:
        r11, r12 = r6 - varTols, r6 + varTols
    y, e, S = [], [], []
    Tnlhf_curr_local = []
    n = p.n
    N = 0
    maxSolutions = p.maxSolutions
    
#    new = 1
#    # new
#    if new and p.probType in ('MOP', 'SNLE', 'GLP', 'NLP', 'MINLP') and p.maxSolutions == 1:
#        
#        
#        return y, e, _in, _s
        
    
    while True:
        an1Candidates, _in = func3(_in, maxActiveNodes, p.solver.dataHandling)

        #print nanmax(2**(-an1Candidates[0].tnlh_curr)) ,  nanmax(2**(-an1Candidates[-1].tnlh_curr))
        yc, ec, oc, ac, SIc = asarray([t.y for t in an1Candidates]), \
        asarray([t.e for t in an1Candidates]), \
        asarray([t.o for t in an1Candidates]), \
        asarray([t.a for t in an1Candidates]), \
        asarray([t._s for t in an1Candidates])

        
        if p.probType == 'MOP':
            tnlhf_curr = asarray([t.tnlh_all for t in an1Candidates])
            tnlhf = None        
        elif p.solver.dataHandling == 'raw':
            tnlhf = asarray([t.tnlhf for t in an1Candidates]) 
            tnlhf_curr = asarray([t.tnlh_curr for t in an1Candidates]) 
        else:
            tnlhf, tnlhf_curr = None, None
        
        
        if p.probType != 'IP': 
            #nlhc = asarray([t.nlhc for t in an1Candidates])
            
            #residual = asarray([t.residual for t in an1Candidates]) 
            residual = None
            
            indT = func4(p, yc, ec, oc, ac, fo, tnlhf_curr)

            if an1Candidates[0].indtc is not None:
                indtc = asarray([t.indtc for t in an1Candidates])
                indT = logical_or(indT, indtc)
        else:
            residual = None
            indT = None
        t, _s, indD = func1(tnlhf, tnlhf_curr, residual, yc, ec, oc, ac, SIc, p, indT)

        new = 0
        nn = 0
        if new and p.probType in ('MOP', 'SNLE', 'NLSP','GLP', 'NLP', 'MINLP') and p.maxSolutions == 1:
            arr = tnlhf_curr if p.solver.dataHandling == 'raw' else oc
            M = arr.shape[0]
            w = arange(M)
            Midles = 0.5*(yc[w, t] + ec[w, t])
            arr_1, arr2 = arr[w, t], arr[w, n+t]
            Arr = hstack((arr_1, arr2))
            ind = np.argsort(Arr)
            Ind = set(ind[:maxActiveNodes])
            tag_all, tag_1, tag_2 = [], [], []
            sn = []
            
            # TODO: get rid of the cycles
            for i in range(M):
                cond1, cond2 = i in Ind, (i+M) in Ind
                if cond1:
                    if cond2:
                        tag_all.append(i)
                    else:
                        tag_1.append(i)
                else:
                    if cond2:
                        tag_2.append(i)
                    else:
                        sn.append(an1Candidates[i])

            list_lx, list_ux = [], []
            
            _s_new = []
            updateTC = an1Candidates[0].indtc is not None
            isRaw = p.solver.dataHandling == 'raw'
            for i in tag_1:
                node = an1Candidates[i]
                I = t[i]
#                if node.o[n+I] >= node.o[I]:
#                    print '1'
#                else:
#                    print i, I, node.o[n+I] ,  node.o[I], node.key, node.a[n+I] ,  node.a[I], node.nlhc[n+I], node.nlhc[I]
                node.key = node.o[n+I]
                node._s = _s[i]
                
                if isRaw:
                    node.tnlh_curr[I] = node.tnlh_curr[n+I]
                    node.tnlh_curr_best = nanmin(node.tnlh_curr)
                
                #assert node.o[n+I] >= node.o[I]
                #lx, ux = node.y, node.e
                lx, ux = yc[i], ec[i]
                if nn:
                    #node.o[I], node.a[I] = node.o[n+I], node.a[n+I]
                    node.o[I], node.a[I] = node.o[n+I], node.a[n+I]
                    node.o[node.o<node.o[n+I]], node.a[node.a>node.a[n+I]] = node.o[n+I], node.a[n+I]
                else:
                    node.o[n+I], node.a[n+I] = node.o[I], node.a[I]
                    node.o[node.o<node.o[I]], node.a[node.a>node.a[I]] = node.o[I], node.a[I]

#                if p.solver.dataHandling == 'raw':
                for Attr in ('nlhf','nlhc', 'tnlhf', 'tnlh_curr', 'tnlh_all'):
                    r = getattr(node, Attr, None)
                    if r is not None:
                        if nn: r[I] = r[n+I]
                        else: 
                            r[n+I] = r[I]

                mx = ux.copy()
                mx[I] = Midles[i]#0.5*(lx[I] + ux[I])
                list_lx.append(lx)
                list_ux.append(mx)
                node.y = lx.copy()
                node.y[I] = Midles[i]#0.5*(lx[I] + ux[I])
                if updateTC: 
                    node.indtc = True
                
                _s_new.append(node._s)
                sn.append(node)
            
            for i in tag_2:
                node = an1Candidates[i]
                I = t[i]
                node.key = node.o[I]

                
                node._s = _s[i]
                
                # for raw only
                if isRaw:
                    node.tnlh_curr[n+I] = node.tnlh_curr[I]
                    node.tnlh_curr_best = nanmin(node.tnlh_curr)
                
                #assert node.o[I] >= node.o[n+I]
                #lx, ux = node.y, node.e
                lx, ux = yc[i], ec[i]

                if nn:
                    node.o[n+I], node.a[n+I] = node.o[I], node.a[I]
                    node.o[node.o<node.o[I]], node.a[node.a>node.a[I]] = node.o[I], node.a[I]
                else:
                    node.o[I], node.a[I] = node.o[n+I], node.a[n+I]
                    node.o[node.o<node.o[n+I]], node.a[node.a>node.a[n+I]] = node.o[n+I], node.a[n+I]
                for Attr in ('nlhf','nlhc', 'tnlhf', 'tnlh_curr', 'tnlh_all'):
                    r = getattr(node, Attr, None)
                    if r is not None:
                        if nn: r[n+I] = r[I]
                        else: 
                            r[I] = r[n+I]

                mx = lx.copy()
                mx[I] = Midles[i]#0.5*(lx[I] + ux[I])
                list_lx.append(mx)
                list_ux.append(ux)
                node.e = ux.copy()
                node.e[I] = Midles[i]#0.5*(lx[I] + ux[I])
                if updateTC: 
                    node.indtc = True

                _s_new.append(node._s)
                sn.append(node)
            
            for i in tag_all:
                node = an1Candidates[i]
                I = t[i]
                
                #lx, ux = node.y, node.e
                lx, ux = yc[i], ec[i]
                mx = ux.copy()
                mx[I] = Midles[i]#0.5 * (lx[I] + ux[I])
                
                list_lx.append(lx)
                list_ux.append(mx)
                
                mx = lx.copy()
                mx[I] = Midles[i]#0.5 * (lx[I] + ux[I])
                #mx[n+ t] = 0.5 * (lx[n + t] + ux[n + t])
                list_lx.append(mx)
                list_ux.append(ux)
                
                #_s_new += [_s[i]] * 2
                _s_new.append(_s[i])
                _s_new.append(_s[i])
                
#            print 'y_new:', vstack(list_lx)
#            print 'e_new:', vstack(list_ux)
#            print '_s_new:', hstack(_s)
            _in = sn + _in.tolist()
            if p.solver.dataHandling == 'sorted':
                _in.sort(key = lambda obj: obj.key)
            else:
                #pass
                _in.sort(key = lambda obj: obj.tnlh_curr_best)
#            print 'tag 1:', len(tag_1), 'tag 2:', len(tag_2), 'tag all:', len(tag_all)
#            print 'lx:', list_lx
#            print 'sn lx:', [node.y for node in sn]
#            print 'ux:', list_ux
#            print 'sn ux:', [node.e for node in sn]
#            print '-'*10
            #print '!', vstack(list_lx), vstack(list_ux), hstack(_s_new)
            NEW_lx, NEW_ux, NEW__in, NEW__s = \
            vstack(list_lx), vstack(list_ux), array(_in), hstack(_s_new)
            return NEW_lx, NEW_ux, NEW__in, NEW__s
        
        NewD = 1
        
        if NewD and indD is not None: 
            s4d = _s[indD]
            sf = _s[logical_not(indD)]

            _s = hstack((s4d, s4d, sf))
            yf, ef = yc[logical_not(indD)], ec[logical_not(indD)]
            yc, ec = yc[indD], ec[indD]
            t = t[indD]
        else:
            _s = tile(_s, 2)

        #yc, ec, tnlhf_curr_local = func2(yc, ec, t, vv, tnlhf_curr)
        yc, ec = func2(yc, ec, t, vv)

        if NewD and indD is not None:
            yc = vstack((yc, yf))
            ec = vstack((ec, ef))
            
        if maxSolutions == 1 or len(solutions) == 0: 
            #y, e, Tnlhf_curr_local = yc, ec, tnlhf_curr_local
            y, e = yc, ec
            break
        
        # TODO: change cycle variable if len(solutions) >> maxActiveNodes
        for i in range(len(solutions)):
            ind = logical_and(all(yc >= r11[i], 1), all(ec <= r12[i], 1))
            if any(ind):
                j = where(logical_not(ind))[0]
                lj = j.size
                yc = take(yc, j, axis=0, out=yc[:lj])
                ec = take(ec, j, axis=0, out=ec[:lj])
                _s = _s[j]
#                if tnlhf_curr_local is not None:
#                    tnlhf_curr_local = tnlhf_curr_local[j]
        y.append(yc)
        e.append(ec)
        S.append(_s)
        #Tnlhf_curr_local.append(tnlhf_curr_local)
        N += yc.shape[0]
        if len(_in) == 0 or N >= maxActiveNodes: 
            y, e, _s = vstack(y), vstack(e), hstack(S)
            #Tnlhf_curr_local = hstack(Tnlhf_curr_local)
            break
            
#    if Tnlhf_curr_local is not None and len(Tnlhf_curr_local) != 0 and Tnlhf_curr_local[0] is not None:
#        #print len(where(isfinite(Tnlhf_curr_local))[0]), Tnlhf_curr_local.size
#        pass

#    print 'y_prev:', y
#    print 'e_prev:', e
#    print '_s_prev:', hstack(_s)
    #print 'prev!', y, e, _s
    
#    from numpy import array_equal
#    if not array_equal(NEW_lx.sort(), y.sort()):
#        pass
#    if not array_equal(NEW_ux.sort(), e.sort()):
#        pass
#    if not array_equal(NEW__s.sort(), _s.sort()):
#        pass
        
        #, NEW_ux, NEW__in, NEW__s
    return y, e, _in, _s
Ejemplo n.º 22
0
def func1(tnlhf, tnlhf_curr, residual, y, e, o, a, _s_prev, p, indT):
    m, n = y.shape
    w = arange(m)
    
    if p.probType == 'IP':
        oc_modL, oc_modU = o[:, :n], o[:, n:]
        ac_modL, ac_modU = a[:, :n], a[:, n:]
#            # TODO: handle nans
        mino = where(oc_modL < oc_modU, oc_modL, oc_modU)
        maxa = where(ac_modL < ac_modU, ac_modU, ac_modL)
    
        # Prev
        tmp = a[:, 0:n]-o[:, 0:n]+a[:, n:]-o[:, n:]
        t = nanargmin(tmp,1)
        d = 0.5*tmp[w, t]
        
        
        #New
#        tmp = a - o
#        t_ = nanargmin(tmp,1)
#        t = t_% n
#        d = tmp[w, t_]

#        ind = 2**(-n) >= (_s_prev - d)/asarray(d, 'float64')
        ind = 2**(1.0/n) * d >= _s_prev
        #new
#        ind = 2**(1.0/n) * d >= nanmax(maxa-mino, 1)
        
        #ind = 2**(-n) >= (_s_prev - _s)/asarray(_s, 'float64')
    
        #s2 = nanmin(maxa - mino, 1)
        #print (abs(s2/_s))
        
        # Prev
        _s = nanmin(maxa - mino, 1)
        
        # New
        #_s = nanmax(maxa - mino, 1)
#        _s = nanmax(a - o, 1)
        
        #ind = _s_prev  <= _s + ((2**-n / log(2)) if n > 15 else log2(1+2**-n)) 
        indD = logical_not(ind)
        indD = ind
        indD = None
        #print len(where(indD)[0]), len(where(logical_not(indD))[0])
#    elif p.probType == 'MOP':
#
#        raise 'unimplemented'
    else:
        if p.solver.dataHandling == 'sorted':
            _s = func13(o, a)
            
            t = nanargmin(a, 1) % n

            d = nanmax([a[w, t] - o[w, t], 
                    a[w, n+t] - o[w, n+t]], 0)
            
            ## !!!! Don't replace it by (_s_prev /d- 1) to omit rounding errors ###
            #ind = 2**(-n) >= (_s_prev - d)/asarray(d, 'float64')
            
            #NEW
            ind = d  >=  _s_prev / 2 ** (1.0e-12/n)
            #ind = d  >=  _s_prev / 2 ** (1.0/n)
            indD = empty(m, bool)
            indD.fill(True)
            #ind.fill(False)
            ###################################################
        elif p.solver.dataHandling == 'raw':
            if p.probType == 'MOP':
                t = p._t[:m]
                p._t = p._t[m:]
                d = _s = p.__s[:m]
                p.__s = p.__s[m:]
            else:
#                tnlh_1, tnlh_2 = tnlhf[:, 0:n], tnlhf[:, n:]
#                TNHLF_min =  where(logical_or(tnlh_1 > tnlh_2, isnan(tnlh_1)), tnlh_2, tnlh_1)
#               # Set _s
#                _s = nanmin(TNHLF_min, 1)
                T = tnlhf_curr
                tnlh_curr_1, tnlh_curr_2 = T[:, 0:n], T[:, n:]
                TNHL_curr_min =  where(logical_or(tnlh_curr_1 < tnlh_curr_2, isnan(tnlh_curr_2)), tnlh_curr_1, tnlh_curr_2)
                t = nanargmin(TNHL_curr_min, 1)
                T = tnlhf
                d = nanmin(vstack(([T[w, t], T[w, n+t]])), 0)
                _s = d

            #OLD
            #!#!#!#! Don't replace it by _s_prev - d <= ... to omit inf-inf = nan !#!#!#
            #ind = _s_prev  <= d + ((2**-n / log(2)) if n > 15 else log2(1+2**-n)) 
            #ind = _s_prev - d <= ((2**-n / log(2)) if n > 15 else log2(1+2**-n)) 
            
            #NEW
            if any(_s_prev < d):
                pass
            ind = _s_prev  <= d + 1.0/n
#            T = TNHL_curr_min
            #ind2 = nanmin(TNHL_curr_min, 0)
            
            indQ = d >= _s_prev - 1.0/n 
            #indQ = logical_and(indQ, False)
            indD = logical_or(indQ, logical_not(indT))
#            print '------'
#            print indQ[:10]
#            print indD[:10]
#            print _s_prev[:2], d[:2]
            #print len(where(indD)[0]), len(where(indQ)[0]), len(where(indT)[0])
            #print _s_prev - d
            ###################################################
            #d = ((tnlh[w, t]* tnlh[w, n+t])**0.5)
        else:
            assert 0

    if any(ind):
        r10 = where(ind)[0]
        #print('r10:', r10)
#        print _s_prev
#        print ((_s_prev -d)*n)[r10]
#        print('ind length: %d' % len(where(ind)[0]))
#        print where(ind)[0].size
        #bs = e[ind] - y[ind]
        #t[ind] = nanargmax(bs, 1) # ordinary numpy.argmax can be used as well
        bs = e[r10] - y[r10]
        t[r10] = nanargmax(bs, 1) # ordinary numpy.argmax can be used as well

    return t, _s, indD
Ejemplo n.º 23
0
def func11(y, e, nlhc, indTC, residual, o, a, _s, p): 
    m, n = y.shape
    if p.probType == "IP":
        w = arange(m)
        # TODO: omit recalculation from func1
        ind = nanargmin(a[:, 0:n] - o[:, 0:n] + a[:, n:] - o[:, n:], 1)
        sup_inf_diff = 0.5*(a[w, ind] - o[w, ind] + a[w, n+ind] - o[w, n+ind])
        diffao = a - o
        minres_ind = nanargmin(diffao, 1) 
        minres = diffao[w, minres_ind]
        complementary_minres = diffao[w, where(minres_ind<n, minres_ind+n, minres_ind-n)]
        volume = prod(e-y, 1)
        volumeResidual = volume * sup_inf_diff
        F = 0.25 * (a[w, ind] + o[w, ind] + a[w, n+ind] + o[w, n+ind])
        return [si(IP_fields, sup_inf_diff[i], minres[i], minres_ind[i], complementary_minres[i], y[i], e[i], o[i], a[i], _s[i], F[i], volume[i], volumeResidual[i]) for i in range(m)]
        
    else:
        residual = None
        
        isSNLE = p.probType in ('SNLE', 'NLSP')
        if 1 or not isSNLE:
            o, a = asarray(o), asarray(a)
            a[a==inf] = 1e300
            o[o==-inf] = -1e300
            tmp = a - o
            tmp[tmp<1e-300] = 1e-300
#            ind_uf_inf = where(a==inf)[0]
#            if ind_uf_inf.size:
#                Tmp = o[ind_uf_inf]
#                Tmp[Tmp==-inf] = -1e100
#                M = nanmax(abs(Tmp))
#                if M is nan or M == 0.0: 
#                    M = 1.0
#                tmp[ind_uf_inf] = 1e200 * (1.0 + Tmp/M)
            nlhf = log2(tmp)#-log2(p.fTol)
            
#        nlhf[a==inf] = 1e300# to make it not inf and nan
#        nlhf[o==-inf] = 1e300# to make it not inf and nan
        
        if p.probType == "MOP":
            if nlhf.ndim == 3: # in MOP
                nlhf = nlhf.sum(axis=1)
            else:
                assert 0, 'bug in interalg'
            # make correct o,a wrt each target
            return [si(MOP_Fields, y[i], e[i], nlhf[i], 
                          nlhc[i] if nlhc is not None else None, 
                          indTC[i] if indTC is not None else None, 
                          residual[i] if residual is not None else None, 
                          [o[i][k] for k in range(p.nf)], [a[i][k] for k in range(p.nf)], 
                          _s[i]) for i in range(m)]
        else:
            assert p.probType in ('GLP', 'NLP', 'NSP', 'SNLE', 'NLSP', 'MINLP', 'QP', 'LP', 'MILP')
            
            if 0 and isSNLE:
                nlhf = Tmp = o = a = [None]*m
            else:
                s, q = o[:, 0:n], o[:, n:2*n]
                Tmp = nanmax(where(q<s, q, s), 1)
                
                nlhf[logical_and(isinf(a), isinf(nlhf))] = 1e300
            
#            residual = None

            return [si(Fields, Tmp[i], y[i], e[i], nlhf[i], 
                          nlhc[i] if nlhc is not None else None, 
                          indTC[i] if indTC is not None else None, 
                          residual[i] if residual is not None else None, 
                          o[i], a[i], _s[i]) for i in range(m)]
Ejemplo n.º 24
0
def func1(tnlhf, tnlhf_curr, residual, y, e, o, a, _s_prev, p, indT):
    m, n = y.shape
    w = arange(m)

    if p.probType == 'IP':
        oc_modL, oc_modU = o[:, :n], o[:, n:]
        ac_modL, ac_modU = a[:, :n], a[:, n:]
        #            # TODO: handle nans
        mino = where(oc_modL < oc_modU, oc_modL, oc_modU)
        maxa = where(ac_modL < ac_modU, ac_modU, ac_modL)

        # Prev
        tmp = a[:, 0:n] - o[:, 0:n] + a[:, n:] - o[:, n:]
        t = nanargmin(tmp, 1)
        d = 0.5 * tmp[w, t]

        #New
        #        tmp = a - o
        #        t_ = nanargmin(tmp,1)
        #        t = t_% n
        #        d = tmp[w, t_]

        #        ind = 2**(-n) >= (_s_prev - d)/asarray(d, 'float64')
        ind = 2**(1.0 / n) * d >= _s_prev
        #new
        #        ind = 2**(1.0/n) * d >= nanmax(maxa-mino, 1)

        #ind = 2**(-n) >= (_s_prev - _s)/asarray(_s, 'float64')

        #s2 = nanmin(maxa - mino, 1)
        #print (abs(s2/_s))

        # Prev
        _s = nanmin(maxa - mino, 1)

        # New
        #_s = nanmax(maxa - mino, 1)
        #        _s = nanmax(a - o, 1)

        #ind = _s_prev  <= _s + ((2**-n / log(2)) if n > 15 else log2(1+2**-n))
        indD = logical_not(ind)
        indD = ind
        indD = None
        #print len(where(indD)[0]), len(where(logical_not(indD))[0])
#    elif p.probType == 'MOP':
#
#        raise 'unimplemented'
    else:
        if p.solver.dataHandling == 'sorted':
            _s = func13(o, a)

            t = nanargmin(a, 1) % n

            d = nanmax([a[w, t] - o[w, t], a[w, n + t] - o[w, n + t]], 0)

            ## !!!! Don't replace it by (_s_prev /d- 1) to omit rounding errors ###
            #ind = 2**(-n) >= (_s_prev - d)/asarray(d, 'float64')

            #NEW
            ind = d >= _s_prev / 2**(1.0e-12 / n)
            #ind = d  >=  _s_prev / 2 ** (1.0/n)
            indD = empty(m, bool)
            indD.fill(True)
            #ind.fill(False)
            ###################################################
        elif p.solver.dataHandling == 'raw':
            if p.probType == 'MOP':
                t = p._t[:m]
                p._t = p._t[m:]
                d = _s = p.__s[:m]
                p.__s = p.__s[m:]
            else:
                #                tnlh_1, tnlh_2 = tnlhf[:, 0:n], tnlhf[:, n:]
                #                TNHLF_min =  where(logical_or(tnlh_1 > tnlh_2, isnan(tnlh_1)), tnlh_2, tnlh_1)
                #               # Set _s
                #                _s = nanmin(TNHLF_min, 1)
                T = tnlhf_curr
                tnlh_curr_1, tnlh_curr_2 = T[:, 0:n], T[:, n:]
                TNHL_curr_min = where(
                    logical_or(tnlh_curr_1 < tnlh_curr_2, isnan(tnlh_curr_2)),
                    tnlh_curr_1, tnlh_curr_2)
                t = nanargmin(TNHL_curr_min, 1)
                T = tnlhf
                d = nanmin(vstack(([T[w, t], T[w, n + t]])), 0)
                _s = d

            #OLD
            #!#!#!#! Don't replace it by _s_prev - d <= ... to omit inf-inf = nan !#!#!#
            #ind = _s_prev  <= d + ((2**-n / log(2)) if n > 15 else log2(1+2**-n))
            #ind = _s_prev - d <= ((2**-n / log(2)) if n > 15 else log2(1+2**-n))

            #NEW
            if any(_s_prev < d):
                pass
            ind = _s_prev <= d + 1.0 / n
            #            T = TNHL_curr_min
            #ind2 = nanmin(TNHL_curr_min, 0)

            indQ = d >= _s_prev - 1.0 / n
            #indQ = logical_and(indQ, False)
            indD = logical_or(indQ, logical_not(indT))


#            print '------'
#            print indQ[:10]
#            print indD[:10]
#            print _s_prev[:2], d[:2]
#print len(where(indD)[0]), len(where(indQ)[0]), len(where(indT)[0])
#print _s_prev - d
###################################################
#d = ((tnlh[w, t]* tnlh[w, n+t])**0.5)
        else:
            assert 0

    if any(ind):
        r10 = where(ind)[0]
        #print('r10:', r10)
        #        print _s_prev
        #        print ((_s_prev -d)*n)[r10]
        #        print('ind length: %d' % len(where(ind)[0]))
        #        print where(ind)[0].size
        #bs = e[ind] - y[ind]
        #t[ind] = nanargmax(bs, 1) # ordinary numpy.argmax can be used as well
        bs = e[r10] - y[r10]
        t[r10] = nanargmax(bs, 1)  # ordinary numpy.argmax can be used as well

    return t, _s, indD
Ejemplo n.º 25
0
def r14MOP(p, nlhc, residual, definiteRange, y, e, vv, asdf1, C, r40, g, nNodes,  \
         r41, fTol, Solutions, varTols, _in, dataType, \
         maxNodes, _s, indTC, xRecord):

    assert p.probType == 'MOP'
    
    if len(p._discreteVarsNumList):
        y, e, _s, indTC = adjustDiscreteVarBounds(y, e, _s, indTC, p)
    
    
    if p.nProc != 1 and getattr(p, 'pool', None) is None:
        p.pool = Pool(processes = p.nProc)
    elif p.nProc == 1:
        p.pool = None
    
    ol, al = [], []
    targets = p.targets # TODO: check it
    m, n = y.shape
    ol, al = [[] for k in range(m)], [[] for k in range(m)]
    for i, t in enumerate(targets):
        o, a, definiteRange, exactRange = func82(y, e, vv, t.func, dataType, p)
        o, a = o.reshape(2*n, m).T, a.reshape(2*n, m).T
        for j in range(m):
            ol[j].append(o[j])
            al[j].append(a[j])
        #ol.append(o.reshape(2*n, m).T.tolist())
        #al.append(a.reshape(2*n, m).T.tolist())

    nlhf = r43(targets, Solutions.F, ol, al, p.pool, p.nProc)
    
    fo_prev = 0
    # TODO: remove NaN nodes here

    if y.size == 0:
        return _in, g, fo_prev, _s, Solutions, xRecord, r41, r40
    
    nodes = func11(y, e, nlhc, indTC, residual, ol, al, _s, p)
    
    #y, e = func4(y, e, o, a, fo)
    
    
    assert p.solver.dataHandling == 'raw', '"sorted" mode is unimplemented for MOP yet'
    
    if nlhf is None:
        new_nodes_tnlh_all = nlhc
    elif nlhc is None: 
        new_nodes_tnlh_all = nlhf
    else:
        new_nodes_tnlh_all = nlhf + nlhc

    asdf1 = [t.func for t in p.targets]
    r5F, r5Coords = getr4Values(vv, y, e, new_nodes_tnlh_all, asdf1, C, p.contol, dataType, p) 
    
    nIncome, nOutcome = r44(Solutions, r5Coords, r5F, targets, p.solver.sigma)
    fo = 0 # unused for MOP
    
    # TODO: better of nlhc for unconstrained probs

#    if len(_in) != 0:
#        an = hstack((nodes,  _in))
#    else:
#        an = atleast_1d(nodes)

    an = nodes + _in

    hasNewParetoNodes = False if nIncome == 0 else True
    if hasNewParetoNodes:
        ol2 = [node.o for node in an]
        al2 = [node.a for node in an]
        nlhc2 = [node.nlhc for node in an]
        nlhf2 = r43(targets, Solutions.F, ol2, al2, p.pool, p.nProc)
        tnlh_all = asarray(nlhc2) if nlhf2 is None else nlhf2 if nlhc2[0] is None else asarray(nlhc2) + nlhf2
    else:
        tnlh_all = vstack([new_nodes_tnlh_all] + [node.tnlh_all for node in _in]) if len(_in) != 0 else new_nodes_tnlh_all
        
    for i, node in enumerate(nodes):
        node.tnlh_all = tnlh_all[i]

    r10 = logical_not(any(isfinite(tnlh_all), 1))
    if any(r10):
        ind = where(logical_not(r10))[0]
#        an = asarray(an[ind])
        an = [an[i] for i in ind]
        tnlh_all = take(tnlh_all, ind, axis=0, out=tnlh_all[:ind.size])
    
#    else:
#        tnlh_all = hstack([node.tnlh_all for node in an])
        
    T1, T2 = tnlh_all[:, :tnlh_all.shape[1]/2], tnlh_all[:, tnlh_all.shape[1]/2:]
    T = where(logical_or(T1 < T2, isnan(T2)), T1, T2)
    t = nanargmin(T, 1)
    
    w = arange(t.size)
    NN = T[w, t].flatten()

    for i, node in enumerate(an):
        node.tnlh_all = tnlh_all[i]
        node.tnlh_curr_best = NN[i]
        
    astnlh = argsort(NN)
    
#    an = an[astnlh]
    an = [an[i] for i in astnlh]

    p._t = t
    
    # TODO: form _s in other level (for active nodes only), to reduce calculations
    if len(an) != 0:
        T = asarray([node.nlhf for node in an])
#        nlhc_fixed = asarray([node.nlhc for node in an])
        if an[0].nlhc is not None:
            T += asarray([node.nlhc for node in an])
#        T = nlhf_fixed + nlhc_fixed if nlhc_fixed[0] is not None else nlhf_fixed 
        p.__s = \
        nanmin(vstack(([T[w, t], T[w, n+t]])), 0)
    else:
        p.__s = array([])

#        p._nObtainedSolutions = len(solutions)
#        if p._nObtainedSolutions > maxSolutions:
#            solutions = solutions[:maxSolutions]
#            p.istop = 0
#            p.msg = 'user-defined maximal number of solutions (p.maxSolutions = %d) has been exeeded' % p.maxSolutions
#            return an, g, fo, None, solutions, coords, xRecord, r41, r40
    
    
    # TODO: fix it
    p._frontLength = len(Solutions.F)
    p._nIncome = nIncome
    p._nOutcome = nOutcome
    p.iterfcn(p.x0)
    #print('iter: %d (%d) frontLenght: %d' %(p.iter, itn, len(Solutions.coords)))
    
    if p.istop != 0: 
        return an, g, fo, None, Solutions, xRecord, r41, r40
        
    #an, g = func9(an, fo, g, p)

    nn = maxNodes#1 if asdf1.isUncycled and all(isfinite(o)) and p._isOnlyBoxBounded and not p.probType.startswith('MI') else maxNodes
    
    an, g = func5(an, nn, g, p)
    nNodes.append(len(an))
    return an, g, fo, _s, Solutions, xRecord, r41, r40
Ejemplo n.º 26
0
def truncateByPlane(y, e, indT, A, b):
    #!!!!!!!!!!!!!!!!!!!
    # TODO: vectorize it by matrix A
    #!!!!!!!!!!!!!!!!!!!
    ind_trunc = True
    assert np.asarray(b).size <= 1, 'unimplemented yet'
    m, n = y.shape
    if m == 0:
        assert e.size == 0, 'bug in interalg engine'
        return y, e, indT, ind_trunc

    ind_positive = where(A > 0)[0]
    ind_negative = where(A < 0)[0]

    A1 = A[ind_positive]
    S1 = A1 * y[:, ind_positive]
    A2 = A[ind_negative]
    S2 = A2 * e[:, ind_negative]
    s1, s2 = np.sum(S1, 1), np.sum(S2, 1)
    S = s1 + s2

    if ind_positive.size != 0:
        S1_ = b - S.reshape(-1, 1) + S1
        Alt_ub = S1_ / A1
        #        ind = e[:, ind_positive] > Alt_ub
        #        #e[:, ind_positive[ind]] = Alt_ub[ind]
        #        e[:, np.tile(ind_positive,(ind.shape[0],1))[ind]] = Alt_ub[ind]
        #        #e[:, ind_positive.reshape(ind.shape)[ind]] = Alt_ub[ind]
        #
        #        indT[np.any(ind, 1)] = True

        for _i, i in enumerate(ind_positive):
            #s = S - S1[:, _i]
            #alt_ub = (b - s) / A[i]
            #alt_ub = S1_[:, _i] / A[i]
            alt_ub = Alt_ub[:, _i]
            ind = e[:, i] > alt_ub
            Ind = where(ind)[0]
            if Ind.size:
                e[Ind, i] = alt_ub[Ind]
                indT[Ind] = True

    if ind_negative.size != 0:
        S2_ = b - S.reshape(-1, 1) + S2
        Alt_lb = S2_ / A2
        for _i, i in enumerate(ind_negative):
            #s = S - S2[:, _i]
            #alt_lb = (b - s) / A[i]
            #alt_lb = S2_[:, _i] / A[i]
            alt_lb = Alt_lb[:, _i]
            ind = y[:, i] < alt_lb
            Ind = where(ind)[0]
            if Ind.size:
                y[Ind, i] = alt_lb[Ind]
                indT[Ind] = True

    ind = np.all(e >= y, 1)
    if not np.all(ind):
        ind_trunc = where(ind)[0]
        lj = ind_trunc.size
        y = take(y, ind_trunc, axis=0, out=y[:lj])
        e = take(e, ind_trunc, axis=0, out=e[:lj])
        indT = indT[ind_trunc]

    return y, e, indT, ind_trunc
Ejemplo n.º 27
0
def r14IP(p, nlhc, residual, definiteRange, y, e, vv, asdf1, C, CBKPMV, g, nNodes,  \
         frc, fTol, Solutions, varTols, _in, dataType, \
         maxNodes, _s, indTC, xRecord):

    required_sigma = p.ftol * 0.99 # to suppress roundoff effects
    
    m, n = y.shape
    
    ip = func10(y, e, vv)
    ip.dictOfFixedFuncs = p.dictOfFixedFuncs
    ip.surf_preference = True

    tmp = asdf1.interval(ip, ia_surf_level=2)
#    print(type(tmp))
    if hasattr(tmp, 'resolve'):#type(tmp) == boundsurf:
#            print('b')
        #adjustr4WithDiscreteVariables(wr4, p)
        
        if 1:
        # changes
            val_l, val_u = zeros(2*n*m), zeros(2*n*m)
            
            val_l += tmp.l.c # may be array <- scalar
            val_u += tmp.u.c
            
            for v in tmp.dep:
                ind = p._oovarsIndDict[v]
                ts, te = ip[v]#y[:, ind], e[:, ind]
                A, B = (te**2 + te*ts + ts**2) / 3.0, 0.5 * (te + ts)
                #A, B, C = (te**3 - ts**3) / 3.0, 0.5 * (te**2 - ts**2), te - ts
                b = tmp.l.d.get(v, 0.0)
                val_l += b * B
                if tmp.level == 2:
                    a = tmp.l.d2.get(v, 0.0)
                    val_l += a * A
                
                b= tmp.u.d.get(v, 0.0)
                val_u +=  b * B
                if tmp.level == 2:
                    a = tmp.u.d2.get(v, 0.0)
                    val_u += a * A

            #r20 = val_u - val_l
            #approx_value = 0.5 * (val_l + val_u)
            o, a = val_l, val_u
        # changes end
        else:
            cs = oopoint((v, asarray(0.5*(val[0] + val[1]), dataType)) for v, val in ip.items())
            cs.dictOfFixedFuncs = p.dictOfFixedFuncs
            o, a = tmp.values(cs)
        
        definiteRange = tmp.definiteRange
    else:
        o, a, definiteRange = tmp.lb, tmp.ub, tmp.definiteRange

    
    if not all(definiteRange):
        p.err('''
        numerical integration with interalg is implemented 
        for definite (real) range only, no NaN values in integrand are allowed''')

    o, a = o.reshape(2*n, m).T, a.reshape(2*n, m).T

    nodes = func11(y, e, None, indTC, None, o, a, _s, p)

    #an = nodes if len(_in) == 0 else hstack((_in, nodes)).tolist()
    an = nodes + _in
    
    if 1: 
        an.sort(key = lambda obj: obj.key, reverse=False)
        #an.sort(key = lambda obj: obj.minres, reverse=False)
    else:
        an.sort(key=lambda obj: obj.volumeResidual, reverse=False)

    ao_diff = array([node.key for node in an])
    volumes = array([node.volume for node in an])
    
    r10 = ao_diff <= 0.95*(required_sigma-p._residual) / (prod(p.ub-p.lb) - p._volume)
    ind = where(r10)[0]
    # TODO: use true_sum
    v = volumes[ind]
    p._F += sum(array([an[i].F for i in ind]) * v)
    residuals = ao_diff[ind] * v
    p._residual += residuals.sum()
    p._volume += v.sum()
    
    #an = asarray(an, object)
    #an = an[where(logical_not(r10))[0]]
    an = [elem for i,  elem in enumerate(an) if not r10[i]]
        
    nNodes.append(len(an))
   
    p.iterfcn(xk=array(nan), fk=p._F, rk = 0)#TODO: change rk to something like p._r0 - p._residual
    if p.istop != 0: 
        ao_diff = array([node.key for node in an])
        volumes = array([node.volume for node in an])
        p._residual += sum(ao_diff * volumes)
        _s = None
 
    #an, g = func9(an, fo, g, 'IP')
    #nn = 1 if asdf1.isUncycled and all(isfinite(a)) and all(isfinite(o)) and p._isOnlyBoxBounded else maxNodes
    #an, g = func5(an, nn, g)

    return an, g, inf, _s, Solutions, xRecord, frc, CBKPMV
Ejemplo n.º 28
0
def r14IP(p, nlhc, residual, definiteRange, y, e, vv, asdf1, C, CBKPMV, g, nNodes,  \
         frc, fTol, Solutions, varTols, _in, dataType, \
         maxNodes, _s, indTC, xRecord):

    required_sigma = p.ftol * 0.99  # to suppress roundoff effects

    m, n = y.shape

    ip = func10(y, e, vv)
    ip.dictOfFixedFuncs = p.dictOfFixedFuncs
    ip.surf_preference = True

    tmp = asdf1.interval(ip, ia_surf_level=2)
    #    print(type(tmp))
    if hasattr(tmp, 'resolve'):  #type(tmp) == boundsurf:
        #            print('b')
        #adjustr4WithDiscreteVariables(wr4, p)

        if 1:
            # changes
            val_l, val_u = zeros(2 * n * m), zeros(2 * n * m)

            val_l += tmp.l.c  # may be array <- scalar
            val_u += tmp.u.c

            for v in tmp.dep:
                ind = p._oovarsIndDict[v]
                ts, te = ip[v]  #y[:, ind], e[:, ind]
                A, B = (te**2 + te * ts + ts**2) / 3.0, 0.5 * (te + ts)
                #A, B, C = (te**3 - ts**3) / 3.0, 0.5 * (te**2 - ts**2), te - ts
                b = tmp.l.d.get(v, 0.0)
                val_l += b * B
                if tmp.level == 2:
                    a = tmp.l.d2.get(v, 0.0)
                    val_l += a * A

                b = tmp.u.d.get(v, 0.0)
                val_u += b * B
                if tmp.level == 2:
                    a = tmp.u.d2.get(v, 0.0)
                    val_u += a * A

            #r20 = val_u - val_l
            #approx_value = 0.5 * (val_l + val_u)
            o, a = val_l, val_u
        # changes end
        else:
            cs = oopoint((v, asarray(0.5 * (val[0] + val[1]), dataType))
                         for v, val in ip.items())
            cs.dictOfFixedFuncs = p.dictOfFixedFuncs
            o, a = tmp.values(cs)

        definiteRange = tmp.definiteRange
    else:
        o, a, definiteRange = tmp.lb, tmp.ub, tmp.definiteRange

    if not all(definiteRange):
        p.err('''
        numerical integration with interalg is implemented 
        for definite (real) range only, no NaN values in integrand are allowed'''
              )

    o, a = o.reshape(2 * n, m).T, a.reshape(2 * n, m).T

    nodes = func11(y, e, None, indTC, None, o, a, _s, p)

    #an = nodes if len(_in) == 0 else hstack((_in, nodes)).tolist()
    an = nodes + _in

    if 1:
        an.sort(key=lambda obj: obj.key, reverse=False)
        #an.sort(key = lambda obj: obj.minres, reverse=False)
    else:
        an.sort(key=lambda obj: obj.volumeResidual, reverse=False)

    ao_diff = array([node.key for node in an])
    volumes = array([node.volume for node in an])

    r10 = ao_diff <= 0.95 * (required_sigma -
                             p._residual) / (prod(p.ub - p.lb) - p._volume)
    ind = where(r10)[0]
    # TODO: use true_sum
    v = volumes[ind]
    p._F += sum(array([an[i].F for i in ind]) * v)
    residuals = ao_diff[ind] * v
    p._residual += residuals.sum()
    p._volume += v.sum()

    #an = asarray(an, object)
    #an = an[where(logical_not(r10))[0]]
    an = [elem for i, elem in enumerate(an) if not r10[i]]

    nNodes.append(len(an))

    p.iterfcn(xk=array(nan), fk=p._F,
              rk=0)  #TODO: change rk to something like p._r0 - p._residual
    if p.istop != 0:
        ao_diff = array([node.key for node in an])
        volumes = array([node.volume for node in an])
        p._residual += sum(ao_diff * volumes)
        _s = None

    #an, g = func9(an, fo, g, 'IP')
    #nn = 1 if asdf1.isUncycled and all(isfinite(a)) and all(isfinite(o)) and p._isOnlyBoxBounded else maxNodes
    #an, g = func5(an, nn, g)

    return an, g, inf, _s, Solutions, xRecord, frc, CBKPMV
Ejemplo n.º 29
0
    def __solver__(self, p):

        alp, h0, nh, q1, q2 = self.alp, self.h0, self.nh, self.q1, self.q2

        if isPyPy:
            if p.nc != 0 or p.nh != 0:
                p.warn(
                    "in PyPy ralg may work incorrectly with nonlinear constraints yet"
                )
            if p.nbeq != 0 or any(p.lb == p.ub):
                p.err(
                    'in PyPy ralg cannot handle linear equality constraints yet'
                )

        if type(q1) == str:
            if p.probType == 'NLP' and p.isUC: q1 = 0.9
            else: q1 = 1.0
        T = self.T
        # alternatively instead of alp=self.alp etc you can use directly self.alp etc

        n = p.n
        x0 = p.x0

        if p.nbeq == 0 or any(
                abs(p._get_AeqX_eq_Beq_residuals(x0)) > p.contol
        ):  # TODO: add "or Aeqconstraints(x0) out of contol"
            x0[x0 < p.lb] = p.lb[x0 < p.lb]
            x0[x0 > p.ub] = p.ub[x0 > p.ub]

        ind_box_eq = where(p.lb == p.ub)[0]
        nEQ = ind_box_eq.size
        if nEQ != 0:
            initLenBeq = p.nbeq
            Aeq, beq, nbeq = copy(p.Aeq), copy(p.beq), p.nbeq
            p.Aeq = zeros([Len(p.beq) + nEQ, p.n])
            p.beq = zeros(Len(p.beq) + nEQ)
            p.beq[:Len(beq)] = beq
            p.Aeq[:Len(beq)] = Aeq
            for i in range(len(ind_box_eq)):
                p.Aeq[initLenBeq + i, ind_box_eq[i]] = 1
                p.beq[initLenBeq + i] = p.lb[ind_box_eq[
                    i]]  # = p.ub[indEQ[i]], because they are the same
            p.nbeq += nEQ

        if not self.newLinEq or p.nbeq == 0:
            #needProjection = False
            B0 = eye(n, dtype=T)
            restoreProb = lambda *args: 0
            Aeq_r, beq_r, nbeq_r = None, None, 0
        else:
            #needProjection = True
            B0 = self.getPrimevalDilationMatrixWRTlinEqConstraints(p)
            #Aeq, beq, nbeq = p.Aeq, p.beq, p.nbeq

            if any(abs(p._get_AeqX_eq_Beq_residuals(x0)) > p.contol / 16.0):
                #p.debugmsg('old point Aeq residual:'+str(norm(dot(Aeq, x0)-beq)))
                try:
                    x0 = self.linEqProjection(x0, p.Aeq, p.beq)
                except LinAlgError:
                    s = 'Failed to obtain projection of start point to linear equality constraints subspace, probably the system is infeasible'
                    p.istop, p.msg = -25, s
                    return

                #p.debugmsg('new point Aeq residual:'+str(norm(dot(Aeq, x0)-beq)))
            if nEQ == 0:
                Aeq_r, beq_r, nbeq_r = p.Aeq, p.beq, p.nbeq
            else:
                Aeq_r, beq_r, nbeq_r = Aeq, beq, nbeq

            p.Aeq, p.beq, p.nbeq = None, None, 0

            # TODO: return prob with unmodified Aeq, beq

            def restoreProb():
                p.Aeq, p.beq, p.nbeq = Aeq_r, beq_r, nbeq_r
                #if nEQ != 0: restore lb, ub

        b = B0.copy() if self.B is None else self.B
        #        B_f = diag(ones(n))
        #        B_constr = diag(ones(n))
        hs = asarray(h0, T)

        if self.innerState is not None:
            hs = self.innerState['hs']
            b = self.innerState['B']

        ls_arr = []
        w = asarray(1.0 / alp - 1.0, T)
        """                            Shor r-alg engine                           """
        bestPoint = p.point(array(copy(x0).tolist(),
                                  T))  # tolist() for PyPy compatibility
        prevIter_best_ls_point = bestPoint
        prevIter_PointForDilation = bestPoint

        g = bestPoint._getDirection(self.approach)
        prevDirectionForDilation = g
        moveDirection = g
        if not any(g) and all(isfinite(g)):
            # TODO: create ENUMs
            p.iterfcn(bestPoint)
            restoreProb()
            p.istop = 14 if bestPoint.isFeas(False) else -14
            p.msg = 'move direction has all-zero coords'
            return

        HS = []
        LS = []

        #SwitchEncountered = False
        selfNeedRej = False
        #doScale = False

        #directionVectorsList = []
        #        #pass-by-ref! not copy!
        #        if p.isFeas(p.x0): b = B_f
        #        else: b = B_constr

        #        if p.debug and hasattr(p, 'x_opt'):
        #            import scipy
        #            exactDirection = x0-p.x_opt
        #            asdf_0 = exactDirection * (0.2+scipy.rand(n))
        #            #asdf = asdf_0.copy()

        #fTol = p.fTol if p.fTol is not None else 15*p.ftol

        # CHANGES
        if self.penalties:
            oldVal = p.f(p.x0)
            newVal = inf
            x = p.x0

            #H,  DH = p.h, p.dh
            if p.nh != 0:
                #S = 1.0

                _Aeq = p.dh(x)
                _beq = -p.h(x)
                df = p.df(x)
                if n >= 150 and not scipyInstalled:
                    p.pWarn(scipyAbsentMsg)
                if n > 100 and scipyInstalled:
                    from scipy.sparse import eye as Eye  # to prevent numpy.eye overwrite
                    HH = Eye(n, n)
                else:
                    HH = eye(n)
                qp = openopt.QP(H=HH, f=df, Aeq=_Aeq, beq=_beq)
                QPsolver = openopt.oosolver('cvxopt_qp', iprint=-1)
                if not QPsolver.isInstalled:
                    S = None
                else:
                    r = qp.solve(QPsolver)
                    S = 10.0 * sum(abs(r.duals)) if r.istop > 0 else None

                while any(p.h(x)) > p.contol:
                    if S is not None:
                        p2 = getattr(openopt, p.probType)(p.f, x)
                        p.fill(p2)
                        p2.x0 = x
                        p2.h = p2.dh = None
                        p2.userProvided.h = p2.userProvided.dh = False
                        p2.nh = 0
                        p2.f = lambda *args, **kwargs: p.f(
                            *args, **kwargs) + sum(
                                abs(S * p.h(*args, **kwargs)))
                        p2.df = lambda *args, **kwargs: p.df(
                            *args, **kwargs) + dot(
                                S * sign(p.h(*args, **kwargs)),
                                p.dh(*args, **kwargs))
                        #p2.iterfcn = p.iterfcn
                        #                    def df2(*args, **kwargs):
                        #                        r1 = p.df(*args, **kwargs)
                        #                        r2 = S * dot(p.dh(*args, **kwargs).reshape(-1, 1), sign(p.h(*args, **kwargs))).flatten()
                        #                        #raise 0
                        #                        return r1+r2
                        #                    #p2.df = lambda *args, **kwargs: p.df(*args, **kwargs) + S * dot(p.dh(x).reshape(-1, 1), sign(p.h(*args, **kwargs))).flatten()
                        #                    p2.df = df2
                        #                    #raise 0
                        r2 = p2.solve(p.solver, iprint=10)
                        if r2.stopcase >= 0:
                            x = r2.xf
                            p.solver.innerState = r2.extras['innerState']
                            oldVal, newVal = newVal, r2.ff
                        else:
                            if r2.istop == IS_LINE_SEARCH_FAILED:
                                # TODO: custom S as raising penalties
                                pass

                        if p.isFeas(p2.xk):
                            p.xf = p.xk = p2.xk
                            p.istop, p.msg = p2.istop, p2.msg
                            return
                        else:
                            S *= 50
                            #print('max residual:%0.2e'% r2.rf)

                    else:  # failed to solve QP
                        break

        # CHANGES END
        """                           Ralg main cycle                                    """

        for itn in range(p.maxIter + 10):
            doDilation = True
            lastPointOfSameType = None  # to prevent possible bugs
            alp_addition = 0.0

            iterStartPoint = prevIter_best_ls_point
            x = iterStartPoint.x.copy()

            g_tmp = economyMult(b.T, moveDirection)
            if any(g_tmp): g_tmp /= p.norm(g_tmp)
            g1 = p.matmult(b, g_tmp)

            #            norm_moveDirection = p.norm(g1)
            #            if doScale:
            #                g1 *= (norm_moveDirection_prev/norm_moveDirection) ** 0.5
            #            norm_moveDirection_prev = norm_moveDirection

            #            if p.debug and hasattr(p, 'x_opt'):
            #                cos_phi_0 = p.matmult(moveDirection,  prevIter_best_ls_point.x - p.x_opt)/p.norm(moveDirection)/p.norm(prevIter_best_ls_point.x - p.x_opt)
            #                cos_phi_1 = p.matmult(g1,  prevIter_best_ls_point.x - p.x_opt)/p.norm(g1)/p.norm(prevIter_best_ls_point.x - p.x_opt)
            #                print('beforeDilation: %f  afterDilation: %f' % (cos_phi_0, cos_phi_1) )
            #                asdf = asdf_0.copy()
            #                g_tmp = economyMult(b.T, asdf)
            #
            #                #g_tmp = p.matmult(b.T, asdf)
            #
            #                if any(g_tmp): g_tmp /= p.norm(g_tmp)
            #                asdf = p.matmult(b, g_tmp)
            #                cos_phi = dot(asdf, exactDirection) / p.norm(asdf) / p.norm(exactDirection)
            #                p.debugmsg('cos_phi:%f' % cos_phi)
            #                assert cos_phi >0
            """                           Forward line search                          """

            hs_cumsum = 0
            hs_start = hs
            for ls in range(p.maxLineSearch):
                hs_mult = 1.0
                if ls > 20:
                    hs_mult = 2.0
                elif ls > 10:
                    hs_mult = 1.5
                elif ls > 2:
                    hs_mult = 1.05
                hs *= hs_mult

                x -= hs * g1
                hs_cumsum += hs

                newPoint = p.point(x) if ls == 0 else iterStartPoint.linePoint(
                    hs_cumsum /
                    (hs_cumsum - hs), oldPoint)  #  TODO: take ls into account?

                if not p.isUC:
                    if newPoint.isFeas(True) == iterStartPoint.isFeas(True):
                        lastPointOfSameType = newPoint

                if self.show_nnan:
                    p.info('ls: %d nnan: %d' % (ls, newPoint.__nnan__()))

                if ls == 0:
                    oldPoint = prevIter_best_ls_point  #prevIterPoint
                    oldoldPoint = oldPoint

                #if not self.checkTurnByGradient:
                if newPoint.betterThan(oldPoint, altLinInEq=True):
                    if newPoint.betterThan(bestPoint, altLinInEq=False):
                        bestPoint = newPoint
                    oldoldPoint = oldPoint
                    oldPoint, newPoint = newPoint, None
                else:
                    if not itn % 4:
                        for fn in ['_lin_ineq', '_lin_eq']:
                            if hasattr(newPoint, fn): delattr(newPoint, fn)
                    break

            hs /= hs_mult

            if ls == p.maxLineSearch - 1:
                p.istop, p.msg = IS_LINE_SEARCH_FAILED, 'maxLineSearch (' + str(
                    p.maxLineSearch
                ) + ') has been exceeded, the problem seems to be unbounded'
                restoreProb()
                return

            #iterPoint  = newPoint
            PointForDilation = newPoint
            #best_ls_point = newPoint if ls == 0 else oldPoint
            #if p.debug and ls != 0: assert not oldPoint.betterThan(best_ls_point)
            """                          Backward line search                          """
            mdx = max((150, 1.5 * p.n)) * p.xtol
            if itn == 0:
                mdx = max((hs / 128.0,
                           128 * p.xtol))  # TODO: set it after B rej as well
            ls_backward = 0
            maxLS = 3 if ls == 0 else 1
            #            if ls <=3 or ls > 20:
            if self.doBackwardSearch:
                if self.new_bs:
                    best_ls_point,  PointForDilation, ls_backward = \
                    getBestPointAfterTurn(oldoldPoint, newPoint, maxLS = maxLS, maxDeltaF = 150*p.ftol, \
                                          maxDeltaX = mdx, altLinInEq = True, new_bs = True)
                    if PointForDilation.isFeas(True) == iterStartPoint.isFeas(
                            True):
                        lastPointOfSameType = PointForDilation
#                        elif best_ls_point.isFeas(altLinInEq=True) == iterStartPoint.isFeas(altLinInEq=True):
#                            lastPointOfSameType = best_ls_point
                else:
                    best_ls_point, ls_backward = \
                    getBestPointAfterTurn(oldoldPoint, newPoint, maxLS = maxLS, altLinInEq = True, new_bs = False)
                    PointForDilation = best_ls_point

                # TODO: extract last point from backward search, that one is better than iterPoint
                if best_ls_point.betterThan(bestPoint):
                    bestPoint = best_ls_point
                #p.debugmsg('ls_backward:%d' % ls_backward)
                if ls == 0 and ls_backward == -maxLS:
                    #pass
                    alp_addition += 0.25
                    #hs *= 0.9

                if ls_backward <= -1 and itn != 0:  # TODO: mb use -1 or 0 instead?
                    pass
                    #alp_addition -= 0.25*ls_backward # ls_backward less than zero

                #hs *= 2 ** min((ls_backward+1, 0))
            else:
                pass
                #hs *= 0.95

            best_ls_point = PointForDilation  # elseware lots of difficulties
            """                                 Updating hs                                 """
            step_x = p.norm(PointForDilation.x - prevIter_PointForDilation.x)
            step_f = abs(PointForDilation.f() - prevIter_PointForDilation.f())
            HS.append(hs_start)
            assert ls >= 0
            LS.append(ls)
            if itn > 3:
                mean_ls = (3 * LS[-1] + 2 * LS[-2] + LS[-3]) / 6.0
                j0 = 3.3
                if mean_ls > j0:
                    hs = (mean_ls - j0 + 1)**0.5 * hs_start
                else:
                    #hs = (ls/j0) ** 0.5 * hs_start
                    hs = hs_start
                    if ls == 0 and ls_backward == -maxLS:
                        shift_x = step_x / p.xtol
                        RD = log10(shift_x + 1e-100)
                        if PointForDilation.isFeas(
                                True) or prevIter_PointForDilation.isFeas(
                                    True):
                            RD = min(
                                (RD,
                                 asscalar(
                                     asarray(log10(step_f / p.ftol +
                                                   1e-100)))))
                        if RD > 1.0:
                            mp = (0.5, (ls / j0)**0.5, 1 - 0.2 * RD)
                            hs *= max(mp)
                            #from numpy import argmax
                            #print argmax(mp), mp
            """                            Handling iterPoints                            """

            best_ls_point = PointForDilation

            #if not SwitchEncountered and p.nh != 0 and PointForDilation.isFeas(altLinInEq=False) != prevIter_PointForDilation.isFeas(altLinInEq=False):
            #SwitchEncountered = True
            #selfNeedRej = True

            involve_lastPointOfSameType = False
            if lastPointOfSameType is not None and PointForDilation.isFeas(
                    True) != prevIter_PointForDilation.isFeas(True):
                # TODO: add middle point for the case ls = 0
                assert self.dilationType == 'plain difference'
                #directionForDilation = lastPointOfSameType._getDirection(self.approach)
                PointForDilation = lastPointOfSameType
                involve_lastPointOfSameType = True

            #directionForDilation = newPoint.__getDirection__(self.approach) # used for dilation direction obtaining

#            if not self.new_bs or ls != 0:
#                moveDirection = iterPoint.__getDirection__(self.approach)
#            else:
#                moveDirection = best_ls_point.__getDirection__(self.approach)

#directionForDilation = pointForDilation.__getDirection__(self.approach)

#                cos_phi = -p.matmult(moveDirection, prevIterPoint.__getDirection__(self.approach))
#                assert cos_phi.size == 1
#                if cos_phi> 0:
#                    g2 = moveDirection#pointForDilation.__getDirection__(self.approach)
#                else:
#                    g2 = pointForDilation.__getDirection__(self.approach)

            if itn == 0:
                p.debugmsg('hs: ' + str(hs))
                p.debugmsg('ls: ' + str(ls))
            if self.showLS: p.info('ls: ' + str(ls))
            if self.show_hs: p.info('hs: ' + str(hs))
            if self.show_nnan: p.info('nnan: ' + str(best_ls_point.__nnan__()))
            if self.showRes:
                r, fname, ind = best_ls_point.mr(True)
                p.info(fname + str(ind))
            """                         Set dilation direction                            """

            #if sum(p.dotmult(g, g2))>0:
            #p.debugmsg('ralg warning: slope angle less than pi/2. Mb dilation for the iter will be omitted.')
            #doDilation = False

            # CHANGES
            #            if lastPointOfSameType is None:
            #                if currIterPointIsFeasible and not prevIterPointIsFeasible:
            #                    alp_addition += 0.1
            #                elif prevIterPointIsFeasible and not currIterPointIsFeasible:
            #                    alp_addition -= 0.0

            # CHANGES END

            #            r_p, ind_p, fname_p = prevIter_best_ls_point.mr(1)
            #            r_, ind_, fname_ = PointForDilation.mr(1)

            #else:

            #print itn,'>>>>>>>>>', currIterPointIsFeasible
            """                                    Excluding derivatives switched to/from NaN                                    """

            if self.skipPrevIterNaNsInDilation:
                c_prev, c_current = prevIter_PointForDilation.c(
                ), PointForDilation.c()
                h_prev, h_current = prevIter_PointForDilation.h(
                ), PointForDilation.h()
            """                                             Handling switch to NaN                                            """
            NaN_derivatives_excluded = False
            if self.skipPrevIterNaNsInDilation:
                assert self.approach == 'all active'

                if not prevIter_PointForDilation.isFeas(True):
                    """                          processing NaNs in nonlin inequality constraints                          """
                    ind_switch_ineq_to_nan = where(
                        logical_and(isnan(c_current), c_prev > 0))[0]
                    if len(ind_switch_ineq_to_nan) != 0:
                        NaN_derivatives_excluded = True
                        tmp = prevIter_PointForDilation.dc(
                            ind_switch_ineq_to_nan)
                        if hasattr(tmp, 'toarray'):
                            tmp = tmp.A
                        if len(ind_switch_ineq_to_nan) > 1:
                            tmp *= (c_prev[ind_switch_ineq_to_nan] / sqrt(
                                (tmp**2).sum(1))).reshape(-1, 1)
                        else:
                            tmp *= c_prev[ind_switch_ineq_to_nan] / norm(tmp)
                        if tmp.ndim > 1: tmp = tmp.sum(0)
                        if not isinstance(tmp, ndarray) or isinstance(
                                tmp, matrix):
                            tmp = tmp.A.flatten()  # dense or sparse matrix
                        #print '1: excluded:', norm(tmp), norm(prevDirectionForDilation)
                        prevDirectionForDilation -= tmp
                        #print '1: result=', norm(prevDirectionForDilation)
                    """                           processing NaNs in nonlin equality constraints                           """
                    ind_switch_eq_to_nan = where(
                        logical_and(isnan(h_current), h_prev > 0))[0]
                    if len(ind_switch_eq_to_nan) != 0:
                        NaN_derivatives_excluded = True
                        tmp = prevIter_PointForDilation.dh(
                            ind_switch_eq_to_nan)
                        if tmp.ndim > 1: tmp = tmp.sum(0)
                        if not isinstance(tmp, ndarray) or isinstance(
                                tmp, matrix):
                            tmp = tmp.A.flatten()  # dense or sparse matrix
                        prevDirectionForDilation -= tmp

                    ind_switch_eq_to_nan = where(
                        logical_and(isnan(h_current), h_prev < 0))[0]
                    if len(ind_switch_eq_to_nan) != 0:
                        NaN_derivatives_excluded = True
                        tmp = prevIter_PointForDilation.dh(
                            ind_switch_eq_to_nan)
                        if tmp.ndim > 1: tmp = tmp.sum(0)
                        if not isinstance(tmp, ndarray) or isinstance(
                                tmp, matrix):
                            tmp = tmp.A.flatten()  # dense or sparse matrix
                        prevDirectionForDilation += tmp

            directionForDilation = PointForDilation._getDirection(
                self.approach)
            """                                            Handling switch from NaN                                           """
            if self.skipPrevIterNaNsInDilation:

                if not PointForDilation.isFeas(True):
                    """                          processing NaNs in nonlin inequality constraints                          """
                    ind_switch_ineq_from_nan = where(
                        logical_and(isnan(c_prev), c_current > 0))[0]
                    if len(ind_switch_ineq_from_nan) != 0:
                        NaN_derivatives_excluded = True
                        tmp = PointForDilation.dc(ind_switch_ineq_from_nan)
                        if hasattr(tmp, 'toarray'):
                            tmp = tmp.A
                        if len(ind_switch_ineq_from_nan) > 1:
                            tmp *= (c_current[ind_switch_ineq_from_nan] / sqrt(
                                (tmp**2).sum(1))).reshape(-1, 1)
                        else:
                            tmp *= c_current[ind_switch_ineq_from_nan] / norm(
                                tmp)
                        if tmp.ndim > 1: tmp = tmp.sum(0)
                        if not isinstance(tmp, ndarray) or isinstance(
                                tmp, matrix):
                            tmp = tmp.A.flatten()  # dense or sparse matrix
                        #print '2: excluded:', norm(tmp), norm(directionForDilation)
                        directionForDilation -= tmp
                        #print '2: result=', norm(directionForDilation)
                    """                           processing NaNs in nonlin equality constraints                           """
                    ind_switch_eq_from_nan = where(
                        logical_and(isnan(h_prev), h_current > 0))[0]
                    if len(ind_switch_eq_from_nan) != 0:
                        NaN_derivatives_excluded = True
                        tmp = PointForDilation.dh(ind_switch_eq_from_nan)
                        if tmp.ndim > 1: tmp = tmp.sum(0)
                        if not isinstance(tmp, ndarray) or isinstance(
                                tmp, matrix):
                            tmp = tmp.A.flatten()  # dense or sparse matrix
                        directionForDilation -= tmp

                    ind_switch_eq_from_nan = where(
                        logical_and(isnan(h_prev), h_current < 0))[0]
                    if len(ind_switch_eq_from_nan) != 0:
                        NaN_derivatives_excluded = True
                        tmp = PointForDilation.dh(ind_switch_eq_from_nan)
                        if tmp.ndim > 1: tmp = tmp.sum(0)
                        if not isinstance(tmp, ndarray) or isinstance(
                                tmp, matrix):
                            tmp = tmp.A.flatten()  # dense or sparse matrix
                        directionForDilation += tmp

#            # CHANGES
#            gn = g2/norm(g2)
#            if len(directionVectorsList) == 0 or n < 3: pass
#            else:
#                if len(directionVectorsList) == 1 or abs(dot(directionVectorsList[-1], directionVectorsList[-2]))>0.999:
#                    projectionComponentLenght = abs(dot(directionVectorsList[-1], gn))
#                    restLength = sqrt(1 - min((1, projectionComponentLenght))**2)
#                else:
#                    e1 = directionVectorsList[-1]
#                    e2 = directionVectorsList[-2] - dot(directionVectorsList[-1], directionVectorsList[-2]) * directionVectorsList[-1]
#                    e2 /= norm(e2)
#
#                    proj1, proj2 = dot(e1, gn), dot(e2, gn)
#                    rest = gn - proj1 * e1 - proj2 * e2
#                    restLength = norm(rest)
#                if restLength > 1+1e-5: p.pWarn('possible error in ralg solver: incorrect restLength, exceeds 1.0')
#
#                # TODO: make it parameters of ralg
#                commonCoeff, alp_add_coeff = 0.5, 1.0
#
#                if restLength < commonCoeff * (n - 2.0) / n:
#                    #pass
#                    alpAddition = 0.5+(arctan((n - 2.0) / (n * restLength)) - pi / 4.0) / (pi / 2.0) * alp_add_coeff
#                    #p.debugmsg('alpAddition:' + str(alpAddition))
#                    assert alpAddition > 0 # if someone incorrectly modifies commonCoeff it can be less than zero
#                    alp_addition += alpAddition
#                    #p.debugmsg('alp_addition:' + str(alp_addition))
#
#            directionVectorsList.append(gn)
#            if len(directionVectorsList) > 2: directionVectorsList = directionVectorsList[:-2]
#            # CHANGES END


            if self.dilationType == 'normalized' and (not fname_p in ('lb', 'ub', 'lin_eq', 'lin_ineq') \
                                                      or not fname_ in ('lb', 'ub', 'lin_eq', 'lin_ineq')) and (fname_p != fname_  or ind_p != ind_):
                G2, G = directionForDilation / norm(
                    directionForDilation), prevDirectionForDilation / norm(
                        prevDirectionForDilation)
            else:
                G2, G = directionForDilation, prevDirectionForDilation

            if prevIter_PointForDilation.isFeas(
                    True) == PointForDilation.isFeas(True):
                g1 = G2 - G
            elif prevIter_PointForDilation.isFeas(True):
                g1 = G2.copy()
            else:
                g1 = G.copy()
                alp_addition += 0.05

            #print p.getMaxResidual(PointForDilation.x, 1)
            ##############################################
            # the case may be occured when
            #  1) lastPointOfSameType is used
            # or
            #  2) some NaN from constraints have been excluded
            if norm(G2 - G) < 1e-12 * min(
                (norm(G2), norm(G))) and (involve_lastPointOfSameType
                                          or NaN_derivatives_excluded):
                p.debugmsg("ralg: 'last point of same type gradient' is used")
                g1 = G2
            ##############################################

            #g1 = -G.copy() # signum doesn't matter here

            # changes wrt infeas constraints
#            if prevIterPoint.nNaNs() != 0:
#                cp, hp = prevIterPoint.c(), prevIterPoint.h()
#                ind_infeas_cp, ind_infeas_hp = isnan(cp), isnan(hp)
#
#                c, h = iterPoint.c(), iterPoint.h()
#                ind_infeas_c, ind_infeas_h = isnan(c), isnan(h)
#
#                ind_goodChange_c = logical_and(ind_infeas_cp,  logical_not(ind_infeas_c))
#                ind_goodChange_h = logical_and(ind_infeas_hp,  logical_not(ind_infeas_h))
#
#                any_c, any_h = any(ind_goodChange_c), any(ind_goodChange_h)
#                altDilation = zeros(n)
#                if any_c:
#                    altDilation += sum(atleast_2d(iterPoint.dc(where(ind_goodChange_c)[0])), 0)
#                    assert not any(isnan(altDilation))
#                if any_h:
#                    altDilation += sum(atleast_2d(iterPoint.dh(where(ind_goodChange_h)[0])), 0)
#                if any_c or any_h:
#                    #print '!>', altDilation
#                    #g1 = altDilation
#                    pass
# changes end
            """                             Perform dilation                               """

            # CHANGES
            #            g = economyMult(b.T, g1)
            #            gn = g/norm(g)
            #            if len(directionVectorsList) == 0 or n < 3 or norm(g1) < 1e-20: pass
            #            else:
            #                if len(directionVectorsList) == 1 or abs(dot(directionVectorsList[-1], directionVectorsList[-2]))>0.999:
            #                    projectionComponentLenght = abs(dot(directionVectorsList[-1], gn))
            #                    restLength = sqrt(1 - min((1, projectionComponentLenght))**2)
            #                else:
            #                    e1 = directionVectorsList[-1]
            #                    e2 = directionVectorsList[-2] - dot(directionVectorsList[-1], directionVectorsList[-2]) * directionVectorsList[-1]
            #                    print dot(directionVectorsList[-1], directionVectorsList[-2])
            #                    e2 /= norm(e2)
            #                    proj1, proj2 = dot(e1, gn), dot(e2, gn)
            #                    rest = gn - proj1 * e1 - proj2 * e2
            #                    restLength = norm(rest)
            #                assert restLength < 1+1e-5, 'error in ralg solver: incorrect restLength'
            #
            #                # TODO: make it parameters of ralg
            #                commonCoeff, alp_add_coeff = 0.5, 1.0
            #
            #                if restLength < commonCoeff * (n - 2.0) / n:
            #                    #pass
            #                    alpAddition = 0.5+(arctan((n - 2.0) / (n * restLength)) - pi / 4.0) / (pi / 2.0) * alp_add_coeff
            #                    #p.debugmsg('alpAddition:' + str(alpAddition))
            #                    assert alpAddition > 0 # if someone incorrectly modifies commonCoeff it can be less than zero
            #                    alp_addition += alpAddition
            #                    #p.debugmsg('alp_addition:' + str(alp_addition))
            #
            #            directionVectorsList.append(gn)
            #            if len(directionVectorsList) > 2: directionVectorsList = directionVectorsList[:-2]
            # CHANGES END

            if doDilation:
                g = economyMult(b.T, g1)
                ng = p.norm(g)

                if self.needRej(p, b, g1, g) or selfNeedRej:
                    selfNeedRej = False
                    if self.showRej or p.debug:
                        p.info(
                            'debug msg: matrix B restoration in ralg solver')
                    b = B0.copy()
                    hs = p.norm(prevIter_best_ls_point.x - best_ls_point.x)
                    # TODO: iterPoint = projection(iterPoint,Aeq) if res_Aeq > 0.75*contol

                if ng < 1e-40:
                    hs *= 0.9
                    p.debugmsg('small dilation direction norm (%e), skipping' %
                               ng)
                if all(isfinite(g)) and ng > 1e-50 and doDilation:
                    g = (g / ng).reshape(-1, 1)
                    vec1 = economyMult(b, g).reshape(
                        -1, 1)  # TODO: remove economyMult, use dot?
                    #if alp_addition != 0: p.debugmsg('alp_addition:' + str(alp_addition))
                    w = asarray(1.0 / (alp + alp_addition) - 1.0, T)
                    vec2 = w * g.T
                    b += p.matmult(vec1, vec2)
            """                               Call OO iterfcn                                """
            if hasattr(p, '_df'): delattr(p, '_df')
            if best_ls_point.isFeas(False) and hasattr(best_ls_point, '_df'):
                p._df = best_ls_point.df().copy()

            p.iterfcn(best_ls_point)
            """                             Check stop criteria                           """

            cond_same_point = array_equal(best_ls_point.x,
                                          prevIter_best_ls_point.x)
            if cond_same_point and not p.istop:
                p.istop = 14
                p.msg = 'X[k-1] and X[k] are same'
                p.stopdict[SMALL_DELTA_X] = True
                restoreProb()
                self.innerState = {'B': b, 'hs': hs}
                return

            s2 = 0
            if p.istop and not p.userStop:
                if p.istop not in p.stopdict:
                    p.stopdict[
                        p.
                        istop] = True  # it's actual for converters, TODO: fix it
                if SMALL_DF in p.stopdict:
                    if best_ls_point.isFeas(False): s2 = p.istop
                    p.stopdict.pop(SMALL_DF)
                if SMALL_DELTA_F in p.stopdict:
                    # TODO: implement it more properly
                    if best_ls_point.isFeas(
                            False
                    ) and prevIter_best_ls_point.f() != best_ls_point.f():
                        s2 = p.istop
                    p.stopdict.pop(SMALL_DELTA_F)
                if SMALL_DELTA_X in p.stopdict:
                    if best_ls_point.isFeas(
                            False) or not prevIter_best_ls_point.isFeas(
                                False) or cond_same_point:
                        s2 = p.istop
                    p.stopdict.pop(SMALL_DELTA_X)
#                if s2 and (any(isnan(best_ls_point.c())) or any(isnan(best_ls_point.h()))) \
#                and not p.isNaNInConstraintsAllowed\
#                and not cond_same_point:
#                    s2 = 0

                if not s2 and any(p.stopdict.values()):
                    for key, val in p.stopdict.items():
                        if val == True:
                            s2 = key
                            break
                p.istop = s2

                for key, val in p.stopdict.items():
                    if key < 0 or key in set([
                            FVAL_IS_ENOUGH, USER_DEMAND_STOP,
                            BUTTON_ENOUGH_HAS_BEEN_PRESSED
                    ]):
                        p.iterfcn(bestPoint)
                        self.innerState = {'B': b, 'hs': hs}
                        return
            """                                If stop required                                """

            if p.istop:
                #                if self.needRej(p, b, g1, g) or not feasiblePointWasEncountered:
                #                    b = B0.copy()
                #                    hs = max((p.norm(prevIter_best_ls_point.x - best_ls_point.x) , 128*p.xtol))
                #                    p.istop = 0
                #                else:
                restoreProb()
                p.iterfcn(bestPoint)
                #p.istop, p.msg = istop, msg
                self.innerState = {'B': b, 'hs': hs}
                return
            """                Some final things for ralg main cycle                """
            #            p.debugmsg('new point Aeq residual:'+str(norm(dot(Aeq, iterPoint.x)-beq)))
            #            if needProjection and itn!=0:
            #                #pass
            #                x2 = self.linEqProjection(iterPoint.x, Aeq, beq)
            #                p.debugmsg('norm(delta):' + str(norm(iterPoint.x-x2)))
            #                iterPoint = p.point(x2)
            #                p.debugmsg('2: new point Aeq residual:'+str(norm(dot(Aeq, iterPoint.x)-beq)))
            #p.hs.append(hs)
            #g = moveDirection.copy()

            #prevDirectionForDilation = directionForDilation

            #iterPoint = None
            #doScale = self.new_s and prevIter_PointForDilation.isFeas(True) !=  best_ls_point.isFeas(True)
            #print doScale
            prevIter_best_ls_point = best_ls_point
            prevIter_PointForDilation = best_ls_point
            prevDirectionForDilation = best_ls_point._getDirection(
                self.approach)
            moveDirection = best_ls_point._getDirection(self.approach)
Ejemplo n.º 30
0
def truncateByPlane(y, e, indT, A, b):
    #!!!!!!!!!!!!!!!!!!!
    # TODO: vectorize it by matrix A
    #!!!!!!!!!!!!!!!!!!!
    ind_trunc = True
    assert np.asarray(b).size <= 1, 'unimplemented yet'
    m, n = y.shape
    if m == 0:
        assert e.size == 0, 'bug in interalg engine'
        return y, e, indT, ind_trunc

    ind_positive = where(A > 0)[0]
    ind_negative = where(A < 0)[0]
    
    A1 = A[ind_positive] 
    S1 = A1 * y[:, ind_positive]
    A2 = A[ind_negative]
    S2 = A2 * e[:, ind_negative]
    s1, s2 = np.sum(S1, 1), np.sum(S2, 1)
    S = s1 + s2
    
    if ind_positive.size != 0:
        S1_ = b - S.reshape(-1, 1) + S1
        Alt_ub = S1_ / A1
#        ind = e[:, ind_positive] > Alt_ub
#        #e[:, ind_positive[ind]] = Alt_ub[ind]
#        e[:, np.tile(ind_positive,(ind.shape[0],1))[ind]] = Alt_ub[ind]
#        #e[:, ind_positive.reshape(ind.shape)[ind]] = Alt_ub[ind]
#        
#        indT[np.any(ind, 1)] = True
        
        for _i, i in enumerate(ind_positive):
            #s = S - S1[:, _i]
            #alt_ub = (b - s) / A[i]
            #alt_ub = S1_[:, _i] / A[i]
            alt_ub = Alt_ub[:, _i]
            ind = e[:, i] > alt_ub
            Ind = where(ind)[0]
            if Ind.size:
                e[Ind, i] = alt_ub[Ind]
                indT[Ind] = True
    
    if ind_negative.size != 0:
        S2_ = b - S.reshape(-1, 1) + S2
        Alt_lb = S2_ / A2
        for _i, i in enumerate(ind_negative):
            #s = S - S2[:, _i]
            #alt_lb = (b - s) / A[i]
            #alt_lb = S2_[:, _i] / A[i]
            alt_lb = Alt_lb[:, _i]
            ind = y[:, i] < alt_lb
            Ind = where(ind)[0]
            if Ind.size:
                y[Ind, i] = alt_lb[Ind]
                indT[Ind] = True

    ind = np.all(e>=y, 1)
    if not np.all(ind):
        ind_trunc = where(ind)[0]
        lj = ind_trunc.size
        y = take(y, ind_trunc, axis=0, out=y[:lj])
        e = take(e, ind_trunc, axis=0, out=e[:lj])
        indT = indT[ind_trunc]
            
    return y, e, indT, ind_trunc
Ejemplo n.º 31
0
def truncateByPlane2(cs, centerValues, y, e, indT, gradient, fo, p):

    ind_trunc = True
    assert np.asarray(fo).size <= 1, 'unimplemented yet'
    m, n = y.shape
    if m == 0:
        assert e.size == 0, 'bug in interalg engine'
        return y, e, indT, ind_trunc

    oovarsIndDict = p._oovarsIndDict
    ind = np.array([oovarsIndDict[oov][0] for oov in gradient.keys()])
    y2, e2 = y[:, ind], e[:, ind]

    A = np.vstack(
        [np.asarray(elem).reshape(1, -1) for elem in gradient.values()]).T
    cs = 0.5 * (y2 + e2)
    b = np.sum(A * cs, 1) - centerValues.view(np.ndarray) + fo

    #    ind_positive = where(A > 0)
    #    ind_negative = where(A < 0)

    A_positive = where(A > 0, A, 0)
    A_negative = where(A < 0, A, 0)
    #S1 = A[ind_positive] * y2[ind_positive]
    #S2 = A[ind_negative] * e2[ind_negative]
    S1 = A_positive * y2
    S2 = A_negative * e2
    s1, s2 = np.sum(S1, 1), np.sum(S2, 1)
    S = s1 + s2

    alt_fo1 = where(A_positive != 0,
                    (b.reshape(-1, 1) - S.reshape(-1, 1) + S1) / A_positive,
                    np.inf)
    ind1 = logical_and(e2 > alt_fo1, A_positive != 0)
    e2[ind1] = alt_fo1[ind1]

    alt_fo2 = where(A_negative != 0,
                    (b.reshape(-1, 1) - S.reshape(-1, 1) + S2) / A_negative,
                    -np.inf)
    ind2 = logical_and(y2 < alt_fo2, A_negative != 0)
    y2[ind2] = alt_fo2[ind2]

    # TODO: check it
    y[:, ind], e[:, ind] = y2, e2

    # TODO: check indT
    indT[np.any(ind1, 1)] = True
    indT[np.any(ind2, 1)] = True

    #    for _i, i in enumerate(ind_positive):
    #        s = S - S1[:, _i]
    #        alt_ub = (b - s) / A[i]
    #        ind = e[:, i] > alt_ub
    #        e[ind, i] = alt_ub[ind]
    #        indT[ind] = True
    #
    #    for _i, i in enumerate(ind_negative):
    #        s = S - S2[:, _i]
    #        alt_lb = (b - s) / A[i]
    #        ind = y[:, i] < alt_lb
    #        y[ind, i] = alt_lb[ind]
    #        indT[ind] = True

    ind = np.all(e >= y, 1)
    if not np.all(ind):
        ind_trunc = where(ind)[0]
        lj = ind_trunc.size
        y = take(y, ind_trunc, axis=0, out=y[:lj])
        e = take(e, ind_trunc, axis=0, out=e[:lj])
        indT = indT[ind_trunc]

    return y, e, indT, ind_trunc
Ejemplo n.º 32
0
from numpy import empty, logical_and, logical_not, take, zeros, isfinite, any, \
asarray, ndarray, bool_#where
from interalgT import adjustDiscreteVarBounds, truncateByPlane
import numpy as np

# for PyPy
from openopt.kernel.nonOptMisc import where

hasPoint = lambda y, e, point:\
    True if y.size != 0 and any([(np.all(y[i]<=point) and np.all(e[i]>=point)) for i in range(y.shape[0])]) else False
pointInd = lambda y, e, point:\
    where([(np.all(y[i]<=point) and np.all(e[i]>=point)) for i in range(y.shape[0])])[0].tolist()
    
def processConstraints(C0, y, e, _s, p, dataType):
    #P = np.array([  7.64673334e-01,    4.35551807e-01,    5.93869991e+02,   5.00000000e+00])
#    P = np.array([-0.63521194458007812, -0.3106536865234375, 0.0905609130859375, 0.001522064208984375, -0.69999999999999996, -0.99993896484375, 0.90000152587890625, 1.0, 4.0])

#    print('c-1', p.iter, hasPoint(y, e, P), pointInd(y, e, P))
    n = p.n
    m = y.shape[0]
    indT = empty(m, bool)
    indT.fill(False)
#    isSNLE = p.probType in ('NLSP', 'SNLE')
    
    for i in range(p.nb):
        y, e, indT, ind_trunc = truncateByPlane(y, e, indT, p.A[i], p.b[i]+p.contol)
        if ind_trunc is not True:
            _s = _s[ind_trunc]
    for i in range(p.nbeq):
        # TODO: handle it via one func
        y, e, indT, ind_trunc = truncateByPlane(y, e, indT, p.Aeq[i], p.beq[i]+p.contol)
Ejemplo n.º 33
0
def r44(Solutions, r5Coords, r5F, targets, sigma):
#    print Solutions.F
#    if len(Solutions.F) != Solutions.coords.shape[0]:
#        raise 0
    # TODO: rework it
    #sf = asarray(Solutions.F)
    nIncome, nOutcome = 0, 0
    m= len(r5Coords)
    #n = len(r5Coords[0])
    # TODO: mb use inplace r5Coords / r5F modification instead?
    for j in range(m):
        if isnan(r5F[0][0]):
            continue
        if Solutions.coords.size == 0:
            Solutions.coords = array(r5Coords[j]).reshape(1, -1)
            Solutions.F.append(r5F[0])
            nIncome += 1
            continue
        M = Solutions.coords.shape[0] 
        
        r47 = empty(M, bool)
        r47.fill(False)
#        r48 = empty(M, bool)
#        r48.fill(False)
        for i, target in enumerate(targets):
            
            f = r5F[j][i]
            
            # TODO: rewrite it
            F = asarray([Solutions.F[k][i] for k in range(M)])
            #d = f - F # vector-matrix
            
            val, tol = target.val, target.tol
            Tol = sigma * tol
            if val == inf:
                r52 = f > F + Tol
#                r36olution_better = f <= F#-tol
            elif val == -inf:
                r52 = f < F - Tol
#                r36olution_better = f >= F#tol
            else:
                r52 = abs(f - val) < abs(F - val) - Tol
#                r36olution_better = abs(f - val) >= abs(Solutions.F[i] - val)#-tol # abs(Solutions.F[i] - target)  < abs(f[i] - target) + tol
            
            r47 = logical_or(r47, r52)
#            r48 = logical_or(r48, r36olution_better)
        
        accept_c = all(r47)
        #print sum(asarray(Solutions.F))/asarray(Solutions.F).size
        if accept_c:
            nIncome += 1
            #new
            r48 = empty(M, bool)
            r48.fill(False)
            for i, target in enumerate(targets):
                f = r5F[j][i]
                F = asarray([Solutions.F[k][i] for k in range(M)])
                val, tol = target.val, target.tol
                if val == inf:
                    r36olution_better = f < F
                elif val == -inf:
                    r36olution_better = f > F
                else:
                    r36olution_better = abs(f - val) > abs(F - val)
                r48 = logical_or(r48, r36olution_better)

            r49 = logical_not(r48)
            remove_s = any(r49)
            if remove_s:
                r50 = where(r49)[0]
                nOutcome += r50.size
                Solutions.coords[r50[0]] = r5Coords[j]
                Solutions.F[r50[0]] = r5F[j]
                
                if r50.size > 1:
                    r49[r50[0]] = False
                    indLeft = logical_not(r49)
                    indLeftPositions = where(indLeft)[0]
                    newSolNumber = Solutions.coords.shape[0] - r50.size + 1
                    Solutions.coords = take(Solutions.coords, indLeftPositions, axis=0, out = Solutions.coords[:newSolNumber])
                    Solutions.F = [Solutions.F[i] for i in indLeftPositions]
            else:
                Solutions.coords = vstack((Solutions.coords, r5Coords[j]))
                Solutions.F.append(r5F[j])
    return nIncome, nOutcome
Ejemplo n.º 34
0
def processConstraints(C0, y, e, _s, p, dataType):
    #P = np.array([  7.64673334e-01,    4.35551807e-01,    5.93869991e+02,   5.00000000e+00])
#    P = np.array([-0.63521194458007812, -0.3106536865234375, 0.0905609130859375, 0.001522064208984375, -0.69999999999999996, -0.99993896484375, 0.90000152587890625, 1.0, 4.0])

#    print('c-1', p.iter, hasPoint(y, e, P), pointInd(y, e, P))
    n = p.n
    m = y.shape[0]
    indT = empty(m, bool)
    indT.fill(False)
#    isSNLE = p.probType in ('NLSP', 'SNLE')
    
    for i in range(p.nb):
        y, e, indT, ind_trunc = truncateByPlane(y, e, indT, p.A[i], p.b[i]+p.contol)
        if ind_trunc is not True:
            _s = _s[ind_trunc]
    for i in range(p.nbeq):
        # TODO: handle it via one func
        y, e, indT, ind_trunc = truncateByPlane(y, e, indT, p.Aeq[i], p.beq[i]+p.contol)
        if ind_trunc is not True:
            _s = _s[ind_trunc]
        y, e, indT, ind_trunc = truncateByPlane(y, e, indT, -p.Aeq[i], -p.beq[i]+p.contol)
        if ind_trunc is not True:
            _s = _s[ind_trunc]
   
    
    DefiniteRange = True
    if len(p._discreteVarsNumList):
        y, e, _s, indT = adjustDiscreteVarBounds(y, e, _s, indT, p)

    m = y.shape[0]
    nlh = zeros((m, 2*n))
    nlh_0 = zeros(m)
    fullOutput = False#isSNLE and not p.hasLogicalConstraints
    residual = zeros((m, 2*n)) if fullOutput else None
    residual_0 = zeros(m) if fullOutput else None
    
    for itn, (c, f, lb, ub, tol) in enumerate(C0):
#        print ('c_1', itn, c.dep, hasPoint(y, e, P))
        m = y.shape[0] # is changed in the cycle
        if m == 0: 
            return y.reshape(0, n), e.reshape(0, n), nlh.reshape(0, 2*n), residual, True, False, _s
            #return y.reshape(0, n), e.reshape(0, n), nlh.reshape(0, 2*n), residual.reshape(0, 2*n), True, False, _s
        assert nlh.shape[0] == y.shape[0]
        
        if fullOutput:
            (T0, Res0), (res, R_res), DefiniteRange2 = c.nlh(y, e, p, dataType, fullOutput = True)
            residual_0 += Res0
        else:
            # may be logical constraint and doesn't have kw fullOutput at all
            T0, res, DefiniteRange2 = c.nlh(y, e, p, dataType)
        DefiniteRange = logical_and(DefiniteRange, DefiniteRange2)
        
        assert T0.ndim <= 1, 'unimplemented yet'

        nlh_0 += T0
        assert nlh.shape[0] == m
        # TODO: rework it for case len(p._freeVarsList) >> 1

        for v, tmp in res.items():
            j = p._freeVarsDict.get(v)
            nlh[:, n+j] += tmp[:, tmp.shape[1]/2:].flatten() - T0
            nlh[:, j] += tmp[:, :tmp.shape[1]/2].flatten() - T0
            if fullOutput:
                Tmp = R_res[v]
                residual[:, n+j] += Tmp[:, Tmp.shape[1]/2:].flatten() - Res0
                residual[:, j] += Tmp[:, :Tmp.shape[1]/2].flatten() - Res0
                    
        assert nlh.shape[0] == m
        ind = where(logical_and(any(isfinite(nlh), 1), isfinite(nlh_0)))[0]
        lj = ind.size
        if lj != m:
            y = take(y, ind, axis=0, out=y[:lj])
            e = take(e, ind, axis=0, out=e[:lj])
            nlh = take(nlh, ind, axis=0, out=nlh[:lj])
            nlh_0 = nlh_0[ind]
#            residual = take(residual, ind, axis=0, out=residual[:lj])
            indT = indT[ind]
            _s = _s[ind]
            if fullOutput:
                residual_0 = residual_0[ind]
                residual = take(residual, ind, axis=0, out=residual[:lj])
            if asarray(DefiniteRange).size != 1: 
                DefiniteRange = take(DefiniteRange, ind, axis=0, out=DefiniteRange[:lj])
#            print ('c_2', itn, c.dep, hasPoint(y, e, P))
        assert nlh.shape[0] == y.shape[0]


        ind = logical_not(isfinite(nlh))
        if any(ind):
            indT[any(ind, 1)] = True
            
            ind_l,  ind_u = ind[:, :ind.shape[1]/2], ind[:, ind.shape[1]/2:]
            tmp_l, tmp_u = 0.5 * (y[ind_l] + e[ind_l]), 0.5 * (y[ind_u] + e[ind_u])
            y[ind_l], e[ind_u] = tmp_l, tmp_u
            # TODO: mb implement it
            if len(p._discreteVarsNumList):
                if tmp_l.ndim > 1:
                    # shouldn't reduce y,e shape here, so output values doesn't matter
                    l1 = len(_s)
                    tmp_l, tmp_u, _s2, indT2 = adjustDiscreteVarBounds(tmp_l, tmp_u, _s, indT, p)
                    l2 = len(_s2)
                    if l1 != l2:
                        print('Warning: possible bug in interalg constraints processing, inform developers')
                else:
                    y, e, _s, indT = adjustDiscreteVarBounds(y, e, _s, indT, p)

            nlh_l, nlh_u = nlh[:, nlh.shape[1]/2:], nlh[:, :nlh.shape[1]/2]
            
            # copy() is used because += and -= operators are involved on nlh in this cycle and probably some other computations
            nlh_l[ind_u], nlh_u[ind_l] = nlh_u[ind_u].copy(), nlh_l[ind_l].copy()   
            if fullOutput:
                residual_l, residual_u = residual[:, residual.shape[1]/2:], residual[:, :residual.shape[1]/2]
                residual_l[ind_u], residual_u[ind_l] = residual_u[ind_u].copy(), residual_l[ind_l].copy()   
#            print ('c_3', itn, c.dep, hasPoint(y, e, P))

    if nlh.size != 0:
        if DefiniteRange is False:
            nlh_0 += 1e-300
        elif type(DefiniteRange) == ndarray and not all(DefiniteRange):
            nlh_0[logical_not(DefiniteRange)] += 1e-300
        else:
            assert type(DefiniteRange) in (bool, bool_, ndarray)
    # !! matrix - vector
    nlh += nlh_0.reshape(-1, 1)
    
    if fullOutput:
        # !! matrix - vector
        residual += residual_0.reshape(-1, 1)
        residual[residual_0>=1e300] = 1e300
    
    #print('c2', p.iter, hasPoint(y, e, P), pointInd(y, e, P))
    return y, e, nlh, residual, DefiniteRange, indT, _s
Ejemplo n.º 35
0
def truncateByPlane2(cs, centerValues, y, e, indT, gradient, fo, p):
    
    ind_trunc = True
    assert np.asarray(fo).size <= 1, 'unimplemented yet'
    m, n = y.shape
    if m == 0:
        assert e.size == 0, 'bug in interalg engine'
        return y, e, indT, ind_trunc

    oovarsIndDict = p._oovarsIndDict
    ind = np.array([oovarsIndDict[oov][0] for oov in gradient.keys()])
    y2, e2 = y[:, ind], e[:, ind]
    
    A = np.vstack([np.asarray(elem).reshape(1, -1) for elem in gradient.values()]).T
    cs = 0.5 * (y2 + e2)
    b = np.sum(A * cs, 1) - centerValues.view(np.ndarray) + fo

#    ind_positive = where(A > 0)
#    ind_negative = where(A < 0)
    
    A_positive = where(A>0, A, 0)
    A_negative = where(A<0, A, 0)
    #S1 = A[ind_positive] * y2[ind_positive]
    #S2 = A[ind_negative] * e2[ind_negative]
    S1 = A_positive * y2
    S2 = A_negative * e2
    s1, s2 = np.sum(S1, 1), np.sum(S2, 1)
    S = s1 + s2
    
    alt_fo1 = where(A_positive != 0, (b.reshape(-1, 1) - S.reshape(-1, 1) + S1) / A_positive, np.inf)
    ind1 = logical_and(e2 > alt_fo1, A_positive != 0)
    e2[ind1] = alt_fo1[ind1]
    
    alt_fo2 = where(A_negative != 0, (b.reshape(-1, 1) - S.reshape(-1, 1) + S2) / A_negative, -np.inf)
    ind2 = logical_and(y2 < alt_fo2, A_negative != 0)
    y2[ind2] = alt_fo2[ind2]
    
    
    # TODO: check it
    y[:, ind], e[:, ind] = y2, e2
    
    # TODO: check indT
    indT[np.any(ind1, 1)] = True
    indT[np.any(ind2, 1)] = True
    
#    for _i, i in enumerate(ind_positive):
#        s = S - S1[:, _i]
#        alt_ub = (b - s) / A[i]
#        ind = e[:, i] > alt_ub
#        e[ind, i] = alt_ub[ind]
#        indT[ind] = True
#    
#    for _i, i in enumerate(ind_negative):
#        s = S - S2[:, _i]
#        alt_lb = (b - s) / A[i]
#        ind = y[:, i] < alt_lb
#        y[ind, i] = alt_lb[ind]
#        indT[ind] = True

    ind = np.all(e>=y, 1)
    if not np.all(ind):
        ind_trunc = where(ind)[0]
        lj = ind_trunc.size
        y = take(y, ind_trunc, axis=0, out=y[:lj])
        e = take(e, ind_trunc, axis=0, out=e[:lj])
        indT = indT[ind_trunc]
    
    return y, e, indT, ind_trunc