Chromosome.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.math4.legacy.genetics;

  18. /**
  19.  * Individual in a population. Chromosomes are compared based on their fitness.
  20.  * <p>
  21.  * The chromosomes are IMMUTABLE, and so their fitness is also immutable and
  22.  * therefore it can be cached.
  23.  *
  24.  * @since 2.0
  25.  */
  26. public abstract class Chromosome implements Comparable<Chromosome>,Fitness {
  27.     /** Value assigned when no fitness has been computed yet. */
  28.     private static final double NO_FITNESS = Double.NEGATIVE_INFINITY;

  29.     /** Cached value of the fitness of this chromosome. */
  30.     private double fitness = NO_FITNESS;

  31.     /**
  32.      * Access the fitness of this chromosome. The bigger the fitness, the better the chromosome.
  33.      * <p>
  34.      * Computation of fitness is usually very time-consuming task, therefore the fitness is cached.
  35.      *
  36.      * @return the fitness
  37.      */
  38.     public double getFitness() {
  39.         if (this.fitness == NO_FITNESS) {
  40.             // no cache - compute the fitness
  41.             this.fitness = fitness();
  42.         }
  43.         return this.fitness;
  44.     }

  45.     /**
  46.      * Compares two chromosomes based on their fitness. The bigger the fitness, the better the chromosome.
  47.      *
  48.      * @param another another chromosome to compare
  49.      * @return
  50.      * <ul>
  51.      *   <li>-1 if <code>another</code> is better than <code>this</code></li>
  52.      *   <li>1 if <code>another</code> is worse than <code>this</code></li>
  53.      *   <li>0 if the two chromosomes have the same fitness</li>
  54.      * </ul>
  55.      */
  56.     @Override
  57.     public int compareTo(final Chromosome another) {
  58.         return Double.compare(getFitness(), another.getFitness());
  59.     }

  60.     /**
  61.      * Returns <code>true</code> iff <code>another</code> has the same representation and therefore the same fitness. By
  62.      * default, it returns false -- override it in your implementation if you need it.
  63.      *
  64.      * @param another chromosome to compare
  65.      * @return true if <code>another</code> is equivalent to this chromosome
  66.      */
  67.     protected boolean isSame(final Chromosome another) {
  68.         return false;
  69.     }

  70.     /**
  71.      * Searches the <code>population</code> for another chromosome with the same representation. If such chromosome is
  72.      * found, it is returned, if no such chromosome exists, returns <code>null</code>.
  73.      *
  74.      * @param population Population to search
  75.      * @return Chromosome with the same representation, or <code>null</code> if no such chromosome exists.
  76.      */
  77.     protected Chromosome findSameChromosome(final Population population) {
  78.         for (Chromosome anotherChr : population) {
  79.             if (this.isSame(anotherChr)) {
  80.                 return anotherChr;
  81.             }
  82.         }
  83.         return null;
  84.     }

  85.     /**
  86.      * Searches the population for a chromosome representing the same solution, and if it finds one,
  87.      * updates the fitness to its value.
  88.      *
  89.      * @param population Population to search
  90.      */
  91.     public void searchForFitnessUpdate(final Population population) {
  92.         Chromosome sameChromosome = findSameChromosome(population);
  93.         if (sameChromosome != null) {
  94.             fitness = sameChromosome.getFitness();
  95.         }
  96.     }
  97. }