📄 immunitysystem.java
字号:
import swarm.objectbase.SwarmImpl;
import swarm.defobj.Zone;
import swarm.collections.ListImpl;
import swarm.activity.ScheduleImpl;
import swarm.Globals;
import swarm.Selector;
import swarm.activity.ActionGroupImpl;
import swarm.activity.Activity;
import swarm.objectbase.Swarm;
public class ImmunitySystem extends SwarmImpl
{
private int now = 0;
private int endTime = 50; //免疫反应进行50代
private int cellNum = 50; //免疫细胞个数
private int genLen1 = 15; //免疫细胞的基因长度
private double reaction = 1; //每代参加反应的免疫细胞的比例
private double multiplication = 0.1; //参加反应的增值的比例
private int door = 1024; //匹配程度高的一个阈值
private double filtration = 0.05; //每代免疫细胞的淘汰率
private double aberrance = 0.5; //优秀免疫细胞的变异比例
private Antigen antigen1,antigen2,antigen3; //定义一个抗原
private MyListImpl cellList; //包含免疫细胞群
// private int[] geneIC = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //要从文件读入
private int[] geneIA1 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //要从标准输入读入
private int[] geneIA2 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
private int[] geneIA3 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
private ScheduleImpl immuSchedule;
public ImmunitySystem(Zone aZone)
{
super(aZone);
}
/**
* 函数功能:随机建立免疫细胞网络,并载入抗原
*/
public Object buildObjects()
{
super.buildObjects();
Cell cell; //免疫细胞
cellList = new MyListImpl(Globals.env.globalZone);
for (int i = 0; i < cellNum; ++i)
{
cellList.addLast(new Cell(Globals.env.globalZone,genLen1));
}
antigen1 = new Antigen(Globals.env.globalZone,geneIA1);
antigen2 = new Antigen(Globals.env.globalZone,geneIA2);
antigen3 = new Antigen(Globals.env.globalZone,geneIA3);
return this;
}
/**
* 函数功能:定义免疫反应全过程,分:
* 训练阶段:抗原初次入侵
* 测试阶段:抗原和类抗原再次入侵
*/
public Object buildActions()
{
Selector sel;
ActionGroupImpl immuActions;
super.buildActions();
immuActions = new ActionGroupImpl(getZone());
/**
* 训练阶段:抗原初次入侵,免疫系统与之斗争50代之后,
* 或在平均水平上匹配了抗原 ,则结束这一阶段
*/
try
{
sel = new Selector(this.getClass(),
"train",false);
immuActions.createActionTo$message(this,sel);
} catch (Exception e)
{
System.err.println("Exception train : "+
e.getMessage());
System.exit(1);
}
/**
* 测试阶段:将训练后的免疫网络,与再次入侵的抗原或类抗原
* 进行免疫反应,得出:
* 最大匹配度
* 最小匹配度
* 平均匹配度
*/
try
{
sel = new Selector(this.getClass(),
"test",false);
immuActions.createActionTo$message(this,sel);
} catch (Exception e)
{
System.err.println("Exception test : "+
e.getMessage());
System.exit(1);
}
/**
* 初始化动作序列
*/
immuSchedule = new ScheduleImpl(getZone(),1);
immuSchedule.at$createAction(0,immuActions);
return this;
}
/**
* 指定模型将要执行的动作,包含在immuSchedule中
*/
public Activity activateIn(Swarm swarmContext)
{
super.activateIn(swarmContext);
immuSchedule.activateIn(this);
return getActivity();
}
/**
* 函数功能:定义免疫系统免疫阶段(与抗原初次斗争)的动作:
* 免疫反应: 求出每种抗体的匹配度
* 排序: 按匹配度排序(从高匹配度——〉低匹配度)
* 淘汰:
*/
public void train()
{
Antigen antigen;
MyListImpl my;
++now;
if (now == 1 | now == 50)
cellList.print();
antigen = choose(); //随机选择一个免疫细胞进行免疫反应
my = cellList.immunoreaction(antigen,reaction); //将部分的免疫细胞与抗原进行反应
my = my.sort(); //按匹配度排序——从高到低
my.filtration(filtration); //淘汰低匹配度的免疫细胞
my = my.multiplacation(multiplication,aberrance,antigen); //匹配度高的细胞增殖、变异
cellList.conformity(my); //将my中免疫细胞并入cellList中
// System.out.println("i = "+i+"\tnum : "+cellList.getCount());
System.out.println("num of cell : "+cellList.getCount());
}
public void test()
{
if (now == endTime) Globals.env.getCurrentSwarmActivity().terminate();
}
/**
* 函数功能:随机选择抗原参加免疫反应
*/
private Antigen choose()
{
double rate = Math.random();
if (rate < 0.33)
return antigen1;
else if (rate < 0.66)
return antigen2;
else
return antigen3;
}
/**
* 函数功能:返回免疫细胞群
*/
public ListImpl getCellList() {
return cellList;
}
/**
* 函数功能:得到系统的截止时间
*/
public int getEndTime() {
return endTime;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -