def mutate_and_evaluate(listantStr, listdist, listrl): global countf global counts eval_dist = np.empty(mut_size) eval_model = np.empty(mut_size, dtype='object') eval_rl = np.empty(mut_size, dtype='object') for m in mut_range: r = te.loada(listantStr[m]) r.steadyStateApproximate() concCC = r.getScaledConcentrationControlCoefficientMatrix() cFalse = (1 + np.sum(np.not_equal(np.sign(np.array(realConcCC)), np.sign(np.array(concCC))), axis=0)) tempdiff = cFalse*np.linalg.norm(realConcCC - concCC, axis=0) stt = [[],[],[]] reactionList = rl_track[0] o = 0 while ((stt[1] != realFloatingIdsInd or stt[2] != realBoundaryIdsInd or reactionList in rl_track) and (o < maxIter_mut)): r_idx = np.random.choice(np.arange(nr), p=np.divide(tempdiff,np.sum(tempdiff))) posRctInd = np.append(np.array(realFloatingIdsInd)[np.where( np.abs(realConcCC[:,r_idx]) > 1e-12)[0]], np.array(realBoundaryIdsInd)).astype(int) reactionList = copy.deepcopy(listrl[m]) rct = [col[3] for col in reactionList] prd = [col[4] for col in reactionList] rType, regType, revType = ng.pickReactionType() # TODO: pick reactants and products based on control coefficients if rType == ng.ReactionType.UNIUNI: # UniUni rct_id = np.random.choice(posRctInd, size=1).tolist() prd_id = np.random.choice(np.delete(nsList, rct_id), size=1).tolist() all_rct = [i for i,x in enumerate(rct) if x==rct_id] all_prd = [i for i,x in enumerate(prd) if x==prd_id] while (((np.any(np.isin(rct_id, realBoundaryIdsInd))) and (np.any(np.isin(prd_id, realBoundaryIdsInd)))) or (len(set(all_rct) & set(all_prd)) > 0)): rct_id = np.random.choice(posRctInd, size=1).tolist() prd_id = np.random.choice(np.delete(nsList, rct_id), size=1).tolist() # Search for potentially identical reactions all_rct = [i for i,x in enumerate(rct) if x==rct_id] all_prd = [i for i,x in enumerate(prd) if x==prd_id] elif rType == ng.ReactionType.BIUNI: # BiUni rct_id = np.random.choice(posRctInd, size=2, replace=True).tolist() prd_id = np.random.choice(np.delete(nsList, rct_id), size=1).tolist() all_rct = [i for i,x in enumerate(rct) if set(x)==set(rct_id)] all_prd = [i for i,x in enumerate(prd) if x==prd_id] while (((np.any(np.isin(rct_id, realBoundaryIdsInd))) and (np.any(np.isin(prd_id, realBoundaryIdsInd)))) or (len(set(all_rct) & set(all_prd)) > 0)): rct_id = np.random.choice(posRctInd, size=2, replace=True).tolist() prd_id = np.random.choice(np.delete(nsList, rct_id), size=1).tolist() # Search for potentially identical reactions all_rct = [i for i,x in enumerate(rct) if set(x)==set(rct_id)] all_prd = [i for i,x in enumerate(prd) if x==prd_id] elif rType == ng.ReactionType.UNIBI: # UniBi rct_id = np.random.choice(posRctInd, size=1).tolist() prd_id = np.random.choice(np.delete(nsList, rct_id), size=2, replace=True).tolist() all_rct = [i for i,x in enumerate(rct) if x==rct_id] all_prd = [i for i,x in enumerate(prd) if set(x)==set(prd_id)] while (((np.any(np.isin(rct_id, realBoundaryIdsInd))) and (np.any(np.isin(prd_id, realBoundaryIdsInd)))) or (len(set(all_rct) & set(all_prd)) > 0)): rct_id = np.random.choice(posRctInd, size=1).tolist() prd_id = np.random.choice(np.delete(nsList, rct_id), size=2, replace=True).tolist() # Search for potentially identical reactions all_rct = [i for i,x in enumerate(rct) if x==rct_id] all_prd = [i for i,x in enumerate(prd) if set(x)==set(prd_id)] else: # BiBi rct_id = np.random.choice(posRctInd, size=2, replace=True).tolist() prd_id = np.random.choice(np.delete(nsList, rct_id), size=2, replace=True).tolist() all_rct = [i for i,x in enumerate(rct) if set(x)==set(rct_id)] all_prd = [i for i,x in enumerate(prd) if set(x)==set(prd_id)] while (((np.any(np.isin(rct_id, realBoundaryIdsInd))) and (np.any(np.isin(prd_id, realBoundaryIdsInd)))) or (len(set(all_rct) & set(all_prd)) > 0)): rct_id = np.random.choice(posRctInd, size=2, replace=True).tolist() prd_id = np.random.choice(np.delete(nsList, rct_id), size=2, replace=True).tolist() # Search for potentially identical reactions all_rct = [i for i,x in enumerate(rct) if set(x)==set(rct_id)] all_prd = [i for i,x in enumerate(prd) if set(x)==set(prd_id)] if regType == ng.RegulationType.DEFAULT: act_id = [] inhib_id = [] elif regType == ng.RegulationType.INHIBITION: act_id = [] delList = np.concatenate([rct_id, prd_id]) if len(realBoundaryIdsInd) > 0: delList = np.unique(np.append(delList, realBoundaryIdsInd)) cList = np.delete(nsList, delList) if len(cList) == 0: inhib_id = [] regType = ng.RegulationType.DEFAULT else: inhib_id = np.random.choice(cList, size=1).tolist() elif regType == ng.RegulationType.ACTIVATION: inhib_id = [] delList = np.concatenate([rct_id, prd_id]) if len(realBoundaryIdsInd) > 0: delList = np.unique(np.append(delList, realBoundaryIdsInd)) cList = np.delete(nsList, delList) if len(cList) == 0: act_id = [] regType = ng.RegulationType.DEFAULT else: act_id = np.random.choice(cList, size=1).tolist() else: delList = np.concatenate([rct_id, prd_id]) if len(realBoundaryIdsInd) > 0: delList = np.unique(np.append(delList, realBoundaryIdsInd)) cList = np.delete(nsList, delList) if len(cList) < 2: act_id = [] inhib_id = [] regType = ng.RegulationType.DEFAULT else: reg_id = np.random.choice(cList, size=2, replace=False) act_id = [reg_id[0]] inhib_id = [reg_id[1]] reactionList[r_idx] = [rType, regType, revType, rct_id, prd_id, act_id, inhib_id] st = ng.getFullStoichiometryMatrix(reactionList, ns).tolist() stt = ng.removeBoundaryNodes(np.array(st)) o += 1 if o >= maxIter_mut: eval_dist[m] = listdist[m] eval_model[m] = listantStr[m] eval_rl[m] = listrl[m] else: antStr = ng.generateAntimony(realFloatingIds, realBoundaryIds, stt[1], stt[2], reactionList, boundary_init=realBoundaryVal) try: r = te.loada(antStr) counts = 0 countf = 0 r.steadyStateApproximate() p_bound = ng.generateParameterBoundary(r.getGlobalParameterIds()) res = scipy.optimize.differential_evolution(f1, args=(r,), bounds=p_bound, maxiter=optiMaxIter, tol=optiTol, polish=optiPolish, seed=r_seed) if not res.success: eval_dist[m] = listdist[m] eval_model[m] = listantStr[m] eval_rl[m] = listrl[m] else: r = te.loada(antStr) r.setValues(r.getGlobalParameterIds(), res.x) r.steadyStateApproximate() SS_i = r.getFloatingSpeciesConcentrations() r.steadyStateApproximate() if np.any(SS_i < 1e-5) or np.any(SS_i > 1e5): eval_dist[m] = listdist[m] eval_model[m] = listantStr[m] eval_rl[m] = listrl[m] else: concCC_i = r.getScaledConcentrationControlCoefficientMatrix() if np.isnan(concCC_i).any(): eval_dist[m] = listdist[m] eval_model[m] = listantStr[m] eval_rl[m] = listrl[m] else: concCC_i[np.abs(concCC_i) < 1e-12] = 0 # Set small values to zero concCC_i_row = concCC_i.rownames concCC_i_col = concCC_i.colnames concCC_i = concCC_i[np.argsort(concCC_i_row)] concCC_i = concCC_i[:,np.argsort(concCC_i_col)] dist_i = ((np.linalg.norm(realConcCC - concCC_i))*(1 + np.sum(np.not_equal(np.sign(np.array(realConcCC)), np.sign(np.array(concCC_i)))))) if dist_i < listdist[m]: eval_dist[m] = dist_i r.reset() eval_model[m] = r.getAntimony(current=True) eval_rl[m] = reactionList rl_track.append(reactionList) else: eval_dist[m] = listdist[m] eval_model[m] = listantStr[m] eval_rl[m] = listrl[m] except: eval_dist[m] = listdist[m] eval_model[m] = listantStr[m] eval_rl[m] = listrl[m] antimony.clearPreviousLoads() return eval_dist, eval_model, eval_rl
def initialize(): global countf global counts numBadModels = 0 numGoodModels = 0 numIter = 0 ens_dist = np.empty(ens_size) ens_model = np.empty(ens_size, dtype='object') ens_rl = np.empty(ens_size, dtype='object') rl_track = [] # Initial Random generation while (numGoodModels < ens_size): rl = ng.generateReactionList(ns, nr, realBoundaryIdsInd) st = ng.getFullStoichiometryMatrix(rl, ns).tolist() stt = ng.removeBoundaryNodes(np.array(st)) # Ensure no redundant model while (stt[1] != realFloatingIdsInd or stt[2] != realBoundaryIdsInd or rl in rl_track): rl = ng.generateReactionList(ns, nr, realBoundaryIdsInd) st = ng.getFullStoichiometryMatrix(rl, ns).tolist() stt = ng.removeBoundaryNodes(np.array(st)) antStr = ng.generateAntimony(realFloatingIds, realBoundaryIds, stt[1], stt[2], rl, boundary_init=realBoundaryVal) try: r = te.loada(antStr) counts = 0 countf = 0 r.steadyStateApproximate() p_bound = ng.generateParameterBoundary(r.getGlobalParameterIds()) res = scipy.optimize.differential_evolution(f1, args=(r,), bounds=p_bound, maxiter=optiMaxIter, tol=optiTol, polish=optiPolish, seed=r_seed) if not res.success: numBadModels += 1 else: # TODO: Might be able to cut the bottom part by simply using # the obj func value from optimizer r = te.loada(antStr) r.setValues(r.getGlobalParameterIds(), res.x) r.steadyStateApproximate() SS_i = r.getFloatingSpeciesConcentrations() r.steadyStateApproximate() if np.any(SS_i < 1e-5) or np.any(SS_i > 1e5): numBadModels += 1 else: concCC_i = r.getScaledConcentrationControlCoefficientMatrix() if np.isnan(concCC_i).any(): numBadModels += 1 else: concCC_i[np.abs(concCC_i) < 1e-12] = 0 # Set small values to zero concCC_i_row = concCC_i.rownames concCC_i_col = concCC_i.colnames concCC_i = concCC_i[np.argsort(concCC_i_row)] concCC_i = concCC_i[:,np.argsort(concCC_i_col)] dist_i = ((np.linalg.norm(realConcCC - concCC_i))*(1 + np.sum(np.not_equal(np.sign(np.array(realConcCC)), np.sign(np.array(concCC_i)))))) ens_dist[numGoodModels] = dist_i r.reset() ens_model[numGoodModels] = r.getAntimony(current=True) ens_rl[numGoodModels] = rl rl_track.append(rl) numGoodModels = numGoodModels + 1 except: numBadModels = numBadModels + 1 antimony.clearPreviousLoads() numIter = numIter + 1 if int(numIter/1000) == (numIter/1000): print("Number of iterations = " + str(numIter)) if int(numIter/10000) == (numIter/10000): print("Number of good models = " + str(numGoodModels)) print("In generation: 1") print("Number of total iterations = " + str(numIter)) print("Number of bad models = " + str(numBadModels)) return ens_dist, ens_model, ens_rl, rl_track
def random_gen(listAntStr, listDist, listrl): global countf global counts rndSize = len(listDist) rnd_dist = np.empty(rndSize) rnd_model = np.empty(rndSize, dtype='object') rnd_rl = np.empty(rndSize, dtype='object') for l in range(rndSize): d = 0 rl = ng.generateReactionList(ns, nr, realBoundaryIdsInd) st = ng.getFullStoichiometryMatrix(rl, ns).tolist() stt = ng.removeBoundaryNodes(np.array(st)) # Ensure no redundant models while ((stt[1] != realFloatingIdsInd or stt[2] != realBoundaryIdsInd or rl in rl_track) and (d < maxIter_gen)): rl = ng.generateReactionList(ns, nr, realBoundaryIdsInd) st = ng.getFullStoichiometryMatrix(rl, ns).tolist() stt = ng.removeBoundaryNodes(np.array(st)) d += 1 if d >= maxIter_gen: rnd_dist[l] = listDist[l] rnd_model[l] = listAntStr[l] rnd_rl[l] = listrl[l] else: antStr = ng.generateAntimony(realFloatingIds, realBoundaryIds, stt[1], stt[2], rl, boundary_init=realBoundaryVal) try: r = te.loada(antStr) counts = 0 countf = 0 r.steadyStateApproximate() p_bound = ng.generateParameterBoundary(r.getGlobalParameterIds()) res = scipy.optimize.differential_evolution(f1, args=(r,), bounds=p_bound, maxiter=optiMaxIter, tol=optiTol, polish=optiPolish, seed=r_seed) # Failed to find solution if not res.success: rnd_dist[l] = listDist[l] rnd_model[l] = listAntStr[l] rnd_rl[l] = listrl[l] else: r = te.loada(antStr) r.setValues(r.getGlobalParameterIds(), res.x) r.steadyStateApproximate() SS_i = r.getFloatingSpeciesConcentrations() r.steadyStateApproximate() if np.any(SS_i < 1e-5) or np.any(SS_i > 1e5): rnd_dist[l] = listDist[l] rnd_model[l] = listAntStr[l] rnd_rl[l] = listrl[l] else: concCC_i = r.getScaledConcentrationControlCoefficientMatrix() if np.isnan(concCC_i).any(): rnd_dist[l] = listDist[l] rnd_model[l] = listAntStr[l] rnd_rl[l] = listrl[l] else: concCC_i[np.abs(concCC_i) < 1e-12] = 0 # Set small values to zero concCC_i_row = concCC_i.rownames concCC_i_col = concCC_i.colnames concCC_i = concCC_i[np.argsort(concCC_i_row)] concCC_i = concCC_i[:,np.argsort(concCC_i_col)] dist_i = ((np.linalg.norm(realConcCC - concCC_i))*(1 + np.sum(np.not_equal(np.sign(np.array(realConcCC)), np.sign(np.array(concCC_i)))))) if dist_i < listDist[l]: rnd_dist[l] = dist_i r.reset() rnd_model[l] = r.getAntimony(current=True) rnd_rl[l] = rl rl_track.append(rl) else: rnd_dist[l] = listDist[l] rnd_model[l] = listAntStr[l] rnd_rl[l] = listrl[l] except: rnd_dist[l] = listDist[l] rnd_model[l] = listAntStr[l] rnd_rl[l] = listrl[l] antimony.clearPreviousLoads() return rnd_dist, rnd_model, rnd_rl
def mutate_and_evaluate(Parameters, listantStr, listdist, listrl, rl_track): global countf global counts eval_dist = np.empty(Parameters.mut_size) eval_model = np.empty(Parameters.mut_size, dtype='object') eval_rl = np.empty(Parameters.mut_size, dtype='object') for m in Parameters.mut_range: o = 0 rl = ng.generateMutation(Parameters, listrl[m], listantStr[m]) st = ng.getFullStoichiometryMatrix(rl, Parameters.ns).tolist() stt = ng.removeBoundaryNodes(np.array(st)) while ((rl in rl_track) and (o < Parameters.maxIter_mut)): rl = ng.generateMutation(Parameters, listrl[m], listantStr[m]) st = ng.getFullStoichiometryMatrix(rl, Parameters.ns).tolist() stt = ng.removeBoundaryNodes(np.array(st)) o += 1 if o >= Parameters.maxIter_mut: eval_dist[m] = listdist[m] eval_model[m] = listantStr[m] eval_rl[m] = listrl[m] else: antStr = ng.generateAntimony( Parameters.realFloatingIds, Parameters.realBoundaryIds, stt[1], stt[2], rl, boundary_init=Parameters.realBoundaryVal) try: r = te.loada(antStr) r.steadyStateApproximate() p_bound = ng.generateParameterBoundary( r.getGlobalParameterIds()) res = scipy.optimize.differential_evolution( f1, args=(r, Parameters.realConcCC, Parameters.realFlux, Parameters.FLUX), bounds=p_bound, maxiter=Parameters.optiMaxIter, tol=Parameters.optiTol, polish=Parameters.optiPolish, seed=Parameters.r_seed) if not res.success: eval_dist[m] = listdist[m] eval_model[m] = listantStr[m] eval_rl[m] = listrl[m] else: r = te.loada(antStr) r.setValues(r.getGlobalParameterIds(), res.x) r.steadyStateApproximate() SS_i = r.getFloatingSpeciesConcentrations() r.steadyStateApproximate() if np.any(SS_i < 1e-5) or np.any(SS_i > 1e5): eval_dist[m] = listdist[m] eval_model[m] = listantStr[m] eval_rl[m] = listrl[m] else: concCC_i = r.getScaledConcentrationControlCoefficientMatrix( ) if Parameters.FLUX: flux_i = r.getReactionRates() if np.isnan(concCC_i).any(): eval_dist[m] = listdist[m] eval_model[m] = listantStr[m] eval_rl[m] = listrl[m] else: concCC_i[np.abs(concCC_i) < 1e-12] = 0 # Set small values to zero if Parameters.FLUX: flux_i[np.abs(flux_i) < 1e-12] = 0 # Set small values to zero concCC_i_row = concCC_i.rownames concCC_i_col = concCC_i.colnames concCC_i = concCC_i[np.argsort(concCC_i_row)] concCC_i = concCC_i[:, np.argsort(concCC_i_col)] if Parameters.FLUX: flux_i = flux_i[np.argsort(concCC_i_col)] dist_i = ( ((np.linalg.norm(Parameters.realConcCC - concCC_i)) + (np.linalg.norm(Parameters.realFlux - flux_i))) * ((1 + np.sum( np.not_equal( np.sign( np.array(Parameters.realConcCC) ), np.sign(np.array(concCC_i))))) + (1 + np.sum( np.not_equal( np.sign( np.array(Parameters.realFlux) ), np.sign(np.array(flux_i))))))) else: dist_i = ( (np.linalg.norm(Parameters.realConcCC - concCC_i)) * (1 + np.sum( np.not_equal( np.sign( np.array(Parameters.realConcCC) ), np.sign(np.array(concCC_i)))))) if dist_i < listdist[m]: eval_dist[m] = dist_i r.reset() eval_model[m] = r.getAntimony(current=True) eval_rl[m] = rl rl_track.append(rl) else: eval_dist[m] = listdist[m] eval_model[m] = listantStr[m] eval_rl[m] = listrl[m] except: eval_dist[m] = listdist[m] eval_model[m] = listantStr[m] eval_rl[m] = listrl[m] antimony.clearPreviousLoads() return eval_dist, eval_model, eval_rl, rl_track