⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inversionoperator.java

📁 java实现的遗传算法
💻 JAVA
字号:
/*
 * This file is part of JGAP.
 *
 * JGAP offers a dual license model containing the LGPL as well as the MPL.
 *
 * For licencing information please see the file license.txt included with JGAP
 * or have a look at the top of class org.jgap.Chromosome which representatively
 * includes the JGAP license policy applicable for any file delivered with JGAP.
 */
package org.jgap.impl;

import java.util.*;
import org.jgap.*;

/**
 * The inversion operator randomly selects one chromosomes from the
 * population and inverses it by randomly picking a splitting locus on which
 * to swap the first part with the last part of the chromosome.
 *
 * @author Klaus Meffert
 * @since 2.3
 */
public class InversionOperator
    implements GeneticOperator {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.4 $";

  /**
   * Constructs a new instance of this operator
   *
   * @author Klaus Meffert
   * @since 2.3
   */
  public InversionOperator() {
  }

  /**
   * @author Klaus Meffert
   * @since 2.3
   */
  public void operate(final Population a_population,
                      final List a_candidateChromosomes) {
    // Work out the number of crossovers that should be performed.
    // -----------------------------------------------------------
    int size = Math.min(Genotype.getConfiguration().getPopulationSize(),
                        a_population.size());
    RandomGenerator generator = Genotype.getConfiguration().getRandomGenerator();
    // For the inversion, grab a random chromosome, pick a random
    // locus (gene location), and then swap that gene and all genes
    // to the "right" (those with greater loci) of that gene with the left
    // genes.
    // -------------------------------------------------------------------
    int index1;
    index1 = generator.nextInt(size);
    Chromosome chrom1 = a_population.getChromosome(index1);
    Chromosome firstMate = (Chromosome) chrom1.clone();
    Gene[] firstGenes = firstMate.getGenes();
    int locus = generator.nextInt(firstGenes.length);
    // Swap the genes.
    // ---------------
    Gene[] invertedGenes = new Gene[firstGenes.length];
    int index = 0;
    int len = firstGenes.length;
    for (int j = locus; j < len; j++) {
      invertedGenes[index++] = firstGenes[j];
    }
    for (int j = 0; j < locus; j++) {
      invertedGenes[index++] = firstGenes[j];
    }
    try {
      firstMate.setGenes(invertedGenes);
    }
    catch (InvalidConfigurationException cex) {
      throw new Error("Gene type not allowed by constraint checker", cex);
    }
    // Add the modified chromosome to the candidate pool so that it'll be
    // considered for natural selection during the next phase of evolution.
    // --------------------------------------------------------------------
    a_candidateChromosomes.add(firstMate);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -