我这里给出了一个简单的模板如果需要编代码填代码的地方已经有提示了
/package otherFile;
import javautilRandom;
import tGraphTdcppGraph;
import shuffP;
/
/
@author vaqeteart
这里是遗传算法的核心框架遗传算法的步骤:
遗传算法核心部分的算法描述
算法步骤:
1、初始化
11、生成初始种群编码
12、计算每个个体的适配值。
13、记录当前最优适配值和最优个体
2、选择和遗传,
20、若当前最优适配值多次小于已有的最优适配值(或相差不大)很多次,或者进化的次数超过设定的限制,转4。
21、按照与每个个体的适配值成正比的概率选择个体并复制,复制之后个体的数目和原始种群数目一样。
22、(最好先打乱复制后种群的个体次序)对复制后个体进行两两配对交叉,生成相同数目的的下一代种群。
23、对下一代种群按照一定的概率进行变异
24、计算每个个体的适配值。
25、记录当前最优适配值和最优个体
26、转2
3、返回当前最优适配值以及其对应的编码,结束。
注意:
1这里的内容相当于一个模板,编写具体的遗传算法的时候,可以按照这个模板的形式编写。
2应该填写代码的地方都有提示的标记。
/
public class GAKernel
{
//number of population
int popNum;//set the number to 20 in constructor
//current evolution times
int evolutionTim;
//limit of the evolution times
int evolutionLim;//set the number to 20 in constructor
//unaccepted times
//int eliminTim;
//limit of unaccepted times
//int eliminLim;
//current best euler code
//int curBestCode[];
//current best fitness
int curBestFitness;
//fitness of every individual
int iFitness[];
//fator of compute the fitness
int factor;
//other members
//the graph
//public TdcppGraph tpGraph;
//the eula code group
//int codes[][];
//every population
//
//constructor
GAKernel(TdcppGraph tG,int eulerCode[])
{
popNum = 32;//22222
//factor = IntegerMAX_VALUE / popNum;//to avoid overflow when select,for every fitness
evolutionTim = 0;/////
evolutionLim = 15;///////
//thistpGraph=new TdcppGraph(tG);
//eliminTim = 0;
//eliminLim
curBestFitness = 0;
//curBestCode = new int[eulerCodelength];
//for(int i = 0; i < curBestCodelength; ++i)
//{
// curBestCode[i] = eulerCode[i];
//}
//curBestFitness
iFitness = new int[popNum];
//codes = new int[popNum][];//lines
for(int i = 0; i < popNum; ++i)
{
//codes[i] = new int[eulerCodelength];
iFitness[i] = 0;
}
Systemoutprintln("构造函数,需要填入代码");
}
//initialize the originalpopulation
void initPopulation()
{
//初始化种群
//int tmpCode[] = new int[curBestCodelength];
//get the initial individual
//for(int i = 0; i < curBestCodelength; ++i)
//{
// tmpCode[i] = curBestCode[i];
// codes[0][i] = tmpCode[i];
//}
//ShuffEP s = new ShuffEP(thistpGraph);
//for(int i = 1; i < popNum; ++i)
//{
// sshuff(tmpCode);
// for(int j = 0; j < tmpCodelength; ++j)
// {
// codes[i][j] = tmpCode[j];
// }
//}
Systemoutprintln("初始化种群,需要填入代码");
//get the initial fitness to the member iFitness
computeFitness();
//get the initial best individual and fitness
recordBest();
}
//compute the fitness of every individual in current population
void computeFitness()
{
//计算每个个体适应度
//int time = 0;
//for(int i = 0; i < popNum; ++i)
//{
// time = 0;
// for(int j = 0; j < codes[i]length - 1; ++j)
// {
// time += tpGraphEdge(codes[i][j], codes[i][j + 1])getCost(time);
// }
// iFitness[i] = factor - time;
// if(iFitness[i] < 0)
// {
// Systemoutprintln("错误,某个个体适应度过小使得适配值出现负数");//lkdebug
// Systemexit(1);
// }
//}
Systemoutprintln("计算每个个体适应度,需要填入代码");
}
//record the current best fitness and the according individual
void recordBest()
{
int bestIndex = -1;
for(int i = 0; i < popNum; ++i)
{
if(curBestFitness < iFitness[i])
{
curBestFitness = iFitness[i];
bestIndex = i;
}
}
//记录最优个体
if(bestIndex > -1)
{
// for(int i = 0; i < curBestCodelength; ++i)
// {
// curBestCode[i] = codes[bestIndex][i];
// }
}
Systemoutprintln("记录最优个体,需要填入代码");
}
//selection and reproduce individual in population
void selIndividual()
{
int tmpiFitness[] = new int[iFitnesslength];
tmpiFitness[0] = iFitness[0];
//建立临时群体用于选择交换
//复制个体
//清除原来的群体
//int tmpCode[][] = new int[popNum][];
//for(int i = 0; i < codeslength; ++i)
//{
// tmpCode[i] = new int[codes[i]length];//
// for(int j = 0; j < codes[i]length; ++j)
// {//copy to tmpCode and reset codes
// tmpCode[i][j] = codes[i][j];
// codes[i][j] = -1;
// }
//}
Systemoutprintln("复制个体,需要填入代码");
for(int i = 1; i < tmpiFitnesslength; ++i)
{
tmpiFitness[i] = tmpiFitness[i - 1] + iFitness[i];
//iFitness[i] = 0;
}
//轮盘赌选择个体
for(int i = 0; i < popNum; ++i)
{
int rFit = new Random()nextInt(tmpiFitness[tmpiFitnesslength - 1]);
for(int j = 0; j < tmpiFitnesslength; ++j)
{
if(rFit < tmpiFitness[j])
{
rFit = j;//record the index of the individual
break;
}
}
if(rFit == 0)
{
iFitness[i] = tmpiFitness[rFit];
}
else
{
iFitness[i] = tmpiFitness[rFit] - tmpiFitness[rFit - 1];//copy fitness
}
//选择个体
//for(int j = 0; j < tmpCode[rFit]length; ++j)
//{
// codes[i][j] =tmpCode[rFit][j];
//}
Systemoutprintln("选择个体,需要填入代码");
}
//get the copied fitness in iFitness
}
//match every two individual and cross the code
void matchCross()
{
//需要填入代码
Systemoutprintln("配对交叉,需要填入代码");
}
//mutate by a specifical probability
void mutate()
{
//按照一定的概率进行变异
Systemoutprintln("按照一定的概率进行变异,需要填入代码");
}
//evolve current population
void evolve()
{
selIndividual();
matchCross();
mutate();
}
//compute the approximative best value by GA
//find approximative best solution by GA
public void compute()
{
initPopulation();
//while((evolutionTim < evolutionLim) && (eliminTim < eliminLim))
while(evolutionTim < evolutionLim)
{
evolve();
//get the initial fitness to the member iFitness
computeFitness();
//get the initial best individual and fitness
recordBest();
++evolutionTim;
}
}
}
minmax(P_train)得到矩阵P_train的最小和最大值(找到每行的最小和最大,有多少行就有多少对最小和最大);
[17,3]意思是在隐层建立17个神经元,输出层建立一个神经元;
'tansig','logsig'},'traingdm'是神经网络的一些算法;
做BP网络的话,大概要用到
newff, 建立BP
init 网络初始化
train 网络训练
sim 仿真
记得还要数据归一化啊
以上就是关于遗传算法的模拟 数据结构题目全部的内容,包括:遗传算法的模拟 数据结构题目、求matlab大神解释一下net = newff(minmax(P_train),[17,3],{'tansig','logsig'},'traingdm');、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)