Java问题中的遗传算法 [英] Genetic Algorithm in Java problems

查看:71
本文介绍了Java问题中的遗传算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用Java创建遗传算法时遇到了麻烦.我正在参加在线GA竞赛.我试图每次返回索引0时都保存最佳结果,但是它只是成为对原始索引的引用.意思是,当我开发其余索引时,如果它演化出最佳成员原始索引,我就会失去它.

I am having trouble creating a Genetic Algorithm in java. I am competing in an online GA contest. I am trying to save the best result each time back into index 0, but it just becomes a reference to the original index. Meaning when I evolve the rest of the indexes, if it evolves the best members original index I lose it.

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

I have tried shimming it with a getClone method that converts the objects data to and int array and creates a new object from it.

个人班:

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();
    }
}

sim类中的必需品:

The necessaries from the sim class:

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
    }

如何确保最好的个人得到保存,而不是作为参考?当我错误打印最佳分数时,有时会丢失并且选择较差的得分手.我认为这不一定是对象克隆的问题,我可以克隆模拟得很好的游戏对象,并在每次运行时将其重置.

How can I make sure that the best individual is getting saved, not as a reference? When I error print the best score, sometimes it is lost and a worse scoring move is chosen. I don't think it is necessarily a object cloning issue, I can clone the game objects that are simulated just fine, resetting them each run.

正如我所说的,这是为了比赛,所以我不能使用该站点上的任何库,这也是我未发布完整代码的原因,模拟器自己对动作进行评分的复杂性并不在于只是被放弃.但是只要说出在纸上算出来的分数就可以按预期返回,就足够了.

As I said, this is for a contest, so I cannot use any libraries on the site, and also is the reason I am not posting the full code, the intricacies of the simulator it self that scores the moves are not to be just given away. But suffice it to say the scores come back as expected for the move when worked out on paper.

我对NWS的反应是,我以为我的getClone方法正在进行深层复制.

I response to NWS, I thought my getClone method was doing a deep copy.

Wiki和其他有关遗传算法知识的参考文献:

Reference used beside wiki and other knowledge on Genetic Algorithms: http://www.theprojectspot.com/tutorial-post/creating-a-genetic-algorithm-for-beginners/3

我已通过不重新模拟索引0处的个人来解决此问题.但这意味着我的代码存在其他与问题无关的问题.

I have fixed it by not resimming the individual at index 0. However this means there are other issue with my code not related to the question.

推荐答案

我已通过不重新模拟索引为0的个人来解决了该问题.但这意味着我的代码存在其他与问题无关的问题,因为重新模拟了相同的问题与以前相同的时间点的个人不应改变其适应性.

I have fixed it by not resimming the individual at index 0. However this means there are other issue with my code not related to the question, since resimming the same individual from the same point in time as before should not change it's fitness.

这篇关于Java问题中的遗传算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆