Beispiel #1
0
def get_candidate_lyapunov(Xc, Xc_next, m, n):
    Nc = len(Xc)
    assert all([len(xc) == n for xc in Xc])
    assert len(Xc_next) == Nc
    assert all([len(xc_next) == n for xc_next in Xc_next])

    A = cvxpy.Variable(m, n)
    cons = list()
    for xc, xc_next in itertools.izip(Xc, Xc_next):
        cvxpy.max_entries(A * xc)  # TODO: check multiplication

    pass
Beispiel #2
0
def infinite_push(theta, Xp, Xn):
    m, d = Xp.shape
    n = Xn.shape[0]
    Z = cp.max_elemwise(
        1 - (Xp * theta * np.ones((1, n)) - (Xn * theta * np.ones((1, m))).T),
        0)
    return cp.max_entries(cp.sum_entries(Z, axis=0))
 def find_minimum(A, b):
     x = cp.Variable(columns, 1)
     function = cp.max_entries(A * x + b)
     obj = cp.Minimize(function)
     prob = cp.Problem(obj)
     opt_val = cp.Problem.solve(prob)
     return x.value, opt_val
def cvxsolve():
    global n, m, T, A, W, Zp, N, K, lmd, mu, R, m2v, v2m
    Z = cvx.Variable(T, m)
    #for v in N.keys():
    #    i,j=v
    #    Z[i,j]=0
    #for v in K.keys():
    #    i,j=v
    #    Z[i,j]=1

    zsm = 0
    for i in xrange(T):
        zsm += Z[i, :]
    obf = lmd * cvx.max_entries(zsm)
    for i in xrange(T):
        obf += (W[i].reshape(1, m)) * (Z[i, :].T)
    obj = cvx.Minimize(obf)
    const = []
    const += [0 <= Z, Z <= 1, zsm >= 1]
    for v in N.keys():
        i, j = v
        const += [Z[i, j] == 0]
    for v in K.keys():
        i, j = v
        const += [Z[i, j] == 1]
    for i in xrange(T):
        const += [cvx.log_det((A.T) * cvx.diag(Z[i, :]) * A) >= mu]

    prob = cvx.Problem(obj, const)
    result = prob.solve(solver='SCS', verbose=True, eps=1e-1)

    return Z.value
Beispiel #5
0
def create(**kwargs):
    m = kwargs["m"]
    n = kwargs["n"]
    k = 10
    A = [problem_util.normalized_data_matrix(m,n,1) for i in range(k)]
    B = problem_util.normalized_data_matrix(k,n,1)
    c = np.random.rand(k)

    x = cp.Variable(n)
    t = cp.Variable(k)
    f = cp.max_entries(t+cp.abs(B*x-c))
    C = []
    for i in range(k):
        C.append(cp.pnorm(A[i]*x, 2) <= t[i])

	t_eval = lambda: np.array([cp.pnorm(A[i]*x, 2).value for i in range(k)])
    f_eval = lambda: cp.max_entries(t_eval() + cp.abs(B*x-c)).value

    return cp.Problem(cp.Minimize(f), C), f_eval
Beispiel #6
0
 def solve_lp(self):
     prob = cvx.Problem(
         # cvx.Maximize(cvx.sum_entries(cvx.entr(
         #     self.Xs
         # ))),
         # cvx.Maximize(0),
         cvx.Minimize(cvx.max_entries(self.Xs)),
         self.constraints)
     sol = prob.solve(solver=cvx.ECOS, verbose=True)
     values = self.Xs.value
     return values
Beispiel #7
0
    def problem(self):
        """The network optimization problem.

        :rtype: :class:`cvxpy.Problem`
        """
        return cvx.Problem(
            cvx.Minimize(cvx.sum_entries(cvx.max_entries(self.cost, axis=1))),
            self.constraints +
            [terminal._power[0,k] == terminal._power[0,0]
             for terminal in self.terminals
             for k in xrange(1, terminal._power.size[1])])
Beispiel #8
0
def chebyshevEpigraphProblem(problemOptions, solverOptions):
    n = problemOptions['n']
    m = problemOptions['m']
    k = problemOptions['k']
    A = [__normalized_data_matrix(m,n,1) for i in range(k)]
    B = __normalized_data_matrix(k,n,1)
    c = np.random.rand(k)
    x = cp.Variable(n)
    t = cp.Variable(k)
    f = cp.max_entries(t+cp.abs(B*x-c))
    C = []
    for i in range(k):
        C.append(cp.pnorm(A[i]*x, 2) <= t[i])
    prob = cp.Problem(cp.Minimize(f), C)
    prob.solve(**solverOptions)
    return {'Problem':prob, 'name':'chebyshevEpigraphProblem'}
Beispiel #9
0
 def solve_cvx(self, solver='MOSEK'):
     '''
     solve the full capacity reservation problem
     :param solver: solver interfaced via CVXPY
     :return: objective value, flow matrix, dual variable
     '''
     F = cvx.Variable(self.m, self.K)
     Ft = cvx.Variable(self.m, self.K)
     cost = self.p.T * cvx.max_entries(Ft, axis=1)
     constr = F <= Ft
     constrs = [self.A*F + self.S == 0, F >=0, constr] + \
               [F[:,k] <= self.c for k in range(self.K)]
     prob = cvx.Problem(cvx.Minimize(cost), constrs)
     print("Solver " + solver + " begins:")
     prob.solve(solver=solver)
     print("Solver " + solver + " ends")
     return cost.value, F.value, constr.dual_value
Beispiel #10
0
 def solve(self):
     if self.solver == "lp":
         xsol = np.linalg.lstsq(self.loc_moments, self.moments)[0]
         self.values = xsol
     else:
         # Moment values of the boundaries
         Xs = cvx.Variable(self.resolution)
         constraints = [
             Xs >= 0, Xs <= 1.0, self.loc_moments * Xs == self.moments
         ]
         if self.solver == "mindensity":
             o = cvx.Minimize(cvx.max_entries(Xs))
         else:
             o = cvx.Maximize(cvx.sum_entries(cvx.entr(Xs)))
         prob = cvx.Problem(o, constraints)
         sol = prob.solve(solver=cvx.ECOS)
         self.values = Xs.value
     return self.values * 1000
def optimize(n_cluster,
             error_cons,
             pred_file,
             label_file,
             solution_file,
             ifInt=0,
             obj='balance',
             singleCluster=False):
    # Problem data.
    n_class = 1000

    #mapping = np.genfromtxt('9cluster.csv',delimiter=',')
    #ori_label = np.genfromtxt('label_squeezenet_9cluster.csv', delimiter=',')
    ori_pred = np.genfromtxt(pred_file, delimiter=',')
    ori_top1_pred = ori_pred.argmax(1)
    class_label = np.genfromtxt(label_file, delimiter=',')
    ori_top1_pred, class_label = map_to_cluster(ori_top1_pred, class_label)

    # Construct the problem.
    if ifInt:
        P = cvx.Bool(n_cluster, n_class)
        constraints = []
        use_solver = cvx.GUROBI
    else:
        P = cvx.Variable(n_cluster, n_class)
        constraints = [0 <= P, P <= 1]
        use_solver = cvx.ECOS

    if singleCluster:
        constraints.append(cvx.sum_entries(P, axis=0) <= 1)

    # Objective function
    if obj == 'sum':
        obj_func = cvx.sum_entries(P)
    elif obj == 'sum_sq':
        obj_func = cvx.sum_squares(cvx.sum_entries(P, axis=1))
    elif obj == 'max':
        obj_func = cvx.max_entries(cvx.sum_entries(P, axis=1))
    elif obj == 'max_sq':
        obj_func = cvx.max_entries(cvx.power(cvx.sum_entries(P, axis=1), 2))
    else:
        obj_func = cvx.sum_squares(
            cvx.sum_entries(P, axis=1) - n_class / float(n_cluster))

    objective = cvx.Minimize(obj_func)

    # Error rate constraint
    error_constraint = 0
    for cluster, label in zip(ori_top1_pred, class_label):
        error_constraint += P[cluster, label]
    error_constraint = 1.0 - 1.0 / len(class_label) * error_constraint
    constraints.append(error_constraint <= error_cons)

    # The optimal objective is returned by prob.solve().
    prob = cvx.Problem(objective, constraints)
    result = prob.solve(solver=use_solver)

    save_mat(solution_file, P.value)

    print 'status:', prob.status
    print 'optimal value: ', prob.value
    print 'Optimal P matrix:', P.value
    print 'Error rate constraint dual value: ', constraints[-1].dual_value
    print 'Error rate constraint {0}:{1} <= {2}'.format(
        constraints[-1].value, error_constraint.value, error_cons)
Beispiel #12
0
def infinite_push(theta, Xp, Xn):
    m, d = Xp.shape
    n = Xn.shape[0]
    Z = cp.max_elemwise(
        1 - (Xp*theta*np.ones((1,n)) - (Xn*theta*np.ones((1,m))).T), 0)
    return cp.max_entries(cp.sum_entries(Z, axis=0))
Beispiel #13
0
def multiclass_hinge_loss(Theta, X, y):
    k = Theta.size[1]
    Y = one_hot(y, k)
    return (cp.sum_entries(cp.max_entries(X*Theta + 1 - Y, axis=1)) -
            cp.sum_entries(cp.mul_elemwise(X.T.dot(Y), Theta)))
def ntf_fir_from_digested(order, osrs, H_inf, f0s, zf, **opts):
    """
    Synthesize FIR NTF with minmax approach from predigested specification

    Version for the cvxpy_tinoco modeler.
    """
    verbose = opts['show_progress']
    if opts['cvxpy_opts']['solver'] == 'cvxopt':
        opts['cvxpy_opts']['solver'] = cvxpy.CVXOPT
    elif opts['cvxpy_opts']['solver'] == 'scs':
        opts['cvxpy_opts']['solver'] = cvxpy.SCS

    # State space representation of NTF
    A = np.matrix(np.eye(order, order, 1))
    B = np.matrix(np.vstack((np.zeros((order-1, 1)), 1.)))
    # C contains the NTF coefficients
    D = np.matrix(1)

    # Set up the problem
    bands = len(f0s)
    c = cvxpy.Variable(1, order)
    F = []
    gg = cvxpy.Variable(bands, 1)

    for idx in range(bands):
        f0 = f0s[idx]
        osr = osrs[idx]
        omega0 = 2*f0*np.pi
        Omega = 1./osr*np.pi
        P = cvxpy.Symmetric(order)
        Q = cvxpy.Semidef(order)
        if f0 == 0:
            # Lowpass modulator
            M1 = A.T*P*A+Q*A+A.T*Q-P-2*Q*np.cos(Omega)
            M2 = A.T*P*B + Q*B
            M3 = B.T*P*B - gg[idx, 0]
            M = cvxpy.bmat([[M1, M2, c.T],
                            [M2.T, M3, D],
                            [c, D, -1]])
            F += [M << 0]
            if zf:
                # Force a zero at DC
                F += [cvxpy.sum_entries(c) == -1]
        else:
            # Bandpass modulator
            M1r = (A.T*P*A + Q*A*np.cos(omega0) + A.T*Q*np.cos(omega0) -
                   P - 2*Q*np.cos(Omega))
            M2r = A.T*P*B + Q*B*np.cos(omega0)
            M3r = B.T*P*B - gg[idx, 0]
            M1i = A.T*Q*np.sin(omega0) - Q*A*np.sin(omega0)
            M21i = -Q*B*np.sin(omega0)
            M22i = B.T*Q*np.sin(omega0)
            Mr = cvxpy.bmat([[M1r, M2r, c.T],
                             [M2r.T, M3r, D],
                             [c, D, -1]])
            Mi = cvxpy.bmat([[M1i, M21i, np.zeros((order, 1))],
                             [M22i, 0, 0],
                             [np.zeros((1, order)), 0, 0]])
            M = cvxpy.bmat([[Mr, Mi],
                            [-Mi, Mr]])
            F += [M << 0]
            if zf:
                # Force a zero at z=np.exp(1j*omega0)
                nn = np.arange(order).reshape((order, 1))
                vr = np.matrix(np.cos(omega0*nn))
                vi = np.matrix(np.sin(omega0*nn))
                vn = np.matrix(
                    [-np.cos(omega0*order), -np.sin(omega0*order)])
                F += [c*cvxpy.hstack(vr, vi) == vn]
    if H_inf < np.inf:
        # Enforce the Lee constraint
        R = cvxpy.Semidef(order)
        MM = cvxpy.bmat([[A.T*R*A-R, A.T*R*B, c.T],
                         [B.T*R*A, -H_inf**2+B.T*R*B, D],
                         [c, D, -1]])
        F += [MM << 0]
    target = cvxpy.Minimize(cvxpy.max_entries(gg))
    p = cvxpy.Problem(target, F)
    p.solve(verbose=verbose, **opts['cvxpy_opts'])
    return np.hstack((1, np.asarray(c.value)[0, ::-1]))
Beispiel #15
0
def prox_l1(v, lbd):
    x = cvx.max_entries(0, v - lbd) - cvx.max_entries(0, -v - lbd)
    return x
Beispiel #16
0
def prox_l1(v, lbd):
    x = cvx.max_entries(0, v-lbd) - cvx.max_entries(0, -v - lbd)
    return x
Beispiel #17
0
np.random.seed(0)
m = 200
n = 400
k = 10
A = [normalized_data_matrix(m,n,1) for i in range(k)]
B = normalized_data_matrix(k,n,1)
c = np.random.rand(k)


# Problem construction

x = cp.Variable(n)
t = cp.Variable(k)

f = cp.max_entries(t+cp.abs(B*x-c))
C = []
for i in range(k):
    C.append(cp.pnorm(A[i]*x, 2) <= t[i])

prob = cp.Problem(cp.Minimize(f), C)
opt_val = None


# Problem collection

# Single problem collection
problemDict = {
    "problemID" : problemID,
    "problem"   : prob,
    "opt_val"   : opt_val
#haha=np.array([[0,1,2],
#               [3,4,5],
#               [6,7,8]])

tt = time.time()
#print('\ntime elapsed=', time.time()-tt)
#file = 'ls_perm_meas_data.py'
#exec(open(file).read())
#import ls_perm_meas_data as ct

p = np.array([.5, .6, .6, .6, .2])
q = np.array([10, 5, 5, 20, 10])
A = np.array([[1, 1, 0, 0, 0], [0, 0, 0, 1, 0], [1, 0, 0, 1, 1],
              [0, 1, 0, 0, 1], [0, 0, 1, 0, 0]]).T
x = cv.Variable(5)
obj = cv.Minimize(cv.max_entries(A * x) - p.T * x)
cst = [x <= q, 0 <= x]
prb = cv.Problem(obj, cst)
prb.solve()
print(prb.status, prb.value, x.value.T)

t = cv.Variable()
obj = cv.Minimize(t - p.T * x)
cst = [A * x <= t, x <= q, 0 <= x]
prb = cv.Problem(obj, cst)
prb.solve()
print(prb.status, prb.value, x.value.T)

np.set_printoptions(precision=3)

print(cst[0].dual_value.T)
Beispiel #19
0
def C_soc_translated():
    return [cp.norm2(x + randn()) <= t + randn()]

def C_soc_scaled_translated():
    return [cp.norm2(randn()*x + randn()) <= randn()*t + randn()]

# Proximal operators
PROX_TESTS = [
    #prox("MATRIX_FRAC", lambda: cp.matrix_frac(p, X)),
    #prox("SIGMA_MAX", lambda: cp.sigma_max(X)),
    prox("AFFINE", lambda: randn(n).T*x),
    prox("CONSTANT", lambda: 0),
    prox("LAMBDA_MAX", lambda: cp.lambda_max(X)),
    prox("LOG_SUM_EXP", lambda: cp.log_sum_exp(x)),
    prox("MAX", lambda: cp.max_entries(x)),
    prox("NEG_LOG_DET", lambda: -cp.log_det(X)),
    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("QUAD_OVER_LIN", lambda: cp.quad_over_lin(p, q1)),
    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]),
Beispiel #20
0
def solve(n_p,
          n_d,
          n_s,
          G,
          T,
          M,
          indisp,
          forced,
          slot_choice,
          demand,
          prop=0.5,
          hist=None):
    """ Solves the Integer Programming problem that generates a schedule.
    The current constraints are, maximum of one man per slot...

    Args:
        n_p (int): the number of people in the list
        n_d (int): the number of days being considered
        n_s (int): the number of slots of work per day
        G (ndarray): A np array (colum vector) of 1s and 0s representing
        the gender of each person in the list, 1 for man and 0 for woman
        T (ndarray): A np array (colum vector) representing the teacher
        status of each person in the list, 1 for teacher and 0 for auxiliar
        M (ndarray): A np array (colum vector) representing the maturity
        status of each person in the list, 1 for mature and 0 otherwise
        indisp (list): List of tuples corresponding to the the date
        indisponibilities in the form (person, day)
        forced (list): Similarly to indisp, this list contains the tuples
        of people that we want to force to be present in a given slot in the
        solution. In this case the tuples are (person, day*n_s+slot).
        slot_choice (ndarray): A somewhat dense matrix of shape (n_p, n_s)
        representing the disponibility of each person to work on each slot.
        1 means available, 0 otherwise
        demand (ndarray): A (n_d*n_s) column vector of the demand of people
        for each slot.
        prop (float): Maximum proportion of men in each slot

    Returns:
        solution (ndarray): A matrix of shape (n_p, n_d*n_s) where xij = 1
        represents a person p working on day j//n_s on slot j%n_s 
    """
    # TODO Change the disp matrix to slots? Maybe add another constraint
    # source, specifically targeting the day and slot.
    # To fix someone on a specific role you can set the other slots to 0
    # every day.

    X = cvx.Bool(n_p, n_d * n_s)

    if demand is not None:
        # print('demand is not None, but is: ', demand)
        demand_constr = []
        for s in range(n_d * n_s):
            demand_constr.append(cvx.sum_entries(X[:, s]) == demand[s])

    # This is just an example, I need to change the rest of the code to use it
    else:
        demand_constr = [1 > 0]

    # Date Indisponibility constraints
    ind_constr = []
    for p, d in indisp:
        # Check if the list is being correctly appended: The elements, not the list
        ind_constr = ind_constr + [X[p, d * n_s + k] == 0 for k in range(n_s)]

    # Gender constraints
    gend_constr = []
    for s in range(n_d * n_s):
        if demand[s] > 1:
            gend_constr.append(G.reshape(1, n_p) * X[:, s] <= prop * demand[s])

    # Teacher constraints
    teach_constr = []
    for s in range(n_d * n_s):
        if demand[s] > 1:
            teach_constr.append(T.reshape(1, n_p) * X[:, s] == 1)

    # Maturity constraints
    matur_constr = []
    for s in range(n_d * n_s):
        if demand[s] > 1:
            matur_constr.append(M.reshape(1, n_p) * X[:, s] >= 1)

    # No Repeat constraints
    no_rep_constr = []
    for d in range(n_d):
        for p in range(n_p):
            no_rep_constr.append(
                cvx.sum_entries(X[p, d * n_s:n_s * (d + 1)]) <= 1)

    # Slot (Activity) choice constraint
    slot_constr = []
    for p in range(n_p):
        for s in range(n_s):
            slot_constr = slot_constr + [
                X[p, s + n_s * d] <= slot_choice[p, s] for d in range(n_d)
            ]

    # Forced constraint
    force_constr = []
    for p, d in forced:
        force_constr.append(X[p, d] == 1)

    constraints = (demand_constr + ind_constr + gend_constr + teach_constr +
                   matur_constr + no_rep_constr + slot_constr + force_constr)

    # Objective Function
    obj = cvx.Minimize(cvx.max_entries(cvx.sum_entries(X, axis=1)))

    prob = cvx.Problem(obj, constraints)
    prob.solve(solver=cvx.GLPK_MI)
    sol = X.value
    value = prob.value
    if sol is not None:
        # Gets rids of the residues, rounds everything to zero or one
        sol = np.int8(sol.round(2))
        value = int(round(value))

    return prob.status, sol, value
b = npy.dot(A, x_known) + v

r = cvx.Variable(m, 1)
x = cvx.Variable(n, 1)
z = npy.zeros((n, 1))
a = .25
cst = [r == A * x - b]
f, (ax) = plt.subplots(4, sharex=True)
bins = npy.linspace(-2, 2, 81) + .025
bins = bins[:-1]
for idx in [0, 1, 2, 3]:
    if idx == 0:
        fcn = cvx.sum_entries(cvx.abs(r))
    elif idx == 1:
        fcn = cvx.sum_entries(cvx.square(r))
    elif idx == 2:
        fcn = cvx.sum_entries(
            cvx.max_entries(cvx.hstack(cvx.abs(r) - a, npy.zeros((m, 1))),
                            axis=1))
    elif idx == 3:
        fcn = cvx.sum_entries(-cvx.log(1 - cvx.square(r)))
    else:
        print('bad')

    print(idx)
    prb = cvx.Problem(cvx.Minimize(fcn), cst)
    prb.solve()
    print(prb.status)
    ax[idx].hist(r.value, bins)
plt.show()
Beispiel #22
0
def multiclass_hinge_loss(Theta, X, y):
    k = Theta.size[1]
    Y = one_hot(y, k)
    return (cp.sum_entries(cp.max_entries(X*Theta + 1 - Y, axis=1)) -
            cp.sum_entries(cp.mul_elemwise(X.T.dot(Y), Theta)))
Beispiel #23
0
 def get_U(self, F):
     '''
     :param F: feasible flows
     :return: upper bound
     '''
     return (self.p.T * cvx.max_entries(F, axis=1)).value
Beispiel #24
0
def C_soc_translated():
    return [cp.norm2(x + randn()) <= t + randn()]

def C_soc_scaled_translated():
    return [cp.norm2(randn()*x + randn()) <= randn()*t + randn()]

# Proximal operators
PROX_TESTS = [
    #prox("MATRIX_FRAC", lambda: cp.matrix_frac(p, X)),
    #prox("SIGMA_MAX", lambda: cp.sigma_max(X)),
    prox("AFFINE", lambda: randn(n).T*x),
    prox("CONSTANT", lambda: 0),
    prox("LAMBDA_MAX", lambda: cp.lambda_max(X)),
    prox("LOG_SUM_EXP", lambda: cp.log_sum_exp(x)),
    prox("MAX", lambda: cp.max_entries(x)),
    prox("NEG_LOG_DET", lambda: -cp.log_det(X)),
    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]),