Examples of Chromosome


Examples of com.trimga.Chromosome

  }
 
  public static void randomlyAddGeneratedChromosomes(List<Chromosome> chromosomeList) {
    int numChromosomesToAdd = (int)Math.round(Math.sqrt(chromosomeList.size()) * Math.random() * Math.random());
    if(chromosomeList.size() > 0) {
      Chromosome aChromosome = chromosomeList.get(0);
      for(int i=0; i<numChromosomesToAdd; i++) {
        try {
          int insertionIndex = (int) (Math.random() * chromosomeList.size());
          chromosomeList.add(insertionIndex, aChromosome.generateRandomChromosome());
        } catch(RuntimeException ignore) {}
      }
    }
  }
View Full Code Here

Examples of com.trimga.Chromosome

  public static void randomlyMutateChromosomes(List<Chromosome> chromosomeList) {
    int numChromosomesToMutate = (int)Math.round(Math.sqrt(chromosomeList.size()) * Math.random() * Math.random());
      for(int i=0; i<numChromosomesToMutate; i++) {
        try {
          int randomIndex = (int)Math.round(chromosomeList.size() * Math.random());
        Chromosome chromosomeToMutate = chromosomeList.remove(randomIndex);
          chromosomeList.add(chromosomeToMutate.generateMutation());
        } catch(RuntimeException ignore) {}
      }
  }
View Full Code Here

Examples of forestry.core.genetics.Chromosome

  public static Chromosome[] templateAsChromosomes(IAllele[] template) {
    Chromosome[] chromosomes = new Chromosome[template.length];
    for (int i = 0; i < template.length; i++)
      if (template[i] != null)
        chromosomes[i] = new Chromosome(template[i]);

    return chromosomes;
  }
View Full Code Here

Examples of jcgp.backend.population.Chromosome

  }
 
  @Test
  public void preinitialisedChromosomeTest() {
    // the original chromosome that will be cloned
    Chromosome oc = new Chromosome(resources);

    // initialise a population with a copy of it
    population = new Population(oc, resources);
    // check that the first parent chromosome is identical to, but not the same instance as, the one given
    assertTrue("Incorrect chromosome in population.", population.get(0).compareGenesTo(oc) && population.get(0) != oc);
View Full Code Here

Examples of jcgp.backend.population.Chromosome

      }
      if (report.get()) getResources().reportln("[ES] Selected contenders: " + Arrays.toString(contenders));
      Arrays.sort(contenders);
      if (report.get()) getResources().reportln("[ES] Chr " + contenders[contenders.length - 1] + " wins the tournament, copying and mutating...");
      // create a copy of the selected chromosome and mutate it
      newPopulation[i] = new Chromosome(population.get(contenders[contenders.length - 1]));
      mutator.mutate(newPopulation[i]);
    }
    if (report.get()) getResources().reportln("[ES] Tournaments are finished, copying new chromosomes into population");
    // newPopulation has been generated, copy into the population
    for (int c = 0; c < getResources().populationSize(); c++) {
View Full Code Here

Examples of jcgp.backend.population.Chromosome

  @BeforeClass
  public static void setUpBeforeClass() {
   
    resources = new ModifiableResources();
    resources.setFunctionSet(new TestFunctionSet());
    chromosome = new Chromosome(resources);
  }
View Full Code Here

Examples of jcgp.backend.population.Chromosome

  @BeforeClass
  public static void setUpBeforeClass() {
    resources = new ModifiableResources();
    resources.setFunctionSet(new SymbolicRegressionFunctions());
    chromosome = new Chromosome(resources);
  }
View Full Code Here

Examples of jcgp.backend.population.Chromosome

    resources.setFunctionSet(new TestFunctionSet());
  }

  @Before
  public void setUp() throws Exception {
    chromosome = new Chromosome(resources);
  }
View Full Code Here

Examples of jcgp.backend.population.Chromosome

   *
   */
  @Test
  public void cloneTest() {
    // create a clone, check to see if it really is a clone
    Chromosome clone = new Chromosome(chromosome);

    // compare all elements, one by one
    // check outputs
    for (int o = 0; o < resources.outputs(); o++) {
      // check that no cross-references exist between chromosomes
      assertTrue("Cloned chromosome contains a reference to a member of the original chromosome.",
          clone.getOutput(o) != chromosome.getOutput(o) &&
          clone.getOutput(o).getSource() != chromosome.getOutput(o).getSource());
      // check that the connections are equivalent
      if (clone.getOutput(o).getSource() instanceof Input && chromosome.getOutput(o).getSource() instanceof Input) {
        assertTrue("Outputs did not connect to equivalent inputs.",
            ((Input) clone.getOutput(o).getSource()).getIndex() == ((Input) chromosome.getOutput(o).getSource()).getIndex());
      } else if (clone.getOutput(o).getSource() instanceof Node && chromosome.getOutput(o).getSource() instanceof Node) {
        assertTrue("Outputs did not connect to equivalent nodes.",
            ((Node) clone.getOutput(o).getSource()).getRow() == ((Node) chromosome.getOutput(o).getSource()).getRow() &&
            ((Node) clone.getOutput(o).getSource()).getColumn() == ((Node) chromosome.getOutput(o).getSource()).getColumn());
      } else {
        fail("Output source types did not match.");
      }
    }
    // check nodes, rows first
    for (int row = 0; row < resources.rows(); row++) {
      for (int column = 0; column < resources.columns(); column++) {
        // check that nodes are not pointers to the same instance
        assertTrue("Both chromosomes contain a reference to the same node.", clone.getNode(row, column) != chromosome.getNode(row, column));
        // check that both nodes reference their own position in the grid correctly
        assertTrue("Equivalent nodes self-reference differently.", clone.getNode(row, column).getRow() == chromosome.getNode(row, column).getRow() &&
            clone.getNode(row, column).getColumn() == chromosome.getNode(row, column).getColumn());
        // check that the two nodes have the same function
        assertTrue("Equivalent nodes have different functions.", clone.getNode(row, column).getFunction() == chromosome.getNode(row, column).getFunction());

        // compare each connection
        for (int connection = 0; connection < resources.arity(); connection++) {
          // first look at whether they are actually the same instance
          assertTrue("Nodes are connected to the same connection instance.",
              clone.getNode(row, column).getConnection(connection) !=  chromosome.getNode(row, column).getConnection(connection));

          // if the connections aren't the same instance, check that their addresses are the same
          if (clone.getNode(row, column).getConnection(connection) instanceof Input &&
              chromosome.getNode(row, column).getConnection(connection) instanceof Input) {

            assertTrue("Nodes did not connect to equivalent inputs.",
                ((Input) clone.getNode(row, column).getConnection(connection)).getIndex() ==
                ((Input) chromosome.getNode(row, column).getConnection(connection)).getIndex());

          } else if (clone.getNode(row, column).getConnection(connection) instanceof Node &&
              chromosome.getNode(row, column).getConnection(connection) instanceof Node) {

            assertTrue("Nodes did not connect to equivalent nodes.",
                ((Node) clone.getNode(row, column).getConnection(connection)).getRow() ==
                ((Node) chromosome.getNode(row, column).getConnection(connection)).getRow() &&

                ((Node) clone.getNode(row, column).getConnection(connection)).getColumn() ==
                ((Node) chromosome.getNode(row, column).getConnection(connection)).getColumn());

          } else {
            fail("Connection types did not match.");
          }
        }
      }
    }

    // check cloning given a known topology
    chromosome = createKnownConfiguration();
    clone = new Chromosome(chromosome);
   
    Integer[] testInputs = new Integer[] {5, 8, 4};
    chromosome.setInputs((Object[]) testInputs);
    clone.setInputs((Object[]) testInputs);

    // check that both chromosomes have the same outputs
    for (int i = 0; i < resources.outputs(); i++) {
      assertTrue("Incorrect output returned", ((Integer) chromosome.getOutput(i).calculate()) == ((Integer) clone.getOutput(i).calculate()));
    }

    // mutate an output in clone, check that the same node in chromosome produces a different output
    clone.getOutput(1).setSource(clone.getInput(2));

    assertTrue("Mutation affected nodes in both chromosomes.",
        clone.getOutput(1).calculate() != chromosome.getOutput(1).calculate());

  }
View Full Code Here

Examples of jcgp.backend.population.Chromosome

   *
   */
  @Test
  public void compareActiveTest() {
    // create a clone of the chromosome, compare active nodes - should return true
    Chromosome c = new Chromosome(chromosome);
    assertTrue("Active nodes did not match.", chromosome.compareActiveGenesTo(c));
    assertTrue("Symmetry not obeyed.", c.compareActiveGenesTo(chromosome));

    // create a new random chromosome, this time they should not match
    c = new Chromosome(resources);
    assertTrue("Active nodes did match.", !chromosome.compareActiveGenesTo(c));
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.