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)
Exemplo n.º 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)
Exemplo n.º 3
0
 def con_ctm(self):
   return list(flatten([geq(link.v_flow, 0),
                        leq(link.v_flow, link.fd.q_max),
                        leq(link.v_flow, link.fd.v * link.v_dens),
                        leq(link.v_flow, link.fd.w * (link.fd.rho_max - link.v_dens)),
                        geq(link.v_dens, 0),
                        leq(link.v_dens, link.fd.rho_max),
                        ] for link in self.get_links()))
 def constraint(self):
   if self.type == self.Type.EQ:
     return eq(self.a, self.b)
   if self.type == self.Type.LEQ:
     return leq(self.a, self.b)
   if self.type == self.Type.GEQ:
     return geq(self.a, self.b)
Exemplo n.º 5
0
    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
Exemplo n.º 6
0
def attach_flownx_constraints( flownx ) :
    """ add flow capacity constraints to edges """
    for _,__, data in flownx.edges_iter( data=True ) :
        flowvar = data.get('flow')
        constr = []
        
        minflow = data.get( 'minflow', -np.inf )
        if minflow > -np.inf : constr.append( cvxpy.geq( flowvar, minflow ) )
        maxflow = data.get( 'maxflow', np.inf )
        if maxflow < np.inf : constr.append( cvxpy.leq( flowvar, maxflow ) )
        
        data['constraints'] = constr
    
    """ add flow conservation constraints to nodes, besides 's' and 't' """
    for u, node_data in flownx.nodes_iter( data=True ) :
        if u in [ 's', 't' ] : continue
        
        in_flow = 0.
        for _,__, data in flownx.in_edges_iter( u, data=True ) :
            in_flow += data.get('flow')
            
        out_flow = 0.
        for _,__, data in flownx.out_edges_iter( u, data=True ) :
            out_flow += data.get('flow')
            
        node_data['constraints'] = [ cvxpy.eq( in_flow, out_flow ) ]
Exemplo n.º 7
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
Exemplo n.º 8
0
    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
Exemplo n.º 9
0
 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
Exemplo n.º 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
Exemplo n.º 11
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
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
Exemplo n.º 13
0
    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
Exemplo n.º 14
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
Exemplo n.º 15
0
 def _MakeLnConcentratonBounds(self, ln_conc, bounds=None, c_range=None):
     """Make bounds on logarithmic concentrations."""
     _c_range = c_range or self.DEFAULT_C_RANGE
     c_lower, c_upper = c_range
     ln_conc_lb = np.ones((1, self.Nc)) * np.log(c_lower)
     ln_conc_ub = np.ones((1, self.Nc)) * np.log(c_upper)
     
     if bounds:
         for i, bound in enumerate(bounds):
             lb, ub = bound
             log_lb = np.log(lb or c_lower)
             log_ub = np.log(ub or c_upper)
             if log_lb > log_ub:
                 raise Exception("Lower bound is greater than upper bound: "
                                 "%d > %d" % (log_lb, log_ub))
             elif abs(log_lb - log_ub) < 1e-2:
                 log_lb = log_ub - 1e-2
                 
             ln_conc_lb[0, i] = log_lb
             ln_conc_ub[0, i] = log_ub
     
     return [cvxpy.geq(ln_conc, cvxpy.matrix(ln_conc_lb)) + \
             cvxpy.leq(ln_conc, cvxpy.matrix(ln_conc_ub))]
Exemplo n.º 16
0
    def _MakeLnConcentratonBounds(self, ln_conc, bounds=None, c_range=None):
        """Make bounds on logarithmic concentrations."""
        _c_range = c_range or self.DEFAULT_C_RANGE
        c_lower, c_upper = c_range
        ln_conc_lb = np.ones((1, self.Nc)) * np.log(c_lower)
        ln_conc_ub = np.ones((1, self.Nc)) * np.log(c_upper)

        if bounds:
            for i, bound in enumerate(bounds):
                lb, ub = bound
                log_lb = np.log(lb or c_lower)
                log_ub = np.log(ub or c_upper)
                if log_lb > log_ub:
                    raise Exception("Lower bound is greater than upper bound: "
                                    "%d > %d" % (log_lb, log_ub))
                elif abs(log_lb - log_ub) < 1e-2:
                    log_lb = log_ub - 1e-2

                ln_conc_lb[0, i] = log_lb
                ln_conc_ub[0, i] = log_ub

        return [cvxpy.geq(ln_conc, cvxpy.matrix(ln_conc_lb)) + \
                cvxpy.leq(ln_conc, cvxpy.matrix(ln_conc_ub))]
Exemplo n.º 17
0
 def con_route_tt(self):
   return [
   leq(self.route_tt_heuristic(route), 10000.0)
   for route in self.all_routes()
   ]
Exemplo n.º 18
0
 def check_feasible_constraints(self):
   return list(flatten(
     [geq(link.v_flow, link.flow), geq(link.v_dens, link.rho),leq(link.v_flow, link.flow), leq(link.v_dens, link.rho)]
       for link in self.get_links()
   ))
Exemplo n.º 19
0
    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
        
        
        
Exemplo n.º 20
0
    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
        
        
        
Exemplo n.º 21
0
    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
Exemplo n.º 22
0
    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
Exemplo n.º 23
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