Package ec.vector

Class GeneVectorIndividual

All Implemented Interfaces:
Prototype, Setup, Serializable, Cloneable, Comparable<Individual>
Direct Known Subclasses:
NEATIndividual

public class GeneVectorIndividual extends VectorIndividual
GeneVectorIndividual is a VectorIndividual whose genome is an array of Genes. The default mutation method calls the mutate() method on each gene independently with species.mutationProbability. Initialization calls reset(), which should call reset() on each gene. Do not expect that the genes will actually exist during initialization -- see the default implementation of reset() as an example for how to handle this.

From ec.Individual:

In addition to serialization for checkpointing, Individuals may read and write themselves to streams in three ways.

  • writeIndividual(...,DataOutput)/readIndividual(...,DataInput)   This method transmits or receives an individual in binary. It is the most efficient approach to sending individuals over networks, etc. These methods write the evaluated flag and the fitness, then call readGenotype/writeGenotype, which you must implement to write those parts of your Individual special to your functions-- the default versions of readGenotype/writeGenotype throw errors. You don't need to implement them if you don't plan on using read/writeIndividual.
  • printIndividual(...,PrintWriter)/readIndividual(...,LineNumberReader)   This approach transmits or receives an indivdual in text encoded such that the individual is largely readable by humans but can be read back in 100% by ECJ as well. To do this, these methods will encode numbers using the ec.util.Code class. These methods are mostly used to write out populations to files for inspection, slight modification, then reading back in later on. readIndividual reads in the fitness and the evaluation flag, then calls parseGenotype to read in the remaining individual. You are responsible for implementing parseGenotype: the Code class is there to help you. printIndividual writes out the fitness and evaluation flag, then calls genotypeToString and printlns the resultant string. You are responsible for implementing the genotypeToString method in such a way that parseGenotype can read back in the individual println'd with genotypeToString. The default form of genotypeToString simply calls toString, which you may override instead if you like. The default form of parseGenotype throws an error. You are not required to implement these methods, but without them you will not be able to write individuals to files in a simultaneously computer- and human-readable fashion.
  • printIndividualForHumans(...,PrintWriter)   This approach prints an individual in a fashion intended for human consumption only. printIndividualForHumans writes out the fitness and evaluation flag, then calls genotypeToStringForHumans and printlns the resultant string. You are responsible for implementing the genotypeToStringForHumans method. The default form of genotypeToStringForHumans simply calls toString, which you may override instead if you like (though note that genotypeToString's default also calls toString). You should handle one of these methods properly to ensure individuals can be printed by ECJ.

In general, the various readers and writers do three things: they tell the Fitness to read/write itself, they read/write the evaluated flag, and they read/write the gene array. If you add instance variables to a VectorIndividual or subclass, you'll need to read/write those variables as well.

Default Base
vector.gene-vect-ind

See Also:
  • Field Details

  • Constructor Details

    • GeneVectorIndividual

      public GeneVectorIndividual()
  • Method Details

    • defaultBase

      public Parameter defaultBase()
      Description copied from interface: Prototype
      Returns the default base for this prototype. This should generally be implemented by building off of the static base() method on the DefaultsForm object for the prototype's package. This should be callable during setup(...).
    • clone

      public Object clone()
      Description copied from interface: Prototype
      Creates a new individual cloned from a prototype, and suitable to begin use in its own evolutionary context.

      Typically this should be a full "deep" clone. However, you may share certain elements with other objects rather than clone hem, depending on the situation:

      • If you hold objects which are shared with other instances, don't clone them.
      • If you hold objects which must be unique, clone them.
      • If you hold objects which were given to you as a gesture of kindness, and aren't owned by you, you probably shouldn't clone them.
      • DON'T attempt to clone: Singletons, Cliques, or Populations, or Subpopulation.
      • Arrays are not cloned automatically; you may need to clone an array if you're not sharing it with other instances. Arrays have the nice feature of being copyable by calling clone() on them.

      Implementations.

      • If no ancestor of yours implements clone(), and you have no need to do clone deeply, and you are abstract, then you should not declare clone().
      • If no ancestor of yours implements clone(), and you have no need to do clone deeply, and you are not abstract, then you should implement it as follows:

         public Object clone() 
             {
             try
                 { 
                 return super.clone();
                 }
             catch ((CloneNotSupportedException e)
                 { throw new InternalError(); } // never happens
             }
                
      • If no ancestor of yours implements clone(), but you need to deep-clone some things, then you should implement it as follows:

         public Object clone() 
             {
             try
                 { 
                 MyObject myobj = (MyObject) (super.clone());
        
                 // put your deep-cloning code here...
                 }
             catch ((CloneNotSupportedException e)
                 { throw new InternalError(); } // never happens
             return myobj;
             } 
                
      • If an ancestor has implemented clone(), and you also need to deep clone some things, then you should implement it as follows:

         public Object clone() 
             { 
             MyObject myobj = (MyObject) (super.clone());
        
             // put your deep-cloning code here...
        
             return myobj;
             } 
                
      Specified by:
      clone in interface Prototype
      Overrides:
      clone in class Individual
    • setup

      public void setup(EvolutionState state, Parameter base)
      Description copied from class: Individual
      This should be used to set up only those things which you share in common with all other individuals in your species; individual-specific items which make you you should be filled in by Species.newIndividual(...), and modified by breeders.
      Specified by:
      setup in interface Prototype
      Specified by:
      setup in interface Setup
      Overrides:
      setup in class Individual
      See Also:
    • defaultCrossover

      public void defaultCrossover(EvolutionState state, int thread, VectorIndividual ind)
      Description copied from class: VectorIndividual
      Destructively crosses over the individual with another in some default manner. In most implementations provided in ECJ, one-, two-, and any-point crossover is done with a for loop, rather than a possibly more efficient approach like arrayCopy(). The disadvantage is that arrayCopy() takes advantage of a CPU's bulk copying. The advantage is that arrayCopy() would require a scratch array, so you'd be allocing and GCing an array for every crossover. Dunno which is more efficient.
      Overrides:
      defaultCrossover in class VectorIndividual
    • split

      public void split(int[] points, Object[] pieces)
      Splits the genome into n pieces, according to points, which *must* be sorted. pieces.length must be 1 + points.length
      Overrides:
      split in class VectorIndividual
    • join

      public void join(Object[] pieces)
      Joins the n pieces and sets the genome to their concatenation.
      Overrides:
      join in class VectorIndividual
    • defaultMutate

      public void defaultMutate(EvolutionState state, int thread)
      Destructively mutates the individual in some default manner. The default form simply randomizes genes to a uniform distribution from the min and max of the gene values.
      Overrides:
      defaultMutate in class VectorIndividual
    • reset

      public void reset(EvolutionState state, int thread)
      Initializes the individual by calling reset(...) on each gene.
      Specified by:
      reset in class VectorIndividual
    • hashCode

      public int hashCode()
      Description copied from class: Individual
      Returns a hashcode for the individual, such that individuals which are equals(...) each other always return the same hash code.
      Specified by:
      hashCode in class Individual
    • genotypeToStringForHumans

      public String genotypeToStringForHumans()
      Description copied from class: Individual
      Print to a string the genotype of the Individual in a fashion readable by humans, and not intended to be parsed in again. The fitness and evaluated flag should not be included. The default form simply calls toString(), but you'll probably want to override this to something else.
      Overrides:
      genotypeToStringForHumans in class Individual
    • genotypeToString

      public String genotypeToString()
      Description copied from class: Individual
      Print to a string the genotype of the Individual in a fashion intended to be parsed in again via parseGenotype(...). The fitness and evaluated flag should not be included. The default form simply calls toString(), which is almost certainly wrong, and you'll probably want to override this to something else.
      Overrides:
      genotypeToString in class Individual
    • parseGenotype

      protected void parseGenotype(EvolutionState state, LineNumberReader reader) throws IOException
      Description copied from class: Individual
      This method is used only by the default version of readIndividual(state,reader), and it is intended to be overridden to parse in that part of the individual that was outputted in the genotypeToString() method. The default version of this method exits the program with an "unimplemented" error. You'll want to override this method, or to override readIndividual(...) to not use this method.
      Overrides:
      parseGenotype in class Individual
      Throws:
      IOException
    • equals

      public boolean equals(Object ind)
      Description copied from class: Individual
      Returns true if I am genetically "equal" to ind. This should mostly be interpreted as saying that we are of the same class and that we hold the same data. It should NOT be a pointer comparison.
      Specified by:
      equals in class Individual
    • getGenome

      public Object getGenome()
      Description copied from class: VectorIndividual
      Returns the gene array. If you know the type of the array, you can cast it and work on it directly. Otherwise, you can still manipulate it in general, because arrays (like all objects) respond to clone() and can be manipulated with arrayCopy without bothering with their type. This might be useful in creating special generalized crossover operators -- we apologize in advance for the fact that Java doesn't have a template system. :-( The default version returns null.
      Overrides:
      getGenome in class VectorIndividual
    • setGenome

      public void setGenome(Object gen)
      Description copied from class: VectorIndividual
      Sets the gene array. See getGenome(). The default version does nothing.
      Overrides:
      setGenome in class VectorIndividual
    • genomeLength

      public int genomeLength()
      Description copied from class: VectorIndividual
      Returns the length of the gene array. By default, this method returns 0.
      Overrides:
      genomeLength in class VectorIndividual
    • cloneGenes

      public void cloneGenes(Object piece)
      Description copied from class: VectorIndividual
      Clones the genes in pieces, and replaces the genes with their copies. Does NOT copy the array, but modifies it in place. If the VectorIndividual holds numbers or booleans etc. instead of genes, nothing is cloned (why bother?).
      Overrides:
      cloneGenes in class VectorIndividual
    • writeGenotype

      public void writeGenotype(EvolutionState state, DataOutput dataOutput) throws IOException
      Description copied from class: Individual
      Writes the genotypic information to a DataOutput. Largely called by writeIndividual(), and nothing else. The default simply throws an error. Various subclasses of Individual override this as appropriate. For example, if your custom individual's genotype consists of an array of integers, you might do this:
      
       dataOutput.writeInt(integers.length);
       for(int x=0;xinvalid input: '<'integers.length;x++)
           dataOutput.writeInt(integers[x]);
       
      Overrides:
      writeGenotype in class Individual
      Throws:
      IOException
    • setGenomeLength

      public void setGenomeLength(int len)
      Description copied from class: VectorIndividual
      Sets the genome length. If the length is longer, then it is filled with a default value (likely 0 or false). This may or may not be a valid value -- you will need to set appropriate values here. The default implementation does nothing; but all subclasses in ECJ implement a subset of this.
      Overrides:
      setGenomeLength in class VectorIndividual
    • readGenotype

      public void readGenotype(EvolutionState state, DataInput dataInput) throws IOException
      Description copied from class: Individual
      Reads in the genotypic information from a DataInput, erasing the previous genotype of this Individual. Largely called by readIndividual(), and nothing else. If you are trying to create an Individual from information read in from a stream or DataInput, see the various newIndividual() methods in Species. The default simply throws an error. Various subclasses of Individual override this as appropriate. For example, if your custom individual's genotype consists of an array of integers, you might do this:
      
       integers = new int[dataInput.readInt()];
       for(int x=0;xinvalid input: '<'integers.length;x++)
           integers[x] = dataInput.readInt();
       
      Overrides:
      readGenotype in class Individual
      Throws:
      IOException