【问题标题】:Java - crossover algorithmJava - 交叉算法
【发布时间】:2017-08-19 03:03:54
【问题描述】:

我正在为 TSP 问题开发 android 应用程序。

我有一个交叉算法,我希望最小化循环数以实现更快的算法。 我该怎么做?

这是代码:

public static Path crossover(Path dad, Path mom) {
    //Create new child path
    Path child = new Path();

    //Get start and sub path positions for dads path
    double startPos = (double) (Math.random() * dad.pathSize());
    double endPos = (double) (Math.random() * dad.pathSize());

    //Loop and add the sub path from dad to our child
    for (int i = 0; i < child.pathSize(); i++) {
        //If our start position is less than the end position
        if (startPos < endPos && i > startPos && i < endPos) {
            child.setDestination(i, dad.getDestination(i));
        } // if our start position is larger
        else if (startPos > endPos) {
            if (!(i < startPos && i > endPos)) {
                child.setDestination(i, dad.getDestination(i));
            }
        }
    }


    // Loop through mom destination path
    for (int i = 0; i < mom.pathSize(); i++){
        // If child doesn't have the destination add it
        if (!child.containsDestination(mom.getDestination(i))) {
            // Loop to find a spare position in the child's path
            for (int j = 0; j < child.pathSize(); j++) {
                //Spare position found, add destination
                if (child.getDestination(j) == null) {
                    child.setDestination(j, mom.getDestination(i));
                    break;
                }
            }
        }
    }
    return child;
}

【问题讨论】:

    标签: java android algorithm genetic-algorithm crossover


    【解决方案1】:

    如果我正确理解了 GA 交叉,您只能使用一个 for 循环从父母那里返回一个孩子。

    请看我的示例代码:

    public Chromosomes crossoverChrom(Chromosomes inpChrom1, Chromosomes inpChrom2){
            // offspring chromosome has the same size as the target chromosome
            Chromosomes offspring = new Chromosomes(inpChrom1.getGenes().length);
    
            for (int i = 0; i < offspring.getGenes().length;i++){
                double randOffspring = Math.random();
                //          System.out.println("i_offspring [" + i + "] , randOffspring = " + randOffspring);
                if(randOffspring <= crossoverRate){
                    //              System.out.println("gene from chrom 1");
                    offspring.setGenes(i, inpChrom1.getGenes()[i]);
                } else {
                    //              System.out.println("gene from chrom 2");
                    offspring.setGenes(i, inpChrom2.getGenes()[i]);
                }
            }
            //      System.out.println("Offspring      = " + offspring + " | Fitness = " + offspring.getFitness());
            //      System.out.println("--------------------------------------------------");
            return offspring;
        }
    

    【讨论】:

      猜你喜欢
      • 2017-08-01
      • 2011-04-20
      • 2014-08-12
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 2012-05-01
      • 2016-08-31
      相关资源
      最近更新 更多