📄 mylistimpl.java
字号:
import swarm.collections.ListImpl;
import swarm.defobj.Zone;
import swarm.Globals;
public class MyListImpl extends ListImpl
{
public MyListImpl(Zone aZone)
{
super(aZone);
}
/**
* 函数功能:对免疫细胞群按受激发水平排序——插入排序
*/
public MyListImpl sort()
{
System.out.println("MyListImpl : sort : before for"); //调试用
MyListImpl my = new MyListImpl(Globals.env.globalZone);
int num = this.getCount();
Cell cell;
for (int i = 0; i < num; ++i)
{
cell = (Cell)this.removeMax();
my.addLast(cell);
}
return my;
}
/**
* 函数功能:将占reaction比例的免疫细胞与抗原进行反应,
* 求出匹配度,保存在免疫细胞对象中
* 返回:参加免疫反应的免疫细胞集合
*/
public MyListImpl immunoreaction(Antigen antigen,double reaction)
{
int num = this.getCount();
Cell cell;
MyListImpl my = new MyListImpl(Globals.env.globalZone);
for (int i = 0; i < num; ++i)
{
cell = (Cell)this.removeFirst();
if (Math.random() < reaction)
{
cell.immunoreaction(antigen);
my.addLast(cell);
}
else
{
this.addLast(cell);
}
}
return my;
}
/**
* 函数功能:按filtration的比例淘汰与抗原匹配度低的免疫细胞
* 并返回淘汰的细胞个数
*/
public int filtration(double filtration)
{
int numfilter = (int)(this.getCount() * filtration);
for (int j = 0; j < numfilter; ++j)
{
this.removeLast();
}
return numfilter;
}
/**
* 函数功能:将匹配度高的占总数multiplication比例的免疫细胞增殖
* 一个变两个,一半直接进入下一代,一半有选择的变异进入下一代
*/
public MyListImpl multiplacation(double multiplication,double aberrance,Antigen antigen)
{
int num = (int)(this.getCount()*multiplication);
Cell c1,c2;
MyListImpl my = new MyListImpl(Globals.env.globalZone);
for (int i = 0;i < num; ++i)
{
c1 = (Cell)this.removeFirst();
c2 = new Cell(Globals.env.globalZone,c1.getGene());
my.addLast(c1);
/**
* 以aberrance的概率进行变异
*/
if (Math.random() < aberrance)
{
c2.aberrance();
c2.immunoreaction(antigen);
if (c2.compareTo(c1) == 1)
my.addLast(c2);
}
/**
* 其他的直接进入下一代
*/
else
{
c2.immunoreaction(antigen);
my.addLast(c2);
}
}
num = this.getCount();
for (int i = 0; i < num; ++i)
{
c1 = (Cell)this.removeFirst();
my.addLast(c1);
}
return my;
}
/**
* 函数功能:模拟免疫细胞群的变异过程
*/
public void aberrance()
{
Cell cell;
int num = this.getCount();
for (int i = 0; i < num; ++i)
{
cell = (Cell)this.removeFirst();
cell.aberrance();
this.addLast(cell);
}
}
/**
* 函数功能:将my中的免疫细胞并入当前容器中
*/
public void conformity(MyListImpl my)
{
int num = my.getCount();
for (int i = 0; i < num; ++i)
this.addLast(my.removeFirst());
}
/**
* 函数功能:打印免疫细胞群的最大匹配度、最小匹配度、平均匹配度
*/
public void print()
{
Cell cell;
int num = this.getCount();
int now;
int max1 = 0,min1 = 1000000000,average = 0;
for (int i = 0; i < num; ++i)
{
cell = (Cell)this.removeFirst();
now = cell.getMatchValue();
System.out.print(now+"\t");
if (now > max1)
max1 = now;
else if (now < min1)
min1 = now;
average += now;
this.addLast(cell);
}
average /= num;
System.out.println("max = "+max1);
System.out.println("min = "+min1);
System.out.println("average = "+average);
}
/**
* 函数功能:删除匹配度最大的免疫细胞
*/
public Object removeMax()
{
Cell cellmax = (Cell)this.removeFirst();
Cell cell;
int num = this.getCount();
for (int i = 0; i < num; ++i)
{
cell = (Cell)this.removeFirst();
if (cell.compareTo(cellmax) == 1)
{
this.addLast(cellmax);
cellmax = cell;
}
else
this.addLast(cell);
}
return cellmax;
}
public static void main(String[] args)
{
int[] gc = {1,1,1,1,1,1,1,1,1,1,1};
int[] ga = {0,0,0,0,0,0,0,0,0,0,0};
Globals.env.initSwarm("Cell","2.1",
"bug-swarm@santafe.edu",args);
MyListImpl my = new MyListImpl(Globals.env.globalZone);
for (int i = 0; i < 8; ++i)
{
my.addFirst(new Cell(Globals.env.globalZone,gc));
}
Antigen antigen = new Antigen(Globals.env.globalZone,ga);
my.immunoreaction(antigen,0.5);
my.print();
// my.immunoreaction(antigen);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -