?? cell.java
字號:
import swarm.objectbase.SwarmObjectImpl;
import swarm.defobj.Zone;
import java.lang.Math;
import swarm.Globals;
public class Cell extends SwarmObjectImpl implements Comparable
{
private int[] gene; //抗體的基因
private int matchValue = 0; //免疫細(xì)胞與抗體的匹配度
private static double abePro = 0.01;
/**
* 函數(shù)功能:指定基因的免疫細(xì)胞的構(gòu)造函數(shù)
*/
public Cell(Zone aZone,int[] geneA)
{
super(aZone);
gene = geneA;
}
/**
* 函數(shù)功能:僅指定基因的免疫細(xì)胞的構(gòu)造函數(shù)
*/
public Cell(Zone aZone,int genLen1)
{
super(aZone);
gene = new int[genLen1];
for (int i = 0; i < genLen1; ++i)
{
if (Math.random() < 0.5)
gene[i] = 0;
else gene[i] = 1;
}
}
/**
* 函數(shù)功能:將當(dāng)前免疫細(xì)胞與抗原的匹配度與o的相比較,
* 若當(dāng)前的匹配度高,則返回1,
* 匹配度相等,返回0
* 匹配度低,返回-1
*/
public int compareTo(Object o)
{
int matchValue1 = ((Cell)o).getMatchValue();
if (matchValue < matchValue1)
return -1;
else if (matchValue == matchValue1)
return 0;
else
return 1;
}
/**
* 函數(shù)功能:當(dāng)前免疫細(xì)胞(含抗體)與抗原發(fā)生免疫反應(yīng),
* 計(jì)算抗體與抗原antigen的匹配度:matchValue
*/
public int immunoreaction(Antigen antigen)
{
int[] geneA = antigen.getGene();
int k = 0,kc = 0;
matchValue = 0;
for (int i = 0; i < gene.length && i < geneA.length; ++i)
{
if (gene[i] != geneA[i])
++k;
else if (k == 0)
continue;
else {
matchValue += (int)(Math.pow(2,k)+0.1);
kc += k;
k = 0;
}
}
if (k != 0) {
matchValue += (int)(Math.pow(2,k)+0.1);
kc += k;
}
matchValue += kc;
return matchValue;
}
/**
* 函數(shù)功能:為了找到與抗原匹配的抗體,
* 免疫細(xì)胞在遺傳中發(fā)生變異
* 變異概率:abePro,需在實(shí)驗(yàn)中慢慢調(diào)整
*/
public void aberrance()
{
for (int i = 0; i < gene.length; ++i)
{
if (Math.random() <0.1)
{
if (gene[i] == 1)
gene[i] = 0;
else gene[i] = 1;
}
}
}
/**
* 函數(shù)功能:返回免疫細(xì)胞的基因信息
*/
public int[] getGene()
{
return gene;
}
/**
* 函數(shù)功能:返回免疫細(xì)胞與抗原的匹配度
*/
public int getMatchValue()
{
return matchValue;
}
public static void main(String[] args)
{
int[] g1 = {0,1,1,0,0,0,0,1,1,1,1,0,1,1,0};
int[] g2 = {1,0,0,1,1,1,0,0,0,1,0,1,1,0,1};
Cell c1 = new Cell(Globals.env.globalZone,g1);
Antigen c2 = new Antigen(Globals.env.globalZone,g2);
System.out.println(c1.immunoreaction(c2));
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -