我也试图在java中实现GA来解决方程式(此时不重要)
我有一个实施可比较的染色体课程(按健身排序) 和我自己的IChromosome界面。
我在这个课程中提供了交叉,变异等功能: 这样的一些事情:
public interface IChromosome
{
public int getSize();
public void setSize(int s);
public IGene[] getGenes();
public void setGenes(IGene[] genes);
public IGene getGene(int i);
public void setGene(int i , IGene val);
public double getFitness();
public IChromosome[] crossOver(IChromosome c2);
public IChromosome mutate();
}
我的GA计划的主要部分是:
public class GeneticAlgorithm
{
final int populationSize;
int crossOverRate;// Probability in persent
int mutationRate;// Probability in persent
int elitismRate;// in persent
int TournamentNumber;//number of random Chromosomes to compete in Tournament
int repeatCount;
Chromosome [] currentGeneration;
Chromosome [] nextGeneration;
public GeneticAlgorithm(int populationSize, int crossOverRate, int mutationRate, int ElitismRate, int TournamentNumber)
{
this.populationSize = populationSize;
this.crossOverRate = crossOverRate;
this.mutationRate = mutationRate;
this.elitismRate = ElitismRate;
this.TournamentNumber = TournamentNumber;
repeatCount = 0;
currentGeneration = new Chromosome[populationSize];
nextGeneration = new Chromosome[populationSize];
for (int i = 0; i < populationSize; i++)
{
currentGeneration[i] =new Chromosome(Chromosome.getPolynomial().getSize());//Also randomize it; //size is from another class
}
}
private Chromosome[] select()
{
Chromosome[] temp = new Chromosome[TournamentNumber];
//choose populationSize random uniqe number
ArrayList<Integer> nums = new ArrayList<>();
for (int i = 0; i < populationSize; i++)
{
nums.add(i);
}
Collections.shuffle(nums);
for (int i = 0; i < TournamentNumber; i++)
{
temp[i]=new Chromosome(currentGeneration[nums.get(i)]);
}
Arrays.sort(temp);
Chromosome[] res = new Chromosome[2];
//choosing best Chromosomes
res[0] = new Chromosome(temp[TournamentNumber-1]);
res[1] = new Chromosome(temp[TournamentNumber-2]);
return res;
}
public void repeat()
{
repeatCount++;
Arrays.sort(currentGeneration,Collections.reverseOrder());
int elitism = (populationSize*elitismRate)/100;
for (int i = 0; i < elitism ; i++)
{
nextGeneration[i] = new Chromosome(currentGeneration[i]);
}
int cop = 0;//Cross Over Probability
int mp;//mutation Probability
Random rnd = new Random();
Chromosome [] sel;
Chromosome [] temp;
Chromosome ch1 , ch2;
for (int i = elitism; i < populationSize; i++)
{
cop = rnd.nextInt(100);
sel = new Chromosome[2];
sel = select();
if(cop <= crossOverRate)
{
temp = (Chromosome[]) ( sel[0].crossOver(sel[1]) );//tested , And I 'm sure cross over function working well
ch1 = temp[0];
ch2 = temp[1];
}
else
{
ch1 = new Chromosome(sel[0]);
ch2 = new Chromosome(sel[1]);
}
mp = rnd.nextInt(100);
if(mp< mutationRate)
{
ch1.mutate();
}
nextGeneration[i]= new Chromosome(ch1);
if(++i<populationSize)
{
nextGeneration[i]= new Chromosome(ch2);
}
}//for
//copiny current generation to nextGeneration
for (int i = 0; i < populationSize; i++)
{
currentGeneration[i] = new Chromosome(nextGeneration[i]);
}
}//repeat
}
每件事似乎都是正常的,但经过几代人(称为repeat()
),我生成的所有染色体都将成为一个保存价值。
我多次调试程序,但我找不到任何东西。
问题是什么?
答案 0 :(得分:2)
我认为如果其他事情没问题,你的算法会早期收敛。
可能的解决方案是: 1-集精英率为零; 2-降低交叉率; 3-增加突变率。
如果要解决的问题非常简单,GA早期收敛是很自然的。