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)
Example #2
0
    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)
Example #3
0
    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)
Example #5
0
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 group_cyclefactor_approx( demand_hist, M, transport_graph, cost_label='cost' ) :
    """ construct and solve the LP relaxation of the IP """
    cost, constr, SERVICE = construct_lp_relaxation( demand_hist, M, transport_graph, cost_label=cost_label )
    prog = cvxpy.program( cvxpy.minimize( cost ), constr )
    res = prog.solve( quiet=DEBUG )
    # still need to debug this garbage!!!! wtf is going on here?
    
    
    """ convert to proper format and "round" the solution """
    fractional = dict()
    
    trips_hist = dict()
    for (i,j) in demand_hist :
        target = dict()
        for edge in itertools.product( M.neighbors_iter(i), M.neighbors_iter(j) ) :
            temp = SERVICE.get_edge_data( *edge ).get('var').value
            fractional[ edge ] = temp
            target[ edge ] = temp
            
        demand_update = greedy_chairman_rounding( target )
        trips_hist.update( demand_update )
        
        
    """ construct and solve the resulting cycle factor instance """
    service_edges = nx.MultiDiGraph()
    for (x,y), NUMTRIPS in trips_hist.iteritems() :
        weight = nx.dijkstra_path_length( transport_graph, x, y, weight=cost_label )
        for k in range(NUMTRIPS) :
            service_edges.add_edge( x,y, cost=weight )
    
    cycles = cyclefactor.cyclefactor( service_edges, transport_graph )
    return cycles
Example #7
0
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 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 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)
Example #10
0
    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
Example #11
0
    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
Example #12
0
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
Example #13
0
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
Example #14
0
 def FindKineticOptimum(self, bounds=None):
     """Use the power of convex optimization!
     
     minimize sum (protein cost)
     
     we can do this by using ln(concentration) as variables and leveraging 
     the convexity of exponentials.         
     """
     assert self.dG0_f_prime is not None
     
     ln_conc, constraints = self._MakeMinimumFeasbileConcentrationsProblem()
     total_inv_conc = cvxpy.sum(cvxpy.exp(-ln_conc))
     program = cvxpy.program(cvxpy.minimize(total_inv_conc), constraints)
     program.solve(quiet=True)
     return ln_conc.value, total_inv_conc.value
def mincost_maxflow( flownx, cost='cost', DELTA=None ) :
    if DELTA is None : DELTA = DEFAULT_DELTA
    
    total_flow = compute_totalflow( flownx )
    total_cost = compute_totalcost( flownx, cost=cost )
    constraints = collect_constraints( flownx )
    
    prog1 = cvxpy.program( cvxpy.maximize( total_flow ), constraints )
    max_flow = prog1.solve()
    
    constraints2 = [ c for c in constraints ]
    constraints2.append( cvxpy.geq( total_flow, max_flow - DELTA ) )
    prog2 = cvxpy.program( cvxpy.minimize( total_cost ), constraints2 )
    res = prog2.solve()
    return res
Example #16
0
    def FindKineticOptimum(self, bounds=None):
        """Use the power of convex optimization!
        
        minimize sum (protein cost)
        
        we can do this by using ln(concentration) as variables and leveraging 
        the convexity of exponentials.         
        """
        assert self.dG0_f_prime is not None

        ln_conc, constraints = self._MakeMinimumFeasbileConcentrationsProblem()
        total_inv_conc = cvxpy.sum(cvxpy.exp(-ln_conc))
        program = cvxpy.program(cvxpy.minimize(total_inv_conc), constraints)
        program.solve(quiet=True)
        return ln_conc.value, total_inv_conc.value
Example #17
0
 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
Example #18
0
 def FindMinimalFeasibleConcentration(self, index_to_minimize,
                                      bounds=None, c_range=(1e-6, 1e-2)):
     """
         Compute the smallest ratio between two concentrations which makes the pathway feasible.
         All other compounds except these two are constrained by 'bounds' or unconstrained at all.
     
         Arguments:
             index_to_minimize - the column index of the compound whose concentration 
                                 is to be minimized
     
         Returns:
             dGs, concentrations, target-concentration
     """
     ln_conc, constraints = self._MakeMinimalFeasbileConcentrationProblem(bounds, c_range)
     objective = cvxpy.minimize(ln_conc[index_to_minimize])
     program = cvxpy.program(objective, constraints)
     program.solve(quiet=True) 
     return ln_conc.value, program.objective.value
Example #19
0
    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 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
Example #21
0
 def FindMinimalFeasibleConcentration(self,
                                      index_to_minimize,
                                      bounds=None,
                                      c_range=(1e-6, 1e-2)):
     """
         Compute the smallest ratio between two concentrations which makes the pathway feasible.
         All other compounds except these two are constrained by 'bounds' or unconstrained at all.
     
         Arguments:
             index_to_minimize - the column index of the compound whose concentration 
                                 is to be minimized
     
         Returns:
             dGs, concentrations, target-concentration
     """
     ln_conc, constraints = self._MakeMinimalFeasbileConcentrationProblem(
         bounds, c_range)
     objective = cvxpy.minimize(ln_conc[index_to_minimize])
     program = cvxpy.program(objective, constraints)
     program.solve(quiet=True)
     return ln_conc.value, program.objective.value
Example #22
0
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)
Example #23
0
    def GetTotalReactionEnergy(self, min_driving_force=0, maximize=True):
        """
            Maximizes the total pathway dG' (i.e. minimize energetic cost).
            Arguments:
                min_driving_force - the lower limit on each reaction's driving force
                                    (it is common to provide the optimize driving force
                                    in order to find the concentrations that minimize the
                                    cost, without affecting the MTDF).
                maximize          - if True then finds the maximal total dG.
                                    if False then finds the minimal total dG.
        """
        ln_conc, constraints, total_g = self._GetTotalReactionEnergy(
            self.c_range, self.bounds, min_driving_force)

        if maximize:
            objective = cvxpy.maximize(total_g)
        else:
            objective = cvxpy.minimize(total_g)

        program = cvxpy.program(objective, constraints)
        program.solve(quiet=True)
        return ln_conc.value, program.objective.value
Example #24
0
 def GetTotalReactionEnergy(self, min_driving_force=0, maximize=True):
     """
         Maximizes the total pathway dG' (i.e. minimize energetic cost).
         Arguments:
             min_driving_force - the lower limit on each reaction's driving force
                                 (it is common to provide the optimize driving force
                                 in order to find the concentrations that minimize the
                                 cost, without affecting the MTDF).
             maximize          - if True then finds the maximal total dG.
                                 if False then finds the minimal total dG.
     """
     ln_conc, constraints, total_g = self._GetTotalReactionEnergy(
                             self.c_range, self.bounds, min_driving_force)
     
     if maximize:
         objective = cvxpy.maximize(total_g)
     else:
         objective = cvxpy.minimize(total_g)
     
     program = cvxpy.program(objective, constraints)
     program.solve(quiet=True)
     return ln_conc.value, program.objective.value
Example #25
0
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
Example #26
0
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
Example #27
0
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 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
Example #29
0
    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()

    # Cutoff those coefficients that are less than cutoff
    cutoff =  nd_coefs_l1.mean() + 2.0*nd_coefs_l1.std(ddof=1)
    nd_coefs_l1_trim = np.where(nd_coefs_l1 > cutoff, nd_coefs_l1, 0)

    # Get indices needed for sorting coefs, in reverse order.
Example #30
0
 def objective(self):
   return minimize(sum(link.l * link.v_dens for link in self.get_links()))
    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
        
        
        
Example #32
0
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
Example #33
0
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
Example #34
0
#!/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()

Example #35
0
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
Example #36
0
    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
Example #37
0
#!/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()
Example #38
0
 def objective(self):
   return minimize(0.0)
Example #39
0
    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))
Example #40
0
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