gpt4 book ai didi

java - Java问题中的遗传算法

转载 作者:行者123 更新时间:2023-11-30 08:30:18 25 4
gpt4 key购买 nike

我在用 Java 创建遗传算法时遇到问题。我正在参加在线 GA 竞赛。我每次都试图将最好的结果保存回索引 0,但它只是成为对原始索引的引用。这意味着当我发展其余索引时,如果它发展出最好的成员原始索引,我就会失去它。

我尝试用 getClone 方法填充它,该方法将对象数据转换为 int 数组并从中创建一个新对象。

个人类:

class Individual {
public int[] angle;
public int[] thrust;
public double fitness;
public Individual(){
angle = new int[2];
thrust = new int[2];
for (int i = 0; i < 2; i++) {
this.angle[i] = ThreadLocalRandom.current().nextInt(0, 37) - 18;
this.thrust[i] = ThreadLocalRandom.current().nextInt(0, 202);
this.thrust[i] = ( (this.thrust[i] == 201) ? 650 : this.thrust[i] );
}
this.fitness = Double.MIN_VALUE;
}

public Individual(int[][] genes, double f){
this.fitness = f;
angle = new int[2];
thrust = new int[2];
this.angle[0] = genes[0][0];
this.angle[1] = genes[0][1];
this.thrust[0] = genes[1][0];
this.thrust[1] = genes[1][1];
}

public Individual getClone() {
int[][] genes = new int[2][2];
genes[0][0] = (int)this.angle[0];
genes[0][1] = (int)this.angle[1];
genes[1][0] = (int)this.thrust[0];
genes[1][1] = (int)this.thrust[1];
return ( new Individual(genes, this.fitness) );
}

public Individual crossover(Individual other) {
int[][] genes = new int[2][2];
genes[0][0] = (int)( (this.angle[0] + other.angle[0])/2 );
genes[0][1] = (int)( (this.angle[1] + other.angle[1])/2 );
genes[1][0] = ( (this.thrust[0] == 650 || other.thrust[0] == 650) ? 650: (int)( (this.thrust[0] + other.thrust[0])/2 ) );
genes[1][1] = ( (this.thrust[1] == 650 || other.thrust[1] == 650) ? 650: (int)( (this.thrust[1] + other.thrust[1])/2 ) );
return ( new Individual(genes, Double.MIN_VALUE) );
}

public void mutate() {
for (int i = 0; i < 2; i++) {
if(ThreadLocalRandom.current().nextInt(0, 2)==1) {
this.angle[i] = ThreadLocalRandom.current().nextInt(0, 37) - 18;
}
if(ThreadLocalRandom.current().nextInt(0, 2)==1) {
this.thrust[i] = ThreadLocalRandom.current().nextInt(0, 202);
this.thrust[i] = ( (this.thrust[i] == 201) ? 650 : this.thrust[i] );
}
}
}

人口类别:

class Population {
public Individual[] individuals;

public Population(int populationSize) {
individuals = new Individual[populationSize];
for (int i = 0; i < populationSize; i ++) {
individuals[i] = new Individual();
}
}

public void resetFitness() {
for (int i = 0; i < individuals.length; i++) {
individuals[i].fitness = Double.MIN_VALUE;
}
}
public void setIndividual(int i, Individual indiv) {
individuals[i] = indiv.getClone();
}

public Individual getIndividual(int i) {
return individuals[i].getClone();
}

public int size() {
return this.individuals.length;
}
public Individual getFittest() {
int fittest = 0;
// Loop through individuals to find fittest
for (int i = 0; i < individuals.length; i++) {
if (individuals[i].fitness > individuals[fittest].fitness) {
fittest = i;
}
}
return individuals[fittest].getClone();
}
}

模拟类的必需品:

class simGA {
private Population pop;
private final static int TSIZE = 5; //tournement size

public simGA (int poolsize) {
this.pop = new Population(poolsize);
}

public Individual search(int generations, int totalMoves) {
//this.pop.resetFitness();
for (int g = 0; g < generations; g++) {
for (int i = 0; i < this.pop.individuals.length; i++) {
this.pop.individuals[i].fitness = sim(this.pop.individuals[i],totalMoves);
}
System.err.print("Generation " + g + " ");
this.pop = evolvePopulation(this.pop);
}
return pop.getFittest();
}

private Population evolvePopulation(Population p) {
//save fittest
Population tempPop = new Population(p.individuals.length);
tempPop.setIndividual(0, p.getFittest().getClone() );
System.err.print("Best move: " + tempPop.individuals[0].fitness);
System.err.println();
for (int i = 1; i < p.individuals.length; i++) {
Individual indiv1 = tournamentSelection(p);
Individual indiv2 = tournamentSelection(p);
Individual newIndiv = indiv1.crossover(indiv2);
newIndiv.mutate();
tempPop.setIndividual(i, newIndiv.getClone() );
}
return tempPop;
}

// Select individuals for crossover
private Individual tournamentSelection(Population pop) {
// Create a tournament population
Population tournament = new Population(TSIZE);
// For each place in the tournament get a random individual
for (int i = 0; i < TSIZE; i++) {
int randomId = ThreadLocalRandom.current().nextInt(1, this.pop.individuals.length);
tournament.setIndividual(i, pop.getIndividual(randomId).getClone() );
}
// Get the fittest
return tournament.getFittest().getClone();
}

private double sim(Individual s, int moves) {
return score; //score of simmed moves
}

我如何确保最优秀的人得到保存,而不是作为引用?当我错误地打印出最好的分数时,有时它会丢失并且选择了更差的得分 Action 。我不认为这一定是对象克隆问题,我可以很好地克隆模拟的游戏对象,每次运行都重置它们。

正如我所说,这是为了比赛,所以我不能使用网站上的任何库,这也是我没有发布完整代码的原因,模拟器本身对 Action 进行评分的复杂性不被赠送。但只要说在纸上计算出来的分数符合预期就足够了。

我回复 NWS,我认为我的 getClone 方法正在执行深拷贝。

除 wiki 和其他遗传算法知识外使用的引用资料:http://www.theprojectspot.com/tutorial-post/creating-a-genetic-algorithm-for-beginners/3

我已经通过不重新调整索引 0 处的个人来修复它。但这意味着我的代码存在与问题无关的其他问题。

最佳答案

Individual newIndiv = indiv1.crossover(indiv2);

上一行将适应度重置为 Double.MIN_VALUE。因此,每当调用 evolvePopulation 时,只有索引 0 处的个体是最适者。

关于java - Java问题中的遗传算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41340615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com