【发布时间】:2011-06-08 04:55:53
【问题描述】:
我想知道是否可以就提高实现遗传算法的程序的整体效率获得一些建议。是的,这是一个作业问题,但我已经自己完成了作业,我只是在寻找一种让它表现更好的方法 Problem Description
目前我的程序读取由成分类型 h 或 p 组成的给定链。 (例如:hphpphhphpphphhpphph)对于每个 H 和 P,它生成一个随机移动(上、下、左、右)并将该移动添加到包含在“染色体”对象中的 arrayList 中。开始时,程序为 10,000 条染色体生成 19 次移动
SecureRandom sec = new SecureRandom();
byte[] sbuf = sec.generateSeed(8);
ByteBuffer bb = ByteBuffer.wrap(sbuf);
Random numberGen = new Random(bb.getLong());
int numberMoves = chromosoneData.length();
moveList = new ArrayList(numberMoves);
for (int a = 0; a < numberMoves; a++) {
int randomMove = numberGen.nextInt(4);
char typeChro = chromosoneData.charAt(a);
if (randomMove == 0) {
moveList.add(Move.Down);
} else if (randomMove == 1) {
moveList.add(Move.Up);
} else if (randomMove == 2) {
moveList.add(Move.Left);
} else if (randomMove == 3) {
moveList.add(Move.Right);
}
}
之后是从种群中选择要交叉的染色体。我的交叉函数从最适合的 20% 的人群中随机选择第一个染色体,从前 20% 之外的人群中随机选择另一个。然后将选择的染色体交叉并调用突变函数。我相信我受到最大打击的领域是计算每个染色体的适应度。目前我的适应度函数创建一个二维数组作为网格,从上面显示的函数生成的移动列表中按顺序放置移动,然后循环遍历数组以进行适应度计算。 (IE 找到并且位置 [2,1] 的 H 是 Cord [1,1] [3,1] [2,0] 或 [2,2] 也是一个 H,如果找到一个 H,它只会增加找到债券)
计算完成后,从我的种群中删除最不适合的染色体并添加新的染色体,然后对染色体的数组列表进行排序。冲洗并重复,直到找到目标溶液
如果你们想看更多我的代码来证明我在寻求帮助之前确实做了这项工作,请告诉我(不想发太多帖子,所以其他学生不能只是复制我的东西)
正如 cmets 中所建议的那样,我已经在我的应用程序上运行了分析器(以前从未使用过,只有一年级的 CS 学生),我最初对我遇到问题的地方的猜测有些不正确。从探查器告诉我的情况看来,大热点是:
- 在将新染色体与群体中的其他染色体进行比较以确定其位置时。我通过实现 Comparable 来做到这一点:
public int compareTo(Chromosome other) {
if(this.fitness >= other.fitness)
return 1;
if(this.fitness ==other.fitness )
return 0;
else
return -1;
}
-
所描述的另一个问题领域是我的实际进化函数,它消耗了大约 40% 的 CPU 时间。下面所述方法的代码示例
double topPercentile = highestValue; topPercentile = topPercentile * .20; topPercentile = Math.ceil(topPercentile); randomOne = numberGen.nextInt((int) topPercentile); //Lower Bount for random two so it comes from outside of top 20% int randomTwo = numberGen.nextInt(highestValue - (int) topPercentile); randomTwo = randomTwo + 25; //System.out.println("Selecting First: " + randomOne + " Selecting Second: " + randomTwo); Chromosome firstChrom = (Chromosome) populationList.get(randomOne); Chromosome secondChrom = (Chromosome) populationList.get(randomTwo); //System.out.println("Selected 2 Chromosones Crossing Over"); Chromosome resultantChromosome = firstChrom.crossOver(secondChrom); populationList.add(resultantChromosome); Collections.sort(populationList); populationList.remove(highestValue); Chromosome bestResult = (Chromosome) populationList.get(0); -
另一个主要的性能命中是由帖子中的第一个代码示例执行的初始种群播种
【问题讨论】:
-
你的 compareTo 函数永远不会返回 0
标签: java performance genetic-algorithm