Beispiel #1
0
    def test_dcp_curvature(self):
        expr = 1 + cvx.exp(cvx.Variable())
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.Parameter()*cvx.Variable(nonneg=True)
        self.assertEqual(expr.curvature, s.AFFINE)

        f = lambda x: x**2 + x**0.5  # noqa E731
        expr = f(cvx.Constant(2))
        self.assertEqual(expr.curvature, s.CONSTANT)

        expr = cvx.exp(cvx.Variable())**2
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = 1 - cvx.sqrt(cvx.Variable())
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.log(cvx.sqrt(cvx.Variable()))
        self.assertEqual(expr.curvature, s.CONCAVE)

        expr = -(cvx.exp(cvx.Variable()))**2
        self.assertEqual(expr.curvature, s.CONCAVE)

        expr = cvx.log(cvx.exp(cvx.Variable()))
        self.assertEqual(expr.is_dcp(), False)

        expr = cvx.entr(cvx.Variable(nonneg=True))
        self.assertEqual(expr.curvature, s.CONCAVE)

        expr = ((cvx.Variable()**2)**0.5)**0
        self.assertEqual(expr.curvature, s.CONSTANT)
Beispiel #2
0
    def test_dcp_curvature(self):
      expr = 1 + cvx.exp(cvx.Variable())
      self.assertEqual(expr.curvature, s.CONVEX)

      expr = cvx.Parameter()*cvx.NonNegative()
      self.assertEqual(expr.curvature, s.AFFINE)

      f = lambda x: x**2 + x**0.5
      expr = f(cvx.Constant(2))
      self.assertEqual(expr.curvature, s.CONSTANT)

      expr = cvx.exp(cvx.Variable())**2
      self.assertEqual(expr.curvature, s.CONVEX)

      expr = 1 - cvx.sqrt(cvx.Variable())
      self.assertEqual(expr.curvature, s.CONVEX)

      expr = cvx.log( cvx.sqrt(cvx.Variable()) )
      self.assertEqual(expr.curvature, s.CONCAVE)

      expr = -( cvx.exp(cvx.Variable()) )**2
      self.assertEqual(expr.curvature, s.CONCAVE)

      expr = cvx.log( cvx.exp(cvx.Variable()) )
      self.assertEqual(expr.is_dcp(), False)

      expr = cvx.entr( cvx.NonNegative() )
      self.assertEqual(expr.curvature, s.CONCAVE)

      expr = ( (cvx.Variable()**2)**0.5 )**0
      self.assertEqual(expr.curvature, s.CONSTANT)
Beispiel #3
0
    def test_exp(self) -> None:
        """Test domain for exp.
        """
        expr = cp.exp(self.a)
        self.a.value = 2
        self.assertAlmostEqual(expr.grad[self.a], np.exp(2))

        self.a.value = 3
        self.assertAlmostEqual(expr.grad[self.a], np.exp(3))

        self.a.value = -1
        self.assertAlmostEqual(expr.grad[self.a], np.exp(-1))

        expr = cp.exp(self.x)
        self.x.value = [3, 4]
        val = np.zeros((2, 2)) + np.diag(np.exp([3, 4]))
        self.assertItemsAlmostEqual(expr.grad[self.x].toarray(), val)

        expr = cp.exp(self.x)
        self.x.value = [-1e-9, 4]
        val = np.zeros((2, 2)) + np.diag(np.exp([-1e-9, 4]))
        self.assertItemsAlmostEqual(expr.grad[self.x].toarray(), val)

        expr = cp.exp(self.A)
        self.A.value = [[1, 2], [3, 4]]
        val = np.zeros((4, 4)) + np.diag(np.exp([1, 2, 3, 4]))
        self.assertItemsAlmostEqual(expr.grad[self.A].toarray(), val)
Beispiel #4
0
    def test_exp(self):
        x = cp.Variable(4, pos=True)
        c = cp.Parameter(4, pos=True)
        expr = cp.exp(cp.multiply(c, x))
        self.assertTrue(expr.is_dgp(dpp=True))

        expr = cp.exp(c.T @ x)
        self.assertTrue(expr.is_dgp(dpp=True))
def simple_convex_alogrithm(z_minus_u, alpha, weight, UENum, minReward=0.0):

    # Do not use numpy when you use cvxopt!!!!!!!!!!@@@@################################################################
    x = cp.Variable(UENum)

    y = [weight[i] * (x[i]**float(alpha[i]))/alpha[i] for i in range(UENum)]

    const = [(x[i]**float(alpha[i]))/alpha[i] for i in range(UENum)]  # the requirement only for real reward (not weighted)

    if use_other_utility_function:
        y = [weight[i] * RESNum/(RESNum * cp.exp(- alpha[i] * x[i]) + 1) for i in range(UENum)]
        const = [ RESNum/(RESNum * cp.exp(- alpha[i] * x[i])+ 1) for i in range(UENum)]

    fx = cp.sum(y) - 0.5 * rho * cp.sum_squares(cp.sum(x) - z_minus_u)

    objective = cp.Minimize(-fx)

    factor, iter, maxiter = 1, 1, 10  # decrease the factor, i.e., loose the constraint, when we cannot solve the problem optimally

    # solve the problem, if not optimal or not well solved, reduce the constraint and do it again
    while True:

        constraints = [factor*minReward <= const[i] for i in range(UENum)] + [cp.sum(x) <= Rmax]

        prob = cp.Problem(objective, constraints,)

        # The optimal objective value is returned by `prob.solve()`.
        result = prob.solve() #gp=True

        assert(iter <= maxiter)

        if prob.status == 'optimal':
            break
        else:
            factor *= 0.5
            iter += 1

    optimal_x = x.value

    utility, real_utility, const_ = np.zeros(UENum), np.zeros(UENum), np.zeros(UENum)

    for i in range(UENum):
        const_[i] = (optimal_x[i]**alpha[i])/alpha[i]
        utility[i] = weight[i] * (optimal_x[i]**alpha[i])/alpha[i] - maxTime * np.clip(minReward - const_[i], 0, None)
        real_utility[i] = weight[i] * (optimal_x[i]**alpha[i])/alpha[i]

        if use_other_utility_function:
            const_[i] = Rmax/(Rmax * np.exp(- alpha[i] * optimal_x[i])+ 1)
            utility[i] = weight[i] * Rmax/(Rmax * np.exp(- alpha[i] * optimal_x[i])+ 1) - maxTime * np.clip(minReward - const_[i], 0, None)
            real_utility[i] = weight[i] * Rmax/(Rmax * np.exp(- alpha[i] * optimal_x[i])+ 1)

    aug_penalty = 0.5 * rho * np.abs(np.sum(optimal_x) - z_minus_u)

    # The optimal value for x is stored in `x.value`.
    return np.sum(utility)-aug_penalty, optimal_x, np.sum(real_utility)
 def to_cvx(self, f, scale=1.0):
     yi = f(self.x[0])
     yj = f(self.x[1])
     yk = f(self.x[2])
     diff1 = yk - yj + eps
     #diff2 = yk - yi + eps
     diff2 = yk + yj - 2 * yi
     exp1 = 1 - cvx.exp(-scale * diff1)
     exp2 = 1 - cvx.exp(-scale * diff2)
     val = cvx.min_elemwise(exp1, exp2)
     log_val = -cvx.log(val)
     return log_val, [diff1 > eps, diff2 > eps]
    def switch(self):
        if self.phi_name == 'logistic':
            self.cvx_phi = lambda z: cvx.logistic(-z)  # / math.log(2, math.e)
        elif self.phi_name == 'hinge':
            self.cvx_phi = lambda z: cvx.pos(1 - z)
        elif self.phi_name == 'squared':
            self.cvx_phi = lambda z: cvx.square(-z)
        elif self.phi_name == 'exponential':
            self.cvx_phi = lambda z: cvx.exp(-z)
        else:
            logger.error('%s is not include' % self.phi_name)
            logger.info('Logistic is the default setting')
            self.cvx_phi = lambda z: cvx.logistic(-z)  # / math.log(2, math.e)

        if self.kappa_name == 'logistic':
            self.cvx_kappa = lambda z: cvx.logistic(z)  # / math.log(2, math.e)
            self.psi_kappa = lambda mu: ((1 + mu) * math.log(1 + mu) +
                                         (1 - mu) * math.log(3 - mu)) / 2
        elif self.kappa_name == 'hinge':
            self.cvx_kappa = lambda z: cvx.pos(1 + z)
            self.psi_kappa = lambda mu: mu
        elif self.kappa_name == 'squared':
            self.cvx_kappa = lambda z: cvx.square(1 + z)
            self.psi_kappa = lambda mu: mu**2
        elif self.kappa_name == 'exponential':
            self.cvx_kappa = lambda z: cvx.exp(z)
            self.psi_kappa = lambda mu: 1 - math.sqrt(1 - mu**2)
        else:
            logger.error('%s is not include' % self.kappa_name)
            logger.info('hinge is the default setting')
            self.cvx_kappa = lambda z: cvx.pos(1 + z)
            self.psi_kappa = lambda mu: mu

        if self.delta_name == 'logistic':
            self.cvx_delta = lambda z: 1 - cvx.logistic(
                -z)  # / math.log(2, math.e)
            self.psi_delta = lambda mu: ((1 + mu) * math.log(1 + mu) +
                                         (1 - mu) * math.log(1 - mu)) / 2
        elif self.delta_name == 'hinge':
            self.cvx_delta = lambda z: 1 - cvx.pos(1 - z)
            self.psi_delta = lambda mu: mu
        elif self.delta_name == 'squared':
            self.cvx_delta = lambda z: 1 - cvx.square(1 - z)
            self.psi_delta = lambda mu: mu**2
        elif self.delta_name == 'exponential':
            self.cvx_delta = lambda z: 1 - cvx.exp(-z)
            self.psi_delta = lambda mu: 1 - math.sqrt(1 - mu**2)
        else:
            logger.error('%s is not include' % self.delta_name)
            logger.info('hinge is the default setting')
            self.cvx_delta = lambda z: cvx.pos(1 + z)
            self.psi_delta = lambda mu: mu
Beispiel #8
0
 def test_expcone_1(self):
     x = cvx.Variable(shape=(1, ))
     tempcons = [cvx.exp(x[0]) <= np.exp(1), cvx.exp(-x[0]) <= np.exp(1)]
     sigma = cvx.suppfunc(x, tempcons)
     y = cvx.Variable(shape=(1, ))
     obj_expr = y[0]
     cons = [sigma(y) <= 1]
     # ^ That just means -1 <= y[0] <= 1
     prob = cvx.Problem(cvx.Minimize(obj_expr), cons)
     prob.solve(solver='ECOS')
     viol = cons[0].violation()
     assert viol <= 1e-6
     assert abs(y.value - (-1)) <= 1e-6
Beispiel #9
0
 def test_expcone_1(self) -> None:
     x = cp.Variable(shape=(1, ))
     tempcons = [cp.exp(x[0]) <= np.exp(1), cp.exp(-x[0]) <= np.exp(1)]
     sigma = cp.suppfunc(x, tempcons)
     y = cp.Variable(shape=(1, ))
     obj_expr = y[0]
     cons = [sigma(y) <= 1]
     # ^ That just means -1 <= y[0] <= 1
     prob = cp.Problem(cp.Minimize(obj_expr), cons)
     prob.solve(solver='ECOS')
     viol = cons[0].violation()
     self.assertLessEqual(viol, 1e-6)
     self.assertLessEqual(abs(y.value - (-1)), 1e-6)
Beispiel #10
0
    def test_partial_optimize_special_constr(self):
        x, y = Variable(1), Variable(1)

        # Solve the (simple) two-stage problem by "combining" the two stages
        # (i.e., by solving a single linear program)
        p1 = Problem(Minimize(x + cp.exp(y)), [x + y >= 3, y >= 4, x >= 5])
        p1.solve()

        # Solve the two-stage problem via partial_optimize
        p2 = Problem(Minimize(cp.exp(y)), [x + y >= 3, y >= 4])
        g = partial_optimize(p2, [y], [x])
        p3 = Problem(Minimize(x + g), [x >= 5])
        p3.solve()
        self.assertAlmostEqual(p1.value, p3.value)
 def generate_cvx(cls, constraints, f, transform=None, scale=1.0):
     x, x_low, x_high = ConvexNeighborConstraint.generate_neighbors_for_scipy_optimize(
         constraints, transform)
     yi = f(x)
     yj = f(x_low)
     yk = f(x_high)
     diff1 = yk - yj
     #diff2 = yk - yi
     diff2 = yk + yj - 2 * yi
     exp1 = 1 - cvx.exp(-scale * diff1) + eps
     exp2 = 1 - cvx.exp(-scale * diff2) + eps
     val = cvx.min_elemwise(exp1, exp2)
     log_val = -cvx.log(val)
     sum_val = cvx.sum_entries(log_val)
     return sum_val, [diff1 > eps, diff2 > eps]
Beispiel #12
0
    def test_signed_curvature(self):
        # Convex argument.
        expr = cvx.abs(1 + cvx.exp(cvx.Variable()) )
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.abs( -cvx.entr(cvx.Variable()) )
        self.assertEqual(expr.curvature, s.UNKNOWN)

        expr = cvx.abs( -cvx.log(cvx.Variable()) )
        self.assertEqual(expr.curvature, s.UNKNOWN)

        # Concave argument.
        expr = cvx.abs( cvx.log(cvx.Variable()) )
        self.assertEqual(expr.curvature, s.UNKNOWN)

        expr = cvx.abs( -cvx.square(cvx.Variable()) )
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.abs( cvx.entr(cvx.Variable()) )
        self.assertEqual(expr.curvature, s.UNKNOWN)

        # Affine argument.
        expr = cvx.abs( cvx.NonNegative() )
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.abs( -cvx.NonNegative() )
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.abs( cvx.Variable() )
        self.assertEqual(expr.curvature, s.CONVEX)
Beispiel #13
0
def _solve_balancing_cvx(J0, cost_fn, factors=None, **solve_args):
    """
    Use CVXPY to solve the synaptic balancing problem.
    This is very fast on small matrices (<100 neurons), but quite slow on
    larger matrices.
    arguments:
    J0 is N x N numpy array
    arguments:
    J0 is N x N numpy array
    cost_fn is CostFunction instance
    returns: summarized transform
    """
    import cvxpy as cvx

    # define constant element-wise weights
    J0 = np.array(J0).astype(float)
    weights = cost_fn.alpha * (np.abs(J0)**cost_fn.p)

    # define variable (state, h, and the log ratio matrix derived from it)
    N = J0.shape[0]
    h = cvx.Variable((N, 1), value=np.zeros((N, 1)))
    log_ratios = -h * np.ones((1, N)) + np.ones((N, 1)) * h.T
    C = cvx.multiply(weights, cvx.exp(cost_fn.p * log_ratios))

    # solve problem and get final transform vector
    problem = cvx.Problem(cvx.Minimize(cvx.sum(C)))
    problem.solve(**solve_args)
    hf = h.value[:, 0]

    # report results
    return summarize_transform(J0, cost_fn, hf)
Beispiel #14
0
def find_optimal_dosage(A, D, x, m=3, n=4):
    z = np.log10(np.array(x))

    # treatment concentration vectors
    u = cp.Variable((m, 1))

    # Perron-Frobenius eigenvalue of ODE system
    lambda_pf = cp.Variable((1, 1))

    # Minimize the Perron-Frobenius eigenvalue to maximize rate of viral decay
    objective = cp.Minimize(lambda_pf)
    constraints = []

    # Treatment concentrations must be non-negative and lie in unit simplex
    constraints.append(u >= 0)
    constraints.append(sum(u) == 1)

    for k in range(n):
        constraints.append(
            A[k, :] @ cp.exp(z - z[k]) + D[k, :] @ u <= lambda_pf)

    problem = cp.Problem(objective, constraints)
    result = problem.solve()

    return lambda_pf, u
    def test_key_error(self):
        """Test examples that caused key error.
        """
        import cvxpy as cvx
        x = cvx.Variable()
        u = -cvx.exp(x)
        prob = cvx.Problem(cvx.Maximize(u), [x == 1])
        prob.solve(verbose=True, solver=cvx.CVXOPT)
        prob.solve(verbose=True, solver=cvx.CVXOPT)

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

        import numpy as np
        import cvxopt
        import cvxpy as cp

        kD=2
        Sk=cp.semidefinite(kD)
        Rsk=cp.Parameter(kD,kD)
        mk=cp.Variable(kD,1)
        musk=cp.Parameter(kD,1)

        logpart=-0.5*cp.log_det(Sk)+0.5*cp.matrix_frac(mk,Sk)+(kD/2.)*np.log(2*np.pi)
        linpart=mk.T*musk-0.5*cp.trace(Sk*Rsk)
        obj=logpart-linpart
        prob=cp.Problem(cp.Minimize(obj))
        musk.value=np.ones((2,1))
        covsk=np.diag([0.3,0.5])
        Rsk.value=covsk+(musk.value*musk.value.T)
        prob.solve(verbose=True,solver=cp.CVXOPT)
        print "second solve"
        prob.solve(verbose=False, solver=cp.CVXOPT)
Beispiel #16
0
    def test_key_error(self):
        """Test examples that caused key error.
        """
        if cvx.CVXOPT in cvx.installed_solvers():
            x = cvx.Variable()
            u = -cvx.exp(x)
            prob = cvx.Problem(cvx.Maximize(u), [x == 1])
            prob.solve(verbose=True, solver=cvx.CVXOPT)
            prob.solve(verbose=True, solver=cvx.CVXOPT)

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

            import numpy as np

            kD = 2
            Sk = cvx.Variable((kD, kD), PSD=True)
            Rsk = cvx.Parameter((kD, kD))
            mk = cvx.Variable((kD, 1))
            musk = cvx.Parameter((kD, 1))

            logpart = -0.5 * cvx.log_det(Sk) + 0.5 * cvx.matrix_frac(
                mk, Sk) + (kD / 2.) * np.log(2 * np.pi)
            linpart = mk.T * musk - 0.5 * cvx.trace(Sk * Rsk)
            obj = logpart - linpart
            prob = cvx.Problem(cvx.Minimize(obj), [Sk == Sk.T])
            musk.value = np.ones((2, 1))
            covsk = np.diag([0.3, 0.5])
            Rsk.value = covsk + (musk.value * musk.value.T)
            prob.solve(verbose=True, solver=cvx.CVXOPT)
            print("second solve")
            prob.solve(verbose=False, solver=cvx.CVXOPT)
Beispiel #17
0
 def test_multiply_nonlinear_nonneg_param_and_nonneg_variable_is_not_dpp(
         self):
     x = cp.Parameter(nonneg=True)
     y = cp.Variable(nonneg=True)
     product = cp.exp(x) * y
     self.assertFalse(product.is_dpp())
     self.assertTrue(product.is_dcp())
Beispiel #18
0
 def test_tutorial_example(self):
     x = cp.Variable()
     y = cp.Variable(pos=True)
     objective_fn = -cp.sqrt(x) / y
     problem = cp.Problem(cp.Minimize(objective_fn), [cp.exp(x) <= y])
     # smoke test
     problem.solve(SOLVER, qcp=True)
Beispiel #19
0
    def test_signed_curvature(self):
        # Convex argument.
        expr = cvx.abs(1 + cvx.exp(cvx.Variable()))
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.abs(-cvx.entr(cvx.Variable()))
        self.assertEqual(expr.curvature, s.UNKNOWN)

        expr = cvx.abs(-cvx.log(cvx.Variable()))
        self.assertEqual(expr.curvature, s.UNKNOWN)

        # Concave argument.
        expr = cvx.abs(cvx.log(cvx.Variable()))
        self.assertEqual(expr.curvature, s.UNKNOWN)

        expr = cvx.abs(-cvx.square(cvx.Variable()))
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.abs(cvx.entr(cvx.Variable()))
        self.assertEqual(expr.curvature, s.UNKNOWN)

        # Affine argument.
        expr = cvx.abs(cvx.Variable(nonneg=True))
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.abs(-cvx.Variable(nonneg=True))
        self.assertEqual(expr.curvature, s.CONVEX)

        expr = cvx.abs(cvx.Variable())
        self.assertEqual(expr.curvature, s.CONVEX)
Beispiel #20
0
 def test_multiply_param_and_nonlinear_variable_is_dpp(self):
     x = cp.Parameter(nonneg=True)
     y = cp.Variable()
     product = x * cp.exp(y)
     self.assertTrue(product.is_convex())
     self.assertTrue(product.is_dcp())
     self.assertTrue(product.is_dpp())
Beispiel #21
0
 def test_warm_start(self):
     if cp.SUPER_SCS in INSTALLED_SOLVERS:
         x = cp.Variable(10)
         obj = cp.Minimize(cp.sum(cp.exp(x)))
         prob = cp.Problem(obj, [cp.sum(x) == 1])
         result = prob.solve(solver='SUPER_SCS', eps=1e-4)
         result2 = prob.solve(solver='SUPER_SCS', warm_start=True, eps=1e-4)
         self.assertAlmostEqual(result2, result, places=2)
Beispiel #22
0
 def test_paper_example_exp_log(self):
     x = cvxpy.Variable(pos=True)
     y = cvxpy.Variable(pos=True)
     obj = cvxpy.Minimize(x * y)
     constr = [cvxpy.exp(y / x) <= cvxpy.log(y)]
     problem = cvxpy.Problem(obj, constr)
     # smoke test.
     problem.solve(SOLVER, gp=True)
def solve_g_dual_cp(C, a, b, eta, tau):
    u = cp.Variable(shape=a.shape)
    v = cp.Variable(shape=b.shape)

    u_stack = cp.vstack([u.T for _ in range(nr)])
    v_stack = cp.hstack([v for _ in range(nc)])
    print(u_stack.shape, v_stack.shape)

    # obj = eta * cp.sum(cp.multiply(cp.exp(u + v.T) * cp.exp(v).T, 1 / cp.exp(C)))
    # obj = eta * cp.sum(cp.multiply(cp.exp(u_stack + v_stack), 1 / cp.exp(C)))
    obj = eta * cp.sum(cp.exp((u_stack + v_stack - C) / eta))
    obj += tau * cp.sum(cp.multiply(cp.exp(-u / tau), a))
    obj += tau * cp.sum(cp.multiply(cp.exp(-v / tau), b))

    prob = cp.Problem(cp.Minimize(obj))
    prob.solve()

    return prob.value, u.value, v.value
Beispiel #24
0
 def choose_phi(self, phi_name):
     if phi_name == 'logistic':
         self.cvx_phi = lambda z: cvx.logistic(-z)
     elif phi_name == 'hinge':
         self.cvx_phi = lambda z: cvx.pos(1.0 - z)
     elif phi_name == 'exponential':
         self.cvx_phi = lambda z: cvx.exp(-z)
     else:
         print("Your surrogate function doesn't exist.")
def cvx_hw2(A, b, x):
    x = cp.Variable(len(x), value=x.reshape(-1, ))
    prob = cp.Problem(cp.Minimize(cp.sum(cp.exp(A @ x - b.squeeze()))))
    prob.solve()
    # print(prob.value)
    # Print result.
    x = x.value

    return x
Beispiel #26
0
def running_constraints(constr, config, x, u, gam, z, dt, N):
    A_w = create_A(config.w)
    alpha = 1.0 / (config.isp * 9.8)
    pointing_angle = np.cos(config.pointing_lim)
    p1 = config.p1
    p2 = config.p2
    v_max = config.v_max
    c = get_cone(config.landing_cone)
    g = config.g

    # Simple Euler integration
    for k in range(N - 1):
        # Rocket dynamics constraints
        constr += [x[0:3, k + 1] == x[0:3, k] + dt * (A_w @ x[:, k])[0:3]]
        constr += [x[3:6, k + 1] == x[3:6, k] + dt * (g + u[:, k])]
        constr += [z[k + 1] == z[k] - dt * alpha * gam[k]]

        constr += [cvp.norm(x[3:6, k]) <= v_max
                   ]  # Velocity remains below maximum
        constr += [
            cvp.norm(u[:, k]) <= gam[k]
        ]  # Helps enforce the magnitude of thrust vector == thrust magnitude
        constr += [u[0, k] >= pointing_angle * gam[k]
                   ]  # Rocket can only point away from vertical by so much
        constr += [
            cvp.norm(_E @ (x[:3, k] - x[:3, -1])) -
            c.T @ (x[:3, k] - x[:3, -1]) <= 0
        ]  # Stay inside the glide cone

        if k > 0:
            z_0 = cvp.log(config.m - alpha * p2 * (k) * dt)
            z_1 = cvp.log(config.m - alpha * p1 * (k) * dt)

            sigma_lower = p1 * cvp.exp(-z_0) * (1 - (z[k] - z_0) +
                                                (z[k] - z_0))
            sigma_upper = p2 * cvp.exp(-z_0) * (1 - (z[k] - z_0))

            # Minimimum and maximum thrust constraints
            constr += [gam[k] <= sigma_upper]
            constr += [gam[k] >= sigma_lower]
            # Minimum and maximum mass constraints
            constr += [z[k] >= z_0]
            constr += [z[k] <= z_1]
    return constr
Beispiel #27
0
 def test_exp(self):
     """Test a problem with exp.
     """
     for n in [5, 10, 25]:
         print(n)
         x = cp.Variable(n)
         obj = cp.Minimize(cp.sum(cp.exp(x)))
         p = cp.Problem(obj, [cp.sum(x) == 1])
         p.solve(solver=cp.SCS)
         self.assertItemsAlmostEqual(x.value, n*[1./n])
Beispiel #28
0
 def test_paper_example_exp_log(self) -> None:
     x = cp.Variable(pos=True)
     y = cp.Variable(pos=True)
     a = cp.Parameter(pos=True, value=0.2)
     b = cp.Parameter(pos=True, value=0.3)
     obj = cp.Minimize(x * y)
     constr = [cp.exp(a * y / x) <= cp.log(b * y)]
     problem = cp.Problem(obj, constr)
     gradcheck(problem, gp=True, atol=1e-2)
     perturbcheck(problem, gp=True, atol=1e-2)
 def test_warm_start(self):
     """Test warm starting.
     """
     if cvx.SUPER_SCS in cvx.installed_solvers():
         x = cvx.Variable(10)
         obj = cvx.Minimize(cvx.sum(cvx.exp(x)))
         prob = cvx.Problem(obj, [cvx.sum(x) == 1])
         result = prob.solve(solver='SUPER_SCS', eps=1e-4)
         result2 = prob.solve(solver='SUPER_SCS', warm_start=True, eps=1e-4)
         self.assertAlmostEqual(result2, result, places=2)
Beispiel #30
0
  def fit(self, warm_start=True, label=None, save=False,
          use_glmnet=False, use_cvxpy=False, cvxpy_tol=1e-4,
          **kwargs):
    '''
    Note: use_cvxpy or use_glmnet only works with CV weights (i.e. all 0 or 1)
    '''
    if not use_cvxpy and not use_glmnet:
      super(ExponentialPoissonRegressionModel, self).fit(warm_start,
                                                         label,
                                                         save,
                                                         **kwargs)

    elif use_glmnet:
      from glmnet_py import glmnet
      lambdau = np.array([self.L1Lambda,])
      inds = self.example_weights.astype(np.bool)
      x = self.training_data.X[inds,:].copy()
      y = self.training_data.Y[inds].copy().astype(np.float)
      fit = glmnet(x=x,
                   y=y,
                   family='poisson',
                   standardize=False,
                   lambdau=lambdau,
                   thresh=1e-20,
                   maxit=10e4,
                   alpha=1.0,
      )

    elif use_cvxpy:
      import cvxpy
      D = self.params.get_free().shape[0]
      Ntrain = self.training_data.X.shape[0]
      theta = cvxpy.Variable(D, value=self.params.get_free())
      weights = self.example_weights

      X = self.training_data.X
      Y = self.training_data.Y
      obj = cvxpy.Minimize(-Y*cvxpy.multiply(weights,(X*(theta)))
         + cvxpy.sum(cvxpy.multiply(weights, cvxpy.exp(X*(theta))))
         + self.L1Lambda * cvxpy.sum(cvxpy.abs((theta)[:-1]))
      )
      problem = cvxpy.Problem(obj)
      problem.solve(solver=cvxpy.SCS, normalize=True,
                    eps=cvxpy_tol, verbose=False, max_iters=2000)
      if (problem.status == 'infeasible_inaccurate' or
          problem.status == 'unbounded_inaccurate'):
        problem.solve(solver=cvxpy.SCS, normalize=True,
                      eps=cvxpy_tol, verbose=False, max_iters=2000)
        problem.solve(solver=cvxpy.SCS, normalize=True,
                      eps=cvxpy_tol, verbose=False, max_iters=10000)
      try:
        self.params.set_free(theta.value)
      except:
        print('Bad problem?', problem.status)
Beispiel #31
0
 def test_warm_start(self):
     """Test warm starting.
     """
     x = cvx.Variable(10)
     obj = cvx.Minimize(cvx.sum(cvx.exp(x)))
     prob = cvx.Problem(obj, [cvx.sum(x) == 1])
     result = prob.solve(solver=cvx.SCS, eps=1e-4)
     time = prob.solver_stats.solve_time
     result2 = prob.solve(solver=cvx.SCS, warm_start=True, eps=1e-4)
     time2 = prob.solver_stats.solve_time
     self.assertAlmostEqual(result2, result, places=2)
 def test_exp(self):
     """Test a problem with exp.
     """
     if cvx.SUPER_SCS in cvx.installed_solvers():
         for n in [5, 10, 25]:
             print(n)
             x = cvx.Variable(n)
             obj = cvx.Minimize(cvx.sum(cvx.exp(x)))
             p = cvx.Problem(obj, [cvx.sum(x) == 1])
             p.solve(solver='SUPER_SCS', verbose=True)
             self.assertItemsAlmostEqual(x.value, n*[1./n])
Beispiel #33
0
    def train(self, level=0, lamb=0.01):
        """

        :param level: 0: 非正则化; 1: 1阶正则化; 2: 2阶正则化
        :param lamb: 正则化系数水平
        :return: 无
        """
        L = cvx.Parameter(sign="positive")
        L.value = lamb  # 正则化系数
        w = cvx.Variable(self.n + 1)  # 参数向量
        loss = 0
        for i in range(self.m):  # 构造成本函数和正则化项
            loss += self.y_trans[i] * \
                    cvx.log_sum_exp(cvx.vstack(0, cvx.exp(self.x_trans[i, :].T * w))) + \
                    (1 - self.y_trans[i]) * \
                    cvx.log_sum_exp(cvx.vstack(0, cvx.exp(-1 * self.x_trans[i, :].T * w)))
        # 为什么一定要用log_sum_exp? cvx.log(1 + cvx.exp(x[i, :].T * w))为什么不行?
        if level > 0:
            reg = cvx.norm(w[:self.n], level)
            prob = cvx.Problem(cvx.Minimize(loss / self.m + L / (2 * self.m) * reg))
        else:
            prob = cvx.Problem(cvx.Minimize(loss / self.m))
        prob.solve()
        self.w = np.array(w.value)
Beispiel #34
0
 def FindKineticOptimum(self, bounds=None):
     """Use the power of convex optimization!
     
     minimize sum (protein cost)
     
     we can do this by using ln(concentration) as variables and leveraging 
     the convexity of exponentials.         
     """
     assert self.dG0_f_prime is not None
     
     ln_conc, constraints = self._MakeMinimumFeasbileConcentrationsProblem()
     total_inv_conc = cvxpy.sum(cvxpy.exp(-ln_conc))
     program = cvxpy.program(cvxpy.minimize(total_inv_conc), constraints)
     program.solve(quiet=True)
     return ln_conc.value, total_inv_conc.value
Beispiel #35
0
def poisson_loss(theta, X, y):
    return (cp.sum_entries(cp.exp(X*theta)) -
            cp.sum_entries(sp.diags([y],[0])*X*theta))
Beispiel #36
0
 prox("NON_NEGATIVE", None, C_non_negative_scaled),
 prox("NON_NEGATIVE", None, C_non_negative_scaled_elemwise),
 prox("NON_NEGATIVE", None, lambda: [x >= 0]),
 prox("NORM_1", f_norm1_weighted),
 prox("NORM_1", lambda: cp.norm1(x)),
 prox("NORM_2", lambda: cp.norm(X, "fro")),
 prox("NORM_2", lambda: cp.norm2(x)),
 prox("NORM_NUCLEAR", lambda: cp.norm(X, "nuc")),
 prox("SECOND_ORDER_CONE", None, C_soc_scaled),
 prox("SECOND_ORDER_CONE", None, C_soc_scaled_translated),
 prox("SECOND_ORDER_CONE", None, C_soc_translated),
 prox("SECOND_ORDER_CONE", None, lambda: [cp.norm(X, "fro") <= t]),
 prox("SECOND_ORDER_CONE", None, lambda: [cp.norm2(x) <= t]),
 prox("SEMIDEFINITE", None, lambda: [X >> 0]),
 prox("SUM_DEADZONE", f_dead_zone),
 prox("SUM_EXP", lambda: cp.sum_entries(cp.exp(x))),
 prox("SUM_HINGE", f_hinge),
 prox("SUM_HINGE", lambda: cp.sum_entries(cp.max_elemwise(1-x, 0))),
 prox("SUM_HINGE", lambda: cp.sum_entries(cp.max_elemwise(1-x, 0))),
 prox("SUM_INV_POS", lambda: cp.sum_entries(cp.inv_pos(x))),
 prox("SUM_KL_DIV", lambda: cp.sum_entries(cp.kl_div(p1,q1))),
 prox("SUM_LARGEST", lambda: cp.sum_largest(x, 4)),
 prox("SUM_LOGISTIC", lambda: cp.sum_entries(cp.logistic(x))),
 prox("SUM_NEG_ENTR", lambda: cp.sum_entries(-cp.entr(x))),
 prox("SUM_NEG_LOG", lambda: cp.sum_entries(-cp.log(x))),
 prox("SUM_QUANTILE", f_quantile),
 prox("SUM_QUANTILE", f_quantile_elemwise),
 prox("SUM_SQUARE", f_least_squares_matrix),
 prox("SUM_SQUARE", lambda: f_least_squares(20)),
 prox("SUM_SQUARE", lambda: f_least_squares(5)),
 prox("SUM_SQUARE", f_quad_form),
import numpy as np
import cvxpy as cp
from matrix_equilibration_data import *
import numpy.linalg as la

B = np.square(A)

u = cp.Variable(m)
v = cp.Variable(n)
Be = 0
for i in xrange(m):
    for j in xrange(n):
        Be += cp.exp(cp.log(B[i, j]) + u[i] + v[j])
obj = cp.Minimize(Be)
cons = [cp.sum_entries(u) == 1, cp.sum_entries(v) == 1]
prob = cp.Problem(obj, cons)
prob.solve()

D = np.zeros((m, m))
for i in xrange(m):
    D[i, i] = np.exp(u.value[i, 0] / p)

E = np.zeros((n, n))
for i in xrange(n):
    E[i, i] = np.exp(v.value[i, 0] / p)

A_equi = np.dot(np.dot(D, A), E)
print "2-norm for every row:"
for i in xrange(m):
    print la.norm(A_equi[i, :])
Beispiel #38
0
    1.7095    2.1351   10.1296    4.0931    2.9001    9.9634;\
    1.4289    3.5800    9.3459    3.8898    2.7663   15.1383;\
    1.3046    3.5610   10.1179    4.3891    7.1302    3.8139;\
    1.1897    2.7807   13.0112    4.2426    6.1611   29.6734'
W = np.matrix(W)

(W_min, W_max) = (1.0, 30.0)

# objective values for the different designs
# entry j gives the objective for design j
P = np.matrix('29.0148   46.3369  282.1749   78.5183  104.8087  253.5439')
D = np.matrix('15.9522   11.5012    4.8148    8.5697    8.0870    6.0273')
A = np.matrix('22.3796   38.7908  204.1574   62.5563   81.2272  200.5119')

# specifications
(P_spec, D_spec, A_spec) = (60.0, 10.0, 50.0)

theta = cvx.Variable(k)
obj = cvx.Minimize(0)
constraints = [cvx.log(P)*theta <= cvx.log(P_spec),
               cvx.log(D)*theta <= cvx.log(D_spec),
               cvx.log(A)*theta <= cvx.log(A_spec),
               cvx.sum_entries(theta) == 1, theta >= 0]          
prob = cvx.Problem(obj, constraints)
sol = prob.solve()

w = cvx.exp(cvx.log(W)*theta)

print('status: {}'.format(sol))
print('theta: {}'.format(theta.T.value))
print('w: {}'.format(w.T.value))
    def MinimizeConcentration(self, metabolite_index=None,
                              concentration_bounds=None):
        """Finds feasible concentrations minimizing the concentration
           of metabolite i.
        
        Args:
            metabolite_index: the index of the metabolite to minimize.
                if == None, minimize the sum of all concentrations.
            concentration_bounds: the Bounds objects setting concentration bounds.
        """
        my_bounds = concentration_bounds or self.DefaultConcentrationBounds()

        ln_conc = cvxpy.variable(m=1, n=self.Ncompounds, name='lnC')
        ln_conc_lb, ln_conc_ub = my_bounds.GetLnBounds(self.compounds)
        constr = [cvxpy.geq(ln_conc, cvxpy.matrix(ln_conc_lb)),
                  cvxpy.leq(ln_conc, cvxpy.matrix(ln_conc_ub))]
        
        my_dG0_r_primes = np.matrix(self.dG0_r_prime)
        
        # Make flux-based constraints on reaction free energies.
        # All reactions must have negative dGr in the direction of the flux.
        # Reactions with a flux of 0 must be in equilibrium.
        S = np.matrix(self.S)
        
        for i, flux in enumerate(self.fluxes):
            
            curr_dg0 = my_dG0_r_primes[0, i]
            # if the dG0 is unknown, this reaction imposes no new constraints
            if np.isnan(curr_dg0):
                continue
            
            rcol = cvxpy.matrix(S[:, i])
            curr_dgr = curr_dg0 + RT * ln_conc * rcol
            if flux == 0:
                constr.append(cvxpy.eq(curr_dgr, 0.0))
            else:
                constr.append(cvxpy.leq(curr_dgr, 0.0))        
        
        objective = None
        if metabolite_index is not None:
            my_conc = ln_conc[0, metabolite_index]
            objective = cvxpy.minimize(cvxpy.exp(my_conc))
        else:
            objective = cvxpy.minimize(
                cvxpy.sum(cvxpy.exp(ln_conc)))
        
        name = 'CONC_OPT'
        if metabolite_index:
            name = 'CONC_%d_OPT' % metabolite_index
        problem = cvxpy.program(objective, constr, name=name)
        optimum = problem.solve(quiet=True)        

        """
        status = problem.solve(quiet=True)
        if status != 'optimal':
            status = optimized_pathway.OptimizationStatus.Infeasible(
                'Pathway infeasible given bounds.')
            return ConcentrationOptimizedPathway(
                self._model, self._thermo,
                my_bounds, optimization_status=status)
        """
        opt_ln_conc = np.matrix(np.array(ln_conc.value))
        result = ConcentrationOptimizedPathway(
            self._model, self._thermo,
            my_bounds, optimal_value=optimum,
            optimal_ln_metabolite_concentrations=opt_ln_conc)
        return result
        
        
        
Beispiel #40
0
#!/usr/bin/env python

import numpy as np
import cvxpy as cp

from epopt import cvxpy_expr
from epopt import expression_vis
from epopt.compiler import canonicalize

if __name__ == "__main__":
    n = 5
    x = cp.Variable(n)

    # Lasso expression tree
    m = 10
    A = np.random.randn(m,n)
    b = np.random.randn(m)
    lam = 1
    f = cp.sum_squares(A*x - b) + lam*cp.norm1(x)
    prob0 = cvxpy_expr.convert_problem(cp.Problem(cp.Minimize(f)))[0]
    expression_vis.graph(prob0.objective).write("expr_lasso.dot")

    # Canonicalization of a more complicated example
    c = np.random.randn(n)
    f = cp.exp(cp.norm(x) + c.T*x) + cp.norm1(x)
    prob0 = cvxpy_expr.convert_problem(cp.Problem(cp.Minimize(f)))[0]
    expression_vis.graph(prob0.objective).write("expr_epigraph.dot")
    prob1 = canonicalize.transform(prob0)
    expression_vis.graph(prob1.objective).write("expr_epigraph_canon.dot")