資源簡介
帶有基因型以及表現(xiàn)型元胞自動機python代碼。其中增加了兩種基因型。
代碼片段和文件信息
#?-*-?coding:utf-8?-*-
import?numpy?as?np
import?matplotlib.pyplot?as?plt
import?matplotlib.animation?as?animation
import?time
import?copy
‘‘‘
該代碼研究的是在賦予每一個元胞可以與其它元胞雜交的基因時產(chǎn)生的現(xiàn)象;
在原有的元胞類里添加基因類里面有Aa?Bb兩種,A為疾病基因,B為強化基因,均顯性有效。
在每一個回合內(nèi):
AA代表正常的cell也有10%死亡的可能性,BB代表根據(jù)規(guī)則應(yīng)該死亡的cell也有10%存活可能性
CC無影響
迭代規(guī)則:
在一個回合中,cell可以隨機與周圍一個交換基因產(chǎn)生子代原有的cell死亡。
也可以選擇與周圍存活率最高的哪一個cell交換基因。
用不同的顏色表示這個cell的基因組成(存活率)
A-B-:不變??150?
A-bb;最暗?100?
aaB-:亮??255
子代產(chǎn)生規(guī)則:
在一個回合中如果空的元胞的周圍環(huán)形邊界內(nèi)如果有兩個活元胞,子代會產(chǎn)生,
其基因型是這兩個元胞按照概率自由組合產(chǎn)生的。
當(dāng)邊界有超過3個元胞,或小于兩個元胞,元胞將會死亡。在此基礎(chǔ)上,A、B兩個
基因會產(chǎn)生額外的生存與死亡幾率
‘‘‘
#根據(jù)上述規(guī)則的基因解碼函數(shù)
def?decode(gene):
????cells?=?np.zeros(len(gene))?????????????????????
????for?i?in?range(len(gene)):
????????if?gene[i]?==‘0‘:
????????????cells[i]?=?0
????????elif?gene[i].find(‘A‘)!=-1?and?gene[i].find(‘B‘)==-1?:
????????????cells[i]=100
????????elif?gene[i].find(‘A‘)==-1?and?gene[i].find(‘B‘)?!=-1:
????????????cells[i]?=150
????????else:
????????????cells[i]?=?255
????return?cells
#以下是地圖生成函數(shù):生成隨機地圖參數(shù)是地圖大小,pA是A出現(xiàn)概率,pB同理p1是地圖中活著的格子出現(xiàn)的概率
def?generaterandom(lengthpApBp1):
????grid?=?np.random.choice([0‘A‘‘a(chǎn)‘]length*length?p=[1-p1p1*pAp1*(1-pA)])
????for?i?in?range(length*length):
????????if?grid[i]!=‘0‘:
????????????grid[i]?=?(grid[i]+??np.random.choice([‘A‘‘a(chǎn)‘]p=[pA1-pA])+
????????????np.random.choice([‘B‘‘b‘]p=[pB1-pB])?+?np.random.choice([‘B‘‘b‘]p=[pB1-pB]))
????return?grid.reshape(lengthlength)
#基因自由組合函數(shù)
def?combine(AB):
????newgene=np.random.choice([A[0]A[1]]p=[0.50.5])+np.random.choice([B[0]B[1]]p=[0.50.5])+\
????np.random.choice([A[2]A[3]]p=[0.50.5])+np.random.choice([B[2]B[3]]p=[0.50.5])
????return?newgene
#地圖類:
class?group:
????def?__init__(selflengthpApBp):
?????????self.grid?=?generaterandom(lengthpApBp)????#grid代表基因型矩陣
?????????self.cells?=?decode(self.grid.reshape(length*length)).reshape(lengthlength)??#cells代表對應(yīng)的顏色矩陣
?????????self.shape?=?self.grid.shape
?????????self._length?=?length
?????????
????def?show(self):
?????????plt.imshow(self.cells)
?????????plt.show()
?????????
????def?gene(self):?#這個函數(shù)是為了統(tǒng)計基因所占比例
????????m=‘‘
????????for?i?in?range(self._length):
????????????for?j?in?range(self._length):
????????????????m=?m+self.grid[i][j]
????????length_A=?len(m)-m.count(‘0‘)-m.count(‘B‘)-m.count(‘b‘)
????????length_B=?len(m)-m.count(‘0‘)-m.count(‘A‘)-m.count(‘a(chǎn)‘)
????????return?m.count(‘A‘)/length_Am.count(‘a(chǎn)‘)/length_Am.count(‘B‘)/length_Bm.count(‘b‘)/length_B
????
????def?number(self):#這個函數(shù)是為了統(tǒng)計各個表現(xiàn)型的比例與活著的元胞的比例
????????all_?=?self.cells.nonzero()[0].size
????????m=copy.copy(self.cells)
????????m[m!=100]=0
????????number_1?=?m.nonzero()[0].size
????????m=copy.copy(self.cells)
????????m[m!=150]=0
????????number_2?=?m.nonzero()[0].size
????????m=copy.copy(self.cells)
????????m[m!=255]=0
????????number_3?=?m.nonzero()[0].size
????
????????return?all_/(self._length*self._length)number_1/all_number_2/all_number_3/all_
????def?set_grid(sel
評論
共有 條評論