def run(self, bases, observation, verbose=True): """Runs the model calculations. Bases and observations are loaded into proper polarized data sets. In this step, arguments are checked to ensure polarization angles match in value and order. Any bases that have not been built already are built with their corresponding ``build()`` method. Bases and observations of multiple polarizations are then concatenated and given to cvxpy and MOSEK for solving. Results are returned and processed in inherited ``Model`` attributes. Args: bases (list of Basis): The basis objects (built or not) of several polarizations, to be used for fitting. observation (list of Observation): The observation objects of several polarizations, to be used for fitting. verbose (bool): The console verbosity of the called solver (MOSEK). """ super().run(bases, observation) print('Bases and observations loaded in model') t0 = time.time() print('Forming Regularized problem:') print(' Setting up basis and observation matrices') A = sp.vstack([self.data_set(angle).basis.basis_matrix for angle in self.polarization_angles]) basis_rows = A.shape[0] n = A.shape[1] + 1 o = sp.csc_matrix((np.ones(basis_rows, ), (np.arange(basis_rows), np.zeros(basis_rows, )))) A = sp.hstack((A, o)) H = cvx.Constant(A) b = np.vstack([self.data_set(angle).observation.data.reshape((180 * 1024, 1), order='F') for angle in self.polarization_angles]) b = cvx.Constant(b) x = cvx.Variable(n) print(' Defining regularization term with alpha = {0}'.format(self.alpha)) D = np.diag(np.ones((A.shape[1] - 1,)), k=1) - np.diag(np.ones((A.shape[1],))) D = cvx.Constant(D[:-1, :]) alpha = self.alpha print(' Defining objective') objective = cvx.Minimize(cvx.norm2(H * x - b) + alpha * cvx.norm2(D * x)) print(' Defining constraints') constraints = [x[:-1] >= 0] print(' Defining problem') prob = cvx.Problem(objective, constraints) print('Problem Formulation DONE\n\nCalling the solver: ' + cvx.MOSEK) ts0 = time.time() prob.solve(solver=cvx.MOSEK, verbose=verbose) ts1 = time.time() print(cvx.MOSEK + ' done in {0:.2f} seconds.'.format(ts1 - ts0)) if x.value is not None: print('\nProcessing solution') self.solver_result = np.array(x.value) self.background = self.solver_result[-1] self._process_result(self.solver_result[:-1]) t1 = time.time() print('Fitting DONE:\n Elapsed time: {0:.2f} s'.format(t1-t0))
def block_solve_cvx(r_j, A_j, a_1_j, a_2_j, m, b_j_init, verbose=False): """Solve the gelcd optimization problem for a single block with cvx. b_j_init and verbose are ignored. b_j_init because cvx doesn't support it. verbose because it doesn't go together with tqdm. """ # Convert everything to numpy r_j = r_j.numpy() A_j = A_j.numpy() # Create the b_j variable b_j = cvx.Variable(A_j.shape[1]) # Form the objective q_j = r_j - A_j * b_j obj_fun = cvx.square(cvx.norm2(q_j)) / (2. * m) obj_fun += a_1_j * cvx.norm2(b_j) + (a_2_j / 2.) * cvx.square( cvx.norm2(b_j)) # Build the optimization problem obj = cvx.Minimize(obj_fun) problem = cvx.Problem(obj, constraints=None) problem.solve(solver="CVXOPT", verbose=False) b_j = np.asarray(b_j.value) if A_j.shape[1] == 1: b_j = b_j.reshape(1) else: b_j = b_j[:, 0] return torch.from_numpy(b_j.astype(np.float32))
def _cvxpy_minimize(self, lb, ub, **kwargs): """ solve quadratic problem using CVXPY interface Keyword arguments: lb, ub -- vectors of lower and upper bounds (potentially modified by added tolerance) Modified member variables: status, solution, obj_value Returns: nothing """ # shorter names v = self.cvxpyV A = self.cvxpyMatrix minus_w = array(self.obj.f).T # negative wildtype solution lb = array(lb).T ub = array(ub).T if self.weights is not None: weights = array(diag(sqrt(self.weights))) if self.Aineq is not None: Aineq = array(self.Aineq) bineq = array(self.bineq) p = cvxpy.Problem(cvxpy.Minimize(cvxpy.norm2(v + minus_w)), [ cvxpy.abs(A * v) <= self.matrix_tol, (Aineq * v, bineq) <= v, v >= lb, v <= ub ]) else: p = cvxpy.Problem( cvxpy.Minimize(cvxpy.norm2(v + minus_w)), [cvxpy.abs(A * v) <= self.matrix_tol, v >= lb, v <= ub]) # Configure program ('abstol', 'feastol', 'reltol', 'maxiters') """for k in kwargs: try: p.options[k] = kwargs[k] except KeyError: continue""" print kwargs self.obj_value = p.solve(solver="CVXOPT", reltol=kwargs['reltol'], abstol=kwargs['abstol'], feastol=kwargs['feastol']) #, #maxiters = kwargs['maxiters'], gtol = kwargs['gtol'], maxIter = kwargs['maxIter']) self.status = cvxpyToSolverStatus(self.obj_value) if len(v.value) == 0 or isnan(v.value[0, 0]): self.solution = [] else: self.solution = array(v.value.T)[0, :]
def Maximum_likelihood_estimation_of_an_increasing_nonnegative_signal(): ''' Setup ''' N = 100 xtrue = np.zeros((N, 1)) xtrue[0:40] = 0.1 xtrue[49] = 2 # matlab is 1-indexed xtrue[70:80] = 0.15 xtrue[79] = 1 # matlab is 1-indexed xtrue = np.cumsum(xtrue, axis=0) h = np.array([[1, -0.85, 0.7, -0.3]]) k = len(h) yhat = np.array([np.convolve(h.flatten(), xtrue.flatten())]).T y = yhat[:-3] + np.array( [[-0.43], [-1.7], [0.13], [0.29], [-1.1], [1.2], [1.2], [-0.038], [0.33], [0.17], [-0.19], [0.73], [-0.59], [2.2], [-0.14], [0.11], [1.1], [0.059], [-0.096], [-0.83], [0.29], [-1.3], [0.71], [1.6], [-0.69], [0.86], [1.3], [-1.6], [-1.4], [0.57], [-0.4], [0.69], [0.82], [0.71], [1.3], [0.67], [1.2], [-1.2], [-0.02], [-0.16], [-1.6], [0.26], [-1.1], [1.4], [-0.81], [0.53], [0.22], [-0.92], [-2.2], [-0.059], [-1], [0.61], [0.51], [1.7], [0.59], [-0.64], [0.38], [-1], [-0.02], [-0.048], [4.3e-05], [-0.32], [1.1], [-1.9], [0.43], [0.9], [0.73], [0.58], [0.04], [0.68], [0.57], [-0.26], [-0.38], [-0.3], [-1.5], [-0.23], [0.12], [0.31], [1.4], [-0.35], [0.62], [0.8], [0.94], [-0.99], [0.21], [0.24], [-1], [-0.74], [1.1], [-0.13], [0.39], [0.088], [-0.64], [-0.56], [0.44], [-0.95], [0.78], [0.57], [-0.82], [-0.27]]) plt.plot(range(len(xtrue)), xtrue, label="xtrue") plt.plot(range(len(y)), y, label="y") x_ml = cp.Variable((N, 1)) constraints = [x_ml >= 0] + [x_ml[i + 1] >= x_ml[i] for i in range(N - 1)] # obj = cp.sum(cp.log(y - cp.conv(h, x_ml))) # prob = cp.Problem(cp.Maximize(obj), constraints) obj = cp.norm2(y - cp.conv(h, x_ml)) prob = cp.Problem(cp.Minimize(obj), constraints) prob.solve() print("Status = " + str(prob.status)) plt.plot(range(len(x_ml.value)), x_ml.value, label="x_ml") x_ml_free = cp.Variable((N, 1)) obj = cp.norm2(y - cp.conv(h, x_ml_free)) prob = cp.Problem(cp.Minimize(obj)) prob.solve() print("Status = " + str(prob.status)) plt.plot(range(len(x_ml_free.value)), x_ml_free.value, label="x_ml_free") plt.legend() plt.show()
def glrd_diverse(V, G, F, r, err_V, err_F): # diversity threshold is 0.5 for k in xrange(r): G_copy = np.copy(G) # create local copies for excluding the k^th col and row of G and F resp. F_copy = np.copy(F) G_copy[:, k] = 0.0 F_copy[k, :] = 0.0 R = V - np.dot(G_copy, F_copy) # compute residual # Solve for optimal G(.)(k) with diversity constraints F_k = F[k, :] x_star_G = linalg.lstsq(R.T, F_k.T)[0].T x_G = cvx.Variable(x_star_G.shape[0]) objective_G = cvx.Minimize(cvx.norm2(x_star_G - x_G)) constraints_G = [x_G >= 0] for j in xrange(r): if j != k: constraints_G += [x_G.T * G[:, j] <= err_V] prob_G = cvx.Problem(objective_G, constraints_G) result = prob_G.solve(solver='SCS') if not np.isinf(result): G_k_min = np.asarray(x_G.value) G[:, k] = G_k_min[:, 0] else: print result # Solve for optimal F(k)(.) with diversity constraints G_k = G[:, k] x_star_F = linalg.lstsq(R, G_k)[0] x_F = cvx.Variable(x_star_F.shape[0]) objective_F = cvx.Minimize(cvx.norm2(x_star_F - x_F)) constraints_F = [x_F >= 0] for j in xrange(r): if j != k: constraints_F += [x_F.T * F[j, :] <= err_F] prob_F = cvx.Problem(objective_F, constraints_F) result = prob_F.solve(solver='SCS') if not np.isinf(result): F_k_min = np.asarray(x_F.value) F[k, :] = F_k_min[0, :] else: print result return G, F
def steiner_pos(tree, flows, flow_exp=1.0, verbose=False): '''minimize flow-weighted length of network with steiner positions args: - tree: steiner tree (with terminal positions) - flows: dict from arc tuple - flow_exp: exponent of flow in objective coefficients returns dict of positions from node to tuple ''' # TODO: rewrite with vector notation an no loops nodes = sorted(tree.get_nodes()) arcs = sorted(tree.get_arcs()) # create variables for 2d-positions x = {n:cvx.Variable(2) for n in nodes} # fix positions of terminals fixed_pos = [x[n] == np.array(tree.dg.node[n]['pos']) for n in nodes if tree.is_terminal(n)] constraints = fixed_pos obj = sum(flows[u,v]**flow_exp * cvx.norm2(x[u] - x[v]) for u,v in arcs) prob = cvx.Problem(cvx.Minimize(obj), constraints) opt = prob.solve(solver=cvx.CVXOPT, verbose=verbose) sol = {n:np.array(x[n].value)[:, 0].tolist() for n in nodes} return sol
def envelope_fit(signal, mu, eta, kind='upper', period=None): ''' Perform an envelope fit of a signal. See: https://en.wikipedia.org/wiki/Envelope_(waves) :param signal: The signal to be fit :param mu: A parameter to control the overall stiffness of the fit :param eta: A parameter to control the permeability of the envelope. A large value result in no data points outside the fitted envelope :param kind: 'upper' or 'lower' :return: An envelope signal of the same length as the input ''' if kind == 'lower': signal *= -1 n_samples = len(signal) envelope = cvx.Variable(len(signal)) mu = cvx.Parameter(sign='positive', value=mu) eta = cvx.Parameter(sign='positive', value=eta) cost = (cvx.sum_entries(cvx.huber(envelope - signal)) + mu * cvx.norm2(envelope[2:] - 2 * envelope[1:-1] + envelope[:-2]) + eta * cvx.norm1(cvx.max_elemwise(signal - envelope, 0))) objective = cvx.Minimize(cost) if period is not None: constraints = [envelope[:n_samples - period] == envelope[period:]] problem = cvx.Problem(objective, constraints) try: problem.solve(solver='MOSEK') except Exception as e: print(e) print('Trying ECOS solver') problem.solve(solver='ECOS') if kind == 'upper': return envelope.value.A1 elif kind == 'lower': signal *= -1 return -envelope.value.A1
def ntf_fir_from_digested(Qs, A, C, H_inf, **opts): """ Synthesize FIR NTF from predigested specification Version for the cvxpy 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 order = np.size(Qs, 0)-1 br = cvxpy.Variable(order, 1, name='br') b = cvxpy.vstack(1, br) X = cvxpy.Symmetric(order, name='X') target = cvxpy.Minimize(cvxpy.norm2(Qs*b)) B = np.vstack((np.zeros((order-1, 1)), 1.)) C = C+br[::-1].T D = np.matrix(1.) M1 = A.T*X M2 = M1*B M = cvxpy.bmat([[M1*A-X, M2, C.T], [M2.T, B.T*X*B-H_inf**2, D], [C, D, np.matrix(-1.)]]) constraints = [M << 0, X >> 0] p = cvxpy.Problem(target, constraints) p.solve(verbose=verbose, **opts['cvxpy_opts']) return np.hstack((1, np.asarray(br.value.T)[0]))
def ntf_fir_from_digested(Qs, A, C, H_inf, **opts): """ Synthesize FIR NTF from predigested specification Version for the cvxpy 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 order = np.size(Qs, 0) - 1 br = cvxpy.Variable(order, 1, name='br') b = cvxpy.vstack(1, br) X = cvxpy.Symmetric(order, name='X') target = cvxpy.Minimize(cvxpy.norm2(Qs * b)) B = np.vstack((np.zeros((order - 1, 1)), 1.)) C = C + br[::-1].T D = np.matrix(1.) M1 = A.T * X M2 = M1 * B M = cvxpy.bmat([[M1 * A - X, M2, C.T], [M2.T, B.T * X * B - H_inf**2, D], [C, D, np.matrix(-1.)]]) constraints = [M << 0, X >> 0] p = cvxpy.Problem(target, constraints) p.solve(verbose=verbose, **opts['cvxpy_opts']) return np.hstack((1, np.asarray(br.value.T)[0]))
def compute_min_norm_solution(x, y, norm_type): """Compute the min-norm solution using a convex-program solver.""" w = cp.Variable((x.shape[0], 1)) if norm_type == 'linf': # compute minimal L_infinity solution constraints = [cp.multiply(y, (w.T @ x)) >= 1] prob = cp.Problem(cp.Minimize(cp.norm_inf(w)), constraints) elif norm_type == 'l2': # compute minimal L_2 solution constraints = [cp.multiply(y, (w.T @ x)) >= 1] prob = cp.Problem(cp.Minimize(cp.norm2(w)), constraints) elif norm_type == 'l1': # compute minimal L_1 solution constraints = [cp.multiply(y, (w.T @ x)) >= 1] prob = cp.Problem(cp.Minimize(cp.norm1(w)), constraints) elif norm_type[0] == 'l': # compute minimal Lp solution p = float(norm_type[1:]) constraints = [cp.multiply(y, (w.T @ x)) >= 1] prob = cp.Problem(cp.Minimize(cp.pnorm(w, p)), constraints) elif norm_type == 'dft1': w = cp.Variable((x.shape[0], 1), complex=True) # compute minimal Fourier L1 norm (||F(w)||_1) solution dft = np.matrix(scipy.linalg.dft(x.shape[0], scale='sqrtn')) constraints = [cp.multiply(y, (cp.real(w).T @ x)) >= 1] prob = cp.Problem(cp.Minimize(cp.norm1(dft @ w)), constraints) prob.solve() logging.info('Min %s-norm solution found (norm=%.4f)', norm_type, float(norm_f(w.value, norm_type))) return cp.real(w).value
def pk_constrained(self, epsilon=0.1): ''' Method to estimate wave number spectrum based on constrained optimization matrix inversion technique. Inputs: epsilon - upper bound of noise floor vector ''' # loop over frequencies bar = ChargingBar('Calculating bounded optmin...', max=len(self.controls.k0), suffix='%(percent)d%%') self.pk = np.zeros((self.n_waves, len(self.controls.k0)), dtype=np.csingle) # print(self.pk.shape) for jf, k0 in enumerate(self.controls.k0): k_vec = k0 * self.dir # Form H matrix h_mtx = np.exp(1j * self.receivers.coord @ k_vec.T) H = h_mtx.astype( complex) # cvxpy does not accept floats, apparently # measured data pm = self.pres_s[:, jf].astype(complex) #### Performing the Tikhonov inversion with cvxpy ######################### x = cp.Variable(h_mtx.shape[1], complex=True) # create x variable # Create the problem problem = cp.Problem( cp.Minimize(cp.norm2(x)**2), [cp.pnorm(cp.matmul(H, x) - pm, p=2) <= epsilon]) problem.solve(solver=cp.SCS, verbose=False) self.pk[:, jf] = x.value bar.next() bar.finish() return self.pk
def eucl_proj(y, C, abstol=1e-7): """ Evaluate the Eculedian projection norm2(argmin_{Cz<=0}(norm2(y-z))). Parameters ---------- y : array The vector being projected. C : array Matrix whose rows are the polytopic set facet normals. abstol : float, optional Absolute tolerance on duality gap. Returns ------- proj : float Eucledian projection value. """ z = cvx.Variable(y.size) cost = cvx.Minimize(cvx.norm2(y - z)) constraints = [C * z <= 0] problem = cvx.Problem(cost, constraints) problem.solve(solver=cvx.ECOS, verbose=False, abstol=abstol, reltol=np.finfo(np.float64).eps) if problem.status != 'optimal': raise RuntimeError('Support function evaluation failed') proj = la.norm(cvx2arr(z), ord=2) return proj
def loss_fn(X, Y, beta, penalty): """ Get loss function for objective function Parameters: ----------- X : CVXPY-variable covariate matrix Y : CVXPY-variable, responses to samples beta : CVXPY-variable beta vector penalty : string Either 'Ridge' or 'Lasso' Returns: -------- * : Loss function for optimization """ if penalty == 'Ridge': return cp.pnorm(cp.matmul(X, beta) - Y, p=2)**2 if penalty == 'Lasso': return cp.norm2(cp.matmul(X, beta) - Y)**2
def _cqp(D, o, k, m, N): ''' solves using cvxpy ''' # cast to object. D = cvxpy.matrix(D) N = cvxpy.matrix(N) o = cvxpy.matrix(o) # create variables. f = cvxpy.variable(k, 1, name='f') x=cvxpy.variable(m, 1, name='x') y=cvxpy.variable(m, 1, name='y') # create constraints. geqs = cvxpy.greater_equals(f,0.0) #TO DO: Sum of all f = sum of observed reads classes (and not equal to 1) sum1 = cvxpy.equals(cvxpy.sum(f), 1.0) #sum1 = cvxpy.equals(cvxpy.sum(f), sum_obs_freq) #3 #dev = cvxpy.equals(D*f-o-x,0.0) #4. matrix N (m x m) * x - y = 0 sizeConstr = cvxpy.equals(N*x-y,0.0) #Check now to do N^2 #sizeConstr = cvxpy.equals(N^2*x-y,0.0) #This might not work but try #sizeConstr = cvxpy.equals(x/N-y,0.0) #constrs = [geqs, sum1, dev, sizeConstr] constrs = [geqs, sum1] log.debug('\tin _cqp function: \n\t\tPrint matrices shapes:') log.debug('\t\t\t%s', D.shape) log.debug('\t\t\t%s', f.shape) log.debug('\t\t\t%s', o.shape) # create the program. #p = cvxpy.program(cvxpy.minimize(cvxpy.norm2(y)),constraints=constrs) p = cvxpy.program(cvxpy.minimize(cvxpy.norm2(D*f-o)),constraints=constrs) p.options['abstol'] = 1e-6 ## 'abstol' - Absolute accuracy Default: 1e-7 p.options['reltol'] = 1e-5 ## 'reltol' - Relative accuracy Default: 1e-6 p.options['feastol'] = 1e-5 ## 'feastol' - Tolerance for feasibility conditions Default: 1e-6 p.options['maxiters'] = 500 ## 'maxiters' - Maximum number of iterations Default: 100 # solve the program. p.solve(quiet=True) # return results. #print np.around(f.value, decimals=20) #print "Print using loop" getcontext().prec = 20 #for i in f.value: # temp_fi=str(i).strip('[]') # print temp_fi return f.value
def cvx_ref(v, max_neg): n = v.size w = cvx.Variable(n) prob = cvx.Problem(cvx.Minimize(cvx.norm2(w - v)), [cvx.sum_entries(w) >= (1-2*max_neg) * cvx.norm1(w)]) prob.solve() return np.asarray(w.value).ravel()
def project_cvxpy(v, B): x = cp.Variable(len(v)) cp.Problem(cp.Minimize( cp.norm2(x - v) ** 2 ), [cp.quad_form(x, B) <= 1]).solve() sol = np.asarray(x.value)[:, 0] return sol
def estimate_c(self, data): x = data.x[data.is_labeled & data.is_train] if self.transform is not None: x = self.transform.fit_transform(x) y = data.y[data.is_labeled] if self.label_transform is not None: y = self.label_transform.fit_transform(y) n = y.size p = data.p c = cvx.Variable(len(self.source_w)) #ws1 = self.source_w[0] #ws2 = self.source_w[1] ws = 0 for i, wsi in enumerate(self.source_w): ws += wsi * c[i] constraints = [c >= 0] constraint_methods = { HypothesisTransfer.WEIGHTS_JUST_OPTIMAL, HypothesisTransfer.WEIGHTS_JUST_FIRST } found_first = False if self.weight_type in constraint_methods: for i in range(c.size[0]): id = i + 1 is_oracle = id in self.oracle_data_set_ids just_first = self.weight_type == HypothesisTransfer.WEIGHTS_JUST_FIRST and found_first if is_oracle and not just_first: found_first = True continue constraints.append(c[i] == 0) loss = 0 for i in range(y.size): xi = x[i, :] yi = y[i] x_mi = np.delete(x, i, axis=0) y_mi = np.delete(y, i, axis=0) b_mi = y_mi.mean() A = x_mi.T.dot(x_mi) + (self.C + self.C2) * np.eye(p) k = x_mi.T.dot(y_mi) - x_mi.T.sum(1) * b_mi + self.C2 * ws w_mi = scipy.linalg.inv(A) * k loss += cvx.power(w_mi.T * xi + b_mi - yi, 2) reg = cvx.norm2(c)**2 #reg = cvx.norm2(c) obj = cvx.Minimize(loss + self.C3 * reg) prob = cvx.Problem(obj, constraints) assert prob.is_dcp() try: prob.solve(cvx.SCS) c_value = np.asarray(c.value) except Exception as e: print str(e) c_value = np.zeros(p) # c_value[np.abs(c_value) <= 1e-4] = 0 # assert np.all(c_value >= 0) c_value[c_value < 0] = 0 return c_value
def gel_solve_cvx(As, y, l_1, l_2, ns): """Solve a group elastic net problem with cvx. Arguments: As: list of numpy arrays. y: numpy array. l_1, l_2: floats. ns: numpy array. """ # Create the b variables b_0 = cvx.Variable() bs = [] for _, n_j in zip(As, ns): bs.append(cvx.Variable(n_j)) # Form g(b) Ab = sum(A_j * b_j for A_j, b_j in zip(As, bs)) m = As[0].shape[0] g_b = cvx.square(cvx.norm2(y - b_0 - Ab)) / (2 * m) # Form h(b) h_b = sum( np.sqrt(n_j) * (l_1 * cvx.norm2(b_j) + l_2 * cvx.square(cvx.norm2(b_j))) for n_j, b_j in zip(ns, bs)) # Build the optimization problem obj = cvx.Minimize(g_b + h_b) problem = cvx.Problem(obj, constraints=None) problem.solve(solver="CVXOPT") b_0 = b_0.value # Form B as returned by gel_solve p = len(As) B = torch.zeros(p, int(max(ns))) for j in range(p): b_j = np.asarray(bs[j].value) if ns[j] == 1: b_j = b_j.reshape(1) else: b_j = b_j[:, 0] B[j, :ns[j]] = torch.from_numpy(b_j.astype(np.float32)) return b_0, B
def find_equilibrium3(log_met_bounds, fullS, mu0, R, T, efflux_mets, uptake_mets, ref_mols=1e-3 ): """ Find the equilibrium given the fixed metabolites in met_bounds """ rxns, mets = fullS.columns, fullS.index log_c = cvx.Variable(len(mets)) #r = cvx.Variable(len(rxns)) mu = log_c*R*T + mu0.values efflux_idx = [mets.get_loc(e) for e in efflux_mets] uptake_idx = [mets.get_loc(u) for u in uptake_mets] deltaG = fullS.T.as_matrix()*mu min_ref_mols_obj = cvx.Minimize(cvx.norm2(log_c -np.log(ref_mols))) metabolite_constraint = [log_c[mets.get_loc(met)] == log_met_bounds[met] for met in log_met_bounds.index] equilibrium_constraint = [deltaG == 0] p = cvx.Problem(min_ref_mols_obj, equilibrium_constraint + metabolite_constraint) # + equilibrium_constraint) p.solve() #p = cvx.Problem( cvx.Minimize(cvx.norm2( deltaG )), # metabolite_constraint) #p.solve(verbose=True) mu_efflux = pd.Series(cvx2a(mu[efflux_idx].value),index=efflux_mets) mu_uptake = pd.Series(cvx2a(mu[uptake_idx].value),index=uptake_mets) G_products = fullS.T[efflux_mets].dot(mu_efflux) G_reactants = fullS.T[uptake_mets].dot(mu_uptake) potential=pd.DataFrame({'c': np.exp(cvx2a(log_c.value)), '$\log c$': cvx2a(log_c.value), '$RT\log c$':R*T*cvx2a(log_c.value), '$\mu^0$': mu0, '$\mu$':cvx2a(mu.value)},index=mets) deltaG = pd.DataFrame({'$\Delta G$':cvx2a(deltaG.value), '$G_{products}$': G_products , '$G_{reactants}$': G_reactants},index=rxns) total_reactant_potential = (fullS.T[uptake_mets]*cvx2a(mu[uptake_idx].value)).T total_reactant_potential['stoichiometry'] = fullS.T[uptake_mets].squeeze() total_reactant_potential['$\mu$'] = cvx2a(mu[uptake_idx].value) total_product_potential = (fullS.T[efflux_mets]*cvx2a(mu[efflux_idx].value)).T total_product_potential['stoichiometry'] = fullS.T[efflux_mets].squeeze() total_product_potential['$\mu$'] = cvx2a(mu[efflux_idx].value) return dict(potential= potential, deltaG=deltaG, total_reactant_potential=total_reactant_potential, total_product_potential = total_product_potential, metabolite_constraint=pd.DataFrame({'metabolite_constraint':[m.dual_value for m in metabolite_constraint]} ,index=log_met_bounds.index), equilibrium_constraint = pd.DataFrame({'equilibrium_constraint':[e.dual_value for e in equilibrium_constraint]}, index=rxns), status=p.status)
def caculateG_for_bounded_uncertainty(lam, N, M, Q, A, b, W, H, eta, etab): x = cvx.Variable(N) obj = cvx.quad_form(x, Q) + cvx.quad_form(A @ x - b.flatten(), W + (W @ H @ (np.linalg.pinv(lam * np.eye(M) - (H.T)@W@H))@(H.T) @ W)) + \ lam*(eta**2)*cvx.quad_form(x, np.eye(M)) + 2*lam*eta*etab*cvx.norm2(x) + lam*(etab**2) prob = cvx.Problem(cvx.Minimize(obj)) prob.solve(solver=solver) return prob.value
def test_ecos_solve(self): np.random.seed(0) m = 20 n = 10 A, b, c, cone_dims = utils.least_squares_eq_scs_data(m, n) cone_dims.pop("q") cone_dims.pop("s") cone_dims.pop("ep") x, y, s, derivative, adjoint_derivative = cone_prog.solve_and_derivative( A, b, c, cone_dims, solve_method="ECOS") # check optimality conditions np.testing.assert_allclose(A @ x + s, b, atol=1e-8) np.testing.assert_allclose(A.T @ y + c, 0, atol=1e-8) np.testing.assert_allclose(s @ y, 0, atol=1e-8) np.testing.assert_allclose(s, cone_lib.pi( s, cone_lib.parse_cone_dict(cone_dims), dual=False), atol=1e-8) np.testing.assert_allclose(y, cone_lib.pi( y, cone_lib.parse_cone_dict(cone_dims), dual=True), atol=1e-8) x = cp.Variable(10) prob = cp.Problem( cp.Minimize( cp.sum_squares(np.random.randn(5, 10) @ x) + np.random.randn(10) @ x), [ cp.norm2(x) <= 1, np.random.randn(2, 10) @ x == np.random.randn(2) ]) A, b, c, cone_dims = utils.scs_data_from_cvxpy_problem(prob) x, y, s, derivative, adjoint_derivative = cone_prog.solve_and_derivative( A, b, c, cone_dims, solve_method="ECOS") # check optimality conditions np.testing.assert_allclose(A @ x + s, b, atol=1e-8) np.testing.assert_allclose(A.T @ y + c, 0, atol=1e-8) np.testing.assert_allclose(s @ y, 0, atol=1e-8) np.testing.assert_allclose(s, cone_lib.pi( s, cone_lib.parse_cone_dict(cone_dims), dual=False), atol=1e-8) np.testing.assert_allclose(y, cone_lib.pi( y, cone_lib.parse_cone_dict(cone_dims), dual=True), atol=1e-8)
def groupRegularizer(groups, *args): exprs = list() if len(args) != len(groups): # if we do not want/cannot provide a lambda for each group rest = np.ones(len(groups) - len(args)) args = list(args) args.extend(rest) for gr, group in enumerate(groups): exprs.append(args[gr] * cp.norm2(cp.hstack(group[0]))) # we assume every arg is a lambda return sum(exprs)
def estimate_c(self, data): x = data.x[data.is_labeled & data.is_train] if self.transform is not None: x = self.transform.fit_transform(x) y = data.y[data.is_labeled] if self.label_transform is not None: y = self.label_transform.fit_transform(y) n = y.size p = data.p c = cvx.Variable(len(self.source_w)) # ws1 = self.source_w[0] # ws2 = self.source_w[1] ws = 0 for i, wsi in enumerate(self.source_w): ws += wsi * c[i] constraints = [c >= 0] constraint_methods = {HypothesisTransfer.WEIGHTS_JUST_OPTIMAL, HypothesisTransfer.WEIGHTS_JUST_FIRST} found_first = False if self.weight_type in constraint_methods: for i in range(c.size[0]): id = i + 1 is_oracle = id in self.oracle_data_set_ids just_first = self.weight_type == HypothesisTransfer.WEIGHTS_JUST_FIRST and found_first if is_oracle and not just_first: found_first = True continue constraints.append(c[i] == 0) loss = 0 for i in range(y.size): xi = x[i, :] yi = y[i] x_mi = np.delete(x, i, axis=0) y_mi = np.delete(y, i, axis=0) b_mi = y_mi.mean() A = x_mi.T.dot(x_mi) + (self.C + self.C2) * np.eye(p) k = x_mi.T.dot(y_mi) - x_mi.T.sum(1) * b_mi + self.C2 * ws w_mi = scipy.linalg.inv(A) * k loss += cvx.power(w_mi.T * xi + b_mi - yi, 2) reg = cvx.norm2(c) ** 2 # reg = cvx.norm2(c) obj = cvx.Minimize(loss + self.C3 * reg) prob = cvx.Problem(obj, constraints) assert prob.is_dcp() try: prob.solve(cvx.SCS) c_value = np.asarray(c.value) except Exception as e: print str(e) c_value = np.zeros(p) # c_value[np.abs(c_value) <= 1e-4] = 0 # assert np.all(c_value >= 0) c_value[c_value < 0] = 0 return c_value
def calc_Koopman(Yf, Yp, flag=1): solver_instance = cvxpy.CVXOPT #solver_instance = cvxpy.ECOS; if flag == 1: # moore penrose inverse, plain ol' least squares Koopman #Yp_inv = np.dot(np.transpose(Yp_final), np.linalg.inv( np.dot(Yp_final,np.transpose(Yp_final)) ) ); Yp_inv = np.linalg.pinv(Yp) K = np.dot(Yf, Yp_inv) if flag == 2: # cvx optimization approach - L2 + L1 lasso norm1_term = 0.0 all_col_handles = [None] * Yf.shape[0] for i in range(0, Yf.shape[0]): all_col_handles[i] = Variable(Yf.shape[0], 1) norm1_term = norm1_term + norm2(all_col_handles[i]) operator = all_col_handles[0] for i in range(1, Yf.shape[0]): operator = cvxpy.hstack(operator, all_col_handles[i]) print "[INFO]: CVXPY Koopman operator variable: " + repr(operator) print "[INFO]: Yf.shape in calc_Koopman: " + repr(Yf.shape) norm2_fit_term = norm2(norm2(Yf - operator * Yp, axis=0)) objective = Minimize(norm2_fit_term + norm1_term) constraints = [] prob = Problem(objective, constraints) result = prob.solve(verbose=True, solver=solver_instance) print "[INFO]: Finished executing cvx solver, printing CVXPY problem status" print(prob.status) K = operator.value if flag == 3: operator = Variable(Yf.shape[0], Yf.shape[0]) objective = Minimize(cvxpynorm(operator, 2)) constraints = [ cvxpynorm(Yf - operator * Yp, 'fro') / cvxpynorm(Yf, 'fro') < 0.01 ] prob = Problem(objective, constraints) result = prob.solve(verbose=True) #(solver=solver_instance); print(prob.status) K = operator.value return K
def zero_norm_inner(self, x_old, A_diff, Lambda): # This function is used to L0 optimization n = np.shape(x_old)[0] x = cp.Variable(n) y = cp.Variable(n - 1) cost = (1 - Lambda) * cp.norm2(x_old - x) + Lambda * cp.norm1(y) prob = cp.Problem(cp.Minimize(cost), [y == A_diff @ x]) prob.solve(solver='SCS') return x.value
def get_coeff(self, X, y, *args): lamb = args[0] if args else 0 x = cp.Variable((1, X.shape[0])) objective_function = cp.Minimize(lamb * cp.norm1(x) + cp.norm2(x @ X - y)) constraints = [] prob = cp.Problem(objective_function, constraints) prob.solve(solver=cp.SCS) # print(prob.status) assert str(prob.status) == 'optimal', 'solve regression problem failed' return x.value
def constrained_l2_estimator(A, Y, alpha, **kwargs): x_sharp = cvx.Variable(rows=A.shape[1], cols=A.shape[2]) objective = cvx.Minimize(cvx.norm2(Y - _expval(A, x_sharp))) constraints = [cvx.norm(x_sharp, p='nuc') <= alpha] problem = cvx.Problem(objective, constraints) problem.solve(**kwargs) if problem.status not in ['optimal']: raise ValueError("Optimization did not converge: " + problem.status) return np.asarray(x_sharp.value)
def sep_hyp(lh_set, rh_set): w = cp.Variable(*lh_set.size) p = cp.Problem(cp.Minimize(cp.norm2(w)), [lh_set - rh_set == w]) p.solve() # Normal vector to the hyperplane. normal = p.constraints[0].dual_value # A point on the hyperplane. point = (lh_set.value + rh_set.value)/2 # The offset of the hyperplane. offset = normal.T*point return (normal, offset[0])
def sep_hyp(lh_set, rh_set): w = cp.Variable(*lh_set.size) p = cp.Problem(cp.Minimize(cp.norm2(w)), [lh_set - rh_set == w]) p.solve() # Normal vector to the hyperplane. normal = p.constraints[0].dual_value # A point on the hyperplane. point = (lh_set.value + rh_set.value) / 2 # The offset of the hyperplane. offset = normal.T * point return (normal, offset[0])
def set_objective(self, X, y, lmbd): self.X, self.y, self.lmbd = X, y, lmbd n_features = self.X.shape[1] self.beta = cp.Variable(n_features) loss = 0.5 * cp.norm2(self.y - cp.matmul(self.X, self.beta))**2 self.problem = cp.Problem( cp.Minimize(loss + self.lmbd * cp.norm(self.beta, 1))) cp.settings.ERROR = ['solver_error']
def residual(self): # TODO: The projection should be implemented directly. from cvxpy import Minimize, Problem, Variable, hstack, norm2 if self.W.value is None or self.z.value is None: return None W = Variable(self.W.shape) z = Variable(self.z.shape) constr = [PowConeND(W, z, self.alpha, axis=self.axis)] obj = Minimize(norm2(hstack([W.flatten(), z.flatten()]) - hstack([self.W.flatten().value, self.z.flatten().value]))) problem = Problem(obj, constr) return problem.solve(solver='SCS', eps=1e-8)
def residual(self): # TODO(akshayka): The projection should be implemented directly. from cvxpy import Problem, Minimize, Variable, norm2, hstack if self.x.value is None or self.y.value is None or self.z.value is None: return None x = Variable(self.x.shape) y = Variable(self.y.shape) z = Variable(self.z.shape) constr = [ExpCone(x, y, z)] obj = Minimize(norm2(hstack([x, y, z]) - hstack([self.x.value, self.y.value, self.z.value]))) problem = Problem(obj, constr) return problem.solve()
def residual(self): # TODO: The projection should be implemented directly. from cvxpy import Minimize, Problem, Variable, hstack, norm2 if self.x.value is None or self.y.value is None or self.z.value is None: return None x = Variable(self.x.shape) y = Variable(self.y.shape) z = Variable(self.z.shape) constr = [PowCone3D(x, y, z, self.alpha)] obj = Minimize(norm2(hstack([x, y, z]) - hstack([self.x.value, self.y.value, self.z.value]))) problem = Problem(obj, constr) return problem.solve(solver='SCS', eps=1e-8)
def cvx_test(*args): n = 5000 p = 100 X = np.random.uniform(-1, 1, (n, p)) C = 1e-3 y = np.random.uniform(-1, 1, n) w = cvx.Variable(p) loss = cvx.sum_entries(cvx.square(X * w - y)) reg = cvx.norm2(w)**2 obj = cvx.Minimize(loss + C * reg) prob = cvx.Problem(obj, []) tic() prob.solve(solver=cvx.SCS, verbose=False) toc()
def cvx_test(*args): n = 5000 p = 100 X = np.random.uniform(-1,1,(n,p)) C = 1e-3 y = np.random.uniform(-1,1, n) w = cvx.Variable(p) loss = cvx.sum_entries(cvx.square(X*w - y)) reg = cvx.norm2(w)**2 obj = cvx.Minimize(loss + C*reg) prob = cvx.Problem(obj, []) tic() prob.solve(solver=cvx.SCS, verbose=False) toc()
def glrd_sparse(V, G, F, r, err_V, err_F): # sparsity threshold is num_nodes / num_roles for k in xrange(r): R = V - get_residual(G, F, k) # compute residual # Solve for optimal G(.)(k) with sparsity constraints F_k = F[k, :] x_star_G = linalg.lstsq(R.T, F_k.T)[0].T x_G = cvx.Variable(x_star_G.shape[0]) objective_G = cvx.Minimize(cvx.norm2(x_star_G - x_G)) constraints_G = [x_G >= 0] constraints_G += [cvx.norm1(x_G) <= err_V] prob_G = cvx.Problem(objective_G, constraints_G) result = prob_G.solve(solver='SCS') if not np.isinf(result): G_k_min = np.asarray(x_G.value) G[:, k] = G_k_min[:, 0] else: print result # Solve for optimal F(k)(.) with sparsity constraints G_k = G[:, k] x_star_F = linalg.lstsq(R, G_k)[0] x_F = cvx.Variable(x_star_F.shape[0]) objective_F = cvx.Minimize(cvx.norm2(x_star_F - x_F)) constraints_F = [x_F >= 0] constraints_F += [cvx.sum_entries(x_F) <= err_F] prob_F = cvx.Problem(objective_F, constraints_F) result = prob_F.solve(solver='SCS') if not np.isinf(result): F_k_min = np.asarray(x_F.value) F[k, :] = F_k_min[0, :] else: print result return G, F
def find_decoders_cvxopt(self, values, neus_in_pop, min_w = 0 , max_w = 1): ''' find optimal decoders using convex bounded optimization ''' tuning = self.tuning_curves[neus_in_pop] A=np.array(tuning) A_m = np.matrix(np.matrix(A)) aa = cx.zeros(np.shape(A)) aa[A_m.nonzero()] = A_m[A_m.nonzero()] bb = cx.zeros(np.shape(value)) bb[value.nonzero()[0]] = value[value.nonzero()[0]] m,n = np.shape(aa) dec = cx.variable(m) p = cx.program(cx.minimize(cx.norm2((aa)*(dec)-(bb))), [cx.leq(dec,max_w),cx.geq(dec,min_w)]) p.solve() return dec.value
def project(W, **kw): verbose = kw.get('verbose', False) bound = kw.get('bound', 0.) M = len(W) V = cvx.Variable(M) p = cvx.Problem(cvx.Minimize(cvx.norm2(V-W)), [cvx.sum_entries(V) == 1., V >= bound]) try: p.solve(max_iters=100000, verbose=verbose) except: p.solve(solver="SCS", max_iters=100000, verbose=verbose) w = np.array(p.variables()[0].value).flatten() if w.min() < 0.: w[w < 0.] = 0. w = project(w) return w
def test_problem_penalty(self): """ Compare cvxpy solutions to cvxopt ground truth """ from cvxpy import (matrix,variable,program,minimize, sum,abs,norm2,log,square,zeros,max, hstack,vstack) m, n = self.m, self.n A = matrix(self.A) b = matrix(self.b) # set tolerance to 5 significant digits tol_exp = 5 # l1 approximation x = variable(n) p = program(minimize(sum(abs(A*x + b)))) p.solve(True) np.testing.assert_array_almost_equal(x.value,self.x1,tol_exp) # l2 approximation x = variable(n) p = program(minimize(norm2(A*x + b))) p.solve(True) np.testing.assert_array_almost_equal(x.value,self.x2,tol_exp) # Deadzone approximation - implementation is currently ugly (need max along axis) x = variable(n) Axbm = abs(A*x+b)-0.5 Axbm_deadzone = vstack([max(hstack((Axbm[i,0],0.0))) for i in range(m)]) p = program(minimize(sum(Axbm_deadzone))) p.solve(True) obj_dz_cvxpy = np.sum(np.max([np.abs(A*x.value+b)-0.5,np.zeros((m,1))],axis=0)) np.testing.assert_array_almost_equal(obj_dz_cvxpy,self.obj_dz,tol_exp) # Log barrier x = variable(n) p = program(minimize(-sum(log(1.0-square(A*x + b))))) p.solve(True) np.testing.assert_array_almost_equal(x.value,self.cxlb,tol_exp)
def FindMtdf_Regularized(self, c_range=(1e-6, 1e-2), bounds=None, c_mid=1e-3, min_mtdf=None, max_mtdf=None): """Find the MTDF (Maximal Thermodynamic Driving Force). Uses l2 regularization to minimize the log difference of concentrations from c_mid. Args: c_range: a tuple (min, max) for concentrations (in M). bounds: a list of (lower bound, upper bound) tuples for compound concentrations. c_mid: the defined midpoint concentration. max_mtdf: the maximum value for the motive force. Returns: A 3 tuple (optimal dGfs, optimal concentrations, optimal mtdf). """ ln_conc, motive_force_lb, constraints = self._MakeMtdfProblem(c_range, bounds) # Set the objective and solve. norm2_resid = cvxpy.norm2(ln_conc - np.log(c_mid)) if max_mtdf is not None and min_mtdf is not None: constraints.append(cvxpy.leq(motive_force_lb, max_mtdf)) constraints.append(cvxpy.geq(motive_force_lb, min_mtdf)) objective = cvxpy.minimize(norm2_resid) elif max_mtdf is not None: constraints.append(cvxpy.leq(motive_force_lb, max_mtdf)) objective = cvxpy.minimize(norm2_resid) elif min_mtdf is not None: constraints.append(cvxpy.geq(motive_force_lb, min_mtdf)) objective = cvxpy.minimize(norm2_resid) else: objective = cvxpy.minimize(motive_force_lb + norm2_resid) program = cvxpy.program(objective, constraints) program.solve(quiet=True) return ln_conc.value, program.objective.value
def prox(self, x0, phi0): terms = [] for a, x in self._xs.iteritems(): for g, v in x.iteritems(): terms.append(v - x0[a][g]) for g, v in self._phi.iteritems(): terms.append(v - phi0[g]) prob = cvx.Problem(cvx.Minimize(cvx.norm2(cvx.vstack(*terms))), self._constr) prob.solve(solver=cvx.SCS, verbose=False) assert prob.status == 'optimal' xs = {} for a, x in self._xs.iteritems(): # take the positive part of the good allocations. (they should # be positive by the constraints anyway, but do this to polish # solution xs[a] = {g: max(v.value, 0) for g, v in x.iteritems()} phi = {g: v.value for g, v in self._phi.iteritems()} return xs, phi
def compute_decoders_convex_bounded(x_values,A,nsteps,function=lambda x:x, min_x = -0.2, max_x = 0.2): '''solve convex optimization problem with cvxpy ''' import cvxpy as cx import numpy as np A_m = np.matrix(np.matrix(A)) aa = cx.zeros(np.shape(A)) aa[A_m.nonzero()] = A_m[A_m.nonzero()] #function to be encoded value=np.array([[function(x)] for x in x_values]) value=np.reshape(value,nsteps) bb = cx.zeros(np.shape(value)) bb[0,value.nonzero()[0]] = value[value.nonzero()[0]] m,n = np.shape(aa) dec = cx.variable(m) p = cx.program(cx.minimize(cx.norm2(np.transpose(aa)*(dec)-np.transpose(bb))), [cx.leq(dec,max_x),cx.geq(dec,min_x)]) p.solve() return dec.value
def create(m, ni, K): np.random.seed(0) part = np.random.randint(1, ni, K) n = np.sum(part) p = 0.2 # Each part is pa[i]:pb[i] pb = np.cumsum(part) pa = np.hstack((0, pb[:-1])) x0 = np.zeros(n) for i in xrange(K): if np.random.rand() < p: x0[pa[i]:pb[i]] = np.random.randn(part[i]) A = problem_util.normalized_data_matrix(m, n, 1) b = A.dot(x0) + np.sqrt(0.001)*np.random.randn(m) lam = 0.1*max(LA.norm(A[:,pa[i]:pb[i]].T.dot(b)) for i in xrange(K)) x = cp.Variable(n) f = (0.5*cp.sum_squares(A*x - b) + lam*sum(cp.norm2(x[pa[i]:pb[i]]) for i in xrange(K))) return cp.Problem(cp.Minimize(f))
def C_soc_translated(): return [cp.norm2(x + randn()) <= t + randn()]
def ACT2Corrected(self,gene,num_iterations=5): """ Next steps: Some way to preserve flows at divergence nodes One way could be reallocate flows at all divergence nodes in the original ratio and fix it Iterate 10 times """ inwgs=self.wgsdict[gene] outwgs=inwgs component1=1.0 for iteri in range(num_iterations): component1=1.0-iteri*1.0/num_iterations wgs=addwgs(inwgs,outwgs,component1) A,B,X=self.wgs2problem(wgs) Xvar = cvx.variable(len(X),1) A=cvx.matrix(A) B=cvx.matrix(B) B=B.T p = cvx.program(cvx.minimize(cvx.norm2(A*Xvar-B)),[cvx.geq(Xvar,0.0)]) try: p.solve(quiet=1) except: message='Could not solve for %s'%(gene) common.printstatus(message,'W',common.func_name(),1) return (outwgs,100.0) if iteri==0: # Get optimal value err=cvx.norm2(A*Xvar-B) #print err.value/len(X) Xval=Xvar.T.value.tolist()[0] X_corr= [a[:] for a in X] for i in range(len(Xval)): X_corr[i][3]=int(Xval[i]*100)/100.0 #print X_corr exonlist=[[a[1],a[2]] for a in X_corr if a[0]==2] exonwtlist=[a[3] for a in X_corr if a[0]==2] #print 'E',exonlist intronlist=[] intronwtlist=[] splicelist=[[a[1],a[2]] for a in X_corr if a[0]==3] splicewtlist=[a[3] for a in X_corr if a[0]==3] removelist=[] for i in range(len(exonlist)): exon=exonlist[i] if exon in splicelist: exonwt=exonwtlist[i] intronlist.append([exon[0]+1,exon[1]-1]) intronwtlist.append(exonwt) removelist.append(i) removelist.reverse() for i in removelist: exonlist.pop(i) exonwtlist.pop(i) #print 'E',exonlist startnodelist=[a[1]for a in X_corr if a[0]==1] endnodelist=[a[1]for a in X_corr if a[0]==-1] novelnodelist=wgs[5] #print exonlist #print wgs[0] #print intronlist #print wgs[1] exonwtlist1=[exonwtlist[i] for i in range(len(exonwtlist)) if exonlist[i] in wgs[0]] intronwtlist1=[exonwtlist[i] for i in range(len(exonwtlist)) if exonlist[i] in wgs[1]] #wgrstuple=(exonlist,intronlist,splicelist,startnodelist,endnodelist,novelnodelist,exonwtlist,intronwtlist,splicewtlist) outwgs=(wgs[0],wgs[1],splicelist,wgs[3],wgs[4],novelnodelist,exonwtlist1,intronwtlist1,splicewtlist) return (outwgs,err.value/len(X))
np.random.seed(1) (m, n) = (70, 500) k = 10 z_true = np.zeros(n, dtype=complex) z_true[0:k] = np.random.randn(k) + 1j * np.random.randn(k) np.random.shuffle(z_true); z_true = np.asmatrix(z_true).T A = np.random.randn(m, n) + 1j * np.random.randn(m, n) A = np.asmatrix(A) b = A*z_true; b = np.asmatrix(b) zr = cvx.Variable(n) zi = cvx.Variable(n) t = cvx.Variable(m) obj = cvx.Minimize(cvx.sum_entries(t)) #AA = cvx.norm2(cvx.hstack(cvx.vstack(A.real,A.imag),cvx.vstack(-A.imag,A.real))) z = cvx.vstack(zr,zi) constraints = [ cvx.norm2(cvx.hstack(cvx.vstack(A[i,:].real,A[i,:].imag), cvx.vstack(-A[i,:].imag,A[i,:].real))*z -cvx.vstack(b[i].real,b[i].imag)) <= t[i] for i in range(m)] ''' constraints = [ cvx.norm2(cvx.hstack(cvx.vstack(A.real,A.imag),cvx.vstack(-A.imag,A.real))*z - cvx.vstack(b.real,b.imag)) <= t] ''' prob = cvx.Problem(obj, constraints) sol = prob.solve(solver=cvx.ECOS) print 'status: {}'.format(prob.status) print 'norm z: {}'.format(cvx.norm2(z).value) print 'norm z_true: {}'.format((cvx.norm2(z_true.real)+cvx.norm2(z_true.imag)).value) for i in range(n): print '{} + {}j' .format(zr[i].value,zi[i].value)
def sparse_kmeans(AllDataMatrix,s,niter,group): w = [1/np.sqrt(AllDataMatrix.shape[1])]*AllDataMatrix.shape[1] wx = np.zeros((len(AllDataMatrix),AllDataMatrix.shape[1])) for j in range(AllDataMatrix.shape[1]): wx[:,j] = AllDataMatrix[:,j]*(np.sqrt(w)[j]) alpha_group = s s_orig = s nclust = 6 #kmt = KMeans(n_clusters=nclust, init='random', n_init=100,verbose=False, tol=0.0000000001) #kmt.fit(wx) #print kmt.labels_ #print adjusted_rand_score(labels, kmt.labels_) kmt = rkmeans(x=numpy2ri(wx),centers=nclust,nstart=100) kmlabels = np.array(kmt[0]) print adjusted_rand_score(labels, np.array(kmt[0])) #overall iterations for i in range(niter): #print i #1.get bcssj aj_list = [] for j in range(AllDataMatrix.shape[1]): dat_j = AllDataMatrix[:,j].reshape((len(AllDataMatrix),1)) djall = euclidean_distances(dat_j, dat_j) sumd_all = np.sum(djall**2)/len(AllDataMatrix) nk_list = [];sumd_k_list = [] for k in range(nclust): dat_j = AllDataMatrix[kmlabels==k,j] dat_j = dat_j.reshape((len(dat_j),1)) if(len(dat_j)<1): d = 0 else: d = euclidean_distances(dat_j, dat_j) nk = len(dat_j) sumd_k = np.sum(d**2) nk_list.append(nk) sumd_k_list.append(sumd_k) nk_list = np.array(nk_list) sumd_k_list = np.array(sumd_k_list) #compute within-sum of squares over feature j nk_list[nk_list==0] = -1 one_nk_list = 1./nk_list one_nk_list[np.sign(one_nk_list)== -1 ] = 0 withinssj = np.sum(one_nk_list*sumd_k_list) #aj = totalss/n - wss/nk aj = sumd_all - withinssj aj_list.append(aj) #2. get w a = np.array(aj_list) lenseq = np.array([256,128,64,32,16,8,4,2,1,1]) lenseq = np.array([256,128,64,32,16,8,8]) nlevels = len(lenseq) sqrtlenseq = np.sqrt(lenseq) indseq = np.cumsum(lenseq) wvar = cvx.Variable(len(a)) t = cvx.Variable(nlevels) ## Form objective. if group: ####GROUP SPARSE #obj = cvx.Minimize(sum(-1*a*wvar) + alpha_group*sum(t)) obj = cvx.Minimize(sum(-1*a*wvar)) group0 = [cvx.norm(wvar[0:(indseq[0]-1)],2)<=t[0]] group1 = [cvx.norm(wvar[indseq[0]:(indseq[1])],2)<=t[1]] group2 = [cvx.norm(wvar[indseq[1]:(indseq[2])],2)<=t[2]] group3 = [cvx.norm(wvar[indseq[2]:(indseq[3])],2)<=t[3]] group4 = [cvx.norm(wvar[indseq[3]:(indseq[4])],2)<=t[4]] group5 = [cvx.norm(wvar[indseq[4]:(indseq[5])],2)<=t[5]] group6 = [cvx.norm(wvar[indseq[5]:(indseq[6])],2)<=t[6]] # group0 = [cvx.norm(wvar[0:indseq[0]],2)<=t[0]] # group1 = [cvx.norm(wvar[indseq[0]:indseq[1]],2)<=t[1]] # group0 = [sqrtlenseq[0]*cvx.norm(wvar[0:(indseq[0]-1)],2)<=t[0]] # group1 = [sqrtlenseq[1]*cvx.norm(wvar[indseq[0]:(indseq[1])],2)<=t[1]] # group2 = [sqrtlenseq[2]*cvx.norm(wvar[indseq[1]:(indseq[2])],2)<=t[2]] # group3 = [sqrtlenseq[3]*cvx.norm(wvar[indseq[2]:(indseq[3])],2)<=t[3]] # group4 = [sqrtlenseq[4]*cvx.norm(wvar[indseq[3]:(indseq[4])],2)<=t[4]] # group5 = [sqrtlenseq[5]*cvx.norm(wvar[indseq[4]:(indseq[5])],2)<=t[5]] # group6 = [sqrtlenseq[6]*cvx.norm(wvar[indseq[5]:(indseq[6])],2)<=t[6]] # #group7 = [cvx.norm(wvar[indseq[6]:(indseq[7])],2)<=t[7]] # group8 = [cvx.norm(wvar[indseq[7]:(indseq[8])],2)<=t[8]] # group9 = [cvx.norm(wvar[indseq[8]:(indseq[9])],2)<=t[9]] ###"correct" constraints #constr = [wvar>=0,sum(wvar)==1] + group0 + group1 + group2 + group3 + group4 + group5 + group6 ##l2 constraints #constr = [cvx.square(cvx.norm2(wvar))<=1,wvar>=0] + group0 + group1 + group2 + group3 + group4 + group5 + group6 + group7 + group8 + group9 #constr = [cvx.square(cvx.norm2(wvar))<=1,wvar>=0] + group0 + group1 + group2 + group3 + group4 + group5 + group6 + group7 constr = [cvx.square(cvx.norm2(wvar))<=1,wvar>=0] + group0 + group1 + group2 + group3 + group4 + group5 + group6 constr = constr + [sum(t)<=alpha_group]#cvx.norm1(wvar)<=s_orig ####GROUP NORM AS IN LASSO # groupnormvec = [cvx.norm(wvar[0:(indseq[0]-1)],2),cvx.norm(wvar[indseq[0]:(indseq[1])],2), # cvx.norm(wvar[indseq[1]:(indseq[2])],2),cvx.norm(wvar[indseq[2]:(indseq[3])],2), # cvx.norm(wvar[indseq[3]:(indseq[4])],2),cvx.norm(wvar[indseq[4]:(indseq[5])],2), # cvx.norm(wvar[indseq[5]:(indseq[6])],2)] # obj = cvx.Minimize(sum(-1*a*wvar) + alpha_group*sum(groupnormvec)) # constr = [cvx.square(cvx.norm2(wvar))<=1,wvar>=0] else: ####ORIGINAL SPARSE KMEANS PROBLEM #obj = cvx.Minimize(cvx.sum(cvx.mul_elemwise(-1*a,wvar))) obj = cvx.Minimize(sum(-1*a*wvar)) constr = [cvx.square(cvx.norm2(wvar))<=1,cvx.norm1(wvar)<=s_orig, wvar>=0] #constr = [cvx.square(cvx.norm2(wvar))<=1, wvar>=0] prob = cvx.Problem(obj, constr) #prob.solve() try: prob.solve() #print "default solver" except: #print "SCS SOLVER" #prob.solve(solver =cvx.CVXOPT) prob.solve(solver = cvx.SCS,verbose=False)#use_indirect=True #print prob.value w = wvar.value #3. update kmeans wx = np.zeros((len(AllDataMatrix),AllDataMatrix.shape[1])) for j in range(AllDataMatrix.shape[1]): wj = np.sqrt(w[j][0,0]) #wj = w[j][0,0] if np.isnan(wj): #print "bad" wj = 10**-20 # else: # #print "yes" # #print wj wx[:,j] = AllDataMatrix[:,j]*wj # kmt = KMeans(n_clusters=nclust, init='random', n_init=100,verbose=False,tol=0.0000000001) # kmt.fit(wx) kmt = rkmeans(x=numpy2ri(wx),centers=nclust,nstart=100) kmlabels = np.array(kmt[0]) return prob.value,kmlabels
group2 = [sqrtlenseq[2]*cvx.norm(wvar[indseq[1]:(indseq[2])],2)<=t[2]] group3 = [sqrtlenseq[3]*cvx.norm(wvar[indseq[2]:(indseq[3])],2)<=t[3]] group4 = [sqrtlenseq[4]*cvx.norm(wvar[indseq[3]:(indseq[4])],2)<=t[4]] group5 = [sqrtlenseq[5]*cvx.norm(wvar[indseq[4]:(indseq[5])],2)<=t[5]] group6 = [sqrtlenseq[6]*cvx.norm(wvar[indseq[5]:(indseq[6])],2)<=t[6]] # #group7 = [cvx.norm(wvar[indseq[6]:(indseq[7])],2)<=t[7]] # group8 = [cvx.norm(wvar[indseq[7]:(indseq[8])],2)<=t[8]] # group9 = [cvx.norm(wvar[indseq[8]:(indseq[9])],2)<=t[9]] ###"correct" constraints #constr = [wvar>=0,sum(wvar)==1] + group0 + group1 + group2 + group3 + group4 + group5 + group6 ##l2 constraints #constr = [cvx.square(cvx.norm2(wvar))<=1,wvar>=0] + group0 + group1 + group2 + group3 + group4 + group5 + group6 + group7 + group8 + group9 #constr = [cvx.square(cvx.norm2(wvar))<=1,wvar>=0] + group0 + group1 + group2 + group3 + group4 + group5 + group6 + group7 constr = [cvx.square(cvx.norm2(wvar))<=1,wvar>=0] + group0 + group1 + group2 + group3 + group4 + group5 + group6 constr = constr + [sum(t)<=alpha_group]#cvx.norm1(wvar)<=s_orig ####GROUP NORM AS IN LASSO # groupnormvec = [cvx.norm(wvar[0:(indseq[0]-1)],2),cvx.norm(wvar[indseq[0]:(indseq[1])],2), # cvx.norm(wvar[indseq[1]:(indseq[2])],2),cvx.norm(wvar[indseq[2]:(indseq[3])],2), # cvx.norm(wvar[indseq[3]:(indseq[4])],2),cvx.norm(wvar[indseq[4]:(indseq[5])],2), # cvx.norm(wvar[indseq[5]:(indseq[6])],2)] # obj = cvx.Minimize(sum(-1*a*wvar) + alpha_group*sum(groupnormvec)) # constr = [cvx.square(cvx.norm2(wvar))<=1,wvar>=0] else: ####ORIGINAL SPARSE KMEANS PROBLEM #obj = cvx.Minimize(cvx.sum(cvx.mul_elemwise(-1*a,wvar))) obj = cvx.Minimize(sum(-1*a*wvar)) constr = [cvx.square(cvx.norm2(wvar))<=1,cvx.norm1(wvar)<=s_orig, wvar>=0]
def sparse_kmeans(AllDataMatrix, nclust, s, niter, group=False, tree=False, multi=False, groups_vector=None): w = [1 / np.sqrt(AllDataMatrix.shape[1])] * AllDataMatrix.shape[1] wx = np.zeros((len(AllDataMatrix), AllDataMatrix.shape[1])) for j in range(AllDataMatrix.shape[1]): wx[:, j] = AllDataMatrix[:, j] * (np.sqrt(w)[j]) alpha_group = s s_orig = s print nclust kmt = KMeans(n_clusters=nclust, init="k-means++", max_iter=200, n_init=100) kmt.fit(wx) kmlabels = np.array(kmt.labels_) for i in range(niter): aj_list = [] for j in range(AllDataMatrix.shape[1]): dat_j = AllDataMatrix[:, j].reshape((len(AllDataMatrix), 1)) djall = euclidean_distances(dat_j, dat_j) sumd_all = np.sum(djall ** 2) / len(AllDataMatrix) nk_list = [] sumd_k_list = [] for k in range(nclust): dat_j = AllDataMatrix[kmlabels == k, j] dat_j = dat_j.reshape((len(dat_j), 1)) if len(dat_j) < 1: d = 0 else: d = euclidean_distances(dat_j, dat_j) nk = len(dat_j) sumd_k = np.sum(d ** 2) nk_list.append(nk) sumd_k_list.append(sumd_k) nk_list = np.array(nk_list) sumd_k_list = np.array(sumd_k_list) # compute within-sum of squares over feature j nk_list[nk_list == 0] = -1 one_nk_list = 1.0 / nk_list one_nk_list[np.sign(one_nk_list) == -1] = 0 withinssj = np.sum(one_nk_list * sumd_k_list) aj = sumd_all - withinssj aj_list.append(aj) # 2. get w a = np.array(aj_list) wvar = cvx.Variable(len(a)) if tree: print "tree structure not supported" return -1 else: if group: obj = cvx.Minimize(sum(-1 * a * wvar)) if groups_vector is None: lenseq = np.hstack((np.power(2, np.arange(np.log2(AllDataMatrix.shape[1])))[::-1], [1])) else: lenseq = np.array(groups_vector) lenseq = lenseq.astype(int) nlevels = len(lenseq) sqrtlenseq = np.sqrt(lenseq) indseq = np.cumsum(lenseq) t = cvx.Variable(nlevels) group0 = [sqrtlenseq[0] * cvx.norm(wvar[0 : (indseq[0])], 2) <= t[0]] group_constraints = group0 for level in range(1, nlevels): # print level group_const = [ sqrtlenseq[level] * cvx.norm(wvar[indseq[(level - 1)] : (indseq[level])], 2) <= t[level] ] group_constraints = group_constraints + group_const constr = [cvx.square(cvx.norm2(wvar)) <= 1, wvar >= 0] + group_constraints constr = constr + [sum(t) <= alpha_group] if multi: T = AllDataMatrix.shape[1] / 3 t = cvx.Variable(T - 1) constr_list = [] for coeff in range((T - 1)): penalty = cvx.norm(wvar[coeff : (T * 3) : T], 2) <= t[coeff] constr_list.append(penalty) constr = [cvx.square(cvx.norm2(wvar)) <= 1, wvar >= 0] + constr_list constr = constr + [sum(t) <= alpha_group] else: ####ORIGINAL SPARSE KMEANS PROBLEM print "ORIGINAL" obj = cvx.Minimize(sum(-1 * a * wvar)) constr = [cvx.square(cvx.norm2(wvar)) <= 1, cvx.norm1(wvar) <= s_orig, wvar >= 0] prob = cvx.Problem(obj, constr) try: prob.solve() except: prob.solve(solver=cvx.SCS, verbose=False) # use_indirect=True w = wvar.value # 3. update kmeans wx = np.zeros((len(AllDataMatrix), AllDataMatrix.shape[1])) for j in range(AllDataMatrix.shape[1]): wj = np.sqrt(w[j][0, 0]) if np.isnan(wj): wj = 10 ** -30 wx[:, j] = AllDataMatrix[:, j] * wj kmt = KMeans(n_clusters=nclust, init="k-means++", max_iter=200, n_init=100) kmt.fit(wx) kmlabels = np.array(kmt.labels_) return prob.value, kmlabels, w
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]), 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)),
def C_soc_scaled(): return [cp.norm2(randn()*x) <= randn()*t]
def steiner_pos(tree, flow, diams, costs, pres_bds, C=1.0, verbose=False): '''minimize diameter cost of network with junction locations. args: - tree : Steiner tree (fixed terminal positions) - flow : dict from arc tuple (assumed positive) - diams : diameter values (sorted increasingly) - costs : diameter cost factors - pres_bds : uniform pressure bounds - C : constant coefficient in Weymouth equation - verbose : show solver output returns: - positions of Steiner nodes - value of objective in solution ''' tree, flow = make_forward_flow(tree, flow) assert all(flow[a] > 0.0 for a in tree.get_arcs()) # Node positions x = {n:cvx.Variable(2) for n in tree.get_nodes()} # Node pressures (squared) pp = {n:cvx.Variable(1) for n in tree.get_nodes()} # Arc costs t = {a:cvx.Variable(1) for a in tree.get_arcs()} # fix positions of terminals fixed_pos = [x[n] == np.array(tree.dg.node[n]['pos']) for n in tree.get_nodes() if tree.is_terminal(n)] # second order cone constraints socs = [] assert len(diams) == len(costs) N = len(diams) J = list(range(N - 1)) gg = [(costs[j+1]*diams[j]**-5 - costs[j]*diams[j+1]**-5) for j in J] ee = [(costs[j+1] - costs[j])/gg[j] for j in J] ff = [(diams[j]**-5 - diams[j+1]**-5)/gg[j] for j in J] for a in tree.get_arcs(): u, v = a # for the diameter segments for j in J: e = ee[j] / (C*flow[a]**2) socs.append(cvx.norm2(x[u] - x[v]) <= e*pp[u] - e*pp[v] + ff[j]*t[a]) # for the lower bound on y h = diams[-1]**5 / (C*flow[a]**2) socs.append(cvx.norm2(x[u] - x[v]) <= h*pp[u] - h*pp[v]) # for the lower bound on z i = 1.0 / costs[0] socs.append(cvx.norm2(x[u] - x[v]) <= i*t[a]) # pressure bounds pres_lower = [pp[n] >= min(pres_bds)**2 for n in tree.get_nodes()] pres_upper = [pp[n] <= max(pres_bds)**2 for n in tree.get_nodes()] # collect constraints conss = [] for c in [fixed_pos, socs, pres_lower, pres_upper]: conss += c # minimize total cost obj = sum(t[a] for a in tree.get_arcs()) prob = cvx.Problem(cvx.Minimize(obj), conss) opt = prob.solve(solver=cvx.CVXOPT, verbose=verbose) if prob.status == cvx.OPTIMAL: pos = {n:np.array(x[n].value)[:, 0].tolist() for n in tree.get_nodes()} return pos, prob.value else: print 'Problem not solved:', prob.status return None, None
import numpy as np import cvxpy as cp img = mpimg.imread('flowgray.png') img_d = np.copy(img) #plt.imshow(np.asarray(img),cmap=cm.binary_r) m,n = img.shape mask = np.random.rand(m,n) known = np.zeros((m,n)) known = mask>0.5 x,y=np.where(mask>0.5) ########### Ul2= cp.Variable(m,n) constraints1=[Ul2[x[i],y[i]]==np.float(img[x[i],y[i]]) for i in range(x.size)] Ux1 = Ul2[1:,1:]-Ul2[1:,0:n-1] Uy1 = Ul2[1:,1:]-Ul2[0:m-1,1:] objective1 = cp.Minimize(cp.norm2(cp.vstack(Ux1,Uy1))) p1 = cp.Problem(objective1,constraints1) ######################################### Utv= cp.Variable(m,n) constraints2=[Utv[x[i],y[i]]==np.float(img[x[i],y[i]]) for i in range(x.size)] Ux2 = Utv[1:,1:]-Utv[1:,0:n-1] Uy2 = Utv[1:,1:]-Utv[0:m-1,1:] objective2 = cp.Minimize(cp.norm(cp.vstack(Ux2,Uy2),1)) p2 = cp.Problem(objective2,constraints2) p1.solve(verbose = True) p2.solve(verbose = True) plt.figure(1) plt.set_cmap(cm.binary_r)
return (t, constr) #return NotImplemented #assert False, 'Not Implemented' if __name__ == '__main__': import cvxpy as cvx a = cvx.Variable() x1 = 1 y1= 3 x2 = 2 y2 = 6 c = logistic_difference([a*x2, a*x2]) #c = 0 obj = cvx.Minimize(cvx.norm2(a*x1 - y1) + c) prob = cvx.Problem(obj, []) ret = prob.solve(solver=cvx.CVXOPT, verbose=True) pass
import read_iris import svm1 import cvxpy data, labels = read_iris.readBinaryClasses('./iris.csv') #print data a, b = svm1.createVars(data.shape[1]) constraints = svm1.createConstraints(a, b, data, labels) obj = cvxpy.Minimize(cvxpy.norm2(a)) prob = cvxpy.Problem(obj, constraints) print prob.solve() print 'A' print a.value print 'B', b.value svm1.test(a.value, b.value, data, labels)
def C_soc_scaled_translated(): return [cp.norm2(randn()*x + randn()) <= randn()*t + randn()]
g = 0.1 m = 10. Fmax = 10. p0 = np.matrix('50;50;100') v0 = np.matrix('-10;0;-10') alpha = 0.5 gamma = 1. K = 35 p = cvx.Variable(3,K+1) v = cvx.Variable(3,K+1) f = cvx.Variable(3,K) e3 = np.array([[0,0,1]]) rep_e3 = np.repeat(e3,K,0) repeat_e3 = np.asmatrix(rep_e3.T) obj = cvx.Minimize(cvx.sum_entries(cvx.norm2(f))) constraints = [v[:,1:K+1] == v[:,0:K] + (1/m)*f-g*repeat_e3, p[:,1:K+1] == p[:,0:K] + (1/2)*(v[:,0:K]+v[:,1:K+1]), p[:,0] == p0, v[:,0] == v0, p[:,K] == 0, v[:,K] == 0, p[2,:] >= alpha*cvx.norm2(p[0:1,:]), cvx.norm2(f) <= Fmax] p = cvx.Problem(obj,constraints) sol = p.solve(solver=cvx.SCS); minfuel = sol*gamma*h p_minfuel = p