def get_initial_guess(S, enzymeInfo, concLB, concUB): nMetabs = len(S.index) ini = [] for i, metab in enumerate(S.index): f = np.zeros(nMetabs) f[i] = 1 A = R * T * S.T b = -np.array([ -R * T * np.log(item[0]) for item in enzymeInfo.loc[:, 'Keq'] ]) lb = [np.log(concLB)] * nMetabs ub = [np.log(concUB)] * nMetabs meth = 'pclp' p = LP(f=f, A=A, b=b, lb=lb, ub=ub, iprint=-1) r = p.solve(meth, plot=0) ini.append(r.xf[i]) ini = np.array(ini) return ini
def optimize_minimal_driving_force(S, Vss, enzymeInfo, concLB, concUB): ''' Parameters S: df, stoichiometric matrix, metabolite in rows, reaction in columns. negative for substrates, positive for products Vss: ser, net fluxes in steady state (including in and out fluxes) enzymeInfo: df, reaction in rows concLB: float, concentration lower bound (mM) for all metabolites concUB: float, concentration upper bound (mM) for all metabolites Returns optConcs: ser, optimal log(concentrations) optDeltaGs: ser, optimal minimal driving forces refDeltaGs: float, reference minimal driving forces (all concentrations at 1 mM) ''' from openopt import LP from constants import R, T f = np.zeros(S.shape[0] + 1) f[0] = -1 S = S * Vss[S.columns] A = np.concatenate((np.ones((S.shape[1], 1)), R * T * S.T), axis=1) b = -np.array( [-R * T * np.log(item[0]) for item in enzymeInfo.loc[:, 'Keq']]) b = b * Vss[S.columns].values lb = [-np.inf] + [np.log(concLB)] * S.shape[0] ub = [np.inf] + [np.log(concUB)] * S.shape[0] meth = 'cvxopt_lp' p = LP(f=f, A=A, b=b, lb=lb, ub=ub, iprint=-1, name='Maximize minimal driving force') r = p.solve(meth, plot=0) optLogConcs = r.xf[1:] optConcs = pd.Series(np.exp(optLogConcs), index=S.index) optDeltaGs = pd.Series(-b + R * T * np.dot(S.T, optLogConcs), index=S.columns) refDeltaGs = pd.Series(-b, index=S.columns) return optConcs, optDeltaGs, refDeltaGs
def minimize(self, **kwargs): """ solve the linear problem with the solver given in self.solver Returns: obj_value, solution obj_value -- value of the objective function at the discovered solution solution -- the solution flux vector (indexed like matrix columns) """ if self.solver == _GLPK: # Use pyMathProg interface to GLPK solver self.glpkP.solve() self.istop = self.glpkP.status() # print "istop:", self.istop self.status = glpkToSolverStatus(self.istop) if self.status == SolverStatus.OPTIMAL: self.obj_value = self.glpkP.vobj() self.solution = [ self.glpkVar[i].primal for i in range(len(self.glpkVar)) ] else: self.obj_value = 0. self.solution = [] else: # Use OpenOpt interface lp = LP(self.obj, A=self.Aineq, Aeq=self.Aeq, b=self.bineq, beq=self.beq, lb=self.lb, ub=self.ub, **kwargs) lp.debug = 1 # lp.iprint=-1 # suppress solver output r = lp.solve(self.solver if self.solver != _OOGLPK else 'glpk') if r.istop <= 0. or r.ff != r.ff: # check halting condition self.obj_value = 0. self.solution = [] self.istop = r.istop else: self.obj_value = r.ff self.solution = r.xf self.istop = r.istop # print self.istop return self.obj_value, self.solution
def __solver__(self, p): n = p.n x0 = copy(p.x0) xPrev = x0.copy() xf = x0.copy() xk = x0.copy() p.xk = x0.copy() f0 = p.f(x0) fk = f0 ff = f0 p.fk = fk df0 = p.df(x0) ##################################################################### ## #handling box-bounded problems ## if p.__isNoMoreThanBoxBounded__(): ## for k in range(int(p.maxIter)): ## ## #end of handling box-bounded problems isBB = p.__isNoMoreThanBoxBounded__() ## isBB = 0 H = diag(ones(p.n)) if not p.userProvided.c: p.c = lambda x: array([]) p.dc = lambda x: array([]).reshape(0, p.n) if not p.userProvided.h: p.h = lambda x: array([]) p.dh = lambda x: array([]).reshape(0, p.n) p.use_subproblem = 'QP' #p.use_subproblem = 'LLSP' for k in range(p.maxIter + 4): if isBB: f0 = p.f(xk) df = p.df(xk) direction = -df f1 = p.f(xk + direction) ind_l = direction <= p.lb - xk direction[ind_l] = (p.lb - xk)[ind_l] ind_u = direction >= p.ub - xk direction[ind_u] = (p.ub - xk)[ind_u] ff = p.f(xk + direction) ## print 'f0', f0, 'f1', f1, 'ff', ff else: mr = p.getMaxResidual(xk) if mr > p.contol: mr_grad = p.getMaxConstrGradient(xk) lb = p.lb - xk #- p.contol/2 ub = p.ub - xk #+ p.contol/2 c, dc, h, dh, df = p.c(xk), p.dc(xk), p.h(xk), p.dh(xk), p.df( xk) A, Aeq = vstack((dc, p.A)), vstack((dh, p.Aeq)) b = concatenate((-c, p.b - p.matmult(p.A, xk))) #+ p.contol/2 beq = concatenate((-h, p.beq - p.matmult(p.Aeq, xk))) if b.size != 0: isFinite = isfinite(b) ind = where(isFinite)[0] A, b = A[ind], b[ind] if beq.size != 0: isFinite = isfinite(beq) ind = where(isFinite)[0] Aeq, beq = Aeq[ind], beq[ind] if p.use_subproblem == 'LP': #linear linprob = LP(df, A=A, Aeq=Aeq, b=b, beq=beq, lb=lb, ub=ub) linprob.iprint = -1 r2 = linprob.solve( 'cvxopt_glpk') # TODO: replace lpSolve by autoselect if r2.istop <= 0: p.istop = -12 p.msg = "failed to solve LP subproblem" return elif p.use_subproblem == 'QP': #quadratic qp = QP(H=H, f=df, A=A, Aeq=Aeq, b=b, beq=beq, lb=lb, ub=ub) qp.iprint = -1 r2 = qp.solve( 'cvxopt_qp') # TODO: replace solver by autoselect #r2 = qp.solve('qld') # TODO: replace solver by autoselect if r2.istop <= 0: for i in range(4): if p.debug: p.warn("iter " + str(k) + ": attempt Num " + str(i) + " to solve QP subproblem has failed") #qp.f += 2*N*sum(qp.A,0) A2 = vstack((A, Aeq, -Aeq)) b2 = concatenate( (b, beq, -beq)) + pow(10, i) * p.contol qp = QP(H=H, f=df, A=A2, b=b2, iprint=-5) qp.lb = lb - pow(10, i) * p.contol qp.ub = ub + pow(10, i) * p.contol # I guess lb and ub don't matter here try: r2 = qp.solve( 'cvxopt_qp' ) # TODO: replace solver by autoselect except: r2.istop = -11 if r2.istop > 0: break if r2.istop <= 0: p.istop = -11 p.msg = "failed to solve QP subproblem" return elif p.use_subproblem == 'LLSP': direction_c = getConstrDirection(p, xk, regularization=1e-7) else: p.err('incorrect or unknown subproblem') if isBB: X0 = xk.copy() N = 0 result, newX = chLineSearch(p, X0, direction, N, isBB) elif p.use_subproblem != 'LLSP': duals = r2.duals N = 1.05 * abs(duals).sum() direction = r2.xf X0 = xk.copy() result, newX = chLineSearch(p, X0, direction, N, isBB) else: # case LLSP direction_f = -df p2 = NSP(LLSsubprobF, [0.8, 0.8], ftol=0, gtol=0, xtol=1e-5, iprint=-1) p2.args.f = (xk, direction_f, direction_c, p, 1e20) r_subprob = p2.solve('ralg') alpha = r_subprob.xf newX = xk + alpha[0] * direction_f + alpha[1] * direction_c # dw = (direction_f * direction_c).sum() # cos_phi = dw/p.norm(direction_f)/p.norm(direction_c) # res_0, res_1 = p.getMaxResidual(xk), p.getMaxResidual(xk+1e-1*direction_c) # print cos_phi, res_0-res_1 # res_0 = p.getMaxResidual(xk) # optimConstrPoint = getDirectionOptimPoint(p, p.getMaxResidual, xk, direction_c) # res_1 = p.getMaxResidual(optimConstrPoint) # # maxConstrLimit = p.contol #xk = getDirectionOptimPoint(p, p.f, optimConstrPoint, -optimConstrPoint+xk+direction_f, maxConstrLimit = maxConstrLimit) #print 'res_0', res_0, 'res_1', res_1, 'res_2', p.getMaxResidual(xk) #xk = getDirectionOptimPoint(p, p.f, xk, direction_f, maxConstrLimit) #newX = xk.copy() result = 0 # x_0 = X0.copy() # N = j = 0 # while p.getMaxResidual(x_0) > Residual0 + 0.1*p.contol: # j += 1 # x_0 = xk + 0.75**j * (X0-xk) # X0 = x_0 # result, newX = 0, X0 # print 'newIterResidual = ', p.getMaxResidual(x_0) if result != 0: p.istop = result p.xf = newX return xk = newX.copy() fk = p.f(xk) p.xk, p.fk = copy(xk), copy(fk) #p._df = p.df(xk) #################### p.iterfcn() if p.istop: p.xf = xk p.ff = fk #p._df = g FIXME: implement me return
f = some_lin_funcs[15] + some_lin_funcs[80] - sum(a) + sum(b) point = {} for i in range(N): point[a[i]] = 1.5 * i**2 point[b[i]] = 1.5 * i**3 # BTW we could use 1-dimensional arrays here, eg point[a[25]] = [2,3,4,5], point[a[27]] = [1,2,5] etc print f(point) # prints 40899980. from openopt import LP # define prob p = LP(f, point) # add some box-bound constraints aLBs = [a[i]>-10 for i in range(N)] bLBs = [b[i]>-10 for i in range(N)] aUBs = [a[i]<15 for i in range(N)] bUBs = [b[i]<15 for i in range(N)] p.constraints = aLBs + bLBs + aUBs + bUBs # add some general linear constraints p.constraints.append(a[4] + b[15] + a[20].size - f.size>-9) # array size, here a[20].size = f.size = 1 # or p.constraints += [a[4] + b[15] + a[20].size - f.size>-9] for i in range(N): p.constraints.append(2 * some_lin_funcs[i] + a[i] < i / 2.0 + some_lin_funcs[N-i-1] + 1.5*b[i]) # or p.constraints += [2 * some_lin_funcs[i] + a[i] < i / 2.0 + some_lin_funcs[N-i-1] + 1.5*b[i] for i in range(N] # solve r = p.solve('cplex') print('opt a[15]=%f'%a[15](r)) # opt a[15]=-10.000000
t = time() # Define some oovars drugs = oovar(2) material = oovar(2) budgets = oovar(3) # Let's define some linear functions f1 = 4*x+5*y + 3*z + 5 f2 = f1.sum() + 2*x + 4*y + 15 f3 = 5*f1 + 4*f2 + 20 # Define objective; sum(a) and a.sum() are same as well as for numpy arrays obj = x.sum() + y - 50*z + sum(f3) + 2*f2.sum() + 4064.6 # Define some constraints via Python list or tuple or set of any length, probably via while/for cycles constraints = [x+5*y<15, x[0]<4, f1<[25, 35], f1>-100, 2*f1+4*z<[80, 800], 5*f2+4*z<100, -5<x, x<1, -20<y, y<20, -4000<z, z<4] # Start point - currently matters only size of variables, glpk, lpSolve and cvxopt_lp use start val = all-zeros startPoint = {x:[8, 15], y:25, z:80} # however, using numpy.arrays is more recommended than Python lists # Create prob p = LP(obj, startPoint, constraints = constraints) # Solve r = p.solve('glpk') # glpk is name of solver involved, see OOF doc for more arguments # Decode solution print('Solution: x = %s y = %f z = %f' % (str(x(r)), y(r), z(r))) # Solution: x = [-4.25 -4.25] y = -20.000000 z = 4.000000 print "elapsed %.3f secs"%(time() - t)
def __solver__(self, p): n = p.n x0 = copy(p.x0) xPrev = x0.copy() xf = x0.copy() xk = x0.copy() p.xk = x0.copy() f0 = p.f(x0) fk = f0 ff = f0 p.fk = fk df0 = p.df(x0) ##################################################################### ## #handling box-bounded problems ## if p.__isNoMoreThanBoxBounded__(): ## for k in range(int(p.maxIter)): ## ## #end of handling box-bounded problems isBB = p.__isNoMoreThanBoxBounded__() ## isBB = 0 H = diag(ones(p.n)) if not p.userProvided.c: p.c = lambda x : array([]) p.dc = lambda x : array([]).reshape(0, p.n) if not p.userProvided.h: p.h = lambda x : array([]) p.dh = lambda x : array([]).reshape(0, p.n) p.use_subproblem = 'QP' #p.use_subproblem = 'LLSP' for k in range(p.maxIter+4): if isBB: f0 = p.f(xk) df = p.df(xk) direction = -df f1 = p.f(xk+direction) ind_l = direction<=p.lb-xk direction[ind_l] = (p.lb-xk)[ind_l] ind_u = direction>=p.ub-xk direction[ind_u] = (p.ub-xk)[ind_u] ff = p.f(xk + direction) ## print 'f0', f0, 'f1', f1, 'ff', ff else: mr = p.getMaxResidual(xk) if mr > p.contol: mr_grad = p.getMaxConstrGradient(xk) lb = p.lb - xk #- p.contol/2 ub = p.ub - xk #+ p.contol/2 c, dc, h, dh, df = p.c(xk), p.dc(xk), p.h(xk), p.dh(xk), p.df(xk) A, Aeq = vstack((dc, p.A)), vstack((dh, p.Aeq)) b = concatenate((-c, p.b-p.matmult(p.A,xk))) #+ p.contol/2 beq = concatenate((-h, p.beq-p.matmult(p.Aeq,xk))) if b.size != 0: isFinite = isfinite(b) ind = where(isFinite)[0] A, b = A[ind], b[ind] if beq.size != 0: isFinite = isfinite(beq) ind = where(isFinite)[0] Aeq, beq = Aeq[ind], beq[ind] if p.use_subproblem == 'LP': #linear linprob = LP(df, A=A, Aeq=Aeq, b=b, beq=beq, lb=lb, ub=ub) linprob.iprint = -1 r2 = linprob.solve('cvxopt_glpk') # TODO: replace lpSolve by autoselect if r2.istop <= 0: p.istop = -12 p.msg = "failed to solve LP subproblem" return elif p.use_subproblem == 'QP': #quadratic qp = QP(H=H,f=df, A=A, Aeq=Aeq, b=b, beq=beq, lb=lb, ub = ub) qp.iprint = -1 r2 = qp.solve('cvxopt_qp') # TODO: replace solver by autoselect #r2 = qp.solve('qld') # TODO: replace solver by autoselect if r2.istop <= 0: for i in range(4): if p.debug: p.warn("iter " + str(k) + ": attempt Num " + str(i) + " to solve QP subproblem has failed") #qp.f += 2*N*sum(qp.A,0) A2 = vstack((A, Aeq, -Aeq)) b2 = concatenate((b, beq, -beq)) + pow(10,i)*p.contol qp = QP(H=H,f=df, A=A2, b=b2, iprint = -5) qp.lb = lb - pow(10,i)*p.contol qp.ub = ub + pow(10,i)*p.contol # I guess lb and ub don't matter here try: r2 = qp.solve('cvxopt_qp') # TODO: replace solver by autoselect except: r2.istop = -11 if r2.istop > 0: break if r2.istop <= 0: p.istop = -11 p.msg = "failed to solve QP subproblem" return elif p.use_subproblem == 'LLSP': direction_c = getConstrDirection(p, xk, regularization = 1e-7) else: p.err('incorrect or unknown subproblem') if isBB: X0 = xk.copy() N = 0 result, newX = chLineSearch(p, X0, direction, N, isBB) elif p.use_subproblem != 'LLSP': duals = r2.duals N = 1.05*abs(duals).sum() direction = r2.xf X0 = xk.copy() result, newX = chLineSearch(p, X0, direction, N, isBB) else: # case LLSP direction_f = -df p2 = NSP(LLSsubprobF, [0.8, 0.8], ftol=0, gtol=0, xtol = 1e-5, iprint = -1) p2.args.f = (xk, direction_f, direction_c, p, 1e20) r_subprob = p2.solve('ralg') alpha = r_subprob.xf newX = xk + alpha[0]*direction_f + alpha[1]*direction_c # dw = (direction_f * direction_c).sum() # cos_phi = dw/p.norm(direction_f)/p.norm(direction_c) # res_0, res_1 = p.getMaxResidual(xk), p.getMaxResidual(xk+1e-1*direction_c) # print cos_phi, res_0-res_1 # res_0 = p.getMaxResidual(xk) # optimConstrPoint = getDirectionOptimPoint(p, p.getMaxResidual, xk, direction_c) # res_1 = p.getMaxResidual(optimConstrPoint) # # maxConstrLimit = p.contol #xk = getDirectionOptimPoint(p, p.f, optimConstrPoint, -optimConstrPoint+xk+direction_f, maxConstrLimit = maxConstrLimit) #print 'res_0', res_0, 'res_1', res_1, 'res_2', p.getMaxResidual(xk) #xk = getDirectionOptimPoint(p, p.f, xk, direction_f, maxConstrLimit) #newX = xk.copy() result = 0 # x_0 = X0.copy() # N = j = 0 # while p.getMaxResidual(x_0) > Residual0 + 0.1*p.contol: # j += 1 # x_0 = xk + 0.75**j * (X0-xk) # X0 = x_0 # result, newX = 0, X0 # print 'newIterResidual = ', p.getMaxResidual(x_0) if result != 0: p.istop = result p.xf = newX return xk = newX.copy() fk = p.f(xk) p.xk, p.fk = copy(xk), copy(fk) #p._df = p.df(xk) #################### p.iterfcn() if p.istop: p.xf = xk p.ff = fk #p._df = g FIXME: implement me return
f3 = 5 * f1 + 4 * f2 + 20 # Define objective; sum(a) and a.sum() are same as well as for numpy arrays obj = sum(f1) + x.sum() + y - 50 * z + 2 * f2.sum() + 4064.6 # Start point - currently matters only size of variables startPoint = { x: [8, 15], y: 25, z: 80 } # however, using numpy.arrays is more recommended than Python lists # Create prob p = LP(obj, startPoint) # Define some constraints p.constraints = [ x + 5 * y < 15, x[0] < 4, f1 < [25, 35], f1 > -100, 2 * f1 + 4 * z < [80, 800], 5 * f2 + 4 * z < 100, -5 < x, x < 1, -20 < y, y < 20, -4000 < z, z < 4 ] # Solve r = p.solve( 'lpSolve', fixedVars=t ) # glpk is name of solver involved, see OOF doc for more arguments # Decode solution print('Solution: x = %s y = %f z = %f' % (r(x), r(y), r(z))) # Solution: x = [-4.25 -4.25] y = -20.000000 z = 4.000000
for i in range(N): point[a[i]] = 1.5 * i**2 point[b[i]] = 1.5 * i**3 # BTW we could use 1-dimensional arrays here, eg point[a[25]] = [2,3,4,5], point[a[27]] = [1,2,5] etc print f(point) # prints 40899980. from openopt import LP # define prob p = LP(f, point) # add some box-bound constraints aLBs = [a[i] > -10 for i in range(N)] bLBs = [b[i] > -10 for i in range(N)] aUBs = [a[i] < 15 for i in range(N)] bUBs = [b[i] < 15 for i in range(N)] p.constraints = aLBs + bLBs + aUBs + bUBs # add some general linear constraints p.constraints.append(a[4] + b[15] + a[20].size - f.size > -9) # array size, here a[20].size = f.size = 1 # or p.constraints += [a[4] + b[15] + a[20].size - f.size>-9] for i in range(N): p.constraints.append(2 * some_lin_funcs[i] + a[i] < i / 2.0 + some_lin_funcs[N - i - 1] + 1.5 * b[i]) # or p.constraints += [2 * some_lin_funcs[i] + a[i] < i / 2.0 + some_lin_funcs[N-i-1] + 1.5*b[i] for i in range(N] # solve r = p.solve('cplex') print('opt a[15]=%f' % a[15](r)) # opt a[15]=-10.000000
from FuncDesigner import * from openopt import LP # Define some oovars x, y, z = oovars(3) # Let's define some linear functions f1 = 4*x+5*y + 3*z + 5 f2 = f1.sum() + 2*x + 4*y + 15 f3 = 5*f1 + 4*f2 + 20 # Define objective; sum(a) and a.sum() are same as well as for numpy arrays obj = sum(f1)+ x.sum()+ y - 50*z + 2*f2.sum() + 4064.6 # Start point - currently matters only size of variables startPoint = {x:[8, 15], y:25, z:80} # however, using numpy.arrays is more recommended than Python lists # Create prob p = LP(obj, startPoint) # Define some constraints p.constraints = [x+5*y<15, x[0]<4, f1<[25, 35], f1>-100, 2*f1+4*z<[80, 800], 5*f2+4*z<100, -5<x, x<1, -20<y, y<20, -4000<z, z<4] # Solve r = p.solve('lpSolve', fixedVars=t) # glpk is name of solver involved, see OOF doc for more arguments # Decode solution print('Solution: x = %s y = %f z = %f' % (r(x), r(y), r(z))) # Solution: x = [-4.25 -4.25] y = -20.000000 z = 4.000000
print len(s.matrix) # for m in bMets: # s.add_reaction(Reaction('R_'+m.identifier+'_Transp', (m,), (), ())) # A_eq = numpy.delete(numpy.array(s.matrix, dtype=float), bIndices, 0) transpColumn = numpy.zeros((len(s.matrix), len(bIndices))) for i, elem in enumerate(bIndices): transpColumn[elem,i] = 1 A_eq = numpy.append(numpy.array(s.matrix, dtype=float), transpColumn, 1) print A_eq print numpy.shape(A_eq) b_eq = numpy.zeros(len(A_eq)) print numpy.shape(A_eq)[1] print len(b_eq) print len(numpy.random.randint(-10, -5, numpy.shape(A_eq)[1])) print len(numpy.random.randint(5, 10, numpy.shape(A_eq)[1])) lp = LP(f=obj, Aeq=A_eq, beq=b_eq, lb=numpy.random.randint(-6, -0, numpy.shape(A_eq)[1]), ub=numpy.random.randint(0, 6, numpy.shape(A_eq)[1])) lp.exportToMPS # print help(lp.manage) # lp.solve('cvxopt_lp') # print help(lp.solve) r = lp.solve('glpk', goal='max') # print dir(r) # print r.ff # print r.evals # print r.isFeasible # print r.msg # print r.xf # print r.duals # print r.rf # print r.solverInfo # problem = openopt.LP(f=objective, Aeq=A_eq, beq=b_eq, lb=lb, ub=ub)
def solve(self): p=LP(self.cost_function,Aeq=self.Aeq,beq=self.beq,lb=self.lb) p.iprint = -1 r=p.solve('pclp') return [r.xf,r.ff]