遗传算法求方程最大值
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C4.chooseNumber++;
}//for
}
//赌轮算法产生选中的估计次数
public void Copy(Chromosome Source,Chromosome Direct)
{
for(int i=0;i<5;i++)
{
Direct.geneString[i]=Source.geneString[i];
{
C1.fitness=fValue(C1.geneString)*fValue(C1.geneString);
C2.fitness=fValue(C2.geneString)*fValue(C2.geneString);
C3.fitness=fValue(C3.geneString)*fValue(C3.geneString);
C1.chooseProbability=C1.fitness/sum;
C2.chooseProbability=C2.fitness/sum;
C3.chooseProbability=C3.fitness/sum;
C4.chooseProbability=C4.fitness/sum;
}
//计算每个染色体的选择概率
public void chooseNumCal()
{
double randomNumber=-1;
C1.chooseNumber=0;
C2.chooseNumber=0;
C3.chooseNumber=0;
C4.chooseNumber=0;
System.out.println("赌轮随机数:");
源代码:
/*
遗传算法应用举例————函数最大值问题的求解:f(x)=x*x(0<=x<=31)
课程名称:人工智能
专业:计算机软件理论
*/
class Chromosome //染色体类的定义
{
int geneString[]=new int[5];//基因位串
int fitness=0;//适应度
double chooseProbability=0.0;//选择概率
}
}
}
class GA
{
int s1[]={0,1,1,0,1};
int s2[]={1,1,0,0,0};
int s3[]={0,1,0,0,0};
int s4[]={1,0,0,1,1};
//初始化种群规模为4,产生初始化种群基因位串编码
Chromosome C1=new Chromosome();
for(int i=0;i<4;i++)
{
randomNumber=Math.random();
System.out.print(" "+randomNumber);
if(randomNumber<=C1.accumulatedProbability)
C1.chooseNumber++;
if(randomNumber>C1.accumulatedProbability&&randomNumber<=C2.accumulatedProbability)
}
//实例化染色体类,产生4给染色体
}
public String change(int a[])
{
char b[]=new char[5];
for(int i=0;i<5;i++)
if(a[i]==0)
b[i]='0';
else
b[i]='1';
String s=new String(b);
return s;
C3.accumulatedProbability=C2.accumulatedProbability+C3.chooseProbability;
C4.accumulatedProbability=C3.accumulatedProbability+C4.chooseProbability;
}
//计算每个染色体的积累概率
C2.chooseNumber++;
if(randomNumber>C2.accumulatedProbability&&randomNumber<=C3.accumulatedProbability)
C3.chooseNumber++;
if(randomNumber>C3.accumulatedProbability&&randomNumber<=C4.accumulatedProbability)
public void accumulatedProCal()
{
C1.accumulatedProbability=C1.chooseProbability;
C2.accumulatedProbability=C1.accumulatedProbability+C2.chooseProbability;
C4.fitness=fValue(C4.geneString)*fValue(C4.geneString);
}Leabharlann Baidu
//求出每个染色体的适应度
public void chooseProCal()
{
double sum=0.0;
sum=C1.fitness+C2.fitness+C3.fitness+C4.fitness;
Chromosome C2=new Chromosome();
Chromosome C3=new Chromosome();
Chromosome C4=new Chromosome();
GA()
{
C1.initGene(s1);
C2.initGene(s2);
C3.initGene(s3);
C4.initGene(s4);
double accumulatedProbability=0.0;//积累概率
int chooseNumber=0;//估计选中次数
public void initGene(int a[])//给基因位串赋初值
{
for(int i=0;i<5;i++)
{
this.geneString[i]=a[i];
}//转换字符串
public int fValue(int a[])
{
int sum=0;
for(int i=0;i<5;i++)
{
sum=sum+a[4-i]*(int)Math.pow(2.0,(double)i);
}
return sum;
}
//基因位串换算成十进制编码值
public void fitnessCal()
}//for
}
//赌轮算法产生选中的估计次数
public void Copy(Chromosome Source,Chromosome Direct)
{
for(int i=0;i<5;i++)
{
Direct.geneString[i]=Source.geneString[i];
{
C1.fitness=fValue(C1.geneString)*fValue(C1.geneString);
C2.fitness=fValue(C2.geneString)*fValue(C2.geneString);
C3.fitness=fValue(C3.geneString)*fValue(C3.geneString);
C1.chooseProbability=C1.fitness/sum;
C2.chooseProbability=C2.fitness/sum;
C3.chooseProbability=C3.fitness/sum;
C4.chooseProbability=C4.fitness/sum;
}
//计算每个染色体的选择概率
public void chooseNumCal()
{
double randomNumber=-1;
C1.chooseNumber=0;
C2.chooseNumber=0;
C3.chooseNumber=0;
C4.chooseNumber=0;
System.out.println("赌轮随机数:");
源代码:
/*
遗传算法应用举例————函数最大值问题的求解:f(x)=x*x(0<=x<=31)
课程名称:人工智能
专业:计算机软件理论
*/
class Chromosome //染色体类的定义
{
int geneString[]=new int[5];//基因位串
int fitness=0;//适应度
double chooseProbability=0.0;//选择概率
}
}
}
class GA
{
int s1[]={0,1,1,0,1};
int s2[]={1,1,0,0,0};
int s3[]={0,1,0,0,0};
int s4[]={1,0,0,1,1};
//初始化种群规模为4,产生初始化种群基因位串编码
Chromosome C1=new Chromosome();
for(int i=0;i<4;i++)
{
randomNumber=Math.random();
System.out.print(" "+randomNumber);
if(randomNumber<=C1.accumulatedProbability)
C1.chooseNumber++;
if(randomNumber>C1.accumulatedProbability&&randomNumber<=C2.accumulatedProbability)
}
//实例化染色体类,产生4给染色体
}
public String change(int a[])
{
char b[]=new char[5];
for(int i=0;i<5;i++)
if(a[i]==0)
b[i]='0';
else
b[i]='1';
String s=new String(b);
return s;
C3.accumulatedProbability=C2.accumulatedProbability+C3.chooseProbability;
C4.accumulatedProbability=C3.accumulatedProbability+C4.chooseProbability;
}
//计算每个染色体的积累概率
C2.chooseNumber++;
if(randomNumber>C2.accumulatedProbability&&randomNumber<=C3.accumulatedProbability)
C3.chooseNumber++;
if(randomNumber>C3.accumulatedProbability&&randomNumber<=C4.accumulatedProbability)
public void accumulatedProCal()
{
C1.accumulatedProbability=C1.chooseProbability;
C2.accumulatedProbability=C1.accumulatedProbability+C2.chooseProbability;
C4.fitness=fValue(C4.geneString)*fValue(C4.geneString);
}Leabharlann Baidu
//求出每个染色体的适应度
public void chooseProCal()
{
double sum=0.0;
sum=C1.fitness+C2.fitness+C3.fitness+C4.fitness;
Chromosome C2=new Chromosome();
Chromosome C3=new Chromosome();
Chromosome C4=new Chromosome();
GA()
{
C1.initGene(s1);
C2.initGene(s2);
C3.initGene(s3);
C4.initGene(s4);
double accumulatedProbability=0.0;//积累概率
int chooseNumber=0;//估计选中次数
public void initGene(int a[])//给基因位串赋初值
{
for(int i=0;i<5;i++)
{
this.geneString[i]=a[i];
}//转换字符串
public int fValue(int a[])
{
int sum=0;
for(int i=0;i<5;i++)
{
sum=sum+a[4-i]*(int)Math.pow(2.0,(double)i);
}
return sum;
}
//基因位串换算成十进制编码值
public void fitnessCal()