Beispiel #1
0
def individual_smart_charge(start,
                            end,
                            energy,
                            version,
                            interval=1,
                            vehicleID=''):
    t = end - start

    if version == 'national':
        b = copy.copy(baseLoad[start:end])
    elif version == 'household':
        b = households[vehicleID][start:end]

    q = matrix(b)

    A = matrix(1.0 / (2 * t_int), (1, t))

    b = matrix([energy])  # amount of energy needed in.... kWh?

    A3 = spdiag([-1] * t)
    A4 = spdiag([1] * t)
    G = sparse([A3, A4])

    h = matrix([0.0] * t + [pMax] * t)

    P = spdiag([1] * t)
    sol = solvers.qp(P, q, G, h, A, b)
    X = sol['x']

    del b

    return X
Beispiel #2
0
    def _arrange_kernel(self):
        Y = [2 * int(y == self.classes_[1]) - 1 for y in self.Y]
        n_sample = len(self.Y)
        ker_matrix = matrix(summation(self.KL))
        YY = spdiag(Y)

        solvers.options['show_progress'] = False
        solvers.options['maxiters'] = self.max_iter
        sol = solvers.qp(
            2 * ((1.0 - self.lam) * YY * ker_matrix * YY +
                 spdiag([self.lam] * n_sample)), matrix([0.0] * n_sample),
            -spdiag([1.0] * n_sample), matrix([0.0] * n_sample, (n_sample, 1)),
            matrix([[float(y == +1) for y in Y],
                    [float(y == -1) for y in Y]]).T, matrix([[1.0] * 2],
                                                            (2, 1)))
        if self.verbose:
            print('[EasyMKL]')
            print('optimization finished, #iter = %d' % sol['iterations'])
            print('status of the solution: %s' % sol['status'])
            print('objval: %.5f' % sol['primal objective'])

        yg = sol['x'].T * YY
        weights = [(yg * matrix(K) * yg.T)[0] for K in self.KL]
        norm2 = sum(weights)
        self.weights = np.array([w / norm2 for w in weights])

        self.ker_matrix = summation(self.KL, self.weights)
        return self.ker_matrix
Beispiel #3
0
    def _arrange_kernel(self):
        Y = [1 if y==self.classes_[1] else -1 for y in self.Y]
        n_sample = len(self.Y)
        ker_matrix = matrix(summation(self.KL))
        YY = spdiag(Y)
        KLL = (1.0-self.lam)*YY*ker_matrix*YY
        LID = spdiag([self.lam]*n_sample)
        Q = 2*(KLL+LID)
        p = matrix([0.0]*n_sample)
        G = -spdiag([1.0]*n_sample)
        h = matrix([0.0]*n_sample,(n_sample,1))
        A = matrix([[1.0 if lab==+1 else 0 for lab in Y],[1.0 if lab2==-1 else 0 for lab2 in Y]]).T
        b = matrix([[1.0],[1.0]],(2,1))
         
        solvers.options['show_progress'] = False
        solvers.options['maxiters'] = self.max_iter
        sol = solvers.qp(Q,p,G,h,A,b)
        gamma = sol['x']
        if self.verbose:
            print ('[EasyMKL]')
            print ('optimization finished, #iter = %d' % sol['iterations'])
            print ('status of the solution: %s' % sol['status'])
            print ('objval: %.5f' % sol['primal objective'])

        yg = gamma.T * YY
        weights = [(yg*matrix(K)*yg.T)[0] for K in self.KL]
         
        norm2 = sum([w for w in weights])
        self.weights = np.array([w / norm2 for w in weights])
        ker_matrix = summation(self.KL, self.weights)
        self.ker_matrix = ker_matrix
        return ker_matrix
Beispiel #4
0
 def _fit(self):
     ker_matrix = matrix(self.arrange_kernel(self.X, self.Y, check=False))
     self.ker_matrix = ker_matrix
     n_sample = len(self.Y)
     YY = spdiag(self.Y)
     KLL = (1.0 - self.lam) * YY * ker_matrix * YY
     LID = spdiag([self.lam] * n_sample)
     Q = 2 * (KLL + LID)
     p = matrix([0.0] * n_sample)
     G = -spdiag([1.0] * n_sample)
     h = matrix([0.0] * n_sample, (n_sample, 1))
     A = matrix([[1.0 if lab == +1 else 0 for lab in self.Y],
                 [1.0 if lab2 == -1 else 0 for lab2 in self.Y]]).T
     b = matrix([[1.0], [1.0]], (2, 1))
     solvers.options['show_progress'] = False
     solvers.options['maxiters'] = self.max_iter
     sol = solvers.qp(Q, p, G, h, A, b)
     self.gamma = sol['x']
     if self.verbose:
         print('[EasyMKL] - second step')
         print('optimization finished, #iter = ', sol['iterations'])
         print('status of the solution: ', sol['status'])
         print('objval: ', sol['primal objective'])
     self.is_fitted = True
     bias = 0.5 * self.gamma.T * ker_matrix * YY * self.gamma
     self.bias = bias
     return self
Beispiel #5
0
def margin(K,Y):
    """evaluate the margin in a classification problem of examples in feature space.
    If the classes are not linearly separable in feature space, then the
    margin obtained is 0.

    Note that it works only for binary tasks.

    Parameters
    ----------
    K : (n,n) ndarray,
        the kernel that represents the data.
    Y : (n) array_like,
        the labels vector.
    """
    check_K_Y(K,Y)
    n = Y.shape[0]
    YY = spdiag(list(Y))
    P = 2*(YY*matrix(K)*YY)
    p = matrix([0.0]*n)
    G = -spdiag([1.0]*n)
    h = matrix([0.0]*n)
    A = matrix([[1.0 if Y[i]==+1 else 0 for i in range(n)],
                [1.0 if Y[j]==-1 else 0 for j in range(n)]]).T
    b = matrix([[1.0],[1.0]],(2,1))
    solvers.options['show_progress']=False
    sol = solvers.qp(P,p,G,h,A,b)
    return np.sqrt(sol['primal objective'])/2.0#prendo la distanza dall'iperpiano
Beispiel #6
0
    def F(W):
        """
        Return a function f(x,y,z) that solves
        [P , G' W^-1]  [ux]       [bx]
        [G ,  -W    ]  [uy]    =  [bz]

        """
        #d   =    spdiag(matrix(numpy.array(W['d'])))
        #dinv=    spdiag(matrix(numpy.array(W['di'])))
        d = spdiag(W['d'])
        dinv = spdiag(W['di'])

        #KKT1 =    d*( P * d + dinv )
        KKT1 = d * P * d + spdiag(matrix(1.0, (2 * dim, 1)))
        lapack.potrf(KKT1)

        #raw_input('inputpppp')
        def f(x, y, z):
            uz = -d * (x + P * z)
            #uz  =   matrix(numpy.linalg.solve(KKT1, uz))  # slow version
            #lapack.gesv(KKT1,uz)  #  JZ: gesv have cond issue
            lapack.potrs(KKT1, uz)
            x[:] = matrix(-z - d * uz)
            blas.copy(uz, z)

        return f
Beispiel #7
0
    def train(self, test_users=None):
        self.model = {}
        iset = set(range(self.n_items))

        if test_users is None:
            test_users = range(self.n_users)

        for i, u in enumerate(test_users):
            #if (i+1) % 100 == 0:
            print "%d/%d" % (i + 1, len(test_users))

            Xp_set = self.data.get_items(u)
            Xn_set = iset - Xp_set

            Z = co.spdiag([1.0 if i in Xp_set else -1.0 for i in iset])
            K = 2 * (Z * self.X.T * self.X * Z)
            I = co.spdiag([
                self.lambda_p if i in Xp_set else self.lambda_n for i in iset
            ])
            P = K + I

            o = utc.zeroes_vec(self.n_items)
            G = -utc.identity(self.n_items)
            A = co.matrix([[1.0 if i in Xp_set else 0.0 for i in iset],
                           [1.0 if j in Xn_set else 0.0 for j in iset]]).T
            b = co.matrix([1.0, 1.0])
            P = co.sparse(P)

            solver.options['show_progress'] = False
            sol = solver.qp(P, o, G, o, A, b)
            self.sol[u] = sol
            self.model[u] = self.X.T * self.X * Z * sol['x']

        # endfor
        return self
Beispiel #8
0
    def _fit(self, K, Y):
        Ks = arrange_kernel(K, Y)
        Ks = matrix(Ks)
        K1 = (1.0 - self.D) * YY * Ks * YY + spdiag([self.D] * n)
        #K2 = self.C * Ks * self.D
        K2 = (1.0 - self.D) * self.C * Ks * self.D + spdiag(
            [self.D] * n) * self.C
        P = 2 * matrix([[
            K1[i, j] if i < n and j < n else
            K2[i - n, j - n] if i >= n and j >= n else 0.0
            for i in range(2 * n)
        ] for j in range(2 * n)])
        q = matrix([0.0 for i in range(2 * n)])
        h = matrix([0.0] * (2 * n), (2 * n, 1))
        G = -spdiag([1 for i in range(2 * n)])
        A = matrix(
            [[1.0 if i < n and Y[i] == +1 else 0.0 for i in range(2 * n)],
             [1.0 if i < n and Y[i] == -1 else 0.0 for i in range(2 * n)],
             [1.0 if i >= n else 0 for i in range(2 * n)]]).T
        b = matrix([1.0, 1.0, 1.0])
        solvers.options['show_progress'] = False
        solvers.options['maxiters'] = 200
        sol = solvers.qp(P, q, G, h, A, b)
        x = sol['x']
        self.gamma = matrix(x[:n])
        self.alpha = matrix(x[n:2 * n])
        self.Y = Y

        return self
Beispiel #9
0
    def solve(self):
        '''
        "Solves" minimization problem using epigraph linear program
        formulation.
        '''
        A, y, x_dim, y_dim = self.A, self.y, self.x_dim, self.y_dim

        A = cvxopt.sparse(cvxopt.matrix(A))
        I_t = cvxopt.spdiag(cvxopt.matrix(ones(y_dim)))
        I_x = cvxopt.spdiag(cvxopt.matrix(ones(x_dim)))
        Z = cvxopt.spmatrix([], [], [], size=(x_dim, y_dim))
        A = cvxopt.sparse([
            [A, -A, -I_x],
            [-I_t, -I_t, Z]
            ])  # Sparse block matrix in COLUMN major order...for some reason

        y = cvxopt.matrix(y)
        z = cvxopt.matrix(zeros((x_dim, 1)))
        one = cvxopt.matrix(ones((y_dim, 1)))
        y = cvxopt.matrix([y, -y, z])
        c = cvxopt.matrix([z, one])

        result = cvxopt.solvers.lp(c, A, y, solver='glpk')

        self.result = result

        return result['status']
Beispiel #10
0
    def _arrange_kernel(self):
        Y = [1 if y == self.classes_[1] else -1 for y in self.Y]
        n_sample = len(self.Y)
        ker_matrix = matrix(summation(self.KL))
        YY = spdiag(Y)
        KLL = (1.0 - self.lam) * YY * ker_matrix * YY
        LID = spdiag([self.lam] * n_sample)
        Q = 2 * (KLL + LID)
        p = matrix([0.0] * n_sample)
        G = -spdiag([1.0] * n_sample)
        h = matrix([0.0] * n_sample, (n_sample, 1))
        A = matrix([[1.0 if lab == +1 else 0 for lab in Y],
                    [1.0 if lab2 == -1 else 0 for lab2 in Y]]).T
        b = matrix([[1.0], [1.0]], (2, 1))

        solvers.options['show_progress'] = False
        solvers.options['maxiters'] = self.max_iter
        sol = solvers.qp(Q, p, G, h, A, b)
        gamma = sol['x']
        if self.verbose:
            print('[EasyMKL]')
            print('optimization finished, #iter = {}', sol['iterations'])
            print('status of the solution: {}', sol['status'])
            print('objval: {}', sol['primal objective'])

        yg = gamma.T * YY
        weights = [(yg * matrix(K) * yg.T)[0] for K in self.KL]

        norm2 = sum([w for w in weights])
        self.weights = np.array([w / norm2 for w in weights])
        ker_matrix = summation(self.KL, self.weights)
        self.ker_matrix = ker_matrix
        return ker_matrix
Beispiel #11
0
def margin(K,Y):
    """evaluate the margin in a classification problem of examples in feature space.
    If the classes are not linearly separable in feature space, then the
    margin obtained is 0.

    Note that it works only for binary tasks.

    Parameters
    ----------
    K : (n,n) ndarray,
        the kernel that represents the data.
    Y : (n) array_like,
        the labels vector.
    """
    K, Y = check_K_Y(K,Y,binary=True)
    n = Y.shape[0]
    Y = [1 if y==Y[0] else -1 for y in Y]
    YY = spdiag(Y)
    P = 2*(YY*matrix(K)*YY)
    p = matrix([0.0]*n)
    G = -spdiag([1.0]*n)
    h = matrix([0.0]*n)
    A = matrix([[1.0 if Y[i]==+1 else 0 for i in range(n)],
                [1.0 if Y[j]==-1 else 0 for j in range(n)]]).T
    b = matrix([[1.0],[1.0]],(2,1))
    solvers.options['show_progress']=False
    sol = solvers.qp(P,p,G,h,A,b)
    return np.sqrt(sol['primal objective'])
Beispiel #12
0
    def _combine_kernels(self):
        assert len(np.unique(self.Y)) == 2
        Y = [1 if y == self.classes_[1] else -1 for y in self.Y]
        n_sample = len(self.Y)
        ker_matrix = matrix(self.func_form(self.KL))
        YY = spdiag(Y)
        #KLL = (1.0-self.lam)*YY*ker_matrix*YY
        #LID = spdiag([self.lam]*n_sample)
        #Q = 2*(KLL+LID)
        Q = 2 * ((1.0 - self.lam) * YY * ker_matrix * YY +
                 spdiag([self.lam] * n_sample))
        p = matrix([0.0] * n_sample)
        G = -spdiag([1.0] * n_sample)
        h = matrix([0.0] * n_sample, (n_sample, 1))
        A = matrix([[1.0 if lab == +1 else 0 for lab in Y],
                    [1.0 if lab2 == -1 else 0 for lab2 in Y]]).T
        b = matrix([[1.0], [1.0]], (2, 1))

        solvers.options['show_progress'] = False
        solvers.options['maxiters'] = 200
        sol = solvers.qp(Q, p, G, h, A, b)
        gamma = sol['x']

        yg = gamma.T * YY
        weights = [(yg * matrix(K) * yg.T)[0] for K in self.KL]

        norm2 = sum([w for w in weights])

        weights = np.array([w / norm2 for w in weights])
        ker_matrix = self.func_form(self.KL, weights)
        return Solution(
            weights=weights,
            objective=None,
            ker_matrix=ker_matrix,
        )
Beispiel #13
0
	def train(self, test_users=None):
		self.model = {}
		iset = set(range(self.n_items))
		
		if test_users is None:
			test_users = range(self.n_users)
		
		for i, u in enumerate(test_users):
			#if (i+1) % 100 == 0:
			print "%d/%d" %(i+1, len(test_users))

			Xp_set = self.data.get_items(u)
			Xn_set = iset - Xp_set

			Z = co.spdiag([1.0 if i in Xp_set else -1.0 for i in iset])
			K = 2 * (Z * self.X.T * self.X * Z)
			I = co.spdiag([self.lambda_p if i in Xp_set else self.lambda_n for i in iset])
			P = K + I

			o = utc.zeroes_vec(self.n_items)
			G = -utc.identity(self.n_items)
			A = co.matrix([[1.0 if i in Xp_set else 0.0 for i in iset],
						   [1.0 if j in Xn_set else 0.0 for j in iset]]).T
			b = co.matrix([1.0, 1.0])
			P = co.sparse(P)

			solver.options['show_progress'] = False
			sol = solver.qp(P, o, G, o, A, b)
			self.sol[u] = sol
			self.model[u] = self.X.T * self.X * Z * sol['x']

		# endfor
		return self
Beispiel #14
0
    def build_gy(self, dae):
        """Build line Jacobian matrix"""
        if not self.n:
            idx = range(dae.m)
            dae.set_jac(Gy, 1e-6, idx, idx)
            return

        Vn = polar(1.0, dae.y[self.a])
        Vc = mul(dae.y[self.v], Vn)
        Ic = self.Y * Vc

        diagVn = spdiag(Vn)
        diagVc = spdiag(Vc)
        diagIc = spdiag(Ic)

        dS = self.Y * diagVn
        dS = diagVc * conj(dS)
        dS += conj(diagIc) * diagVn

        dR = diagIc
        dR -= self.Y * diagVc
        dR = diagVc.H.T * dR

        self.gy_store = sparse([[dR.imag(), dR.real()], [dS.real(),
                                                         dS.imag()]])
        # rebuild = False

        return sparse(self.gy_store)
Beispiel #15
0
def optimize(obj):
    # constraints
    # The equality constraint ensures the required amount of energy is delivered
    A1 = matrix(0.0, (n, t * n))
    A2 = matrix(0.0, (n, t * n))
    b = matrix(0.0, (2 * n, 1))

    for j in range(0, n):
        b[j] = float(results[j][1])
        for i in range(0, t):
            A1[n * (t * j + i) + j] = float(timeInterval) / 60  # kWh -> kW
            if i < results[j][0] or i > results[j][2]:
                A2[n * (t * j + i) + j] = 1.0

    A = sparse([A1, A2])

    A3 = spdiag([-1] * (t * n))
    A4 = spdiag([1] * (t * n))

    # The inequality constraint ensures powers are positive and below a maximum
    G = sparse([A3, A4])

    h = []
    for i in range(0, 2 * t * n):
        if i < t * n:
            h.append(0.0)
        else:
            h.append(pMax)
    h = matrix(h)

    # objective

    if obj == 1:
        q = []
        for i in range(0, n):
            for j in range(0, len(baseLoad)):
                q.append(baseLoad[j])

        q = matrix(q)
    elif obj == 2:
        q = matrix([0.0] * (t * n))

    if obj == 3:
        q = []
        for i in range(0, n):
            for j in range(0, len(baseLoad)):
                q.append(baseLoad[j] - pv[j])

        q = matrix(q)

    if obj == 1 or obj == 2 or obj == 3:
        I = spdiag([1] * t)
        P = sparse([[I] * n] * n)

    sol = solvers.qp(P, q, G, h, A, b)
    X = sol['x']

    return X
Beispiel #16
0
    def arrange_kernel(self, X, Y, check=True):
        if check:
            self._input_checks(X, Y)

        #if len(self.classes_) != 2:
        #    raise ValueError("The number of classes has to be 2; got ", len(self.classes_))

        self.Y = [1 if y == self.classes_[1] else -1 for y in self.Y]
        n_sample = len(self.Y)
        ker_matrix = matrix(summation(self.K))
        YY = spdiag(self.Y)
        KLL = (1.0 - self.lam) * YY * ker_matrix * YY
        #LID = matrix(np.diag([self.lam]*n_sample))
        LID = spdiag([self.lam] * n_sample)
        Q = 2 * (KLL + LID)
        p = matrix([0.0] * n_sample)
        G = -spdiag([1.0] * n_sample)
        h = matrix([0.0] * n_sample, (n_sample, 1))
        A = matrix([[1.0 if lab == +1 else 0 for lab in self.Y],
                    [1.0 if lab2 == -1 else 0 for lab2 in self.Y]]).T
        b = matrix([[1.0], [1.0]], (2, 1))

        solvers.options['show_progress'] = False
        solvers.options['maxiters'] = self.max_iter
        sol = solvers.qp(Q, p, G, h, A, b)
        self.gamma = sol['x']
        if self.verbose:
            print('[EasyMKL] - first step')
            print('optimization finished, #iter = ', sol['iterations'])
            print('status of the solution: ', sol['status'])
            print('objval: ', sol['primal objective'])

        # Bias for classification:
        #bias = 0.5 * self.gamma.T * ker_matrix * YY * self.gamma
        #self.bias = bias

        # Weights evaluation:
        #yg =  mul(self.gamma.T,matrix(self.Y).T)
        yg = self.gamma.T * YY
        self.weights = []
        for kermat in self.K:
            b = yg * matrix(kermat) * yg.T
            self.weights.append(b[0])

        norm2 = sum([w for w in self.weights])
        #norm2 = sum(self.weights)
        self.weights = [w / norm2 for w in self.weights]

        if self.tracenorm:
            for idx, val in enumerate(self.traces):
                self.weights[idx] = self.weights[idx] / val

        ker_matrix = summation(self.K, self.weights)
        self.ker_matrix = ker_matrix
        return ker_matrix
Beispiel #17
0
    def compute_p(self, mu_r, mu_psi):
        """
         min { 1/2 c.T * P * c + q * c }
        Build P matrix
        """
        p = []
        polynomial_r = np.ones(self.order + 1)
        for i in range(0, self.k_r):
            polynomial_r = np.polyder(polynomial_r)

        polynomial_psi = np.ones(self.order + 1)
        for i in range(0, self.k_psi):
            polynomial_psi = np.polyder(polynomial_psi)

        for i in range(0, self.m):
            p_x = np.zeros((self.order + 1, self.order + 1))
            p_y = np.zeros((self.order + 1, self.order + 1))
            p_z = np.zeros((self.order + 1, self.order + 1))
            p_psi = np.zeros((self.order + 1, self.order + 1))
            for j in range(0, self.order + 1):
                for k in range(j, self.order + 1):
                    # position
                    if j <= len(polynomial_r) - 1 and k <= len(polynomial_r) - 1:
                        order_t_r = ((self.order - self.k_r - j) + (self.order - self.k_r - k))
                        if j == k:
                            p_x[j, k] = np.power(polynomial_r[j], 2) / (order_t_r + 1)
                            p_y[j, k] = np.power(polynomial_r[j], 2) / (order_t_r + 1)
                            p_z[j, k] = np.power(polynomial_r[j], 2) / (order_t_r + 1)

                        else:
                            p_x[j, k] = 2 * polynomial_r[j] * polynomial_r[k] / (order_t_r + 1)
                            p_y[j, k] = 2 * polynomial_r[j] * polynomial_r[k] / (order_t_r + 1)
                            p_z[j, k] = 2 * polynomial_r[j] * polynomial_r[k] / (order_t_r + 1)

                    # yaw
                    if j <= len(polynomial_psi) - 1 and k <= len(polynomial_psi) - 1:
                        order_t_psi = ((self.order - self.k_psi - j) + (self.order - self.k_psi - k))
                        if j == k:
                            p_psi[j, k] = np.power(polynomial_psi[j], 2) / (order_t_psi + 1)
                        else:
                            p_psi[j, k] = 2 * polynomial_psi[j] * polynomial_psi[k] / (order_t_psi + 1)
            p_x = matrix(p_x) * mu_r
            p_y = matrix(p_y) * mu_r
            p_z = matrix(p_z) * mu_r
            p_psi = matrix(p_psi) * mu_psi
            if i == 0:
                p = spdiag([p_x, p_y, p_z, p_psi])
            else:
                p = spdiag([p, p_x, p_y, p_z, p_psi])

        p = matrix(p)
        p = p + matrix(np.transpose(p))
        p = p * 0.5
        return p
Beispiel #18
0
def d2Ibr_dV2(Ybr, V, lam):
    """ Computes 2nd derivatives of complex branch current w.r.t. voltage.
    """
    nb = len(V)
    diaginvVm = spdiag(div(matrix(1.0, (nb, 1)), abs(V)))

    Haa = spdiag(mul(-(Ybr.T * lam), V))
    Hva = -1j * Haa * diaginvVm
    Hav = Hva
    Hvv = spmatrix([], [], [], (nb, nb))

    return Haa, Hav, Hva, Hvv
Beispiel #19
0
def d2Ibr_dV2(Ybr, V, lam):
    """ Computes 2nd derivatives of complex branch current w.r.t. voltage.
    """
    nb = len(V)
    diaginvVm = spdiag(div(matrix(1.0, (nb, 1)), abs(V)))

    Haa = spdiag(mul(-(Ybr.T * lam), V))
    Hva = -1j * Haa * diaginvVm
    Hav = Hva
    Hvv = spmatrix([], [], [], (nb, nb))

    return Haa, Hav, Hva, Hvv
Beispiel #20
0
 def F(x=None, z=None):
     # x = (t1, t2)
     # t1 - log(t2) <= 0
     if x is None: return m, cvxopt.matrix(m*[0.0] + m*[1.0])
     if min(x[m:]) <= 0.0: return None
     f = x[0:m] - cvxopt.log(x[m:])
     Df = cvxopt.sparse([[cvxopt.spdiag(cvxopt.matrix(1.0, (m,1)))], [cvxopt.spdiag(-(x[m:]**-1))]])
     if z is None: return f, Df
     ret = cvxopt.mul(z, x[m:]**-2)
     # TODO: add regularization for the Hessian?
     H = cvxopt.spdiag(cvxopt.matrix([cvxopt.matrix(0, (m,1)), ret]))
     return f, Df, H
Beispiel #21
0
def margin(K,Y):
    n = Y.shape[0]
    YY = spdiag(list(Y))
    P = 2*(YY*matrix(K)*YY)
    p = matrix([0.0]*n)
    G = -spdiag([1.0]*n)
    h = matrix([0.0]*n)
    A = matrix([[1.0 if Y[i]==+1 else 0 for i in range(n)],
                [1.0 if Y[j]==-1 else 0 for j in range(n)]]).T
    b = matrix([[1.0],[1.0]],(2,1))
    solvers.options['show_progress']=False
    sol = solvers.qp(P,p,G,h,A,b)
    return np.sqrt(sol['primal objective'])/2.0#prendo la distanza dall'iperpiano
Beispiel #22
0
def radius(K, lam=0, init_sol=None):
    n = K.shape[0]
    K = matrix(K)
    P = 2 * ((1 - lam) * K + spdiag([lam] * n))
    p = -matrix([K[i, i] for i in range(n)])
    G = -spdiag([1.0] * n)
    h = matrix([0.0] * n)
    A = matrix([1.0] * n).T
    b = matrix([1.0])
    solvers.options['show_progress'] = False
    sol = solvers.qp(P, p, G, h, A, b, initvals=init_sol)
    radius2 = (-p.T * sol['x'])[0] - (sol['x'].T * K * sol['x'])[0]
    return sol, radius2
Beispiel #23
0
def d2ASbr_dV2(dSbr_dVa, dSbr_dVm, Sbr, Cbr, Ybr, V, lam):
    """ Computes 2nd derivatives of |complex power flow|**2 w.r.t. V.
    """
    diaglam = spdiag(lam)
    diagSbr_conj = spdiag(conj(Sbr))

    Saa, Sav, Sva, Svv = d2Sbr_dV2(Cbr, Ybr, V, diagSbr_conj * lam)

    Haa = 2 * ( Saa + dSbr_dVa.T * diaglam * conj(dSbr_dVa) ).real()
    Hva = 2 * ( Sva + dSbr_dVm.T * diaglam * conj(dSbr_dVa) ).real()
    Hav = 2 * ( Sav + dSbr_dVa.T * diaglam * conj(dSbr_dVm) ).real()
    Hvv = 2 * ( Svv + dSbr_dVm.T * diaglam * conj(dSbr_dVm) ).real()

    return Haa, Hav, Hva, Hvv
Beispiel #24
0
def d2AIbr_dV2(dIbr_dVa, dIbr_dVm, Ibr, Ybr, V, lam):
    """ Computes 2nd derivatives of |complex current|**2 w.r.t. V.
    """
    diaglam = spdiag(lam)
    diagIbr_conj = spdiag(conj(Ibr))

    Iaa, Iav, Iva, Ivv = d2Ibr_dV2(Ybr, V, diagIbr_conj * lam)

    Haa = 2 * ( Iaa + dIbr_dVa.T * diaglam * conj(dIbr_dVa) ).real()
    Hva = 2 * ( Iva + dIbr_dVm.T * diaglam * conj(dIbr_dVa) ).real()
    Hav = 2 * ( Iav + dIbr_dVa.T * diaglam * conj(dIbr_dVm) ).real()
    Hvv = 2 * ( Ivv + dIbr_dVm.T * diaglam * conj(dIbr_dVm) ).real()

    return Haa, Hav, Hva, Hvv
Beispiel #25
0
def d2AIbr_dV2(dIbr_dVa, dIbr_dVm, Ibr, Ybr, V, lam):
    """ Computes 2nd derivatives of |complex current|**2 w.r.t. V.
    """
    diaglam = spdiag(lam)
    diagIbr_conj = spdiag(conj(Ibr))

    Iaa, Iav, Iva, Ivv = d2Ibr_dV2(Ybr, V, diagIbr_conj * lam)

    Haa = 2 * (Iaa + dIbr_dVa.T * diaglam * conj(dIbr_dVa)).real()
    Hva = 2 * (Iva + dIbr_dVm.T * diaglam * conj(dIbr_dVa)).real()
    Hav = 2 * (Iav + dIbr_dVa.T * diaglam * conj(dIbr_dVm)).real()
    Hvv = 2 * (Ivv + dIbr_dVm.T * diaglam * conj(dIbr_dVm)).real()

    return Haa, Hav, Hva, Hvv
Beispiel #26
0
def d2ASbr_dV2(dSbr_dVa, dSbr_dVm, Sbr, Cbr, Ybr, V, lam):
    """ Computes 2nd derivatives of |complex power flow|**2 w.r.t. V.
    """
    diaglam = spdiag(lam)
    diagSbr_conj = spdiag(conj(Sbr))

    Saa, Sav, Sva, Svv = d2Sbr_dV2(Cbr, Ybr, V, diagSbr_conj * lam)

    Haa = 2 * (Saa + dSbr_dVa.T * diaglam * conj(dSbr_dVa)).real()
    Hva = 2 * (Sva + dSbr_dVm.T * diaglam * conj(dSbr_dVa)).real()
    Hav = 2 * (Sav + dSbr_dVa.T * diaglam * conj(dSbr_dVm)).real()
    Hvv = 2 * (Svv + dSbr_dVm.T * diaglam * conj(dSbr_dVm)).real()

    return Haa, Hav, Hva, Hvv
Beispiel #27
0
def margin(K, Y, init_sol=None):
    n = len(Y)
    YY = spdiag(list(Y))
    P = 2 * (YY * matrix(K) * YY)
    p = matrix([0.0] * n)
    G = -spdiag([1.0] * n)
    h = matrix([0.0] * n)
    A = matrix([[1.0 if Y[i] == +1 else 0 for i in range(n)],
                [1.0 if Y[j] == -1 else 0 for j in range(n)]]).T
    b = matrix([[1.0], [1.0]], (2, 1))
    solvers.options['show_progress'] = False
    sol = solvers.qp(P, p, G, h, A, b, initval=init_sol,
                     solver='glpk') if init_sol else solvers.qp(
                         P, p, G, h, A, b)
    return np.sqrt(sol['primal objective']), sol['x']
Beispiel #28
0
def penaltyQuadraticModel():
    Q = P.T + Z.T * Z + C.T * C
    p = U - 2 * Z.T * z - 2 * C.T * c

    #Variables are between zero and one
    G = sparse([
        spdiag([-1 for r in range(D.numVars)]),
        spdiag([1 for r in range(D.numVars)])
    ])
    h = matrix([[0.0] * D.numVars, [1.0] * D.numVars], (2 * D.numVars, 1), 'd')

    A = None
    b = None

    return (Q, p, G, h, A, b)
Beispiel #29
0
    def build_path_qp(self, s, mode=DELAY_MIN):
        q = [self.wire_cap / (self.gate_res * s[i]) + self.wire_res * self.gate_cap * s[i+1] \
             for i in xrange(self.n - 1)]
        q = cvx.matrix(q)

        p = [0.5 * self.wire_res * self.wire_cap for i in xrange(self.n - 1)]
        P = cvx.spdiag(p)

        G = -cvx.spdiag([1. for i in xrange(self.n - 1)])
        h = cvx.matrix([0. for i in xrange(self.n - 1)])

        A = cvx.matrix([1. for i in xrange(self.n - 1)]).T
        b = cvx.matrix([self.length])

        return P, q, G, h, A, b
Beispiel #30
0
def margin(K, YY, lam=0, init_sol=None):
    n = K.shape[0]
    K = matrix(K)
    lambdaDiag = spdiag([lam] * n)
    P = 2 * ((1 - lam) * (YY * K * YY) + lambdaDiag)
    p = matrix([0.0] * n)
    G = -spdiag([1.0] * n)
    h = matrix([0.0] * n)
    A = matrix([[1.0 if YY[i, i] == +1 else 0 for i in range(n)],
                [1.0 if YY[j, j] == -1 else 0 for j in range(n)]]).T
    b = matrix([[1.0], [1.0]], (2, 1))
    solvers.options['show_progress'] = False
    sol = solvers.qp(P, p, G, h, A, b, initvals=init_sol)
    margin2 = sol['dual objective'] - (sol['x'].T * lambdaDiag * sol['x'])[0]
    return sol, margin2
Beispiel #31
0
def constrainedQuadraticModel():
    Q = P.T
    p = U

    #Variables are between zero and one
    G = sparse([
        spdiag([-1 for r in range(D.numVars)]),
        spdiag([1 for r in range(D.numVars)])
    ])
    h = matrix([[0.0] * D.numVars, [1.0] * D.numVars], (2 * D.numVars, 1), 'd')

    A = extendSparse(Z, C, D.numVars)
    b = extendMatrix(z, c)

    return (Q, p, G, h, A, b)
Beispiel #32
0
    def kkt_solver(W):

        # Returns a function f(x, y, z) that solves
        #
        # [ 0  0  P'      -P'      ] [ x[:n] ]   [ bx[:n] ]
        # [ 0  0 -I       -I       ] [ x[n:] ]   [ bx[n:] ]
        # [ P -I -W1^2     0       ] [ z[:m] ] = [ bz[:m] ]
        # [-P -I  0       -W2      ] [ z[m:] ]   [ bz[m:] ]
        #
        # On entry bx, bz are stored in x, z.
        # On exit x, z contain the solution, with z scaled (W['di'] .* z is
        # returned instead of z). 

        d1, d2 = W['d'][:m], W['d'][m:]
        D = 4*(d1**2 + d2**2)**-1
        A = P.T * cvxopt.spdiag(D) * P
        cvxopt.lapack.potrf(A)

        def func(x, y_unused, z):
            #Ok, that y is not used
            x[:n] += P.T * (cvxopt.mul(cvxopt.div(d2**2 - d1**2, d1**2 + d2**2), x[n:])
                            + cvxopt.mul(.5*D, z[:m]-z[m:]))
            cvxopt.lapack.potrs(A, x)

            u = P*x[:n]
            x[n:] = cvxopt.div(x[n:] - cvxopt.div(z[:m], d1**2) - cvxopt.div(z[m:], d2**2) +
                cvxopt.mul(d1**-2 - d2**-2, u), d1**-2 + d2**-2)

            z[:m] = cvxopt.div(u-x[n:]-z[:m], d1)
            z[m:] = cvxopt.div(-u-x[n:]-z[m:], d2)

        return func
Beispiel #33
0
def radius(K):
    """evaluate the radius of the MEB (Minimum Enclosing Ball) of examples in
    feature space.

    Parameters
    ----------
    K : (n,n) ndarray,
        the kernel that represents the data.

    Returns
    -------
    r : np.float64,
        the radius of the minimum enclosing ball of examples in feature space.
    """
    check_squared(K)
    n = K.shape[0]
    P = 2 * matrix(K)
    p = -matrix([K[i,i] for i in range(n)])
    G = -spdiag([1.0] * n)
    h = matrix([0.0] * n)
    A = matrix([1.0] * n).T
    b = matrix([1.0])
    solvers.options['show_progress']=False
    sol = solvers.qp(P,p,G,h,A,b)
    return np.sqrt(abs(sol['primal objective']))
Beispiel #34
0
def construct_const_matrix(x_dim, D):
    # --------------------------
    #| 0   0
    #| 0   I
    #|        D-eps_I    0
    #|         0        D^{-1}
    #|                         I  0
    #|                         0  I
    #|                              0
    # --------------------------
    # Construct B1
    H1 = zeros((2 * x_dim, 2 * x_dim))
    H1[x_dim:, x_dim:] = eye(x_dim)
    H1 = matrix(H1)

    # Construct B2
    eps = 1e-4
    H2 = zeros((2 * x_dim, 2 * x_dim))
    H2[:x_dim, :x_dim] = D - eps * D
    H2[x_dim:, x_dim:] = pinv(D)
    H2 = matrix(H2)

    # Construct B3
    H3 = eye(2 * x_dim)
    H3 = matrix(H3)

    # Construct B5
    H4 = zeros((x_dim, x_dim))
    H4 = matrix(H4)

    # Construct Block matrix
    H = spdiag([H1, H2, H3, H4])
    hs = [H]
    return hs
Beispiel #35
0
    def make_initial_solution(self, orig_q_0=None):
        # make initial solution q_0
        self.p_0 = dict()
        if orig_q_0==None: # no initial solution provided. I'm gonna get some "minimum entropy" thing
            if self.expensive_initial_solution:
                # initial solution is the one minimizing || p ||^2
                N = len(self.var_idx)

                Q = 2*spdiag( matrix( 1., (N,1), 'd' ) )
                q = matrix( 0., (N,1), 'd' )

                qp_solver = solvers.qp(Q, q, self.G, self.h, self.A, self.b)

                # for xyz,i in self.var_idx.items():
                #     self.p_0[xyz] = max(0, qp_solver['x'][i] )
                print("Min 2-norm: ",list( qp_solver['x'] ))
            else:
                for xyz in self.var_idx.keys():
                    self.p_0[xyz] = 1.
        else:
            for xyz in self.var_idx.keys():
                if xyz in orig_q_0:
                    self.p_0[xyz] = orig_q_0[xyz]
                else:
                    self.p_0[xyz] = 0.
    def constraints(self):
        # construct the constraints for the attack routing problem
        N = self.N
        u = np.tile(range(N), N)
        v = np.repeat(range(N),N)
        w = np.array(range(N*N))
        # build constraint matrix
        A1 = spmatrix(np.repeat(self.nu, N), u, w, (N, N*N))
        A2 = -spmatrix(np.repeat(self.nu + self.phi, N), v, w, (N, N*N))

        I = np.array(range(N))
        J = I + np.array(range(N)) * N
        A3 = spmatrix(self.phi, I, J, (N, N*N))

        tmp = np.dot(np.diag(self.phi), self.delta).transpose()
        A4 = matrix(np.repeat(tmp, N, axis=1))
        A5 = -spmatrix(tmp.flatten(), v, np.tile(J, N), (N, N*N))

        A6 = A1 + A2 + A3 + A4 + A5

        I = np.array([0]*(N-1))
        J = np.array(range(self.k)+range((self.k+1),N)) + N * self.k
        A7 = spmatrix(1., I, J, (1, N*N))
        A = sparse([[A6, -A6, A7, -A7, -spdiag([1.]*(N*N))]])

        tmp = np.zeros(2*N + 2 + N*N)
        tmp[2*N] = 1.
        tmp[2*N + 1] = -1.
        b = matrix(tmp)
        return b, A
Beispiel #37
0
def solver(graph=None, update=False, full=False, data=None, SO=False):
    """Find the UE link flow
    
    Parameters
    ----------
    graph: graph object
    update: if update==True: update link flows and link,path delays in graph
    full: if full=True, also return x (link flows per OD pair)
    data: (Aeq, beq, ffdelays, parameters, type) from get_data(graph)
    """
    if data is None: data = get_data(graph)
    Aeq, beq, ffdelays, pm, type = data
    n = len(ffdelays)
    p = Aeq.size[1]/n
    A, b = spmatrix(-1.0, range(p*n), range(p*n)), matrix(0.0, (p*n,1))
    if type == 'Polynomial':
        if not SO: pm = pm * spdiag([1.0/(j+2) for j in range(pm.size[1])])
        def F(x=None, z=None): return objective_poly(x, z, matrix([[ffdelays], [pm]]), p)
    if type == 'Hyperbolic':
        if SO:
            def F(x=None, z=None): return objective_hyper_SO(x, z, matrix([[ffdelays-div(pm[:,0],pm[:,1])], [pm]]), p)
        else:
            def F(x=None, z=None): return objective_hyper(x, z, matrix([[ffdelays-div(pm[:,0],pm[:,1])], [pm]]), p)
    dims = {'l': p*n, 'q': [], 's': []}
    x = solvers.cp(F, G=A, h=b, A=Aeq, b=beq, kktsolver=get_kktsolver(A, dims, Aeq, F))['x']
    linkflows = matrix(0.0, (n,1))
    for k in range(p): linkflows += x[k*n:(k+1)*n]
    
    if update:
        logging.debug('Update link flows, delays in Graph.'); graph.update_linkflows_linkdelays(linkflows)
        logging.debug('Update path delays in Graph.'); graph.update_pathdelays()
    
    if full: return linkflows, x    
    return linkflows
def inv_solver(xs, ps, xmax):
    """Optimization w.r.t. (Q,r)
    
    Parameters
    ----------
    xs: list of (optimal) demand vectors
    ps: list of price vectors
    xmax: maximum demand vector (for each product)
    """
    n, N = len(xs[0]), len(xs)
    # constraint Q <= 0
    G, h = conic_constraint(n)
    # constraint Q*xmax + r >= 0
    A = -linear_constraint(xmax)
    b = matrix(0., (n,1))
    # constraints Q*x^j + r <= p^j
    for j in range(N):
        A = matrix([A, linear_constraint(xs[j])])
        b = matrix([b, matrix(ps[j])])
    # constraint r >= 0
    tmp = matrix([[matrix(0., (n, (n*(n+1))/2))], [-spdiag([1.]*n)]])
    A = matrix([A, tmp])
    b = matrix([b, matrix(0., (n,1))])
    # linear objective
    c = linear_objective(xs)
    x = solvers.sdp(c, A, b, G, h)['x']
    Q, r = matrix(0., (n,n)), matrix(0., (n,1))
    for i in range(n):
        Q[i,i] = x[(n+1)*i-(i*(i+1))/2]
        r[i] = x[(n*(n+1))/2+i]
        for j in range(i+1,n):
            Q[i,j] = x[n*i+j-(i*(i+1))/2]; Q[j,i] = Q[i,j]
    return Q, r
    def constraints(self):
        # construct the constraints for the attack routing problem
        N = self.N
        u = np.tile(range(N), N)
        v = np.repeat(range(N), N)
        w = np.array(range(N * N))
        # build constraint matrix
        A1 = spmatrix(np.repeat(self.nu, N), u, w, (N, N * N))
        A2 = -spmatrix(np.repeat(self.nu + self.phi, N), v, w, (N, N * N))

        I = np.array(range(N))
        J = I + np.array(range(N)) * N
        A3 = spmatrix(self.phi, I, J, (N, N * N))

        tmp = np.dot(np.diag(self.phi), self.delta).transpose()
        A4 = matrix(np.repeat(tmp, N, axis=1))
        A5 = -spmatrix(tmp.flatten(), v, np.tile(J, N), (N, N * N))

        A6 = A1 + A2 + A3 + A4 + A5

        I = np.array([0] * (N - 1))
        J = np.array(range(self.k) + range((self.k + 1), N)) + N * self.k
        A7 = spmatrix(1., I, J, (1, N * N))
        A = sparse([[A6, -A6, A7, -A7, -spdiag([1.] * (N * N))]])

        tmp = np.zeros(2 * N + 2 + N * N)
        tmp[2 * N] = 1.
        tmp[2 * N + 1] = -1.
        b = matrix(tmp)
        return b, A
 def F(x = None, z = None):
     
     # Case 1
     if x is None and z is None:
         return (0, x_0)
     
     # Case 2 and 3
     else:
         w = x[:n]
         s = x[n:]
         abs_w = abs(w)
         f = scale * (sum(abs_w**q) / q + C * sum(s))
         Df_w = sign(w) * abs_w**(q - 1.0)
         Df_s = C * ones((m, 1))
         Df = scale * vstack((Df_w, Df_s))
         Df = matrix(Df.reshape((1, n + m)))
         
         # Case 2 only
         if z is None:
             return (f, Df)
         
         # Case 3 only
         else:
             try:
                 H_w = scale * z * (q - 1.0) * abs_w**(q - 2.0)
             except (ValueError, RuntimeWarning):
                 #print 'abs_w:', abs_w
                 #print 'power:', (q - 2.0)
                 H_w = scale * z * (q - 1.0) * (abs_w + 1e-20)**(q - 2.0)
             H_s = zeros((m, 1))
             diag_H = matrix(vstack((H_w, H_s)))
             H = spdiag(diag_H)
             return (f, Df, H)
Beispiel #41
0
def solve_fw_tap(A, b, times, caps, I, K, maxiter=100, tol = 0.001):
    h = matrix(0., (I*K, 1))
    G = spdiag([-1.]*I*K)
    block = build_block_matrix(I, K)
    reverse_block = reverse_block_matrix(I, K)
    #start iteration
    #flows = matrix(0.0, (I*K,1))
    #start with a normal lp
    times_expanded = reverse_block * times
    print 'computing first feasible flow'
    flows = solvers.lp(times_expanded, G, h, A = A, b = b, solver='mosek', options={"mosek":{iparam.log:0}})['x']
    for k in range(maxiter):
        print 'iter', k
        #compute gradient
        sum_flows = block * flows
        print 'flows', max(sum_flows),min(sum_flows),max(flows), min(flows)
        Df = vdf_derivative(times, caps, sum_flows)
        Df = reverse_block * Df
        print 'Df', max(Df), min(Df)
        #solve affineminimization subproblem -> xd
        lpsol  = solvers.lp(Df, G, h, A = A, b = b, solver='mosek', options={"mosek":{iparam.log:0}})
        xd = lpsol['x']
        print lpsol['status']
        if lpsol['status'] != 'optimal':
            return {'flows':flows, 'grad':Df, 'G':G, 'h':h}
        #update
        bound = (Df.T * (flows - xd))[0]
        step = (2. / (k + 2.)) * (xd - flows)
        print 'diff', np.sqrt(step.T * step)[0], bound
        if abs(bound) < tol:
            print "finished after " + str(k) + " iterations"
            break
        flows = flows + step

    return flows
def ty_solver(data, l, w_toll, full=False):
    """Solves the block (t,y) of the toll pricing model
    
    Parameters
    ----------
    data: Aeq, beq, ffdelays, coefs
    l: linkflows
    w_toll: weight on the toll collected
    full: if True, return the whole solution
    """
    Aeq, beq, ffdelays, coefs = data
    delays = compute_delays(l, ffdelays, coefs)
    n = len(l)
    p = Aeq.size[1]/n
    m = Aeq.size[0]/p
    c = matrix([(1.0+w_toll)*l, -beq])
    I = spdiag([1.0]*n)
    G1 = matrix([-I]*(p+1))
    G2 = matrix([Aeq.T, matrix(0.0, (n,p*m))])
    G = matrix([[G1],[G2]])
    h = [delays]*p
    h.append(matrix(0.0, (n,1)))
    h = matrix(h)
    if full: return solvers.lp(c,G,h)['x']
    return solvers.lp(c,G,h)['x'][range(n)]
Beispiel #43
0
    def Fkkt(W): 

        # Returns a function f(x, y, z) that solves
        #
        # [ 0  0  P'      -P'      ] [ x[:n] ]   [ bx[:n] ]
        # [ 0  0 -I       -I       ] [ x[n:] ]   [ bx[n:] ]
        # [ P -I -W1^2     0       ] [ z[:m] ] = [ bz[:m] ]
        # [-P -I  0       -W2      ] [ z[m:] ]   [ bz[m:] ]
        #
        # On entry bx, bz are stored in x, z.
        # On exit x, z contain the solution, with z scaled (W['di'] .* z is
        # returned instead of z). 

        d1, d2 = W['d'][:m], W['d'][m:]
        D = 4*(d1**2 + d2**2)**-1
        A = P.T * spdiag(D) * P
        lapack.potrf(A)

        def f(x, y, z):

            x[:n] += P.T * ( mul( div(d2**2 - d1**2, d1**2 + d2**2), x[n:]) 
                + mul( .5*D, z[:m]-z[m:] ) )
            lapack.potrs(A, x)

            u = P*x[:n]
            x[n:] =  div( x[n:] - div(z[:m], d1**2) - div(z[m:], d2**2) + 
                mul(d1**-2 - d2**-2, u), d1**-2 + d2**-2 )

            z[:m] = div(u-x[n:]-z[:m], d1)
            z[m:] = div(-u-x[n:]-z[m:], d2)

        return f
def compute_global_min_portfolio(mu_vec, sigma_mat, shorts=True):
    solvers.options['show_progress'] = False
    P = 2 * matrix(sigma_mat.values)
    q = matrix(0., (len(sigma_mat), 1))
    G = spdiag([-1. for i in range(len(sigma_mat))])
    A = matrix(1., (1, len(sigma_mat)))
    b = matrix(1.0)

    if shorts == True:
        h = matrix(1., (len(sigma_mat), 1))
    else:
        h = matrix(0., (len(sigma_mat), 1))

    # print ('\nP\n\n{}\n\nq\n\n{}\n\nG\n\n{}\n\nh\n\n{}\n\nA\n\n{}\n\nb\n\n{}\n\n'.format(P,q,G,h,A,b))
    # weights_vec = pd.DataFrame(np.array(solvers.qp(P, q, G, h, A, b)['x']),\
    #                                     index=sigma_mat.columns)
    weights_vec = pd.Series(list(solvers.qp(P, q, G, h, A, b)['x']), index=sigma_mat.columns)

    #
    # compute portfolio expected returns and variance
    #
    # print ('*** Debug ***\n_Global Min Portfolio:\nmu_vec:\n', mu_vec, '\nsigma_mat:\n',
    #        sigma_mat, '\nweights:\n', weights_vec)

    mu_p = compute_portfolio_mu(mu_vec, weights_vec)
    sigma_p = compute_portfolio_sigma(sigma_mat, weights_vec)

    return weights_vec, mu_p, sigma_p
Beispiel #45
0
    def calc_state_matrix(self):
        r"""
        Return state matrix and store to ``self.As``.

        Notes
        -----
        For systems with the form

        .. math ::

            T \dot{x} = f(x, y) \\
            0 = g(x, y)

        The state matrix is calculated from

        .. math ::

            A_s = T^{-1} (f_x - f_y * g_y^{-1} * g_x)

        Returns
        -------
        cvxopt.matrix
            state matrix
        """
        system = self.system

        gyx = matrix(system.dae.gx)
        self.solver.linsolve(system.dae.gy, gyx)

        Tfnz = system.dae.Tf + np.ones_like(system.dae.Tf) * np.equal(
            system.dae.Tf, 0.0)
        iTf = spdiag((1 / Tfnz).tolist())
        self.As = matrix(iTf * (system.dae.fx - system.dae.fy * gyx))
        return self.As
Beispiel #46
0
    def Fkkt(W):

        # Returns a function f(x, y, z) that solves
        #
        # [ 0  0  P'      -P'      ] [ x[:n] ]   [ bx[:n] ]
        # [ 0  0 -I       -I       ] [ x[n:] ]   [ bx[n:] ]
        # [ P -I -W1^2     0       ] [ z[:m] ] = [ bz[:m] ]
        # [-P -I  0       -W2      ] [ z[m:] ]   [ bz[m:] ]
        #
        # On entry bx, bz are stored in x, z.
        # On exit x, z contain the solution, with z scaled (W['di'] .* z is
        # returned instead of z).

        d1, d2 = W['d'][:m], W['d'][m:]
        D = 4 * (d1**2 + d2**2)**-1
        A = P.T * spdiag(D) * P
        lapack.potrf(A)

        def f(x, y, z):

            x[:n] += P.T * (mul(div(d2**2 - d1**2, d1**2 + d2**2), x[n:]) +
                            mul(.5 * D, z[:m] - z[m:]))
            lapack.potrs(A, x)

            u = P * x[:n]
            x[n:] = div(
                x[n:] - div(z[:m], d1**2) - div(z[m:], d2**2) +
                mul(d1**-2 - d2**-2, u), d1**-2 + d2**-2)

            z[:m] = div(u - x[n:] - z[:m], d1)
            z[m:] = div(-u - x[n:] - z[m:], d2)

        return f
Beispiel #47
0
def radius(K):
    """evaluate the radius of the MEB (Minimum Enclosing Ball) of examples in
    feature space.

    Parameters
    ----------
    K : (n,n) ndarray,
        the kernel that represents the data.

    Returns
    -------
    r : np.float64,
        the radius of the minimum enclosing ball of examples in feature space.
    """
    check_squared(K)
    n = K.shape[0]
    P = 2 * matrix(K)
    p = -matrix([K[i,i] for i in range(n)])
    G = -spdiag([1.0] * n)
    h = matrix([0.0] * n)
    A = matrix([1.0] * n).T
    b = matrix([1.0])
    solvers.options['show_progress']=False
    sol = solvers.qp(P,p,G,h,A,b)
    return np.sqrt(abs(sol['primal objective']))
Beispiel #48
0
    def reset_Ac(self):
        """
        Reset ``dae.Ac`` sparse matrix for disabled equations
        due to hard_limit and anti_windup limiters.

        :return: None
        """
        if self.ac_reset is False:
            return

        mn = self.m + self.n

        x = index(aandb(self.zxmin, self.zxmax), 0.)
        y = [i + self.n for i in index(aandb(self.zymin, self.zymax), 0.)]
        xy = list(x) + y

        eye = spdiag([1.0] * mn)
        H = spmatrix(1.0, xy, xy, (mn, mn), 'd')

        # Modifying ``eye`` is more efficient than ``eye = eye - H``.
        # CVXOPT modifies eye in place because all the accessed elements exist.

        for idx in xy:
            eye[idx, idx] = 0

        if len(xy) > 0:
            self.Ac = eye * (self.Ac * eye) - H
            self.q[x] = 0

        self.ac_reset = False
        self.factorize = True
Beispiel #49
0
def objective_hyper(x, z, ks, p):
    """Objective function of UE program with hyperbolic delay functions
    f(x) = sum_i f_i(v_i) with v = sum_w x_w
    f_i(u) = ks[i,0]*u - ks[i,1]*log(ks[i,2]-u)
    
    Parameters
    ----------
    x,z: variables for the F(x,z) function for cvxopt.solvers.cp
    ks: matrix of size (n,3) 
    p: number of destinations
    (we use multiple-sources single-sink node-arc formulation)
    """
    n = ks.size[0]
    if x is None: return 0, matrix(1.0/p, (p*n,1))
    l = matrix(0.0, (n,1))
    for k in range(p): l += x[k*n:(k+1)*n]
    f, Df, H = 0.0, matrix(0.0, (1,n)), matrix(0.0, (n,1))
    for i in range(n):
        tmp = 1.0/(ks[i,2]-l[i])
        f += ks[i,0]*l[i] - ks[i,1]*np.log(max(ks[i,2]-l[i], 1e-13))
        Df[i] = ks[i,0] + ks[i,1]*tmp
        H[i] = ks[i,1]*tmp**2
    Df = matrix([[Df]]*p)
    if z is None: return f, Df
    return f, Df, sparse([[spdiag(z[0] * H)]*p]*p)
Beispiel #50
0
def objective_hyper_SO(x, z, ks, p):
    """Objective function of SO program with hyperbolic delay functions
    f(x) = \sum_i f_i(v_i) with v = sum_w x_w
    f_i(u) = ks[i,0]*u + ks[i,1]*u/(ks[i,2]-u)
    
    Parameters
    ----------
    x,z: variables for the F(x,z) function for cvxopt.solvers.cp
    ks: matrix of size (n,3) where ks[i,j] is the j-th parameter of the delay on link i
    p: number of destinations
    (we use multiple-sources single-sink node-arc formulation)
    """
    n = ks.size[0]
    if x is None: return 0, matrix(1.0/p, (p*n,1))
    l = matrix(0.0, (n,1))
    for k in range(p): l += x[k*n:(k+1)*n]
    f, Df, H = 0.0, matrix(0.0, (1,n)), matrix(0.0, (n,1))
    for i in range(n):
        tmp = 1.0/(ks[i,2]-l[i])
        f += ks[i,0]*l[i] + ks[i,1]*l[i]*tmp
        Df[i] = ks[i,0] + ks[i,1]*tmp + ks[i,1]*l[i]*tmp**2
        H[i] = 2.0*ks[i,1]*tmp**2 + 2.0*ks[i,1]*l[i]*tmp**3
    Df = matrix([[Df]]*p)
    if z is None: return f, Df
    return f, Df, matrix([[spdiag(z[0] * H)]*p]*p)
def build_new_weights(ini_pop_desc,
                      future_pop_desc,
                      characteristics,
                      ini_weights,
                      ismosek=True):
    '''optimize new weights to match current households and new population description'''
    t_tilde = cvxopt.matrix(
        (future_pop_desc.values - ini_pop_desc.values).astype(np.float,
                                                              copy=False))
    aa = cvxopt.matrix(characteristics.values.astype(np.float, copy=False))
    w1 = 1 / (ini_weights.values)**2
    n = len(w1)
    P = cvxopt.spdiag(cvxopt.matrix(w1))
    G = -cvxopt.matrix(np.identity(n))
    h = cvxopt.matrix(ini_weights.values.astype(np.float, copy=False))
    q = cvxopt.matrix(0.0, (n, 1))
    if ismosek:
        result = qp(P, q, G, h, aa.T, t_tilde.T, solver='mosek')['x']
    else:
        result = qp(P, q, G, h, aa.T, t_tilde.T)['x']
    if result is None:
        new_weights = 0 * ini_weights
    else:
        new_weights = ini_weights + list(result)
    return new_weights
Beispiel #52
0
 def F(x=None, z=None):
     if x is None:  return 0, matrix(1.0, (n,1))
     if min(x) <= 0.0:  return None
     f = -sum(log(x))
     Df = -(x**-1).T 
     if z is None: return matrix(f), Df
     H = spdiag(z[0] * x**-2)
     return f, Df, H
Beispiel #53
0
def F(x=None, z=None):
   if x is None: return 0, matrix(0.0, (n+1,1))
   w = exp(A*x)
   f = dot(c,x) + sum(log(1+w)) 
   grad = c + A.T * div(w, 1+w)  
   if z is None: return matrix(f), grad.T
   H = A.T * spdiag(div(w,(1+w)**2)) * A
   return matrix(f), grad.T, z[0]*H 
Beispiel #54
0
 def F(x=None, z=None):
     if x is None: return 0, cvxopt.matrix(1.0, (n,1))
     if min(x) <= 0.0: return None
     f = -sum(cvxopt.log(x))
     Df = -(x**-1).T
     if z is None: return f, Df
     H = cvxopt.spdiag(z[0] * x**-2)
     return f, Df, H
Beispiel #55
0
def F(x=None, z=None):
   if x is None: return 0, matrix(0.0, (2,1))
   w = exp(A*x)
   f = c.T*x + sum(log(1+w))
   grad = c + A.T * div(w, 1+w)  
   if z is None: return f, grad.T
   H = A.T * spdiag(div(w,(1+w)**2)) * A
   return f, grad.T, z[0]*H 
def optimalWeightsMultipleModelsFixedProfile(losses, averageWeights, eta, rowSums=None, initWeights=None):
    "Min. W'L s.t. each row w_j of W is a distribution with sum rowSums[j], and the mean of all rows is averageWeights."
    k, n = losses.shape
    rowSums = ones(k) if rowSums is None else rowSums
    if initWeights is not None:
        initvals = {'x': matrix(initWeights.flatten()), 's': matrix(initWeights.flatten())}
    else:
        initvals = None
    q = losses.flatten() * tile(eta, (n, 1)).T.flatten()
    Aparts = [[sparseRowJofK(k, n, j), spdiag(list(ones(n) * eta[j]))] for j in range(k)]
    A = sparse(Aparts)
    b = hstack((rowSums, averageWeights))
    G = spdiag(list(-ones(k * n)))
    h = zeros(k * n)
    "Min. q'x s.t. Gx <= h and Ax=b"
    resDict = lp(matrix(q), G=G, h=matrix(h), A=A, b=matrix(b), solver='glpk')
    return array(resDict['x']).reshape((k, n))
def sqrt_utility(A, b, x):
    """Square root utility function: U(x) = 1'sqrt(Ax+b)"""
    tmp1 = np.sqrt(A*x + b)
    f = sum(tmp1)[0]
    tmp2 = matrix([.5/a[0] for a in tmp1])
    Df = tmp2.T*A
    tmp3 = spdiag([-2.*a**3 for a in tmp2])
    return f, Df, A.T*tmp3*A
Beispiel #58
0
 def test_sparse_quad_form(self):
     """Test quad form with a sparse matrix.
     """
     Q = cvxopt.spdiag([1,1])
     x = cvxpy.Variable(2,1)
     cost = cvxpy.quad_form(x,Q)
     prob = cvxpy.Problem(cvxpy.Minimize(cost), [x == [1,2]])
     self.assertAlmostEqual(prob.solve(), 5)
Beispiel #59
0
def dSbus_dV(Y, V):
    """ Computes the partial derivative of power injection w.r.t. voltage.

        References:
            Ray Zimmerman, "dSbus_dV.m", MATPOWER, version 3.2,
            PSERC (Cornell), http://www.pserc.cornell.edu/matpower/
    """
    I = Y * V

    diagV = spdiag(V)
    diagIbus = spdiag(I)
    diagVnorm = spdiag(div(V, abs(V))) # Element-wise division.

    dS_dVm = diagV * conj(Y * diagVnorm) + conj(diagIbus) * diagVnorm
    dS_dVa = 1j * diagV * conj(diagIbus - Y * diagV)

    return dS_dVm, dS_dVa
Beispiel #60
0
def F(x=None, z=None):
   if x is None: return 0, matrix(1.0, (n,1))
   if min(x) <= 0: return None
   f = x.T*log(x)
   grad = 1.0 + log(x)
   if z is None: return f, grad.T
   H = spdiag(z[0] * x**-1)
   return f, grad.T, H