def con_od_flows(self): od_demands = filter(lambda dem: isinstance(dem, ODDemand), self.demands) route_demands = filter(lambda dem: isinstance(dem, RouteDemand), self.demands) def route_flows(link): flow = 0.0 link_routes = link.routes() for route in link_routes: flow += route.v_flow for dem in route_demands: if dem.route in link_routes: flow += dem.flow return flow def od_flows(source, sink): flow = 0 for route in self.od_routes[source, sink]: flow += route.v_flow return flow return [ eq(route_flows(link), link.v_flow) for link in self.get_links() ] + [ eq(od_flows(dem.source, dem.sink), dem.flow) for dem in od_demands ] + [ geq(route.v_flow, 0.0) for route in self.all_routes() ]
def test_problem_ubsdp(self): # test problem needs review print 'skipping, needs review' return from cvxpy import (matrix, variable, program, minimize, sum, abs, norm2, log, square, zeros, max, hstack, vstack, eye, eq, trace, semidefinite_cone, belongs, reshape) (m, n) = (self.m, self.n) c = matrix(self.c) A = matrix(self.A) B = matrix(self.B) Xubsdp = matrix(self.Xubsdp) tol_exp = 6 # Use cvxpy to solve # # minimize tr(B * X) # s.t. tr(Ai * X) + ci = 0, i = 1, ..., n # X + S = I # X >= 0, S >= 0. # # c is an n-vector. # A is an m^2 x n-matrix. # B is an m x m-matrix. X = variable(m, n) S = variable(m, n) constr = [ eq(trace(reshape(A[:, i], (m, m)) * X) + c[i, 0], 0.0) for i in range(n) ] constr += [eq(X + S, eye(m))] constr += [ belongs(X, semidefinite_cone), belongs(S, semidefinite_cone) ] p = program(minimize(trace(B * X)), constr) p.solve(True) np.testing.assert_array_almost_equal(X.value, self.Xubsdp, tol_exp) # Use cvxpy to solve # # minimize tr(B * X) # s.t. tr(Ai * X) + ci = 0, i = 1, ..., n # 0 <= X <= I X2 = variable(m, n) constr = [ eq(trace(reshape(A[:, i], (m, m)) * X2) + c[i, 0], 0.0) for i in range(n) ] constr += [ belongs(X2, semidefinite_cone), belongs(eye(m) - X2, semidefinite_cone) ] p = program(minimize(trace(B * X2)), constr) p.solve(True) np.testing.assert_array_almost_equal(X2.value, X.value, tol_exp)
def test_problem_ubsdp(self): # test problem needs review print 'skipping, needs review' return from cvxpy import (matrix, variable, program, minimize, sum, abs, norm2, log, square, zeros, max, hstack, vstack, eye, eq, trace, semidefinite_cone, belongs, reshape) (m, n) = (self.m, self.n) c = matrix(self.c) A = matrix(self.A) B = matrix(self.B) Xubsdp = matrix(self.Xubsdp) tol_exp = 6 # Use cvxpy to solve # # minimize tr(B * X) # s.t. tr(Ai * X) + ci = 0, i = 1, ..., n # X + S = I # X >= 0, S >= 0. # # c is an n-vector. # A is an m^2 x n-matrix. # B is an m x m-matrix. X = variable(m, n) S = variable(m, n) constr = [eq(trace(reshape(A[:,i], (m, m)) * X) + c[i,0], 0.0) for i in range(n)] constr += [eq(X + S, eye(m))] constr += [belongs(X, semidefinite_cone), belongs(S, semidefinite_cone)] p = program(minimize(trace(B * X)), constr) p.solve(True) np.testing.assert_array_almost_equal( X.value, self.Xubsdp, tol_exp) # Use cvxpy to solve # # minimize tr(B * X) # s.t. tr(Ai * X) + ci = 0, i = 1, ..., n # 0 <= X <= I X2 = variable(m, n) constr = [eq(trace(reshape(A[:,i], (m, m)) * X2) + c[i,0], 0.0) for i in range(n)] constr += [belongs(X2, semidefinite_cone), belongs(eye(m) - X2, semidefinite_cone)] p = program(minimize(trace(B * X2)), constr) p.solve(True) np.testing.assert_array_almost_equal( X2.value, X.value, tol_exp)
def construct_lp_relaxation( demand_hist, M, transport_graph, cost_label='cost' ) : """ demand_hist is a dictionary from ordered station pairs (i,j) -> whole integers M is a membership graph with edges (i,x) for all corresponding stations-state pairs A is a DiGraph on X, with cost given by property 'cost' """ APSP = nx.all_pairs_dijkstra_path_length( transport_graph, weight=cost_label ) cost = 0. constr = [] """ create enroute variables """ S = nx.DiGraph() # service "edges" for (i,j), NUM in demand_hist.iteritems() : VARS = [] for x,y in itertools.product( M.neighbors_iter(i), M.neighbors_iter(j) ) : var = cvxpy.variable() distance = APSP[x][y] cost += var * distance constr.append( cvxpy.geq( var, 0. ) ) # needs to be non-negative S.add_edge( x, y, var=var, distance=distance ) VARS.append( var ) constr.append( cvxpy.eq( cvxpy.sum(VARS), NUM ) ) # total such trips should sum to num """ create balance variables """ B = nx.DiGraph() HEADS = [ h for h, d in S.in_degree_iter() if d > 0 ] TAILS = [ t for t, d in S.out_degree_iter() if d > 0 ] for y,x in itertools.product( HEADS, TAILS ) : var = cvxpy.variable() cost += var * APSP[y][x] constr.append( cvxpy.geq( var, 0. ) ) B.add_edge( y,x, var=var ) """ generate flow conservation constraints """ for x in S.nodes_iter() : sin = sum( [ data['var'] for _,__,data in S.in_edges_iter( x, data=True ) ] ) sout = sum( [ data['var'] for _,__,data in S.out_edges_iter( x, data=True ) ] ) bin = sum( [ data['var'] for _,__,data in B.in_edges_iter( x, data=True ) ] ) bout = sum( [ data['var'] for _,__,data in B.out_edges_iter( x, data=True ) ] ) constr.append( cvxpy.eq( sin, bout ) ) constr.append( cvxpy.eq( sout, bin ) ) #service_vars = S # alias return cost, constr, S
def _MakeDrivingForceConstraints(self, ln_conc, driving_force_lb=0): """ driving_force_lb can either be a cvxpy variable use later in the optimization or a scalar, which sets it as a constraint. By default the lower bound is 0. """ constraints = [] S = cvxpy.matrix(self.S) dg0r_primes = cvxpy.matrix(self.dG0_r_prime) 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 += cvxpy.eq(curr_dgr, 0) else: if self.normalization == DeltaGNormalization.DIVIDE_BY_FLUX: motive_force = -curr_dgr * (1.0 / self.fluxes[0, i]) elif self.normalization == DeltaGNormalization.TIMES_FLUX: motive_force = -curr_dgr * self.fluxes[0, i] elif self.normalization == DeltaGNormalization.SIGN_FLUX: motive_force = -curr_dgr * np.sign(self.fluxes[0, i]) else: raise ValueError("bad value for normalization method: " + str(self.normalization)) constraints += [cvxpy.geq(motive_force, driving_force_lb)] return constraints
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)
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 ) ]
def _MakeMinimalFeasbileConcentrationProblem(self, bounds=None, c_range=(1e-6, 1e-2)): # Define and apply the constraints on the concentrations constraints = [] # Define and apply the constraints on the concentrations ln_conc = cvxpy.variable(1, self.Nc, name='lnC') constraints += self._MakeLnConcentratonBounds(ln_conc, bounds=bounds, c_range=c_range) # find the row vector describing the overall stoichiometry S = cvxpy.matrix(self.S) dg0r_primes = cvxpy.matrix(self.dG0_r_prime) # Make flux-based constraints on reaction free energies. # All reactions must have negative dGr in the direction of the flux. # Reactions with a flux of 0 must be in equilibrium. for i in xrange(self.Nr): # if the dG0 is unknown, this reaction imposes no new constraints if np.isnan(self.dG0_r_prime[0, i]): continue curr_dgr = dg0r_primes[0, i] + RT * ln_conc * S[:, i] if self.fluxes[0, i] != 0: constraints.append(cvxpy.leq(curr_dgr * np.sign(self.fluxes[0, i]), self.DEFAULT_REACTION_UB)) constraints.append(cvxpy.geq(curr_dgr * np.sign(self.fluxes[0, i]), self.DEFAULT_REACTION_LB)) else: constraints.append(cvxpy.eq(curr_dgr, 0)) # Set the constraints return ln_conc, constraints
def 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_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 _MakeMinimumFeasbileConcentrationsProblem(self, bounds=None, c_range=(1e-6, 1e-2)): """Creates the CVXOPT problem for finding minimum total concentrations. Returns: Two tuple (ln_concentrations var, problem). """ assert self.dG0_f_prime is not None constraints = [] # Define and apply the constraints on the concentrations ln_conc = cvxpy.variable(1, self.Nc, name='lnC') constraints += self._MakeLnConcentratonBounds(ln_conc, bounds=bounds, c_range=c_range) # Make the objective and problem. S = cvxpy.matrix(self.S) # Make flux-based constraints on reaction free energies. # All reactions must have negative dGr in the direction of the flux. # Reactions with a flux of 0 must be in equilibrium. dgf_primes = RT * ln_conc + cvxpy.matrix(self.dG0_f_prime) for i in xrange(self.Nr): if self.fluxes[0, i] > 0: constraints.append( cvxpy.leq(S[i, :] * dgf_primes, self.DEFAULT_REACTION_UB)) constraints.append( cvxpy.geq(S[i, :] * dgf_primes, self.DEFAULT_REACTION_LB)) elif self.fluxes[0, i] == 0: constraints.append(cvxpy.eq(S[i, :] * dgf_primes, 0)) else: constraints.append( cvxpy.geq(S[i, :] * dgf_primes, -self.DEFAULT_REACTION_UB)) constraints.append( cvxpy.leq(S[i, :] * dgf_primes, -self.DEFAULT_REACTION_LB)) return ln_conc, constraints
def _MakeMinimumFeasbileConcentrationsProblem(self, bounds=None, c_range=(1e-6, 1e-2)): """Creates the CVXOPT problem for finding minimum total concentrations. Returns: Two tuple (ln_concentrations var, problem). """ assert self.dG0_f_prime is not None constraints = [] # Define and apply the constraints on the concentrations ln_conc = cvxpy.variable(1, self.Nc, name='lnC') constraints += self._MakeLnConcentratonBounds(ln_conc, bounds=bounds, c_range=c_range) # Make the objective and problem. S = cvxpy.matrix(self.S) # Make flux-based constraints on reaction free energies. # All reactions must have negative dGr in the direction of the flux. # Reactions with a flux of 0 must be in equilibrium. dgf_primes = RT * ln_conc + cvxpy.matrix(self.dG0_f_prime) for i in xrange(self.Nr): if self.fluxes[0, i] > 0: constraints.append(cvxpy.leq(S[i, :] * dgf_primes, self.DEFAULT_REACTION_UB)) constraints.append(cvxpy.geq(S[i, :] * dgf_primes, self.DEFAULT_REACTION_LB)) elif self.fluxes[0, i] == 0: constraints.append(cvxpy.eq(S[i, :] * dgf_primes, 0)) else: constraints.append(cvxpy.geq(S[i, :] * dgf_primes, -self.DEFAULT_REACTION_UB)) constraints.append(cvxpy.leq(S[i, :] * dgf_primes, -self.DEFAULT_REACTION_LB)) return ln_conc, constraints
def _MakeMinimalFeasbileConcentrationProblem(self, bounds=None, c_range=(1e-6, 1e-2)): # Define and apply the constraints on the concentrations constraints = [] # Define and apply the constraints on the concentrations ln_conc = cvxpy.variable(1, self.Nc, name='lnC') constraints += self._MakeLnConcentratonBounds(ln_conc, bounds=bounds, c_range=c_range) # find the row vector describing the overall stoichiometry S = cvxpy.matrix(self.S) dg0r_primes = cvxpy.matrix(self.dG0_r_prime) # Make flux-based constraints on reaction free energies. # All reactions must have negative dGr in the direction of the flux. # Reactions with a flux of 0 must be in equilibrium. for i in xrange(self.Nr): # if the dG0 is unknown, this reaction imposes no new constraints if np.isnan(self.dG0_r_prime[0, i]): continue curr_dgr = dg0r_primes[0, i] + RT * ln_conc * S[:, i] if self.fluxes[0, i] != 0: constraints.append( cvxpy.leq(curr_dgr * np.sign(self.fluxes[0, i]), self.DEFAULT_REACTION_UB)) constraints.append( cvxpy.geq(curr_dgr * np.sign(self.fluxes[0, i]), self.DEFAULT_REACTION_LB)) else: constraints.append(cvxpy.eq(curr_dgr, 0)) # Set the constraints return ln_conc, constraints
def estimate_transcript_frequencies_with_cvxopt(observed_array, expected_array, sparse_penalty, sparse_index, verbose=False): from cvxpy import matrix, variable, geq, log, eq, program, maximize, \ minimize, sum, quad_over_lin Xs = matrix(observed_array) ps = matrix(expected_array) thetas = variable(ps.shape[1]) constraints = [eq(sum(thetas), 1), geq(thetas, 0)] if sparse_penalty == None: p = program(maximize(Xs * log(ps * thetas)), constraints) else: p = program( maximize(Xs * log(ps * thetas) - sparse_penalty * quad_over_lin(1., thetas[sparse_index, 0])), constraints) p.options['maxiters'] = 1500 value = p.solve(quiet=not verbose) thetas_values = numpy.array(thetas.value.T.tolist()[0]) return thetas_values
def con_junc(self): return [eq(sum(link.v_flow for link in junction.in_links), sum(link.v_flow for link in junction.out_links)) for junction in self.junctions]
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
def FindMTDF(self, concentration_bounds=None, normalization=None): """Finds the MTDF. Args: bounds: the Bounds objects setting concentration bounds. """ my_bounds = concentration_bounds or self.DefaultConcentrationBounds() normalization = normalization or self.DeltaGNormalization.DEFAULT # Constrain concentrations ln_conc = cvxpy.variable(m=1, n=self.Ncompounds, name='lnC') ln_conc_lb, ln_conc_ub = my_bounds.GetLnBounds(self.compounds) constr = [ cvxpy.geq(ln_conc, cvxpy.matrix(ln_conc_lb)), cvxpy.leq(ln_conc, cvxpy.matrix(ln_conc_ub)) ] # Make the objective motive_force_lb = cvxpy.variable(name='B') my_dG0_r_primes = np.matrix(self.dG0_r_prime) # Make flux-based constraints on reaction free energies. # All reactions must have negative dGr in the direction of the flux. # Reactions with a flux of 0 must be in equilibrium. S = np.matrix(self.S) for i, flux in enumerate(self.fluxes): curr_dg0 = my_dG0_r_primes[0, i] # if the dG0 is unknown, this reaction imposes no new constraints if np.isnan(curr_dg0): continue rcol = cvxpy.matrix(S[:, i]) curr_dgr = curr_dg0 + RT * ln_conc * rcol if flux == 0: constr.append(cvxpy.eq(curr_dgr, 0)) else: motive_force = self.DeltaGNormalization.NormalizeDGByFlux( curr_dgr, flux, normalization) constr.append(cvxpy.geq(motive_force, motive_force_lb)) objective = cvxpy.maximize(motive_force_lb) problem = cvxpy.program(objective, constr, name='MTDF_OPT') problem.solve(quiet=True) """ if status != 'optimal': status = optimized_pathway.OptimizationStatus.Infeasible( 'Pathway infeasible given bounds.') return MTDFOptimizedPathway( self._model, self._thermo, my_bounds, optimization_status=status) """ mtdf = float(motive_force_lb.value) opt_ln_conc = np.matrix(np.array(ln_conc.value)) result = MTDFOptimizedPathway( self._model, self._thermo, my_bounds, optimal_value=mtdf, optimal_ln_metabolite_concentrations=opt_ln_conc) result.SetNormalization(normalization) return result
def FindMTDF(self, concentration_bounds=None, normalization=None): """Finds the MTDF. Args: bounds: the Bounds objects setting concentration bounds. """ my_bounds = concentration_bounds or self.DefaultConcentrationBounds() normalization = normalization or self.DeltaGNormalization.DEFAULT # Constrain concentrations ln_conc = cvxpy.variable(m=1, n=self.Ncompounds, name='lnC') ln_conc_lb, ln_conc_ub = my_bounds.GetLnBounds(self.compounds) constr = [cvxpy.geq(ln_conc, cvxpy.matrix(ln_conc_lb)), cvxpy.leq(ln_conc, cvxpy.matrix(ln_conc_ub))] # Make the objective motive_force_lb = cvxpy.variable(name='B') my_dG0_r_primes = np.matrix(self.dG0_r_prime) # Make flux-based constraints on reaction free energies. # All reactions must have negative dGr in the direction of the flux. # Reactions with a flux of 0 must be in equilibrium. S = np.matrix(self.S) for i, flux in enumerate(self.fluxes): curr_dg0 = my_dG0_r_primes[0, i] # if the dG0 is unknown, this reaction imposes no new constraints if np.isnan(curr_dg0): continue rcol = cvxpy.matrix(S[:, i]) curr_dgr = curr_dg0 + RT * ln_conc * rcol if flux == 0: constr.append(cvxpy.eq(curr_dgr, 0)) else: motive_force = self.DeltaGNormalization.NormalizeDGByFlux( curr_dgr, flux, normalization) constr.append(cvxpy.geq(motive_force, motive_force_lb)) objective = cvxpy.maximize(motive_force_lb) problem = cvxpy.program(objective, constr, name='MTDF_OPT') problem.solve(quiet=True) """ if status != 'optimal': status = optimized_pathway.OptimizationStatus.Infeasible( 'Pathway infeasible given bounds.') return MTDFOptimizedPathway( self._model, self._thermo, my_bounds, optimization_status=status) """ mtdf = float(motive_force_lb.value) opt_ln_conc = np.matrix(np.array(ln_conc.value)) result = MTDFOptimizedPathway( self._model, self._thermo, my_bounds, optimal_value=mtdf, optimal_ln_metabolite_concentrations=opt_ln_conc) result.SetNormalization(normalization) return result
def 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
def MinimizeConcentration(self, metabolite_index=None, concentration_bounds=None): """Finds feasible concentrations minimizing the concentration of metabolite i. Args: metabolite_index: the index of the metabolite to minimize. if == None, minimize the sum of all concentrations. concentration_bounds: the Bounds objects setting concentration bounds. """ my_bounds = concentration_bounds or self.DefaultConcentrationBounds() ln_conc = cvxpy.variable(m=1, n=self.Ncompounds, name='lnC') ln_conc_lb, ln_conc_ub = my_bounds.GetLnBounds(self.compounds) constr = [ cvxpy.geq(ln_conc, cvxpy.matrix(ln_conc_lb)), cvxpy.leq(ln_conc, cvxpy.matrix(ln_conc_ub)) ] my_dG0_r_primes = np.matrix(self.dG0_r_prime) # Make flux-based constraints on reaction free energies. # All reactions must have negative dGr in the direction of the flux. # Reactions with a flux of 0 must be in equilibrium. S = np.matrix(self.S) for i, flux in enumerate(self.fluxes): curr_dg0 = my_dG0_r_primes[0, i] # if the dG0 is unknown, this reaction imposes no new constraints if np.isnan(curr_dg0): continue rcol = cvxpy.matrix(S[:, i]) curr_dgr = curr_dg0 + RT * ln_conc * rcol if flux == 0: constr.append(cvxpy.eq(curr_dgr, 0.0)) else: constr.append(cvxpy.leq(curr_dgr, 0.0)) objective = None if metabolite_index is not None: my_conc = ln_conc[0, metabolite_index] objective = cvxpy.minimize(cvxpy.exp(my_conc)) else: objective = cvxpy.minimize(cvxpy.sum(cvxpy.exp(ln_conc))) name = 'CONC_OPT' if metabolite_index: name = 'CONC_%d_OPT' % metabolite_index problem = cvxpy.program(objective, constr, name=name) optimum = problem.solve(quiet=True) """ status = problem.solve(quiet=True) if status != 'optimal': status = optimized_pathway.OptimizationStatus.Infeasible( 'Pathway infeasible given bounds.') return ConcentrationOptimizedPathway( self._model, self._thermo, my_bounds, optimization_status=status) """ opt_ln_conc = np.matrix(np.array(ln_conc.value)) result = ConcentrationOptimizedPathway( self._model, self._thermo, my_bounds, optimal_value=optimum, optimal_ln_metabolite_concentrations=opt_ln_conc) return result
def 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