001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.math3.genetics; 018 019/** 020 * Individual in a population. Chromosomes are compared based on their fitness. 021 * <p> 022 * The chromosomes are IMMUTABLE, and so their fitness is also immutable and 023 * therefore it can be cached. 024 * 025 * @since 2.0 026 */ 027public abstract class Chromosome implements Comparable<Chromosome>,Fitness { 028 /** Value assigned when no fitness has been computed yet. */ 029 private static final double NO_FITNESS = Double.NEGATIVE_INFINITY; 030 031 /** Cached value of the fitness of this chromosome. */ 032 private double fitness = NO_FITNESS; 033 034 /** 035 * Access the fitness of this chromosome. The bigger the fitness, the better the chromosome. 036 * <p> 037 * Computation of fitness is usually very time-consuming task, therefore the fitness is cached. 038 * 039 * @return the fitness 040 */ 041 public double getFitness() { 042 if (this.fitness == NO_FITNESS) { 043 // no cache - compute the fitness 044 this.fitness = fitness(); 045 } 046 return this.fitness; 047 } 048 049 /** 050 * Compares two chromosomes based on their fitness. The bigger the fitness, the better the chromosome. 051 * 052 * @param another another chromosome to compare 053 * @return 054 * <ul> 055 * <li>-1 if <code>another</code> is better than <code>this</code></li> 056 * <li>1 if <code>another</code> is worse than <code>this</code></li> 057 * <li>0 if the two chromosomes have the same fitness</li> 058 * </ul> 059 */ 060 public int compareTo(final Chromosome another) { 061 return Double.compare(getFitness(), another.getFitness()); 062 } 063 064 /** 065 * Returns <code>true</code> iff <code>another</code> has the same representation and therefore the same fitness. By 066 * default, it returns false -- override it in your implementation if you need it. 067 * 068 * @param another chromosome to compare 069 * @return true if <code>another</code> is equivalent to this chromosome 070 */ 071 protected boolean isSame(final Chromosome another) { 072 return false; 073 } 074 075 /** 076 * Searches the <code>population</code> for another chromosome with the same representation. If such chromosome is 077 * found, it is returned, if no such chromosome exists, returns <code>null</code>. 078 * 079 * @param population Population to search 080 * @return Chromosome with the same representation, or <code>null</code> if no such chromosome exists. 081 */ 082 protected Chromosome findSameChromosome(final Population population) { 083 for (Chromosome anotherChr : population) { 084 if (this.isSame(anotherChr)) { 085 return anotherChr; 086 } 087 } 088 return null; 089 } 090 091 /** 092 * Searches the population for a chromosome representing the same solution, and if it finds one, 093 * updates the fitness to its value. 094 * 095 * @param population Population to search 096 */ 097 public void searchForFitnessUpdate(final Population population) { 098 Chromosome sameChromosome = findSameChromosome(population); 099 if (sameChromosome != null) { 100 fitness = sameChromosome.getFitness(); 101 } 102 } 103 104}