Example #1
0
    def _pm_expand(self, constr):

        # Get objects
        v = constr.left
        v = vstack([v[i, 0] for i in range(0, v.shape[0])])
        n = v.shape[0] - 1
        x = vstack([v[0:n, 0]])
        y = v[n, 0]
        z = variable()

        # One element
        if n == 1:
            return cvxpy_list([greater_equals(x, 0), greater_equals(x, y)])

        # Get power of 2 size
        m = 0
        while np.log2(n + m) % 1 != 0:
            m += 1

        # Copy elements of x on a list and restrict them
        constr_list = []
        el_list = []
        for i in range(0, n, 1):
            el_list += [x[i, 0]]
            if (not np.isscalar(x[i, 0]) and type(x[i, 0]) is not cvxpy_obj):
                constr_list += [greater_equals(x[i, 0], 0)]

        # Construct expansion
        for i in range(0, m, 1):
            el_list += [z]
        while len(el_list) > 2:
            new_list = []
            for i in range(0, int(len(el_list) / 2)):
                x1 = el_list[2 * i]
                x2 = el_list[2 * i + 1]
                w = variable()
                constr_list += [
                    belongs(vstack((hstack((x1, w)), hstack((w, x2)))),
                            semidefinite_cone)
                ]
                new_list += [w]
            el_list = new_list
        x1 = el_list[0]
        x2 = el_list[1]
        constr_list += [
            belongs(vstack((hstack((x1, z)), hstack((z, x2)))),
                    semidefinite_cone)
        ]
        constr_list += [greater_equals(z, 0), greater_equals(z, y)]
        return cvxpy_list(constr_list)
    def _pm_expand(self,constr):
        
        # Get objects
        v = constr.left
        v = vstack([v[i,0] for i in range(0,v.shape[0])])
        n = v.shape[0]-1
        x = vstack([v[0:n,0]])
        y = v[n,0]
        z = variable()

        # One element
        if n==1:
            return cvxpy_list([greater_equals(x,0),
                               greater_equals(x,y)])

        # Get power of 2 size
        m = 0
        while np.log2(n+m) % 1 != 0:
            m += 1

        # Copy elements of x on a list and restrict them
        constr_list = []
        el_list = []
        for i in range(0,n,1):
            el_list+=[x[i,0]]
            if (not np.isscalar(x[i,0]) and
                type(x[i,0]) is not cvxpy_obj):
                constr_list += [greater_equals(x[i,0],0)]

        # Construct expansion
        for i in range(0,m,1):
            el_list += [z]
        while len(el_list) > 2:
            new_list = []
            for i in range(0,int(len(el_list)/2)):
                x1 = el_list[2*i]
                x2 = el_list[2*i+1]
                w = variable()
                constr_list += [belongs(vstack((hstack((x1,w)),
                                                hstack((w,x2)))),
                                        semidefinite_cone)]
                new_list += [w]
            el_list = new_list
        x1 = el_list[0]
        x2 = el_list[1]
        constr_list += [belongs(vstack((hstack((x1,z)),
                                        hstack((z,x2)))),
                                semidefinite_cone)]
        constr_list += [greater_equals(z,0),greater_equals(z,y)]
        return cvxpy_list(constr_list)
Example #3
0
    def _pm_expand(self,constr):
        
        # Get shape
        v = constr.left
        n = v.shape[0]-1
        x = v[0:n,0]
        y = v[n,0]
        z = var()

        # Get power of 2 size
        m = 0
        while (np.log2(n+m) % 1 != 0):
            m = m + 1

        # Copy elements of x on a list and restrict them
        constr_list = []
        el_list = []
        for i in range(0,n,1):
            el_list+=[x[i,0]]
            if(not np.isscalar(x[i,0]) and
               type(x[i,0]) is not cvxpy_obj):
                constr_list += [greater(x[i,0],0)]

        # Construct expansion
        z = var()
        for i in range(0,m,1):
            el_list += [z]
        while(len(el_list) > 2):
            new_list = []
            for i in range(0,len(el_list)/2):
                x1 = el_list[2*i]
                x2 = el_list[2*i+1]
                w = var()
                constr_list += [belongs(vstack((hstack((x1,w)),
                                                hstack((w,x2)))),
                                        sdc(2))]
                new_list += [w]
            el_list = new_list
        x1 = el_list[0]
        x2 = el_list[1]
        constr_list += [belongs(vstack((hstack((x1,z)),
                                        hstack((z,x2)))),
                                sdc(2))]
        constr_list += [greater(z,0),greater(z,y)]
        return cvxpy_list(constr_list)
Example #4
0
    def _pm_expand(self, constr):

        # Get shape
        v = constr.left
        n = v.shape[0] - 1
        x = v[0:n, 0]
        y = v[n, 0]
        z = var()

        # Get power of 2 size
        m = 0
        while np.log2(n + m) % 1 != 0:
            m = m + 1

        # Copy elements of x on a list and restrict them
        constr_list = []
        el_list = []
        for i in range(0, n, 1):
            el_list += [x[i, 0]]
            if not np.isscalar(x[i, 0]) and type(x[i, 0]) is not cvxpy_obj:
                constr_list += [greater(x[i, 0], 0)]

        # Construct expansion
        z = var()
        for i in range(0, m, 1):
            el_list += [z]
        while len(el_list) > 2:
            new_list = []
            for i in range(0, len(el_list) / 2):
                x1 = el_list[2 * i]
                x2 = el_list[2 * i + 1]
                w = var()
                constr_list += [belongs(vstack((hstack((x1, w)), hstack((w, x2)))), sdc(2))]
                new_list += [w]
            el_list = new_list
        x1 = el_list[0]
        x2 = el_list[1]
        constr_list += [belongs(vstack((hstack((x1, z)), hstack((z, x2)))), sdc(2))]
        constr_list += [greater(z, 0), greater(z, y)]
        return cvxpy_list(constr_list)
Example #5
0
    def _linearize(self, args, t2):
        """
        Description
        -----------
        Form linear underestimator or linear
        overestimator of the function depeniding
        on whether the program represents a convex
        or concave function.

        Arguments
        ---------
        args: list of arguments
        t2: variable of right hand side
        """

        x = vstack(args)
        x0 = x.get_value()
        f_x0 = self._solve_isolating(x0)
        lagrange = self.lagrange_mul_eq[0:len(args), 0]

        if (self.curvature == CONVEX):
            return t2 - (f_x0 - lagrange.T * (x - x0))
        else:
            return (f_x0 + lagrange.T * (x - x0)) - t2
Example #6
0
    def _linearize(self,args,t2):
        """
        Description
        -----------
        Form linear underestimator or linear
        overestimator of the function depeniding
        on whether the program represents a convex
        or concave function.

        Arguments
        ---------
        args: list of arguments
        t2: variable of right hand side
        """

        x = vstack(args)
        x0 = x.get_value()
        f_x0 = self._solve_isolating(x0)
        lagrange = self.lagrange_mul_eq[0:len(args),0]
        
        if(self.curvature == CONVEX):
            return t2 - (f_x0 - lagrange.T*(x-x0))
        else:
            return (f_x0 + lagrange.T*(x-x0)) - t2
Example #7
0
    def _construct(self, el, mp, p):
        n = el.shape[0] - 1
        u = vstack([el[0:n, 0]])
        v = el[n, 0]

        # Prepare u
        for i in range(0, n, 1):
            if type(u[i, 0]) is cvxpy_obj:
                u[i, 0] = u[i, 0].value

        # Prepare v
        if type(v) is cvxpy_obj:
            v = v.value

        # f
        def f(x):
            vals = []
            for i in range(0, n, 1):
                if type(u[i, 0]) is cvxpy_scalar_var:
                    vals += [x[mp[u[i, 0]]]]
                else:
                    vals += [u[i, 0]]
            if type(v) is cvxpy_scalar_var:
                y = x[mp[v]]
            else:
                y = v
            return y - reduce(lambda w, z: w * z, vals, 1.)**(1. / (n * 1.))

        # grad f
        def grad_f(x):
            g = opt.spmatrix(0.0, [], [], (p, 1))
            if type(v) is cvxpy_scalar_var:
                fval = -f(x) + x[mp[v]]
            else:
                fval = -f(x) + v
            for i in range(0, n, 1):
                if type(u[i, 0]) is cvxpy_scalar_var:
                    xi = x[mp[u[i, 0]]]
                    g[mp[u[i, 0]]] = -fval / (n * xi * 1.)
            if type(v) is cvxpy_scalar_var:
                g[mp[v]] = 1.
            return g

        # hess f
        def hess_f(x):
            h = opt.spmatrix(0.0, [], [], (p, p))
            if type(v) is cvxpy_scalar_var:
                fval = -f(x) + x[mp[v]]
            else:
                fval = -f(x) + v
            for i in range(0, n, 1):
                for j in range(0, n, 1):
                    if (type(u[i, 0]) is cvxpy_scalar_var
                            and type(u[j, 0]) is cvxpy_scalar_var):
                        xi = x[mp[u[i, 0]]]
                        xj = x[mp[u[j, 0]]]
                        if i == j:
                            w = (n - 1.) * fval / (n * n * 1. * xi * xi)
                            h[mp[u[i, 0]], mp[u[i, 0]]] = w
                        else:
                            w = -fval / (1. * n * n * xi * xj)
                            h[mp[u[i, 0]], mp[u[j, 0]]] = w
                            h[mp[u[j, 0]], mp[u[i, 0]]] = w
            return h

        # ind f
        def ind_f(x):
            for i in range(0, n, 1):
                if type(u[i, 0]) is cvxpy_scalar_var:
                    if x[mp[u[i, 0]]] <= 0:
                        return False
                else:
                    if u[i, 0] <= 0:
                        return False
            return True

        # Return functions
        return f, grad_f, hess_f, ind_f
Example #8
0
    def _construct(self, el, mp, p):

        n = int((el.shape[0] - 1.) / 2.)
        u = vstack([el[0:n, 0]])
        v = vstack([el[n:2 * n, 0]])
        t = el[2 * n, 0]

        # Prepare u and v
        for i in range(0, n):
            if type(u[i, 0]) is cvxpy_obj:
                u[i, 0] = u[i, 0].value
            if type(v[i, 0]) is cvxpy_obj:
                v[i, 0] = v[i, 0].value

        # Prepare t
        if type(t) is cvxpy_obj:
            t = t.value

        # f
        def f(x):
            temp = 0
            for i in range(0, n):
                if type(u[i, 0]) is cvxpy_scalar_var:
                    u_i = x[mp[u[i, 0]]]
                else:
                    u_i = u[i, 0]
                if type(v[i, 0]) is cvxpy_scalar_var:
                    v_i = x[mp[v[i, 0]]]
                else:
                    v_i = v[i, 0]
                temp += u_i * np.log(u_i * 1. / v_i) - u_i + v_i
            if type(t) is cvxpy_scalar_var:
                return temp - x[mp[t]]
            else:
                return temp - t

        # grad f
        def grad_f(x):
            g = opt.spmatrix(0.0, [], [], (p, 1))
            for i in range(0, n):
                if type(u[i, 0]) is cvxpy_scalar_var:
                    u_i = x[mp[u[i, 0]]]
                else:
                    u_i = u[i, 0]
                if type(v[i, 0]) is cvxpy_scalar_var:
                    v_i = x[mp[v[i, 0]]]
                else:
                    v_i = v[i, 0]
                if type(u[i, 0]) is cvxpy_scalar_var:
                    g[mp[u[i, 0]]] = np.log(u_i * 1. / v_i)
                if type(v[i, 0]) is cvxpy_scalar_var:
                    g[mp[v[i, 0]]] = (-u_i * 1. / v_i) + 1.
            if type(t) is cvxpy_scalar_var:
                g[mp[t]] = -1.
            return g

        # hess f
        def hess_f(x):
            h = opt.spmatrix(0.0, [], [], (p, p))
            for i in range(0, n):
                if type(u[i, 0]) is cvxpy_scalar_var:
                    u_i = x[mp[u[i, 0]]]
                else:
                    u_i = u[i, 0]
                if type(v[i, 0]) is cvxpy_scalar_var:
                    v_i = x[mp[v[i, 0]]]
                else:
                    v_i = v[i, 0]
                if type(u[i, 0]) is cvxpy_scalar_var:
                    h[mp[u[i, 0]], mp[u[i, 0]]] = 1. / u_i
                if type(v[i, 0]) is cvxpy_scalar_var:
                    h[mp[v[i, 0]], mp[v[i, 0]]] = u_i * 1. / (v_i**2.)
                if (type(u[i, 0]) is cvxpy_scalar_var
                        and type(v[i, 0]) is cvxpy_scalar_var):
                    w = -1. / v_i
                    h[mp[u[i, 0]], mp[v[i, 0]]] = w
                    h[mp[v[i, 0]], mp[u[i, 0]]] = w
            return h

        # ind f
        def ind_f(x):
            for i in range(0, n):
                if type(u[i, 0]) is cvxpy_scalar_var:
                    if x[mp[u[i, 0]]] <= 0:
                        return False
                else:
                    if u[i, 0] <= 0:
                        return False
                if type(v[i, 0]) is cvxpy_scalar_var:
                    if x[mp[v[i, 0]]] <= 0:
                        return False
                else:
                    if v[i, 0] <= 0:
                        return False
            return True

        # Return functions
        return f, grad_f, hess_f, ind_f
Example #9
0
    def _construct(self,el,mp,p):
        
        n = int((el.shape[0]-1.)/2.)
        u = vstack([el[0:n,0]])
        v = vstack([el[n:2*n,0]])
        t = el[2*n,0]

        # Prepare u and v
        for i in range(0,n):
            if type(u[i,0]) is cvxpy_obj:
                u[i,0] = u[i,0].value
            if type(v[i,0]) is cvxpy_obj:
                v[i,0] = v[i,0].value

        # Prepare t
        if type(t) is cvxpy_obj:
            t = t.value

        # f
        def f(x):
            temp = 0
            for i in range(0,n):
                if type(u[i,0]) is cvxpy_scalar_var:
                    u_i = x[mp[u[i,0]]]
                else:
                    u_i = u[i,0]
                if type(v[i,0]) is cvxpy_scalar_var:
                    v_i = x[mp[v[i,0]]]
                else:
                    v_i = v[i,0]
                temp += u_i*np.log(u_i*1./v_i)-u_i+v_i
            if type(t) is cvxpy_scalar_var:
                return temp - x[mp[t]]
            else:
                return temp - t

        # grad f
        def grad_f(x):
            g = opt.spmatrix(0.0,[],[],(p,1))
            for i in range(0,n):
                if type(u[i,0]) is cvxpy_scalar_var:
                    u_i = x[mp[u[i,0]]]
                else:
                    u_i = u[i,0]
                if type(v[i,0]) is cvxpy_scalar_var:
                    v_i = x[mp[v[i,0]]]
                else:
                    v_i = v[i,0]
                if type(u[i,0]) is cvxpy_scalar_var:
                    g[mp[u[i,0]]] = np.log(u_i*1./v_i)
                if type(v[i,0]) is cvxpy_scalar_var:
                    g[mp[v[i,0]]] = (-u_i*1./v_i)+1.
            if type(t) is cvxpy_scalar_var:
                g[mp[t]] = -1.
            return g

        # hess f
        def hess_f(x):
            h = opt.spmatrix(0.0,[],[],(p,p))
            for i in range(0,n):
                if type(u[i,0]) is cvxpy_scalar_var:
                    u_i = x[mp[u[i,0]]]
                else:
                    u_i = u[i,0]
                if type(v[i,0]) is cvxpy_scalar_var:
                    v_i = x[mp[v[i,0]]]
                else:
                    v_i = v[i,0]
                if type(u[i,0]) is cvxpy_scalar_var:
                    h[mp[u[i,0]],mp[u[i,0]]] = 1./u_i
                if type(v[i,0]) is cvxpy_scalar_var:
                    h[mp[v[i,0]],mp[v[i,0]]] = u_i*1./(v_i**2.)
                if (type(u[i,0]) is cvxpy_scalar_var and
                    type(v[i,0]) is cvxpy_scalar_var):
                    w = -1./v_i
                    h[mp[u[i,0]],mp[v[i,0]]] = w
                    h[mp[v[i,0]],mp[u[i,0]]] = w
            return h

        # ind f
        def ind_f(x):
            for i in range(0,n):
                if type(u[i,0]) is cvxpy_scalar_var:
                    if x[mp[u[i,0]]] <= 0:
                        return False
                else:
                    if u[i,0] <= 0:
                        return False
                if type(v[i,0]) is cvxpy_scalar_var:
                    if x[mp[v[i,0]]] <= 0:
                        return False
                else:
                    if v[i,0] <= 0:
                        return False
            return True

        # Return functions
        return f, grad_f, hess_f, ind_f
    def _construct(self,el,mp,p):
        n = el.shape[0]-1
        u = vstack([el[0:n,0]])
        v = el[n,0]

        # Prepare u
        for i in range(0,n,1):
            if type(u[i,0]) is cvxpy_obj:
                u[i,0] = u[i,0].value
                
        # Prepare v
        if type(v) is cvxpy_obj:
            v = v.value

        # f
        def f(x):
            vals = []
            for i in range(0,n,1):
                if type(u[i,0]) is cvxpy_scalar_var:
                    vals += [x[mp[u[i,0]]]]
                else:
                    vals += [u[i,0]]
            if type(v) is cvxpy_scalar_var:
                y = x[mp[v]]
            else:
                y = v
            return y - reduce(lambda w,z:w*z,vals,1.)**(1./(n*1.))
        
        # grad f
        def grad_f(x):
            g = opt.spmatrix(0.0,[],[],(p,1))
            if type(v) is cvxpy_scalar_var:
                fval = -f(x)+x[mp[v]]
            else:
                fval = -f(x)+v
            for i in range(0,n,1):
                if type(u[i,0]) is cvxpy_scalar_var:
                    xi = x[mp[u[i,0]]]
                    g[mp[u[i,0]]] = -fval/(n*xi*1.)
            if type(v) is cvxpy_scalar_var:
                g[mp[v]] = 1.
            return g

        # hess f
        def hess_f(x):
            h = opt.spmatrix(0.0,[],[],(p,p))
            if type(v) is cvxpy_scalar_var:
                fval = -f(x)+x[mp[v]]
            else:
                fval = -f(x)+v
            for i in range(0,n,1):
                for j in range(0,n,1):
                    if (type(u[i,0]) is cvxpy_scalar_var and 
                        type(u[j,0]) is cvxpy_scalar_var):
                        xi = x[mp[u[i,0]]]
                        xj = x[mp[u[j,0]]]
                        if i == j:
                            w = (n-1.)*fval/(n*n*1.*xi*xi)
                            h[mp[u[i,0]],mp[u[i,0]]] = w
                        else:
                            w = -fval/(1.*n*n*xi*xj)
                            h[mp[u[i,0]],mp[u[j,0]]] = w
                            h[mp[u[j,0]],mp[u[i,0]]] = w
            return h

        # ind f
        def ind_f(x):
            for i in range(0,n,1):
                if type(u[i,0]) is cvxpy_scalar_var:
                    if x[mp[u[i,0]]] <= 0:
                        return False
                else:
                    if u[i,0] <= 0:
                        return False
            return True

        # Return functions
        return f, grad_f, hess_f, ind_f