001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licencing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package examples.distinctGenes;
011:
012: import org.jgap.*;
013: import org.jgap.impl.*;
014:
015: /**
016: * Sample class: Descendent of Chromosome that creates Chromosomes with n
017: * CompositeGenes. All but one CompositeGene's have 4 sub-genes, the last one
018: * has only 3 sub-genes.
019: *
020: * @author Klaus Meffert
021: * @since 3.0
022: */
023: public class MyChromosome extends Chromosome {
024: /** String containing the CVS revision. Read out via reflection!*/
025: private static final String CVS_REVISION = "$Revision: 1.2 $";
026:
027: public MyChromosome() throws InvalidConfigurationException {
028: super ();
029: }
030:
031: public MyChromosome(final Configuration a_configuration)
032: throws InvalidConfigurationException {
033: super (a_configuration);
034: }
035:
036: public MyChromosome(final Configuration a_configuration,
037: final int a_desiredSize)
038: throws InvalidConfigurationException {
039: super (a_configuration, a_desiredSize);
040: }
041:
042: public MyChromosome(final Configuration a_configuration,
043: final Gene a_sampleGene, final int a_desiredSize)
044: throws InvalidConfigurationException {
045: super (a_configuration, a_sampleGene, a_desiredSize);
046: }
047:
048: public MyChromosome(final Configuration a_configuration,
049: final Gene[] a_initialGenes)
050: throws InvalidConfigurationException {
051: super (a_configuration, a_initialGenes);
052: }
053:
054: public MyChromosome(final Configuration a_configuration,
055: Gene a_sampleGene, int a_desiredSize,
056: IGeneConstraintChecker a_constraintChecker)
057: throws InvalidConfigurationException {
058: this (a_configuration, a_sampleGene, a_desiredSize);
059: }
060:
061: public boolean isHandlerFor(Object a_obj, Class a_class) {
062: if (a_class == MyChromosome.class) {
063: return true;
064: } else {
065: return false;
066: }
067: }
068:
069: /**{@inheritDoc}*/
070: public Object perform(Object a_obj, Class a_class, Object a_params)
071: throws Exception {
072: return randomInitialMyChromosome(getConfiguration());
073: }
074:
075: public static IChromosome randomInitialMyChromosome(
076: Configuration a_configuration)
077: throws InvalidConfigurationException {
078: // Sanity check: make sure the given configuration isn't null.
079: // -----------------------------------------------------------
080: if (a_configuration == null) {
081: throw new IllegalArgumentException(
082: "Configuration instance must not be null");
083: }
084: // Lock the configuration settings so that they can't be changed
085: // from now on.
086: // -------------------------------------------------------------
087: a_configuration.lockSettings();
088: // First see if we can get a Chromosome instance from the pool.
089: // If we can, we'll randomize its gene values (alleles) and then
090: // return it.
091: // -------------------------------------------------------------
092: IChromosomePool pool = a_configuration.getChromosomePool();
093: if (pool != null) {
094: IChromosome randomChromosome = pool.acquireChromosome();
095: if (randomChromosome != null) {
096: Gene[] genes = randomChromosome.getGenes();
097: RandomGenerator generator = a_configuration
098: .getRandomGenerator();
099: for (int i = 0; i < genes.length; i++) {
100: genes[i].setToRandomValue(generator);
101: }
102: randomChromosome
103: .setFitnessValueDirectly(FitnessFunction.NO_FITNESS_VALUE);
104: return randomChromosome;
105: }
106: }
107: // We weren't able to get a Chromosome from the pool, so we have to
108: // construct a new instance and build it from scratch.
109: // ------------------------------------------------------------------
110: IChromosome sampleChromosome = a_configuration
111: .getSampleChromosome();
112: sampleChromosome
113: .setFitnessValue(FitnessFunction.NO_FITNESS_VALUE);
114: Gene[] sampleGenes = sampleChromosome.getGenes();
115: Gene[] newGenes = new Gene[sampleGenes.length];
116: RandomGenerator generator = a_configuration
117: .getRandomGenerator();
118: // All genes except the last one should contain 4 fields.
119: // ------------------------------------------------------
120: for (int i = 0; i < newGenes.length - 1; i++) {
121: CompositeGene newGene = new CompositeGene(a_configuration);
122: for (int j = 0; j < 4; j++) {
123: Gene field = new BooleanGene(a_configuration);
124: newGene.addGene(field);
125: }
126: newGenes[i] = newGene; //sampleGenes[i].newGene();
127: // Set the gene's value (allele) to a random value.
128: // ------------------------------------------------
129: newGenes[i].setToRandomValue(generator);
130: }
131: // The last gene should contain 3 fields only.
132: // -------------------------------------------
133: CompositeGene newGene = new CompositeGene(a_configuration);
134: for (int j = 0; j < 3; j++) {
135: Gene field = new BooleanGene(a_configuration);
136: newGene.addGene(field);
137: newGene.setToRandomValue(generator);
138: }
139: newGenes[newGenes.length - 1] = newGene;
140: // Finally, construct the new chromosome with the new random
141: // genes values and return it.
142: // ---------------------------------------------------------
143: return new MyChromosome(a_configuration, newGenes);
144: }
145: }
|