def _MakeMtdfProblem(self, c_range=(1e-6, 1e-2), bounds=None): """Create a CVXOPT problem for finding the Maximal Thermodynamic Driving Force (MTDF). Does not set the objective function... leaves that to the caller. Args: c_range: a tuple (min, max) for concentrations (in M). bounds: a list of (lower bound, upper bound) tuples for compound concentrations. Returns: A tuple (dgf_var, motive_force_var, problem_object). """ constraints = [] # Define and apply the constraints on the concentrations ln_conc = cvxpy.variable(1, self.Nc, name='lnC') constraints += self._MakeLnConcentratonBounds(ln_conc, bounds=bounds, c_range=c_range) # Create the driving force variable and add the relevant constraints driving_force_lb = cvxpy.variable(name='B') constraints += self._MakeDrivingForceConstraints(ln_conc, driving_force_lb) return ln_conc, driving_force_lb, constraints
def _MakeMtdfProblem(self, c_range=(1e-6, 1e-2), bounds=None): """Create a CVXOPT problem for finding the Maximal Thermodynamic Driving Force (MTDF). Does not set the objective function... leaves that to the caller. Args: c_range: a tuple (min, max) for concentrations (in M). bounds: a list of (lower bound, upper bound) tuples for compound concentrations. Returns: A tuple (dgf_var, motive_force_var, problem_object). """ constraints = [] # Define and apply the constraints on the concentrations ln_conc = cvxpy.variable(1, self.Nc, name='lnC') constraints += self._MakeLnConcentratonBounds(ln_conc, bounds=bounds, c_range=c_range) # Create the driving force variable and add the relevant constraints driving_force_lb = cvxpy.variable(name='B') constraints += self._MakeDrivingForceConstraints( ln_conc, driving_force_lb) return ln_conc, driving_force_lb, constraints
def test_robust_regression(self): # # Compare cvxpy solutions to cvxopt ground truth # from cvxpy import (matrix, variable, program, minimize, huber, sum, leq, geq, square, norm2, ones, zeros, quad_form) tol_exp = 5 # Check solution to within 5 decimal places m, n = self.m, self.n A = matrix(self.A) b = matrix(self.b) # Method 1: using huber directly x = variable(n) p = program(minimize(sum(huber(A * x - b, 1.0)))) p.solve(True) np.testing.assert_array_almost_equal(x.value, self.xh, tol_exp) # Method 2: solving the dual QP x = variable(n) u = variable(m) v = variable(m) p = program(minimize(0.5 * quad_form(u, 1.0) + sum(v)), [ geq(u, 0.0), leq(u, 1.0), geq(v, 0.0), leq(A * x - b, u + v), geq(A * x - b, -u - v) ]) p.solve(True) np.testing.assert_array_almost_equal(x.value, self.xh, tol_exp)
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 test_robust_regression(self): # # Compare cvxpy solutions to cvxopt ground truth # from cvxpy import (matrix, variable, program, minimize, huber, sum, leq, geq, square, norm2, ones, zeros, quad_form) tol_exp = 5 # Check solution to within 5 decimal places m, n = self.m, self.n A = matrix(self.A) b = matrix(self.b) # Method 1: using huber directly x = variable(n) p = program(minimize(sum(huber(A*x - b, 1.0)))) p.solve(True) np.testing.assert_array_almost_equal( x.value, self.xh, tol_exp) # Method 2: solving the dual QP x = variable(n) u = variable(m) v = variable(m) p = program(minimize(0.5*quad_form(u, 1.0) + sum(v)), [geq(u, 0.0), leq(u, 1.0), geq(v, 0.0), leq(A*x - b, u + v), geq(A*x - b, -u - v)]) p.solve(True) np.testing.assert_array_almost_equal( x.value, self.xh, tol_exp)
def cvxify(self): """docstring for cvxify""" self.cache_props() for link in self.get_links(): link.v_flow = variable(name='flow: {0}'.format(link.name)) for (source, sink), routes in self.od_routes.iteritems(): for i, route in enumerate(routes): route.v_flow = variable(name='rf: o: {0}, d: {1} [{2}]'.format(source.name, sink.name, i))
def test_problem_ubsdp(self): # test problem needs review print 'skipping, needs review' return from cvxpy import (matrix, variable, program, minimize, sum, abs, norm2, log, square, zeros, max, hstack, vstack, eye, eq, trace, semidefinite_cone, belongs, reshape) (m, n) = (self.m, self.n) c = matrix(self.c) A = matrix(self.A) B = matrix(self.B) Xubsdp = matrix(self.Xubsdp) tol_exp = 6 # Use cvxpy to solve # # minimize tr(B * X) # s.t. tr(Ai * X) + ci = 0, i = 1, ..., n # X + S = I # X >= 0, S >= 0. # # c is an n-vector. # A is an m^2 x n-matrix. # B is an m x m-matrix. X = variable(m, n) S = variable(m, n) constr = [ eq(trace(reshape(A[:, i], (m, m)) * X) + c[i, 0], 0.0) for i in range(n) ] constr += [eq(X + S, eye(m))] constr += [ belongs(X, semidefinite_cone), belongs(S, semidefinite_cone) ] p = program(minimize(trace(B * X)), constr) p.solve(True) np.testing.assert_array_almost_equal(X.value, self.Xubsdp, tol_exp) # Use cvxpy to solve # # minimize tr(B * X) # s.t. tr(Ai * X) + ci = 0, i = 1, ..., n # 0 <= X <= I X2 = variable(m, n) constr = [ eq(trace(reshape(A[:, i], (m, m)) * X2) + c[i, 0], 0.0) for i in range(n) ] constr += [ belongs(X2, semidefinite_cone), belongs(eye(m) - X2, semidefinite_cone) ] p = program(minimize(trace(B * X2)), constr) p.solve(True) np.testing.assert_array_almost_equal(X2.value, X.value, tol_exp)
def test_problem_ubsdp(self): # test problem needs review print 'skipping, needs review' return from cvxpy import (matrix, variable, program, minimize, sum, abs, norm2, log, square, zeros, max, hstack, vstack, eye, eq, trace, semidefinite_cone, belongs, reshape) (m, n) = (self.m, self.n) c = matrix(self.c) A = matrix(self.A) B = matrix(self.B) Xubsdp = matrix(self.Xubsdp) tol_exp = 6 # Use cvxpy to solve # # minimize tr(B * X) # s.t. tr(Ai * X) + ci = 0, i = 1, ..., n # X + S = I # X >= 0, S >= 0. # # c is an n-vector. # A is an m^2 x n-matrix. # B is an m x m-matrix. X = variable(m, n) S = variable(m, n) constr = [eq(trace(reshape(A[:,i], (m, m)) * X) + c[i,0], 0.0) for i in range(n)] constr += [eq(X + S, eye(m))] constr += [belongs(X, semidefinite_cone), belongs(S, semidefinite_cone)] p = program(minimize(trace(B * X)), constr) p.solve(True) np.testing.assert_array_almost_equal( X.value, self.Xubsdp, tol_exp) # Use cvxpy to solve # # minimize tr(B * X) # s.t. tr(Ai * X) + ci = 0, i = 1, ..., n # 0 <= X <= I X2 = variable(m, n) constr = [eq(trace(reshape(A[:,i], (m, m)) * X2) + c[i,0], 0.0) for i in range(n)] constr += [belongs(X2, semidefinite_cone), belongs(eye(m) - X2, semidefinite_cone)] p = program(minimize(trace(B * X2)), constr) p.solve(True) np.testing.assert_array_almost_equal( X2.value, X.value, tol_exp)
def nuc_al(I, X, turn, epsilon=.1, quiet=False): m,K = np.shape(X) if not turn: V = cp.variable(m,K, name='V') U = X else: V = X U = cp.variable(m,K, name='U') f_cost = cp.parameter(name='f_cost') f_cost.value = 0 J = cp.parameter(K,K, name='J') J.value = cp.zeros((K,K)) # define the cost parameter for k in range(K): # create a list of interferer indeces Ik # from the interference set I Ik = np.arange(1,K+1)*I[k,:] # careful about zero indexing... Ik = Ik[Ik>0] - 1 #print 'I_%d = ' % k, Ik if len(Ik) > 0: for l in Ik: l = int(l) J[k,l] = U[:,k].T*V[:,l] # interference terms f_cost = f_cost + cp.nuclear_norm(J[k,:]) #f_cost = f_cost + cp.norm1(J[k,:]) # create constraints constraints = [] for k in range(K): c = cp.geq(U[:,k].T*V[:,k], epsilon) #c = cp.geq(cp.lambda_min(U[:,k].T*V[:,k]), epsilon) constraints.append(c) p = cp.program(cp.minimize(f_cost), constraints) print 'minimize: ', p.objective print 'subject to:' print p.constraints p.solve(quiet) if not turn: return V.value else: return U.value
def construct_lp_relaxation( demand_hist, M, transport_graph, cost_label='cost' ) : """ demand_hist is a dictionary from ordered station pairs (i,j) -> whole integers M is a membership graph with edges (i,x) for all corresponding stations-state pairs A is a DiGraph on X, with cost given by property 'cost' """ APSP = nx.all_pairs_dijkstra_path_length( transport_graph, weight=cost_label ) cost = 0. constr = [] """ create enroute variables """ S = nx.DiGraph() # service "edges" for (i,j), NUM in demand_hist.iteritems() : VARS = [] for x,y in itertools.product( M.neighbors_iter(i), M.neighbors_iter(j) ) : var = cvxpy.variable() distance = APSP[x][y] cost += var * distance constr.append( cvxpy.geq( var, 0. ) ) # needs to be non-negative S.add_edge( x, y, var=var, distance=distance ) VARS.append( var ) constr.append( cvxpy.eq( cvxpy.sum(VARS), NUM ) ) # total such trips should sum to num """ create balance variables """ B = nx.DiGraph() HEADS = [ h for h, d in S.in_degree_iter() if d > 0 ] TAILS = [ t for t, d in S.out_degree_iter() if d > 0 ] for y,x in itertools.product( HEADS, TAILS ) : var = cvxpy.variable() cost += var * APSP[y][x] constr.append( cvxpy.geq( var, 0. ) ) B.add_edge( y,x, var=var ) """ generate flow conservation constraints """ for x in S.nodes_iter() : sin = sum( [ data['var'] for _,__,data in S.in_edges_iter( x, data=True ) ] ) sout = sum( [ data['var'] for _,__,data in S.out_edges_iter( x, data=True ) ] ) bin = sum( [ data['var'] for _,__,data in B.in_edges_iter( x, data=True ) ] ) bout = sum( [ data['var'] for _,__,data in B.out_edges_iter( x, data=True ) ] ) constr.append( cvxpy.eq( sin, bout ) ) constr.append( cvxpy.eq( sout, bin ) ) #service_vars = S # alias return cost, constr, S
def nuc_al(I, X, turn, epsilon=.1, quiet=False): m, K = np.shape(X) if not turn: V = cp.variable(m, K, name='V') U = X else: V = X U = cp.variable(m, K, name='U') f_cost = cp.parameter(name='f_cost') f_cost.value = 0 J = cp.parameter(K, K, name='J') J.value = cp.zeros((K, K)) # define the cost parameter for k in range(K): # create a list of interferer indeces Ik # from the interference set I Ik = np.arange(1, K + 1) * I[k, :] # careful about zero indexing... Ik = Ik[Ik > 0] - 1 #print 'I_%d = ' % k, Ik if len(Ik) > 0: for l in Ik: l = int(l) J[k, l] = U[:, k].T * V[:, l] # interference terms f_cost = f_cost + cp.nuclear_norm(J[k, :]) #f_cost = f_cost + cp.norm1(J[k,:]) # create constraints constraints = [] for k in range(K): c = cp.geq(U[:, k].T * V[:, k], epsilon) #c = cp.geq(cp.lambda_min(U[:,k].T*V[:,k]), epsilon) constraints.append(c) p = cp.program(cp.minimize(f_cost), constraints) print 'minimize: ', p.objective print 'subject to:' print p.constraints p.solve(quiet) if not turn: return V.value else: return U.value
def estimate_confidence_bound_with_cvx(f_mat, num_reads_in_bams, fixed_i, mle_estimate, bound_type, alpha): if bound_type == 'lb': bound_type = 'LOWER' if bound_type == 'ub': bound_type = 'UPPER' assert bound_type in ('LOWER', 'UPPER'), ("Improper bound type '%s'" % bound_type) expected_array, observed_array = f_mat.expected_and_observed( bam_cnts=num_reads_in_bams) from cvxpy import matrix, variable, geq, log, eq, program, maximize, minimize, sum mle_log_lhd = calc_lhd(mle_estimate, observed_array, expected_array) lower_lhd_bound = mle_log_lhd - chi2.ppf(1 - alpha, 1) / 2. free_indices = set(range(expected_array.shape[1])) - set((fixed_i, )) Xs = matrix(observed_array) ps = matrix(expected_array) thetas = variable(ps.shape[1]) constraints = [ geq(Xs * log(ps * thetas), lower_lhd_bound), eq(sum(thetas), 1), geq(thetas, 0) ] if bound_type == 'UPPER': p = program(maximize(thetas[fixed_i, 0]), constraints) else: p = program(minimize(thetas[fixed_i, 0]), constraints) p.options['maxiters'] = 1500 value = p.solve(quiet=not DEBUG_OPTIMIZATION) thetas_values = numpy.array(thetas.value.T.tolist()[0]) log_lhd = calc_lhd(thetas_values, observed_array, expected_array) return chi2.sf(2 * (mle_log_lhd - log_lhd), 1), value
def _MakeMinimalFeasbileConcentrationProblem(self, bounds=None, c_range=(1e-6, 1e-2)): # Define and apply the constraints on the concentrations constraints = [] # Define and apply the constraints on the concentrations ln_conc = cvxpy.variable(1, self.Nc, name='lnC') constraints += self._MakeLnConcentratonBounds(ln_conc, bounds=bounds, c_range=c_range) # find the row vector describing the overall stoichiometry S = cvxpy.matrix(self.S) dg0r_primes = cvxpy.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. for i in xrange(self.Nr): # if the dG0 is unknown, this reaction imposes no new constraints if np.isnan(self.dG0_r_prime[0, i]): continue curr_dgr = dg0r_primes[0, i] + RT * ln_conc * S[:, i] if self.fluxes[0, i] != 0: constraints.append(cvxpy.leq(curr_dgr * np.sign(self.fluxes[0, i]), self.DEFAULT_REACTION_UB)) constraints.append(cvxpy.geq(curr_dgr * np.sign(self.fluxes[0, i]), self.DEFAULT_REACTION_LB)) else: constraints.append(cvxpy.eq(curr_dgr, 0)) # Set the constraints return ln_conc, constraints
def PROGRAM( roadnet, surplus, objectives ) : """ construct the program """ # optvars assist = dict() cost = dict() DELTA = .00001 # cvxpy isn't quite robust to non-full dimensional optimization for _,__,road in roadnet.edges_iter( keys=True ) : assist[road] = cvxpy.variable( name='z_{%s}' % road ) cost[road] = cvxpy.variable( name='c_{%s}' % road ) #print assist #print cost objfunc = sum( cost.values() ) OBJECTIVE = cvxpy.minimize( objfunc ) CONSTRAINTS = [] # the flow conservation constraints for u in roadnet.nodes_iter() : INFLOWS = [] for _,__,road in roadnet.in_edges( u, keys=True ) : INFLOWS.append( assist[road] + surplus[road] ) OUTFLOWS = [] for _,__,road in roadnet.out_edges( u, keys=True ) : OUTFLOWS.append( assist[road] ) #conserve_u = cvxpy.eq( sum(OUTFLOWS), sum(INFLOWS) ) error_u = sum(OUTFLOWS) - sum(INFLOWS) conserve_u = cvxpy.leq( cvxpy.abs( error_u ), DELTA ) CONSTRAINTS.append( conserve_u ) # the cost-form constraints for road in cost : for f, line in objectives[road].iter_items() : # is this plus or minus alpha? LB = cvxpy.geq( cost[road], line.offset + line.slope * assist[road] ) CONSTRAINTS.append( LB ) prog = cvxpy.program( OBJECTIVE, CONSTRAINTS ) return prog, assist
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 _GetTotalReactionEnergy(self, c_range=(1e-6, 1e-2), bounds=None, min_driving_force=0): constraints = [] # Define and apply the constraints on the concentrations ln_conc = cvxpy.variable(1, self.Nc, name='lnC') constraints += self._MakeLnConcentratonBounds(ln_conc, bounds=bounds, c_range=c_range) # find the row vector describing the overall stoichiometry S = cvxpy.matrix(self.S) f = cvxpy.matrix(self.fluxes) g0 = cvxpy.matrix(self.dG0_r_prime) g = g0 + RT * ln_conc * S total_g = f * g.T constraints += self._MakeDrivingForceConstraints(ln_conc, min_driving_force) return ln_conc, constraints, total_g
def test_problem_expdesign(self): # test problem needs review print 'skipping, needs review' return from cvxpy import (matrix, variable, program, minimize, log, det_rootn, diag, sum, geq, eq, zeros) tol_exp = 6 V = matrix(self.V) n = V.shape[1] x = variable(n) # Use cvxpy to solve the problem above p = program(minimize(-log(det_rootn(V*diag(x)*V.T))), [geq(x, 0.0), eq(sum(x), 1.0)]) p.solve(True) np.testing.assert_array_almost_equal( x.value, self.xd, tol_exp)
def f_Gamma(c): # For fixed c solve the semidefinite program for Algorithm 4 # Variable Gamma_plus (for \Gamma_{i+1}) d_plus = variable(2*n, 1, name='d_plus') # Constraints constr = [] for j in range(2*n): ctt = less_equals(d_plus[j,0], 0) constr.append( less_equals(-d_plus[j,0], 0)) constr.append( less_equals( d_plus[j,0], d[j,0])) constr.append( less_equals( J[j,j]*d_plus[j,0] + sum([abs(J[i,j])*d_plus[i,0] for i in range(2*n)]) - abs(J[j,j])*d_plus[j,0], c* d_plus[j,0])) # Objective function obj = geo_mean(d_plus) # Find solution p = program(maximize(obj), constr) return d_plus.value
def compute_transform(p1, p2, best_matches): import cvxpy, cvxpy.utils # How many good matches are there? num_bad_matches = sum([x == None for x in best_matches]) num_good_matches = p1.shape[0] - num_bad_matches # Need at least three points for a good translation fit... if (num_good_matches < 3): print 'ERROR: not enough matches to compute a 3D affine fit.' exit(1) # Prepare data for fitting X = cvxpy.utils.ones((3, num_good_matches)) Y = cvxpy.utils.ones((3, num_good_matches)) count = 0 for i in range(p1.shape[0]): if best_matches[i] != None: X[0, count] = p1[i, 0] X[1, count] = p1[i, 1] X[2, count] = p1[i, 2] Y[0, count] = p2[best_matches[i], 0] Y[1, count] = p2[best_matches[i], 1] Y[2, count] = p2[best_matches[i], 2] count += 1 # print X.T # print Y.T translation = cvxpy.variable(3, 1) cost_fn = cvxpy.norm1((X[:, 1] + translation) - Y[:, 1]) for c in range(2, num_good_matches): cost_fn += cvxpy.norm1((X[:, c] + translation) - Y[:, c]) p = cvxpy.program(cvxpy.minimize(cost_fn)) p.solve(quiet=True) return ((1, np.identity(3), np.array(translation.value).squeeze()), num_good_matches)
def _MakeMinimumFeasbileConcentrationsProblem(self, bounds=None, c_range=(1e-6, 1e-2)): """Creates the CVXOPT problem for finding minimum total concentrations. Returns: Two tuple (ln_concentrations var, problem). """ assert self.dG0_f_prime is not None constraints = [] # Define and apply the constraints on the concentrations ln_conc = cvxpy.variable(1, self.Nc, name='lnC') constraints += self._MakeLnConcentratonBounds(ln_conc, bounds=bounds, c_range=c_range) # Make the objective and problem. S = cvxpy.matrix(self.S) # 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. dgf_primes = RT * ln_conc + cvxpy.matrix(self.dG0_f_prime) for i in xrange(self.Nr): if self.fluxes[0, i] > 0: constraints.append( cvxpy.leq(S[i, :] * dgf_primes, self.DEFAULT_REACTION_UB)) constraints.append( cvxpy.geq(S[i, :] * dgf_primes, self.DEFAULT_REACTION_LB)) elif self.fluxes[0, i] == 0: constraints.append(cvxpy.eq(S[i, :] * dgf_primes, 0)) else: constraints.append( cvxpy.geq(S[i, :] * dgf_primes, -self.DEFAULT_REACTION_UB)) constraints.append( cvxpy.leq(S[i, :] * dgf_primes, -self.DEFAULT_REACTION_LB)) return ln_conc, constraints
def _MakeMinimumFeasbileConcentrationsProblem(self, bounds=None, c_range=(1e-6, 1e-2)): """Creates the CVXOPT problem for finding minimum total concentrations. Returns: Two tuple (ln_concentrations var, problem). """ assert self.dG0_f_prime is not None constraints = [] # Define and apply the constraints on the concentrations ln_conc = cvxpy.variable(1, self.Nc, name='lnC') constraints += self._MakeLnConcentratonBounds(ln_conc, bounds=bounds, c_range=c_range) # Make the objective and problem. S = cvxpy.matrix(self.S) # 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. dgf_primes = RT * ln_conc + cvxpy.matrix(self.dG0_f_prime) for i in xrange(self.Nr): if self.fluxes[0, i] > 0: constraints.append(cvxpy.leq(S[i, :] * dgf_primes, self.DEFAULT_REACTION_UB)) constraints.append(cvxpy.geq(S[i, :] * dgf_primes, self.DEFAULT_REACTION_LB)) elif self.fluxes[0, i] == 0: constraints.append(cvxpy.eq(S[i, :] * dgf_primes, 0)) else: constraints.append(cvxpy.geq(S[i, :] * dgf_primes, -self.DEFAULT_REACTION_UB)) constraints.append(cvxpy.leq(S[i, :] * dgf_primes, -self.DEFAULT_REACTION_LB)) return ln_conc, constraints
def _MakeMinimalFeasbileConcentrationProblem(self, bounds=None, c_range=(1e-6, 1e-2)): # Define and apply the constraints on the concentrations constraints = [] # Define and apply the constraints on the concentrations ln_conc = cvxpy.variable(1, self.Nc, name='lnC') constraints += self._MakeLnConcentratonBounds(ln_conc, bounds=bounds, c_range=c_range) # find the row vector describing the overall stoichiometry S = cvxpy.matrix(self.S) dg0r_primes = cvxpy.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. for i in xrange(self.Nr): # if the dG0 is unknown, this reaction imposes no new constraints if np.isnan(self.dG0_r_prime[0, i]): continue curr_dgr = dg0r_primes[0, i] + RT * ln_conc * S[:, i] if self.fluxes[0, i] != 0: constraints.append( cvxpy.leq(curr_dgr * np.sign(self.fluxes[0, i]), self.DEFAULT_REACTION_UB)) constraints.append( cvxpy.geq(curr_dgr * np.sign(self.fluxes[0, i]), self.DEFAULT_REACTION_LB)) else: constraints.append(cvxpy.eq(curr_dgr, 0)) # Set the constraints return ln_conc, constraints
def _GetTotalReactionEnergy(self, c_range=(1e-6, 1e-2), bounds=None, min_driving_force=0): constraints = [] # Define and apply the constraints on the concentrations ln_conc = cvxpy.variable(1, self.Nc, name='lnC') constraints += self._MakeLnConcentratonBounds(ln_conc, bounds=bounds, c_range=c_range) # find the row vector describing the overall stoichiometry S = cvxpy.matrix(self.S) f = cvxpy.matrix(self.fluxes) g0 = cvxpy.matrix(self.dG0_r_prime) g = g0 + RT * ln_conc * S total_g = f * g.T constraints += self._MakeDrivingForceConstraints( ln_conc, min_driving_force) return ln_conc, constraints, total_g
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 estimate_transcript_frequencies_with_cvxopt(observed_array, expected_array, sparse_penalty, sparse_index, verbose=False): from cvxpy import matrix, variable, geq, log, eq, program, maximize, \ minimize, sum, quad_over_lin Xs = matrix(observed_array) ps = matrix(expected_array) thetas = variable(ps.shape[1]) constraints = [eq(sum(thetas), 1), geq(thetas, 0)] if sparse_penalty == None: p = program(maximize(Xs * log(ps * thetas)), constraints) else: p = program( maximize(Xs * log(ps * thetas) - sparse_penalty * quad_over_lin(1., thetas[sparse_index, 0])), constraints) p.options['maxiters'] = 1500 value = p.solve(quiet=not verbose) thetas_values = numpy.array(thetas.value.T.tolist()[0]) return thetas_values
def cr_var(self, name): return variable(name=name)
def fit_ellipse_stack2(dx, dy, dz, di, norm_type="l2"): """ fit ellipoid using squared loss idea to learn all stacks together including smoothness """ #TODO create flag for norm1 vs norm2 assert norm_type in ["l1", "l2", "huber"] # sanity check assert len(dx) == len(dy) assert len(dx) == len(dz) assert len(dx) == len(di) # unique zs dat = defaultdict(list) # resort data for idx in range(len(dx)): dat[dz[idx]].append( [dx[idx], dy[idx], di[idx]] ) # init ret ellipse_stack = [] for idx in range(max(dz)): ellipse_stack.append(Ellipse(0, 0, idx, 1, 1, 0)) total_N = len(dx) M = len(dat.keys()) #D = 5 D = 4 X_matrix = [] thetas = [] slacks = [] eps_slacks = [] mean_di = float(numpy.mean(di)) for z in dat.keys(): x = numpy.array(dat[z])[:,0] y = numpy.array(dat[z])[:,1] # intensities i = numpy.array(dat[z])[:,2] ity = numpy.diag(i) / mean_di # dimensionality N = len(x) d = numpy.zeros((N, D)) d[:,0] = x*x d[:,1] = y*y #d[:,2] = x*y d[:,2] = x d[:,3] = y #d[:,4] = numpy.ones(N) #d[:,0] = x*x #d[:,1] = y*y #d[:,2] = x*y #d[:,3] = x #d[:,4] = y #d[:,5] = numpy.ones(N) # consider intensities old_shape = d.shape #d = numpy.dot(ity, d) assert d.shape == old_shape print d.shape d = cvxpy.matrix(d) #### parameters # da X = cvxpy.parameter(N, D, name="X" + str(z)) X.value = d X_matrix.append(X) #### varibales # parameter vector theta = cvxpy.variable(D, name="theta" + str(z)) thetas.append(theta) # construct obj objective = 0 print "norm type", norm_type for i in xrange(M): if norm_type == "l1": objective += cvxpy.norm1(X_matrix[i] * thetas[i] + 1.0) if norm_type == "l2": objective += cvxpy.norm2(X_matrix[i] * thetas[i] + 1.0) #TODO these need to be summed #objective += cvxpy.huber(X_matrix[i] * thetas[i], 1) #objective += cvxpy.deadzone(X_matrix[i] * thetas[i], 1) # add smoothness regularization reg_const = float(total_N) / float(M-1) for i in xrange(M-1): objective += reg_const * cvxpy.norm2(thetas[i] - thetas[i+1]) # create problem p = cvxpy.program(cvxpy.minimize(objective)) prob = p import ipdb ipdb.set_trace() # add constraints #for i in xrange(M): # #p.constraints.append(cvxpy.eq(thetas[i][0,:] + thetas[i][1,:], 1)) # p.constraints.append(cvxpy.eq(thetas[i][4,:], 1)) # set solver settings p.options['reltol'] = 1e-1 p.options['abstol'] = 1e-1 #p.options['feastol'] = 1e-1 # invoke solver p.solve() # wrap up result ellipse_stack = {} active_layers = dat.keys() assert len(active_layers) == M for i in xrange(M): w = numpy.array(thetas[i].value) ## For clarity, fill in the quadratic form variables #A = numpy.zeros((2,2)) #A[0,0] = w[0] #A.ravel()[1:3] = w[2] #A[1,1] = w[1] #bv = w[3:5] #c = w[5] A = numpy.zeros((2,2)) A[0,0] = w[0] A.ravel()[1:3] = 0 #w[2] A[1,1] = w[1] #bv = w[2:4] bv = w[2:] #c = w[4] c = 1.0 ## find parameters z, a, b, alpha = util.conic2parametric(A, bv, c) print "layer (i,z,a,b,alpha):", i, z, a, b, alpha layer = active_layers[i] ellipse_stack[layer] = Ellipse(z[0], z[1], layer, a, b, alpha) return ellipse_stack
def measurenx_to_approxnx(roadnet, epsilon, length="length", weight1="weight1", weight2="weight2"): """ input: a road network, with weights on its elements output: returns a graph summarizing the network optimization problem instance; roadnets are multi-digraph, where edge 'keys' are assumed to be unique, i.e., road names; and should be different from node labels too; """ digraph = nx.DiGraph() digraph.add_node("s") digraph.add_node("t") """ insert supply and demand of roads """ for u, v, road, data in roadnet.edges_iter(keys=True, data=True): roadlen = float(data.get(length, 1)) # float() just in case assert roadlen >= 0.0 oneway = data.get("oneway", False) surplus = float(data.get(weight1, 0.0)) - data.get(weight2, 0.0) deficit = -surplus """ split the road into equal-length segments; create a node for each segment; """ N = int(np.ceil(roadlen / epsilon)) eps = roadlen / N if surplus > 0.0: NODES = [(road, k, "supply") for k in range(N)] for node in NODES: digraph.add_edge( "s", node, flow=cvxpy.variable(), minflow=0.0, maxflow=surplus / N, w_lo=-eps, w_hi=0.0 ) SEQ = [u] + NODES + [v] for lnode, rnode in zip(SEQ[:-1], SEQ[1:]): digraph.add_edge(lnode, rnode, flow=cvxpy.variable(), minflow=0.0, w_lo=eps, w_hi=eps) if not oneway: digraph.add_edge(rnode, lnode, flow=cvxpy.variable(), minflow=0.0, w_lo=eps, w_hi=eps) if deficit > 0.0: NODES = [(road, k, "demand") for k in range(N)] for node in NODES: digraph.add_edge( node, "t", flow=cvxpy.variable(), minflow=0.0, maxflow=deficit / N, w_lo=-eps, w_hi=0.0 ) SEQ = [u] + NODES + [v] for lnode, rnode in zip(SEQ[:-1], SEQ[1:]): digraph.add_edge(lnode, rnode, flow=cvxpy.variable(), minflow=0.0, w_lo=eps, w_hi=eps) if not oneway: digraph.add_edge(rnode, lnode, flow=cvxpy.variable(), minflow=0.0, w_lo=eps, w_hi=eps) """ insert supply and demand of nodes """ for u, data in roadnet.nodes_iter(data=True): surplus = data.get(weight1, 0.0) - data.get(weight2, 0.0) deficit = -surplus # supply layer if surplus > 0.0: digraph.add_edge("s", u, flow=cvxpy.variable(), minflow=0.0, maxflow=surplus) if deficit > 0.0: digraph.add_edge(u, "t", flow=cvxpy.variable(), minflow=0.0, maxflow=deficit) """ setup the network flow structure """ conns = roadmaps.connectivity_graph(roadnet) for u, v, data in conns.edges_iter(data=True): weight = data.get(length, 1) flowvar = cvxpy.variable() digraph.add_edge(u, v, flow=cvxpy.variable(), minflow=0.0, w_lo=weight, w_hi=weight) """ turn the weights into costs """ for _, __, data in digraph.edges_iter(data=True): flowvar = data["flow"] if "w_lo" in data: data["cost_lo"] = data["w_lo"] * flowvar if "w_hi" in data: data["cost_hi"] = data["w_hi"] * flowvar nxopt.attach_flownx_constraints(digraph) return digraph # a flownx
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
def cvxify(self): super(CTMStaticProblem, self).cvxify() self.cache_props() for link in self.get_links(): link.v_dens = variable(name='dens: {0}'.format(link.name))
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))
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 fit_ellipse_eps_insensitive(x, y): """ fit ellipoid using epsilon-insensitive loss """ x = numpy.array(x) y = numpy.array(y) print "shapes", x.shape, y.shape assert len(x) == len(y) N = len(x) D = 5 dat = numpy.zeros((N, D)) dat[:,0] = x*x dat[:,1] = y*y #dat[:,2] = y*x dat[:,2] = x dat[:,3] = y dat[:,4] = numpy.ones(N) print dat.shape dat = cvxpy.matrix(dat) #### parameters # data X = cvxpy.parameter(N, D, name="X") # parameter for eps-insensitive loss eps = cvxpy.parameter(1, name="eps") #### varibales # parameter vector theta = cvxpy.variable(D, name="theta") # dim = (N x 1) s = cvxpy.variable(N, name="s") t = cvxpy.variable(N, name="t") # simple objective objective = cvxpy.sum(t) # create problem p = cvxpy.program(cvxpy.minimize(objective)) # add constraints # (N x D) * (D X 1) = (N X 1) p.constraints.append(X*theta <= s) p.constraints.append(-X*theta <= s) p.constraints.append(s - eps <= t) p.constraints.append(0 <= t) #p.constraints.append(theta[4] == 1) # trace constraint p.constraints.append(theta[0] + theta[1] == 1) ###### set values X.value = dat eps.value = 0.0 #solver = "mosek" #p.solve(lpsolver=solver) p.solve() cvxpy.printval(theta) w = numpy.array(cvxpy.value(theta)) #cvxpy.printval(s) #cvxpy.printval(t) ## For clarity, fill in the quadratic form variables A = numpy.zeros((2,2)) A[0,0] = w[0] A.ravel()[1:3] = 0#w[2] A[1,1] = w[1] bv = w[2:4] c = w[4] ## find parameters z, a, b, alpha = util.conic2parametric(A, bv, c) return z, a, b, alpha
def solve(self): ''' solver for the optimal purchasing of gas at stations does not return anything, but attaches some resulting properties at the end''' n = len(self.route.stations) if self.method == "dynamic": def conditionallyFillUp(gas_in_tank,station): print 'filling up' distance_to_end = sum([self.route.distances[j] for j in range(station,n)] ) bought.append(max(0,min(distance_to_end / self.car.economy, self.car.tank_max) - gas_in_tank)) prices = [station.regular for station in self.route.stations] print 'prices: ' + str(prices) i = 0 range_of_car = (self.car.tank_max - self.gas_min) * self.car.economy print 'range of car: ' + str(range_of_car) #set initial stations current_price = self.route.stations[i].regular station_chosen = i #Probably safe to assume both are zero, call api if necessary distance_to_first_station = 0 distance_from_last_station = 0 if (self.car.start - distance_to_first_station / self.car.economy < self.gas_min): raise Exception("Unfeasible to reach") else: gas_in_tank_at_last_station = self.car.start - (distance_to_first_station / self.car.economy) #Make parameter (probably as proportion of full tank) on website gas_at_end = self.gas_min #for export stations_chosen = [] bought = [] #simulte partially filled tank as miles already driven miles_since_last_stop = (self.car.tank_max - gas_in_tank_at_last_station) * self.car.economy + distance_to_first_station #make sure you can get home self.route.distances.append(distance_from_last_station) while i < n: #for feasibility check firstStationOnTank = i #determine where car should stop while i < n: #check if next stop is cheaper print i print current_price if self.route.stations[i].regular < current_price: current_price = self.route.stations[i].regular station_chosen = i print 'station_chosen: ' + str(station_chosen) #increment miles_since_last_stop += self.route.distances[i] i = i + 1 print 'miles_since_last_stop: ' + str(miles_since_last_stop) if miles_since_last_stop > range_of_car: print i if (gas_in_tank_at_last_station - self.route.distances[firstStationOnTank] / self.car.economy < self.gas_min): raise Exception("Unfeasible to reach") stations_chosen.append(station_chosen) current_price = self.route.stations[i].regular station_chosen = i break #determine how much gas car should get if len(stations_chosen) > 1: distance_between_stations = sum([self.route.distances[j] for j in range(stations_chosen[len(stations_chosen) - 2],stations_chosen[len(stations_chosen) - 1])] ) print stations_chosen print 'last_station: ' + str(stations_chosen[len(stations_chosen) - 2]) + ", price:" + str(self.route.stations[stations_chosen[len(stations_chosen) - 2]].regular) print 'this station: ' + str(stations_chosen[len(stations_chosen) - 1]) + ", price:" + str(self.route.stations[stations_chosen[len(stations_chosen) - 1]].regular) if (self.route.stations[stations_chosen[len(stations_chosen) - 2]].regular < self.route.stations[stations_chosen[len(stations_chosen) - 1]].regular): #fill 'er up, errr, conditionally conditionallyFillUp(gas_in_tank_at_last_station, stations_chosen[len(stations_chosen) - 2]) else: #only get enough gas to get to next station print 'getting minimum' bought.append(distance_between_stations / self.car.economy) gas_in_tank_at_last_station = gas_in_tank_at_last_station - (distance_between_stations / self.car.economy) + bought[len(bought) - 1] miles_since_last_stop = 0 stations_chosen.append(station_chosen) conditionallyFillUp(gas_in_tank_at_last_station, stations_chosen[len(stations_chosen) - 1]) print 'stations_chosen: ' + str(stations_chosen) print 'bought: ' + str(bought) objective_value = sum( [self.route.stations[stations_chosen[j]].regular*bought[j] for j in range(len(stations_chosen))] ) self.stations_chosen = stations_chosen else: # how to constrain the LP in_tank = variable(n,name ='gas in tank') # how much gas in tank var bought = variable(n,name = 'gas bought') # how much to buy at each station var constraints = ([eq(in_tank[0,0],self.car.start)] + # starting gas [eq(in_tank[i+1,0], # mass balance in_tank[i,0] + bought[i,0] - self.route.distances[i]/self.car.economy)#DV: Changed from * to / for i in range(n-1)] + [geq(in_tank,self.gas_min)] + # can't dip below certain amount [leq(in_tank + bought,self.car.tank_max)] + # max size of tank [geq(bought,0)] # physical constraint (cannot cyphon gas for sale to bum) ) # the total cost of the fuel fuel_cost = sum( [self.route.stations[j].regular*bought[j,0] for j in range(n)] ) # define program p = program(minimize(fuel_cost), constraints) objective_value = p.solve() # attach properties to access later self.in_tank = in_tank self.program = p self.bought = bought self.objective_value = objective_value
#!/usr/bin/python import cvxpy import numpy S = cvxpy.matrix([[-1, 1, 0], [0, -1, 1]]) Km = cvxpy.matrix([[1e-4, 0, 0], [0, 1e-4, 0]]) kcat = cvxpy.matrix([[100],[100]]) m_plus = numpy.abs(numpy.clip(S, -1000, 0)) c = cvxpy.variable(3, 1, name='concentrations') opt = cvxpy.minimize()
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
import cvxpy as cp import numpy as np I = np.random.randn(10, 10) > .4 I[np.diag_indices_from(I)] = 0 K = np.shape(I)[0] X = cp.variable(K, K, name='X') const = [] for i in range(K): for j in range(K): if I[i, j] > 0: c = cp.equals(X[i, j], 0) const.append(c) c = cp.equals(cp.diag(X), 1) const.append(c) p = cp.program(cp.minimize(cp.nuclear_norm(X)), const) p.solve(quiet=False) print X.value
#!/usr/bin/python import cvxpy import numpy S = cvxpy.matrix([[-1, 1, 0], [0, -1, 1]]) Km = cvxpy.matrix([[1e-4, 0, 0], [0, 1e-4, 0]]) kcat = cvxpy.matrix([[100], [100]]) m_plus = numpy.abs(numpy.clip(S, -1000, 0)) c = cvxpy.variable(3, 1, name='concentrations') opt = cvxpy.minimize()
noise = noise*(tau/norm(noise)) # Add noise to signal rSig = signal + noise rSig = abs(rSig) #phase is not used in MRI # Choose regularization parameter # lambda > lambda_max -> zero solution lambda_max = 2*norm(dot(nA.T, rSig.T), np.inf) lamb = 1.0e-8*lambda_max print('Solving L1 penalized system with cvxpy...') coefs = cvx.variable(n_qpnts,1) A = cvx.matrix(nA) rhs = cvx.matrix(rSig).T objective = cvx.minimize(cvx.norm2(A*coefs - rhs) + lamb*cvx.norm1(coefs) ) constraints = [cvx.geq(coefs,0.0)] prob = cvx.program(objective, constraints) # Call the solver prob.solve(quiet=True) #Use quiet=True to suppress output # Convert the cvxmod objects to plain numpy arrays for further processing nd_coefs_l1 = np.array(coefs.value).squeeze()
def measurenx_to_approxnx( roadnet, epsilon, length='length', weight1='weight1', weight2='weight2' ) : """ input: a road network, with weights on its elements output: returns a graph summarizing the network optimization problem instance; roadnets are multi-digraph, where edge 'keys' are assumed to be unique, i.e., road names; and should be different from node labels too; """ digraph = nx.DiGraph() #digraph.add_node('s') #digraph.add_node('t') SUPPLY = [] DEMAND = [] """ insert supply and demand of roads """ for u,v, road, data in roadnet.edges_iter( keys=True, data=True ) : roadlen = float( data.get( length, 1 ) ) # float() just in case assert roadlen >= 0. """ split the road into equal-length segments; create a node for each segment; record boundary points, and mass contained """ N = int( np.ceil( roadlen / epsilon ) ) eps = roadlen / N surplus = float( data.get( weight1, 0. ) ) - data.get( weight2, 0. ) deficit = -surplus bd = np.linspace( 0, roadlen, N+1 ) bd = [ roadmaps.RoadAddress( road, x ) for x in bd ] for i, boundary in enumerate( zip( bd[:-1], bd[1:] ) ) : if surplus > 0. : node = (road,i,'supply') digraph.add_node( node, boundary=boundary ) digraph.add_edge( 's', node, flow=cvxpy.variable(), minflow=0., maxflow=surplus/N ) SUPPLY.append( node ) if deficit > 0. : node = (road,i,'demand') digraph.add_node( node, boundary=boundary ) digraph.add_edge( node, 't', flow=cvxpy.variable(), minflow=0., maxflow=deficit/N ) DEMAND.append( node ) """ ...and nodes """ for u, data in roadnet.nodes_iter( data=True ) : surplus = data.get( weight1, 0. ) - data.get( weight2, 0. ) deficit = -surplus if surplus > 0. : boundary = [ roadmaps.roadify( roadnet, u, weight=length ) ] node = (u,'supply') digraph.add_node( node, boundary=boundary ) digraph.add_edge( 's', node, flow=cvxpy.variable(), minflow=0., maxflow=surplus ) SUPPLY.append( node ) if deficit > 0. : boundary = [ roadmaps.roadify( roadnet, v, weight=length ) ] node = (u,'demand') digraph.add_node( node, boundary=boundary ) digraph.add_edge( node, 't', flow=cvxpy.variable(), minflow=0., maxflow=deficit ) DEMAND.append( node ) """ generate bipartite graph b/w SUPPLY and DEMAND """ for u, v in itertools.product( SUPPLY, DEMAND ) : bd_u = digraph.node[u]['boundary'] bd_v = digraph.node[v]['boundary'] options = [ pair for pair in itertools.product( bd_u, bd_v ) ] options = [ roadmaps.distance( roadnet, p, q, weight=length ) for p,q in options ] #options = [ np.inf ] w = min( options ) W = max( options ) flowvar = cvxpy.variable() digraph.add_edge( u, v, flow=flowvar, minflow=0., w=w, W=W, cost_lo = w * flowvar, cost_hi = W * flowvar ) nxopt.attach_flownx_constraints( digraph ) return digraph # a flow network
def FindMTDF(self, concentration_bounds=None, normalization=None): """Finds the MTDF. Args: bounds: the Bounds objects setting concentration bounds. """ my_bounds = concentration_bounds or self.DefaultConcentrationBounds() normalization = normalization or self.DeltaGNormalization.DEFAULT # Constrain concentrations 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)) ] # Make the objective motive_force_lb = cvxpy.variable(name='B') 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)) else: motive_force = self.DeltaGNormalization.NormalizeDGByFlux( curr_dgr, flux, normalization) constr.append(cvxpy.geq(motive_force, motive_force_lb)) objective = cvxpy.maximize(motive_force_lb) problem = cvxpy.program(objective, constr, name='MTDF_OPT') problem.solve(quiet=True) """ if status != 'optimal': status = optimized_pathway.OptimizationStatus.Infeasible( 'Pathway infeasible given bounds.') return MTDFOptimizedPathway( self._model, self._thermo, my_bounds, optimization_status=status) """ mtdf = float(motive_force_lb.value) opt_ln_conc = np.matrix(np.array(ln_conc.value)) result = MTDFOptimizedPathway( self._model, self._thermo, my_bounds, optimal_value=mtdf, optimal_ln_metabolite_concentrations=opt_ln_conc) result.SetNormalization(normalization) return result
import cvxpy as cp import numpy as np I = np.random.randn(10,10) > .4 I[np.diag_indices_from(I)] = 0 K = np.shape(I)[0] X = cp.variable(K,K, name='X') const = [] for i in range(K): for j in range(K): if I[i,j] > 0: c = cp.equals(X[i,j],0) const.append(c) c = cp.equals(cp.diag(X),1) const.append(c) p = cp.program(cp.minimize(cp.nuclear_norm(X)), const) p.solve(quiet=False) print X.value
def FindMTDF(self, concentration_bounds=None, normalization=None): """Finds the MTDF. Args: bounds: the Bounds objects setting concentration bounds. """ my_bounds = concentration_bounds or self.DefaultConcentrationBounds() normalization = normalization or self.DeltaGNormalization.DEFAULT # Constrain concentrations 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))] # Make the objective motive_force_lb = cvxpy.variable(name='B') 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)) else: motive_force = self.DeltaGNormalization.NormalizeDGByFlux( curr_dgr, flux, normalization) constr.append(cvxpy.geq(motive_force, motive_force_lb)) objective = cvxpy.maximize(motive_force_lb) problem = cvxpy.program(objective, constr, name='MTDF_OPT') problem.solve(quiet=True) """ if status != 'optimal': status = optimized_pathway.OptimizationStatus.Infeasible( 'Pathway infeasible given bounds.') return MTDFOptimizedPathway( self._model, self._thermo, my_bounds, optimization_status=status) """ mtdf = float(motive_force_lb.value) opt_ln_conc = np.matrix(np.array(ln_conc.value)) result = MTDFOptimizedPathway( self._model, self._thermo, my_bounds, optimal_value=mtdf, optimal_ln_metabolite_concentrations=opt_ln_conc) result.SetNormalization(normalization) return result
def fit_ellipse(x, y): """ fit ellipoid using squared loss and abs loss """ #TODO introduce flag for switching between losses assert len(x) == len(y) N = len(x) D = 5 dat = numpy.zeros((N, D)) dat[:,0] = x*x dat[:,1] = y*y #dat[:,2] = x*y dat[:,2] = x dat[:,3] = y dat[:,4] = numpy.ones(N) print dat.shape dat = cvxpy.matrix(dat) #### parameters # data X = cvxpy.parameter(N, D, name="X") #### varibales # parameter vector theta = cvxpy.variable(D, name="theta") # simple objective objective = cvxpy.norm1(X*theta) # create problem p = cvxpy.program(cvxpy.minimize(objective)) p.constraints.append(cvxpy.eq(theta[0,:] + theta[1,:], 1)) ###### set values X.value = dat p.solve() w = numpy.array(theta.value) #print weights ## For clarity, fill in the quadratic form variables A = numpy.zeros((2,2)) A[0,0] = w[0] A.ravel()[1:3] = 0 #w[2] A[1,1] = w[1] bv = w[2:4] c = w[4] ## find parameters z, a, b, alpha = util.conic2parametric(A, bv, c) print "XXX", z, a, b, alpha return z, a, b, alpha