Package ec.rule

Class RuleIndividual

java.lang.Object
ec.Individual
ec.rule.RuleIndividual
All Implemented Interfaces:
Prototype, Setup, Serializable, Cloneable, Comparable<Individual>

public class RuleIndividual extends Individual
RuleIndividual is an Individual with an array of RuleSets, each of which is a set of Rules. RuleIndividuals belong to some subclass of RuleSpecies (or just RuleSpecies itself).

RuleIndividuals really have basically one parameter: the number of RuleSets to use. This is determined by the num-rulesets parameter.

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. readIndividualreads 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 Rulesets. If you add instance variables to a RuleIndividual or subclass, you'll need to read/write those variables as well.

Parameters

base.num-rulesets
int >= 1
(number of rulesets used)
base.ruleset.n
Classname, subclass of or = ec.rule.RuleSet
(class of ruleset n)

Parameter bases

base.ruleset.n
RuleSet n

Default Base
rule.individual

See Also:
  • Field Details

  • Constructor Details

    • RuleIndividual

      public RuleIndividual()
  • 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
    • preprocessIndividual

      public void preprocessIndividual(EvolutionState state, int thread)
      Called by pipelines before they've modified the individual and it might need to be "fixed" -- basically a hook for you to override. By default, calls validateRules on each ruleset.
    • postprocessIndividual

      public void postprocessIndividual(EvolutionState state, int thread)
      Called by pipelines after they've modified the individual and it might need to be "fixed" -- basically a hook for you to override. By default, calls validateRules on each ruleset.
    • 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
    • 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
    • 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:
    • printIndividualForHumans

      public void printIndividualForHumans(EvolutionState state, int log)
      Description copied from class: Individual
      Should print the individual out in a pleasing way for humans, with a verbosity of Output.V_NO_GENERAL.
      Overrides:
      printIndividualForHumans in class Individual
    • printIndividual

      public void printIndividual(EvolutionState state, int log)
      Description copied from class: Individual
      Should print the individual in a way that can be read by computer, including its fitness, with a verbosity of Output.V_NO_GENERAL.
      Overrides:
      printIndividual in class Individual
    • printIndividual

      public void printIndividual(EvolutionState state, PrintWriter writer)
      Overridden for the RuleIndividual genotype, writing each ruleset in turn.
      Overrides:
      printIndividual in class Individual
    • writeGenotype

      public void writeGenotype(EvolutionState state, DataOutput dataOutput) throws IOException
      Overridden for the RuleIndividual genotype, writing each ruleset in turn.
      Overrides:
      writeGenotype in class Individual
      Throws:
      IOException
    • readGenotype

      public void readGenotype(EvolutionState state, DataInput dataInput) throws IOException
      Overridden for the RuleIndividual genotype.
      Overrides:
      readGenotype in class Individual
      Throws:
      IOException
    • parseGenotype

      public void parseGenotype(EvolutionState state, LineNumberReader reader) throws IOException
      Overridden for the RuleIndividual genotype.
      Overrides:
      parseGenotype in class Individual
      Throws:
      IOException
    • size

      public long size()
      Description copied from class: Individual
      Returns the "size" of the individual. This is used for things like parsimony pressure. The default form of this method returns 0 -- if you care about parsimony pressure, you'll need to override the default to provide a more descriptive measure of size.
      Overrides:
      size in class Individual
    • reset

      public void reset(EvolutionState state, int thread)
    • mutate

      public void mutate(EvolutionState state, int thread)
      Mutates the Individual. The default implementation simply calls mutate(...) on each of the RuleSets.