def dfdx(guess):

        network.buses_t.v_ang.loc[now,sub_network.pvpqs] = guess[:len(sub_network.pvpqs)]

        network.buses_t.v_mag_pu.loc[now,sub_network.pqs] = guess[len(sub_network.pvpqs):]

        v_mag_pu = network.buses_t.v_mag_pu.loc[now,buses_o]
        v_ang = network.buses_t.v_ang.loc[now,buses_o]

        V = v_mag_pu*np.exp(1j*v_ang)

        index = r_[:len(buses_o)]

        #make sparse diagonal matrices
        V_diag = csr_matrix((V,(index,index)))
        V_norm_diag = csr_matrix((V/abs(V),(index,index)))
        I_diag = csr_matrix((sub_network.Y*V,(index,index)))

        dS_dVa = 1j*V_diag*np.conj(I_diag - sub_network.Y*V_diag)

        dS_dVm = V_norm_diag*np.conj(I_diag) + V_diag * np.conj(sub_network.Y*V_norm_diag)

        J00 = dS_dVa[1:,1:].real
        J01 = dS_dVm[1:,1+len(sub_network.pvs):].real
        J10 = dS_dVa[1+len(sub_network.pvs):,1:].imag
        J11 = dS_dVm[1+len(sub_network.pvs):,1+len(sub_network.pvs):].imag

        J = svstack([
            shstack([J00, J01]),
            shstack([J10, J11])
        ], format="csr")

        return J
Esempio n. 2
0
File: pf.py Progetto: jdedecca/PyPSA
    def dfdx(guess):

        network.buses_t.v_ang.loc[now,sub_network.pvpqs] = guess[:len(sub_network.pvpqs)]

        network.buses_t.v_mag_pu.loc[now,sub_network.pqs] = guess[len(sub_network.pvpqs):]

        v_mag_pu = network.buses_t.v_mag_pu.loc[now,buses_o]
        v_ang = network.buses_t.v_ang.loc[now,buses_o]

        V = v_mag_pu*np.exp(1j*v_ang)

        index = r_[:len(buses_o)]

        #make sparse diagonal matrices
        V_diag = csr_matrix((V,(index,index)))
        V_norm_diag = csr_matrix((V/abs(V),(index,index)))
        I_diag = csr_matrix((sub_network.Y*V,(index,index)))

        dS_dVa = 1j*V_diag*np.conj(I_diag - sub_network.Y*V_diag)

        dS_dVm = V_norm_diag*np.conj(I_diag) + V_diag * np.conj(sub_network.Y*V_norm_diag)

        J00 = dS_dVa[1:,1:].real
        J01 = dS_dVm[1:,1+len(sub_network.pvs):].real
        J10 = dS_dVa[1+len(sub_network.pvs):,1:].imag
        J11 = dS_dVm[1+len(sub_network.pvs):,1+len(sub_network.pvs):].imag

        J = svstack([
            shstack([J00, J01]),
            shstack([J10, J11])
        ], format="csr")

        return J
Esempio n. 3
0
def equilibrium_matrix(C, xyz, free, rtype='array'):
    r"""Construct the equilibrium matrix of a structural system.

    Parameters
    ----------
    C : array-like
        Connectivity matrix (m x n).
    xyz : array-like
        Array of vertex coordinates (n x 3).
    free : list
        The index values of the free vertices.
    rtype : {'array', 'csc', 'csr', 'coo', 'list'}
        Format of the result.

    Returns
    -------
    array-like
        Constructed equilibrium matrix.

    Notes
    -----
    Analysis of the equilibrium matrix reveals some of the properties of the
    structural system, its size is (2ni x m) where ni is the number of free or
    internal nodes. It is calculated by

    .. math::

        \mathbf{E}
        =
        \left[
            \begin{array}{c}
                \mathbf{C}^{\mathrm{T}}_{\mathrm{i}}\mathbf{U} \\[0.3em]
                \hline \\[-0.7em]
                \mathbf{C}^{\mathrm{T}}_{\mathrm{i}}\mathbf{V}
            \end{array}
        \right].

    The matrix of vertex coordinates is vectorised to speed up the
    calculations.

    Examples
    --------
    >>> C = connectivity_matrix([[0, 1], [0, 2], [0, 3]])
    >>> xyz = [[0, 0, 1], [0, 1, 0], [-1, -1, 0], [1, -1, 0]]
    >>> equilibrium_matrix(C, xyz, [0], rtype='array')
        [[ 0.  1. -1.]
         [-1.  1.  1.]]

    """
    xyz = asarray(xyz, dtype=float)
    C = csr_matrix(C)
    xy = xyz[:, :2]
    uv = C.dot(xy)
    U = diags([uv[:, 0].flatten()], [0])
    V = diags([uv[:, 1].flatten()], [0])
    Ct = C.transpose()
    Cti = Ct[free, :]
    E = svstack((Cti.dot(U), Cti.dot(V)))
    return _return_matrix(E, rtype)
Esempio n. 4
0
    def dfdx(guess, distribute_slack=False, slack_weights=None):

        last_pq = -1 if distribute_slack else None
        network.buses_t.v_ang.loc[
            now, sub_network.pvpqs] = guess[:len(sub_network.pvpqs)]
        network.buses_t.v_mag_pu.loc[
            now, sub_network.pqs] = guess[len(sub_network.pvpqs):last_pq]

        v_mag_pu = network.buses_t.v_mag_pu.loc[now, buses_o]
        v_ang = network.buses_t.v_ang.loc[now, buses_o]

        V = v_mag_pu * np.exp(1j * v_ang)

        index = r_[:len(buses_o)]

        #make sparse diagonal matrices
        V_diag = csr_matrix((V, (index, index)))
        V_norm_diag = csr_matrix((V / abs(V), (index, index)))
        I_diag = csr_matrix((sub_network.Y * V, (index, index)))

        dS_dVa = 1j * V_diag * np.conj(I_diag - sub_network.Y * V_diag)

        dS_dVm = V_norm_diag * np.conj(I_diag) + V_diag * np.conj(
            sub_network.Y * V_norm_diag)

        J10 = dS_dVa[1 + len(sub_network.pvs):, 1:].imag
        J11 = dS_dVm[1 + len(sub_network.pvs):, 1 + len(sub_network.pvs):].imag

        if distribute_slack:
            J00 = dS_dVa[:, 1:].real
            J01 = dS_dVm[:, 1 + len(sub_network.pvs):].real
            J02 = csr_matrix(slack_weights, (1, 1 + len(sub_network.pvpqs))).T
            J12 = csr_matrix((1, len(sub_network.pqs))).T
            J_P_blocks = [J00, J01, J02]
            J_Q_blocks = [J10, J11, J12]
        else:
            J00 = dS_dVa[1:, 1:].real
            J01 = dS_dVm[1:, 1 + len(sub_network.pvs):].real
            J_P_blocks = [J00, J01]
            J_Q_blocks = [J10, J11]

        J = svstack([shstack(J_P_blocks), shstack(J_Q_blocks)], format="csr")

        return J
Esempio n. 5
0
def _dfdx(guess, Y):
    """
    Compute the Jacobian matrix.

    Parameters
    ----------
    guess : numpy.ndarray
        The current v_guess for the roots of f(x), of size 2*(N-1), where elements
        [0,...,N-2] are the nodal voltage angles \theta_i and [N-1,...,2(N-1)]
        the nodal voltage magnitudes |V_i|. The slack bus variables are
        excluded.
    Y : scipy.sparse.csc_matrix
        The nodal admittance matrix of shape (N, N) as a sparse matrix.

    Returns
    -------
    J : scipy.sparse.csr_matrix
        The Jacobian matrix as a sparse matrix.
    """

    # Construct nodal voltage vector V, setting V_slack = 1+0j.
    v = _construct_v_from_guess(guess)

    index = np.array(range(len(v)))

    # Construct sparse diagonal matrices.
    v_diag = csr_matrix((v, (index, index)))
    v_norm_diag = csr_matrix((v / abs(v), (index, index)))
    i_diag = csr_matrix((Y * v, (index, index)))

    # Construct the Jacobian matrix.
    dS_dVa = 1j * v_diag * np.conj(i_diag - Y * v_diag)  # dS / d \theta
    dS_dVm = v_norm_diag * np.conj(i_diag) + v_diag * np.conj(
        Y * v_norm_diag)  # dS / d |V|

    J00 = dS_dVa[1:, 1:].real
    J01 = dS_dVm[1:, 1:].real
    J10 = dS_dVa[1:, 1:].imag
    J11 = dS_dVm[1:, 1:].imag

    J = svstack([shstack([J00, J01]), shstack([J10, J11])], format='csr')

    return J
Esempio n. 6
0
    def __init__(self, n, f, **kwargs):
        """
        Initializer for the problem class.

        The user will supply functions and vectors as keyword arguements. The
        input vector x is assumed to be of length n.

        Each function is assumed to take in only the vector x (with the
        exception of the hessian function). Any user arguements must be wrapped
        first instead.

        Parameters
        ==========
            n : integer, length of input vector for NLP problem
            f :   function
                  takes in x
                  returns scalar

        Keyword args
        ============
            f_x : function
                  takes in x
                  returns numpy column array of n x 1
            x_l : a numpy column array of n x 1
            x_u : a numpy column array of n x 1


          and:


            g   : function
                  takes in x
                  returns numpy column array
            h   : function
                  takes in x
                  returns numpy column array
            g_x : function
                  takes in x
                  returns scipy sparse matrix of n x len(g)
            h_x : function
                  takes in x
                  returns scipy sparse matrix of n x len(h)
            hessian : function
                      takes in x, v_g, v_h
                      where v are the lagrange multipliers of the lagrangian,
                      assumed to be of the form:
                      f(x) + v_g^T g + v_h^T h
                      returns scipy sparse matrix of n x n
          or:
            c   : function
                  takes in x
                  returns numpy column array
            c_x : function
                  takes in x
                  returns scipy sparse matrix of n x len(g)
            c_l : numpy column array of len(c) x 1
            c_u : numpy column array of len(c) x 1
            hessian : function
                      takes in x, v
                      where v are the lagrange multipliers (as a numpy column
                      vector) of the lagrangian, assumed to be of the form:
                      f(x) + v^T c
                      returns scipy sparse matrix of n x n

        Methods generated for the solver are:
        f
        f_x
        c_e
        c_i
        A_e
        A_i
        H

        """
        self.n = n
        # First check to make sure problem hasn't been incorrectly supplied
        combined  = ['c', 'cl', 'cu', 'c_x']
        separated = ['g', 'h', 'g_x', 'h_x']
        # now check if components from either style are in the kwargs
        check1 = max([(i in kwargs.keys()) for i in combined])
        check2 = max([(i in kwargs.keys()) for i in separated])
        # Raise error if 2 styles combined, or no constraints supplied
        if check1 and check2:
            raise ValueError('Problem supplied incorrectly (constraint style)')
        elif check1:
            style = 'c'
        elif check2:
            style = 's'
        else:
            raise ValueError('Only constrained problems supported')
        # Also need to create settings for finite differencing
        try:
            # This is equivalent to max(f^y (x0)) where y = order + 1
            # Used to calculate optimal finite difference step size
            self._fdscale = kwargs['fin_diff_scale']
        except:
            self._fdscale = 1. / 3.
        try: # Finite difference order - forward h, central h^2, central h^4
            self._order = kwargs['fin_diff_order']
        except:
            self._order = 2

        ##########################################
        # Functions are now going to be defined. #
        ##########################################

        ########################
        # Common to both forms #
        ########################
        # objective function definition
        resh = lambda x: x.reshape(-1, 1)
        self.f = lambda x: resh(f(x))
        # objective function gradient
        try:
            self.f_x = kwargs['f_x']
        except:
            self.f_x = lambda x: self.approx_jacobian(x, f)

        ##################
        # Separated form #
        ##################
        if style == 's':
            # equality constraint function
            try:
                self.c_e = kwargs['g']
            except:
                self.c_e = lambda x: self.empty_f(x)
            # inequality constraint function
            try:
                self.c_i = kwargs['h']
            except:
                self.c_i = lambda x: self.empty_f(x)
            # equality constraint gradient
            try:
                self.A_e = kwargs['g_x']
            except:
                self.A_e = lambda x: csc_matrix(self.approx_jacobian(x, self.c_e))
            # inequality constraint gradient
            try:
                self.A_i = kwargs['h_x']
            except:
                self.A_i = lambda x: csc_matrix(self.approx_jacobian(x, self.c_i))
            # hessian function
            try:
                self.hessian = kwargs['hessian']
            except:
                self.hessian = None

        #################
        # Combined form #
        #################
        else:
            ######## long and awkward... ###########
            try:
                xl = self.xl = kwargs['xl']
            except:
                xl = self.xl = ones((n, 1)) * -1e20
            try:
                xu = self.xu = kwargs['xu']
            except:
                xu = self.xu = ones((n, 1)) * 1e20
            c = self.c  = kwargs['c']
            cl = self.cl = kwargs['cl']
            cu = self.cu = kwargs['cu']
            try:
                c_x = self.c_x = kwargs['c_x']
            except:
                c_x = self.c_x = lambda x: csc_matrix(self.approx_jacobian(x, self.c))
            o = n
            mn = len(self.cl)
            I = seye(o, o).tocsc()

            (coo_xer, coo_xec, coo_xed, coo_xir, coo_xic, coo_xid, coo_xlr,
             coo_xlc, coo_xld, coo_xur, coo_xuc, coo_xud, coo_cer, coo_cec,
             coo_ced, coo_cir, coo_cic, coo_cid, coo_clr, coo_clc, coo_cld,
             coo_cur, coo_cuc, coo_cud) = ([], [], [], [], [], [], [], [], [],
                                           [], [], [], [], [], [], [], [], [],
                                           [], [], [], [], [], [])

            ############## BOUNDS ################
            xm = 0
            xn = 0
            for i in range(o):
                if xl[i] == xu[i]:
                    coo_xer += [xm]
                    coo_xec += [i]
                    coo_xed += [1]
                    xm += 1
                else:
                    coo_xir += [xn]
                    coo_xic += [i]
                    coo_xid += [1]
                    xn += 1
            l = 0
            u = 0
            for i in range(xn):
                if xl[coo_xic[i]] >= -1e19:
                    coo_xlr += [l]
                    coo_xlc += [coo_xic[i]]
                    coo_xld += [coo_xid[i]]
                    l += 1
                if xu[coo_xic[i]] <= 1e19:
                    coo_xur += [u]
                    coo_xuc += [coo_xic[i]]
                    coo_xud += [coo_xid[i]]
                    u += 1
            try:
                Kxe = coo_matrix((coo_xed, (coo_xer, coo_xec)), shape=(xm, o)).tocsc()
            except:
                Kxe = None
            try:
                Kxl = coo_matrix((coo_xld, (coo_xlr, coo_xlc)), shape=(l, o)).tocsc()
            except:
                Kxl = None
            try:
                Kxu = coo_matrix((coo_xud, (coo_xur, coo_xuc)), shape=(u, o)).tocsc()
            except:
                Kxu = None

            ############## CONSTRAINTS ################
            cm = 0
            cn = 0
            for i in range(mn):
                if cl[i] == cu[i]:
                    coo_cer += [cm]
                    coo_cec += [i]
                    coo_ced += [1]
                    cm += 1
                else:
                    coo_cir += [cn]
                    coo_cic += [i]
                    coo_cid += [1]
                    cn += 1

            l = 0
            u = 0
            for i in range(cn):
                if cl[coo_cic[i]] >= -1e19:
                    coo_clr += [l]
                    coo_clc += [coo_cic[i]]
                    coo_cld += [coo_cid[i]]
                    l += 1
                if cu[coo_cic[i]] <= 1e19:
                    coo_cur += [u]
                    coo_cuc += [coo_cic[i]]
                    coo_cud += [coo_cid[i]]
                    u += 1
            try:
                Kce = coo_matrix((coo_ced, (coo_cer, coo_cec)), shape=(cm, mn)).tocsc()
            except:
                Kce = None
            try:
                Kcl = coo_matrix((coo_cld, (coo_clr, coo_clc)), shape=(l, mn)).tocsc()
            except:
                Kcl = None
            try:
                Kcu = coo_matrix((coo_cud, (coo_cur, coo_cuc)), shape=(u, mn)).tocsc()
            except:
                Kcu = None

            ############## COMBINING ################
            # Equality
            if (Kxe is not None) and (Kce is not None):
                Ke  = bmat([[Kxe, None],
                            [None, Kce]])
                ce  = vstack([Kxe * xl, Kce * cl])
                eq  = lambda x: Ke * vstack([resh(x), c(x)]) - ce
                jeq = lambda x: (Ke * svstack([I, c_x(x)]))
                num_x_eq = len(Kxe * xl)
            elif Kxe is not None:
                Ke  = Kxe
                ce  = Kxe * xl
                eq  = lambda x: Ke * resh(x) - ce
                jeq = lambda x: Ke
                num_x_eq = len(Kxe * xl)
            elif Kce is not None:
                Ke  = Kce
                ce  = Kce * cl
                eq  = lambda x: Ke * c(x) - ce
                jeq = lambda x: (Ke * c_x(x))
                num_x_eq = 0
            else:
                Ke  = None
                ce  = None
                eq  = None
                jeq = None
                num_x_eq = 0
            # Bounds
            if (Kxl is not None) and (Kxu is not None):
                Kiu = bmat([[-Kxl],
                            [ Kxu]])
                ciu = vstack([Kxl * xl, -Kxu * xu])
            elif Kxl is not None:
                Kiu = -Kxl
                ciu = Kxl * xl
            elif Kxu is not None:
                Kiu = Kxu
                ciu = -Kxu * xu
            else:
                Kiu = None
                ciu = None
            # Constraints
            if (Kcl is not None) and (Kcu is not None):
                Kil = bmat([[-Kcl],
                            [ Kcu]])
                cil = vstack([Kcl * cl, -Kcu * cu])
            elif Kcl is not None:
                Kil = -Kcl
                cil = Kcl * cl
            elif Kcu is not None:
                Kil = Kcu
                cil = -Kcu * cu
            else:
                Kil = None
                cil = None
            # Bounds + Constraints
            if (Kiu is not None) and (Kil is not None):
                Ki    = bmat([[ Kiu, None],
                            [None,  Kil]])
                ci    = vstack([ciu, cil])
                ineq  = lambda x: ci + Ki * vstack([resh(x), c(x)])
                jineq = lambda x: (Ki * svstack([I, c_x(x)]))
                num_x_bound = len(ciu)
            elif Kil is not None:
                Ki    = Kil
                ci    = cil
                ineq  = lambda x: ci + Ki * c(x)
                jineq = lambda x: (Ki * c_x(x))
                num_x_bound = 0
            elif Kiu is not None:
                Ki    = Kiu
                ci    = ciu
                ineq  = lambda x: ci + Ki * resh(x)
                jineq = lambda x: Ki
                num_x_bound = len(ciu)
            else:
                Ki    = None
                ci    = None
                ineq  = None
                jineq = None
                num_x_bound = 0

            ############# HESSIAN ###################
            try:
                hess_func = kwargs['hessian']
                self.c_hessian = hess_func
                def hess(x, lam_e, lam_i):
                    lam_e = resh(lam_e)
                    lam_i = resh(lam_i)
                    lam = 0
                    try:
                        lam += Kce.transpose() * lam_e[num_x_eq:]
                    except:
                        pass
                    try:
                        lam += Kcl.transpose() * lam_i[num_x_bound:]
                    except:
                        pass
                    try:
                        lam += Kcu.transpose() * lam_i[num_x_bound:]
                    except:
                        pass
                    return hess_func(x, lam)
            except:
                hess = None

            self.c_e = eq
            self.c_i = ineq
            self.A_e = jeq
            self.A_i = jineq
            self.hessian = hess
Esempio n. 7
0
    def __init__(self, n, f, **kwargs):
        """
        Initializer for the problem class.

        The user will supply functions and vectors as keyword arguements. The
        input vector x is assumed to be of length n.

        Each function is assumed to take in only the vector x (with the
        exception of the hessian function). Any user arguements must be wrapped
        first instead.

        Parameters
        ==========
            n : integer, length of input vector for NLP problem
            f :   function
                  takes in x
                  returns scalar

        Keyword args
        ============
            f_x : function
                  takes in x
                  returns numpy column array of n x 1
            x_l : a numpy column array of n x 1
            x_u : a numpy column array of n x 1


          and:


            g   : function
                  takes in x
                  returns numpy column array
            h   : function
                  takes in x
                  returns numpy column array
            g_x : function
                  takes in x
                  returns scipy sparse matrix of n x len(g)
            h_x : function
                  takes in x
                  returns scipy sparse matrix of n x len(h)
            hessian : function
                      takes in x, v_g, v_h
                      where v are the lagrange multipliers of the lagrangian,
                      assumed to be of the form:
                      f(x) + v_g^T g + v_h^T h
                      returns scipy sparse matrix of n x n
          or:
            c   : function
                  takes in x
                  returns numpy column array
            c_x : function
                  takes in x
                  returns scipy sparse matrix of n x len(g)
            c_l : numpy column array of len(c) x 1
            c_u : numpy column array of len(c) x 1
            hessian : function
                      takes in x, v
                      where v are the lagrange multipliers (as a numpy column
                      vector) of the lagrangian, assumed to be of the form:
                      f(x) + v^T c
                      returns scipy sparse matrix of n x n

        Methods generated for the solver are:
        f
        f_x
        c_e
        c_i
        A_e
        A_i
        H

        """
        self.n = n
        # First check to make sure problem hasn't been incorrectly supplied
        combined = ['c', 'cl', 'cu', 'c_x']
        separated = ['g', 'h', 'g_x', 'h_x']
        # now check if components from either style are in the kwargs
        check1 = max([(i in kwargs.keys()) for i in combined])
        check2 = max([(i in kwargs.keys()) for i in separated])
        # Raise error if 2 styles combined, or no constraints supplied
        if check1 and check2:
            raise ValueError('Problem supplied incorrectly (constraint style)')
        elif check1:
            style = 'c'
        elif check2:
            style = 's'
        else:
            raise ValueError('Only constrained problems supported')
        # Also need to create settings for finite differencing
        try:
            # This is equivalent to max(f^y (x0)) where y = order + 1
            # Used to calculate optimal finite difference step size
            self._fdscale = kwargs['fin_diff_scale']
        except:
            self._fdscale = 1. / 3.
        try:  # Finite difference order - forward h, central h^2, central h^4
            self._order = kwargs['fin_diff_order']
        except:
            self._order = 2

        ##########################################
        # Functions are now going to be defined. #
        ##########################################

        ########################
        # Common to both forms #
        ########################
        # objective function definition
        resh = lambda x: x.reshape(-1, 1)
        self.f = lambda x: resh(f(x))
        # objective function gradient
        try:
            self.f_x = kwargs['f_x']
        except:
            self.f_x = lambda x: self.approx_jacobian(x, f)

        ##################
        # Separated form #
        ##################
        if style == 's':
            # equality constraint function
            try:
                self.c_e = kwargs['g']
            except:
                self.c_e = lambda x: self.empty_f(x)
            # inequality constraint function
            try:
                self.c_i = kwargs['h']
            except:
                self.c_i = lambda x: self.empty_f(x)
            # equality constraint gradient
            try:
                self.A_e = kwargs['g_x']
            except:
                self.A_e = lambda x: csc_matrix(
                    self.approx_jacobian(x, self.c_e))
            # inequality constraint gradient
            try:
                self.A_i = kwargs['h_x']
            except:
                self.A_i = lambda x: csc_matrix(
                    self.approx_jacobian(x, self.c_i))
            # hessian function
            try:
                self.hessian = kwargs['hessian']
            except:
                self.hessian = None

        #################
        # Combined form #
        #################
        else:
            ######## long and awkward... ###########
            try:
                xl = self.xl = kwargs['xl']
            except:
                xl = self.xl = ones((n, 1)) * -1e20
            try:
                xu = self.xu = kwargs['xu']
            except:
                xu = self.xu = ones((n, 1)) * 1e20
            c = self.c = kwargs['c']
            cl = self.cl = kwargs['cl']
            cu = self.cu = kwargs['cu']
            try:
                c_x = self.c_x = kwargs['c_x']
            except:
                c_x = self.c_x = lambda x: csc_matrix(
                    self.approx_jacobian(x, self.c))
            o = n
            mn = len(self.cl)
            I = seye(o, o).tocsc()

            (coo_xer, coo_xec, coo_xed, coo_xir, coo_xic, coo_xid, coo_xlr,
             coo_xlc, coo_xld, coo_xur, coo_xuc, coo_xud, coo_cer, coo_cec,
             coo_ced, coo_cir, coo_cic, coo_cid, coo_clr, coo_clc, coo_cld,
             coo_cur, coo_cuc, coo_cud) = ([], [], [], [], [], [], [], [], [],
                                           [], [], [], [], [], [], [], [], [],
                                           [], [], [], [], [], [])

            ############## BOUNDS ################
            xm = 0
            xn = 0
            for i in range(o):
                if xl[i] == xu[i]:
                    coo_xer += [xm]
                    coo_xec += [i]
                    coo_xed += [1]
                    xm += 1
                else:
                    coo_xir += [xn]
                    coo_xic += [i]
                    coo_xid += [1]
                    xn += 1
            l = 0
            u = 0
            for i in range(xn):
                if xl[coo_xic[i]] >= -1e19:
                    coo_xlr += [l]
                    coo_xlc += [coo_xic[i]]
                    coo_xld += [coo_xid[i]]
                    l += 1
                if xu[coo_xic[i]] <= 1e19:
                    coo_xur += [u]
                    coo_xuc += [coo_xic[i]]
                    coo_xud += [coo_xid[i]]
                    u += 1
            try:
                Kxe = coo_matrix((coo_xed, (coo_xer, coo_xec)),
                                 shape=(xm, o)).tocsc()
            except:
                Kxe = None
            try:
                Kxl = coo_matrix((coo_xld, (coo_xlr, coo_xlc)),
                                 shape=(l, o)).tocsc()
            except:
                Kxl = None
            try:
                Kxu = coo_matrix((coo_xud, (coo_xur, coo_xuc)),
                                 shape=(u, o)).tocsc()
            except:
                Kxu = None

            ############## CONSTRAINTS ################
            cm = 0
            cn = 0
            for i in range(mn):
                if cl[i] == cu[i]:
                    coo_cer += [cm]
                    coo_cec += [i]
                    coo_ced += [1]
                    cm += 1
                else:
                    coo_cir += [cn]
                    coo_cic += [i]
                    coo_cid += [1]
                    cn += 1

            l = 0
            u = 0
            for i in range(cn):
                if cl[coo_cic[i]] >= -1e19:
                    coo_clr += [l]
                    coo_clc += [coo_cic[i]]
                    coo_cld += [coo_cid[i]]
                    l += 1
                if cu[coo_cic[i]] <= 1e19:
                    coo_cur += [u]
                    coo_cuc += [coo_cic[i]]
                    coo_cud += [coo_cid[i]]
                    u += 1
            try:
                Kce = coo_matrix((coo_ced, (coo_cer, coo_cec)),
                                 shape=(cm, mn)).tocsc()
            except:
                Kce = None
            try:
                Kcl = coo_matrix((coo_cld, (coo_clr, coo_clc)),
                                 shape=(l, mn)).tocsc()
            except:
                Kcl = None
            try:
                Kcu = coo_matrix((coo_cud, (coo_cur, coo_cuc)),
                                 shape=(u, mn)).tocsc()
            except:
                Kcu = None

            ############## COMBINING ################
            # Equality
            if (Kxe is not None) and (Kce is not None):
                Ke = bmat([[Kxe, None], [None, Kce]])
                ce = vstack([Kxe * xl, Kce * cl])
                eq = lambda x: Ke * vstack([resh(x), c(x)]) - ce
                jeq = lambda x: (Ke * svstack([I, c_x(x)]))
                num_x_eq = len(Kxe * xl)
            elif Kxe is not None:
                Ke = Kxe
                ce = Kxe * xl
                eq = lambda x: Ke * resh(x) - ce
                jeq = lambda x: Ke
                num_x_eq = len(Kxe * xl)
            elif Kce is not None:
                Ke = Kce
                ce = Kce * cl
                eq = lambda x: Ke * c(x) - ce
                jeq = lambda x: (Ke * c_x(x))
                num_x_eq = 0
            else:
                Ke = None
                ce = None
                eq = None
                jeq = None
                num_x_eq = 0
            # Bounds
            if (Kxl is not None) and (Kxu is not None):
                Kiu = bmat([[-Kxl], [Kxu]])
                ciu = vstack([Kxl * xl, -Kxu * xu])
            elif Kxl is not None:
                Kiu = -Kxl
                ciu = Kxl * xl
            elif Kxu is not None:
                Kiu = Kxu
                ciu = -Kxu * xu
            else:
                Kiu = None
                ciu = None
            # Constraints
            if (Kcl is not None) and (Kcu is not None):
                Kil = bmat([[-Kcl], [Kcu]])
                cil = vstack([Kcl * cl, -Kcu * cu])
            elif Kcl is not None:
                Kil = -Kcl
                cil = Kcl * cl
            elif Kcu is not None:
                Kil = Kcu
                cil = -Kcu * cu
            else:
                Kil = None
                cil = None
            # Bounds + Constraints
            if (Kiu is not None) and (Kil is not None):
                Ki = bmat([[Kiu, None], [None, Kil]])
                ci = vstack([ciu, cil])
                ineq = lambda x: ci + Ki * vstack([resh(x), c(x)])
                jineq = lambda x: (Ki * svstack([I, c_x(x)]))
                num_x_bound = len(ciu)
            elif Kil is not None:
                Ki = Kil
                ci = cil
                ineq = lambda x: ci + Ki * c(x)
                jineq = lambda x: (Ki * c_x(x))
                num_x_bound = 0
            elif Kiu is not None:
                Ki = Kiu
                ci = ciu
                ineq = lambda x: ci + Ki * resh(x)
                jineq = lambda x: Ki
                num_x_bound = len(ciu)
            else:
                Ki = None
                ci = None
                ineq = None
                jineq = None
                num_x_bound = 0

            ############# HESSIAN ###################
            try:
                hess_func = kwargs['hessian']
                self.c_hessian = hess_func

                def hess(x, lam_e, lam_i):
                    lam_e = resh(lam_e)
                    lam_i = resh(lam_i)
                    lam = 0
                    try:
                        lam += Kce.transpose() * lam_e[num_x_eq:]
                    except:
                        pass
                    try:
                        lam += Kcl.transpose() * lam_i[num_x_bound:]
                    except:
                        pass
                    try:
                        lam += Kcu.transpose() * lam_i[num_x_bound:]
                    except:
                        pass
                    return hess_func(x, lam)
            except:
                hess = None

            self.c_e = eq
            self.c_i = ineq
            self.A_e = jeq
            self.A_i = jineq
            self.hessian = hess