实例
package hello;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class AI2 {
/**
* 种群数量
*/
public static int NIND=40;
/**
* 遗传代数
*/
public static int MAXGEN=40;
/**
* 染色体长度
*/
public static int PRECI=22;
/**
* 区间下界
*/
public static double lb=-1.0;
/**
* 区间上界
*/
public static double ub=2.0;
/**
* 交叉概率
*/
public static double px=0.7;
/**
* 变异概率
*/
public static double pm=0.01;
public static Random random=new Random();
/**
* 种群中的最好个体
*/
static Chromosome bestChromosome=new Chromosome();
/**
* 求2的t次方
* @param t
* @return
*/
public static double pow(int t){
double res=1.0;
for(int i=0;i<t;i++){
res*=2.0;
}
return res;
}
/**
* 计算对应十进制,如11对应3
* @param list
* @return
*/
public static double toBinary(List<Integer>list){
String s="";
for (int i = 0; i < list.size(); i++) {
s+=Integer.toString(list.get(i));
}
double t=(double)(Integer.parseInt(s, 2));
return t;
}
/**
* 计算对应的十进制小数
* @param list
* @return
*/
public static double bs2rv(List<Integer> list){
double x=0.0;
double sigma=(ub-lb)/(pow(PRECI)-1);
x=lb+sigma*toBinary(list);
return x;
}
/**
* 某个基因序列是否都一样
* @param gene
* @return
*/
public static boolean isZeroOrOne(List<Integer>gene) {
int t=gene.get(0);
for(int i=1;i<gene.size();i++) {
if (gene.get(i)!=t) {
return false;
}
}
return true;
}
public static int change(int a) {
if (a==1) {
return 0;
}
else return 1;
}
/**
* 初始化,计算对应的十进制,计算适应度
* @param NIND
* @param PRECI
* @param Chrom
* @return
*/
public static List<Chromosome> crtbp(int NIND,int PRECI,List<Chromosome> Chrom){
for(int i=0;i<NIND;i++){
List<Integer> list=new ArrayList<Integer>();
for(int j=0;j<PRECI;j++){
int t=(int)(random.nextDouble()+0.5);
list.add(t);
}
double X=bs2rv(list);
double ObjV=X*Math.sin(10*Math.PI*X)+2.0;
double FitnV=ObjV;
Chrom.add(new Chromosome(list,X,ObjV,FitnV));
}
return Chrom;
}
/**
* *赌
* @param Chrom
* @return
*/
public static List<Chromosome> select(List<Chromosome> Chrom){
int best=0;
int worst=0;
for(int i=1;i<NIND;i++) {
if(Chrom.get(i).FitnV>Chrom.get(best).FitnV)
best=i;
}
for(int i=1;i<NIND;i++) {
if(Chrom.get(i).FitnV<Chrom.get(worst).FitnV)
worst=i;
}
bestChromosome=Chrom.get(best);//精英保存
Chromosome worstChromosome=Chrom.get(worst);
// Chrom.remove(bestChromosome);
Chrom.remove(worstChromosome);
worst=0;
for(int i=1;i<NIND-1;i++) {
if(Chrom.get(i).FitnV<Chrom.get(worst).FitnV)
worst=i;
}
worstChromosome=Chrom.get(worst);
Chrom.remove(worstChromosome);
double sum=0.0;//群体总适应度
for(int i=0;i<Chrom.size();i++){
sum+=Chrom.get(i).FitnV;
}
double []t=new double[NIND];//被选中的概率
double []p=new double[NIND];//在*上的范围
for(int i=0;i<Chrom.size();i++){
t[i]=Chrom.get(i).FitnV/sum;
}
p[0]=t[0];
for(int i=1;i<Chrom.size();i++){
p[i]=t[i]+p[i-1];
}
List<Chromosome> SelCh=new ArrayList<Chromosome>();
for(int i=0;i<NIND-2;i++) {
double rand=random.nextDouble();
if(0<=rand&&rand<=p[0]) SelCh.add(Chrom.get(0));
else {
for(int j=1;j<Chrom.size();j++) {
if(p[j-1]<rand&&rand<=p[j]) {
SelCh.add(Chrom.get(j));
break;
}
}
}
}
for(int i=0;i<SelCh.size();i++) {
List<Integer> list=SelCh.get(i).getGene();
double X=bs2rv(list);
double ObjV=X*Math.sin(10*Math.PI*X)+2.0;
double FitnV=ObjV;
SelCh.get(i).setX(X);
SelCh.get(i).setObjV(ObjV);
SelCh.get(i).setFitnV(FitnV);
}
return SelCh;
}
/**
* 交叉
* @param Chrom
* @return
*/
public static List<Chromosome> recombin(List<Chromosome> Chrom){
for(int k=0;k<(NIND-2)/2;k++) {
if(random.nextDouble()<px) {
int i1=0;
int i2=0;
do {
i1=random.nextInt(NIND-2-1+1);//第i1个个体
i2=random.nextInt(NIND-2-1+1);//第i2个个体
} while (i1==i2);
int pos=random.nextInt(PRECI-1);//最后一个点不能选[0,37)
int []temp1_left=new int[pos];
int []temp2_left=new int[pos];
int []temp1_right=new int[PRECI-pos];
int []temp2_right=new int[PRECI-pos];
List<Integer>parent1=Chrom.get(i1).getGene();
List<Integer>parent2=Chrom.get(i2).getGene();
for(int i=0;i<pos;i++) temp1_left[i]=parent1.get(i);
for(int i=0;i<pos;i++) temp2_left[i]=parent2.get(i);
for(int i=pos;i<PRECI;i++) temp1_right[i-pos]=parent1.get(i);
for(int i=pos;i<PRECI;i++) temp2_right[i-pos]=parent2.get(i);
List<Integer>son1=new ArrayList<Integer>();
List<Integer>son2=new ArrayList<Integer>();
for(int i=0;i<pos;i++) son1.add(temp1_left[i]);
for(int i=0;i<PRECI-pos;i++) son1.add(temp2_right[i]);
for(int i=0;i<pos;i++) son2.add(temp2_left[i]);
for(int i=0;i<PRECI-pos;i++) son2.add(temp1_right[i]);
Chrom.get(i1).setGene(son1);
Chrom.get(i2).setGene(son2);
}
}
for(int i=0;i<Chrom.size();i++) {
List<Integer> list=Chrom.get(i).getGene();
double X=bs2rv(list);
double ObjV=X*Math.sin(10*Math.PI*X)+2.0;
double FitnV=ObjV;
Chrom.get(i).setX(X);
Chrom.get(i).setObjV(ObjV);
Chrom.get(i).setFitnV(FitnV);
}
return Chrom;
}
/**
* 变异
* @param Chrom
* @return
*/
public static List<Chromosome> mut(List<Chromosome> Chrom){
for(int k=0;k<Chrom.size();k++) {
if (isZeroOrOne(Chrom.get(k).getGene())) {
int t=random.nextInt(PRECI);
int changet=change(Chrom.get(k).getGene().get(t));
Chrom.get(k).getGene().set(t, changet);
}
else {
// for(int i=0;i<PRECI;i++) {
double rand=random.nextDouble();
if (rand<pm) {
int i=random.nextInt(PRECI);
int changet=change(Chrom.get(k).getGene().get(i));
Chrom.get(k).getGene().set(i, changet);
}
// }
}
}
for(int i=0;i<Chrom.size();i++) {
List<Integer> list=Chrom.get(i).getGene();
double X=bs2rv(list);
double ObjV=X*Math.sin(10*Math.PI*X)+2.0;
double FitnV=ObjV;
Chrom.get(i).setX(X);
Chrom.get(i).setObjV(ObjV);
Chrom.get(i).setFitnV(FitnV);
}
return Chrom;
}
/**
* 重组
* @param Chrom
* @return
*/
public static List<Chromosome> reins(List<Chromosome> Chrom){
Chrom.add(bestChromosome);
Chrom.add(bestChromosome);
return Chrom;
}
public static void main(String[] args) {
List<Chromosome> Chrom=new ArrayList<Chromosome>();
List<Chromosome> SelCh=new ArrayList<Chromosome>();
Chrom=crtbp(NIND, PRECI,Chrom);
for(int gen=1;gen<=MAXGEN;gen++) {
SelCh=select(Chrom);
SelCh=recombin(SelCh);
SelCh=mut(SelCh);
Chrom=reins(SelCh);
System.out.println("第:"+gen+"代");
System.out.println("X:"+bestChromosome.X+"\t\t"+"Y:"+bestChromosome.FitnV);
}
}
}
/**
* 染色体
*/
class Chromosome{
/**
* 基因组,其长度为PERCI
*/
public List<Integer>Gene=new ArrayList<Integer>();
/**
* 对应的十进制
*/
public double X;
/**
* 对应的十进制的函数值
*/
public double ObjV;
/**
* 适应度
*/
public double FitnV;
public Chromosome(List<Integer> gene, double x, double objV, double fitnV) {
super();
Gene = gene;
X = x;
ObjV = objV;
FitnV = fitnV;
}
public Chromosome() {
super();
}
public List<Integer> getGene() {
return Gene;
}
public void setGene(List<Integer> gene) {
Gene = gene;
}
public double getX() {
return X;
}
public void setX(double x) {
X = x;
}
public double getObjV() {
return ObjV;
}
public void setObjV(double objV) {
ObjV = objV;
}
public double getFitnV() {
return FitnV;
}
public void setFitnV(double fitnV) {
FitnV = fitnV;
}
}
//List<Integer>list=new ArrayList<Integer>();
//list.add(1);
//list.add(0);
//System.out.println(toBinary(list));
//for(int i=0;i<Chrom.size();i++) {
// System.out.println("X:"+Chrom.get(i).X+"\t\t"+"Y:"+Chrom.get(i).ObjV);
//}
//System.out.println("best:"+bestChromosome.FitnV+"worst:"+worstChromosome.FitnV);
运行实例 »
点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号