ec.gp.koza
Class KozaFitness

java.lang.Object
  |
  +--ec.gp.koza.KozaFitness

public class KozaFitness
extends java.lang.Object
implements Fitness

KozaFitness is a Fitness which stores an individual's fitness as described in Koza I. Well, almost. In KozaFitness, standardized fitness and raw fitness are considered the same (there are different methods for them, but they return the same thing). Standardized fitness ranges from 0.0 inclusive (the best) to infinity exclusive (the worst). Adjusted fitness converts this, using the formula adj_f = 1/(1+f), into a scale from 0.0 exclusive (worst) to 1.0 inclusive (best). While it's the standardized fitness that is stored, it is the adjusted fitness that is printed out. This is all just convenience stuff anyway; selection methods generally don't use these fitness values but instead use the betterThan and equalTo methods.

Default Base
gp.koza.fitness

See Also:
Serialized Form

Field Summary
protected  float fitness
          This ranges from 0 (best) to infinity (worst).
static java.lang.String FITNESS_PREAMBLE
           
 int hits
          This auxillary measure is used in some problems for additional information.
static java.lang.String P_KOZAFITNESS
           
 
Fields inherited from interface ec.Fitness
P_FITNESS
 
Constructor Summary
KozaFitness()
           
 
Method Summary
 float adjustedFitness()
          Returns the adjusted fitness metric, which recasts the fitness to the half-open interval (0,1], where 1 is ideal and 0 is worst.
 boolean betterThan(Fitness _fitness)
          Should return true if this fitness is clearly better than _fitness; You may assume that _fitness is of the same class as yourself.
 Parameter defaultBase()
          Returns the default base for this prototype.
 boolean equivalentTo(Fitness _fitness)
          Should return true if this fitness is in the same equivalence class as _fitness, that is, neither is clearly bettter or worse than the other.
 float fitness()
          Returns the adjusted fitness metric, which recasts the fitness to the half-open interval (0,1], where 1 is ideal and 0 is worst.
 boolean isIdealFitness()
          Should return true if this is a good enough fitness to end the run
 void printFitness(EvolutionState state, int log, int verbosity)
          Should print the fitness out in a computer-readable fashion, using state.output.println(...,verbosity,log).
 void printFitness(EvolutionState state, int thread, java.io.PrintWriter writer)
          Should print the fitness out in a computer-readable fashion, using state.output.println(...,verbosity,log).
 void printFitnessForHumans(EvolutionState state, int log, int verbosity)
          Should print the fitness out in a pleasing way to humans, using state.output.println(...,verbosity,log)
 java.lang.Object protoClone()
          Creates a new individual cloned from a prototype, and suitable to begin use in its own evolutionary context.
 java.lang.Object protoCloneSimple()
          This should be implemented in a the top-level Prototype ONLY; in fact, it should probably be declared final.
 float rawFitness()
          Returns the raw fitness metric.
 void readFitness(EvolutionState state, int thread, java.io.LineNumberReader reader)
          Reads in the fitness from a form printed by printFitness().
 void setFitness(EvolutionState state, float _f)
           
 void setup(EvolutionState state, Parameter base)
          Sets up the object by reading it from the parameters stored in state, built off of the parameter base base.
 float standardizedFitness()
          Returns the standardized fitness metric, which is the same as the raw fitness metric in this scheme.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

P_KOZAFITNESS

public static final java.lang.String P_KOZAFITNESS

FITNESS_PREAMBLE

public static final java.lang.String FITNESS_PREAMBLE

fitness

protected float fitness
This ranges from 0 (best) to infinity (worst). Koza leaves the exact definition of rawFitness up to the domain problem, but I define it here as equivalent to the standardized fitness, hence the simple definitions of rawFitness() and standardizedFitness() below.

hits

public int hits
This auxillary measure is used in some problems for additional information. It's a traditional feature of Koza-style GP, and so although I think it's not very useful, I'll leave it in anyway.
Constructor Detail

KozaFitness

public KozaFitness()
Method Detail

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(...).

protoClone

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

The question here is whether or not this means to perform a "deep" or "light" ("shallow") clone, or something in-between. You may need to deep-clone parts of your object rather than simply copying their references, depending on the situation:

Implementations.

If you know that your superclasses will never change their protoClone() implementations, you might try inlining them in your overridden protoClone() method. But this is dangerous (though it yields a small net increase).

In general, you want to keep your deep cloning to an absolute minimum, so that you don't have to call protoClone() but one time.

The approach taken here is the fastest that I am aware of while still permitting objects to be specified at runtime from a parameter file. It would be faster to use the "new" operator; but that would require hard-coding that we can't do. Although using java.lang.Object.clone() entails an extra layer that deals with stripping away the "protected" keyword and also wrapping the exception handling (which is a BIG hit, about three times as slow as using "new"), it's still MUCH faster than using java.lang.Class.newInstance(), and also much faster than rolling our own Clone() method.


protoCloneSimple

public final java.lang.Object protoCloneSimple()
Description copied from interface: Prototype
This should be implemented in a the top-level Prototype ONLY; in fact, it should probably be declared final. It should be implemented as follows:

public final Object protoCloneSimple()
{
try { return protoClone(); }
catch (CloneNotSupportedException e) 
{ throw new InternalError(); } // never happens
} 

setFitness

public final void setFitness(EvolutionState state,
                             float _f)

fitness

public final float fitness()
Returns the adjusted fitness metric, which recasts the fitness to the half-open interval (0,1], where 1 is ideal and 0 is worst. Same as adjustedFitness(). This adheres to Fitness.java's protocol for this method.
Specified by:
fitness in interface Fitness

rawFitness

public final float rawFitness()
Returns the raw fitness metric.

standardizedFitness

public final float standardizedFitness()
Returns the standardized fitness metric, which is the same as the raw fitness metric in this scheme.

adjustedFitness

public final float adjustedFitness()
Returns the adjusted fitness metric, which recasts the fitness to the half-open interval (0,1], where 1 is ideal and 0 is worst. This metric is used when printing the fitness out.

setup

public final void setup(EvolutionState state,
                        Parameter base)
Description copied from interface: Prototype
Sets up the object by reading it from the parameters stored in state, built off of the parameter base base. If an ancestor implements this method, be sure to call super.setup(state,base); before you do anything else.

For prototypes, setup(...) is typically called once for the prototype instance; cloned instances do not receive the setup(...) call. setup(...) may be called more than once; the only guarantee is that it will get called at least once on an instance or some "parent" object from which it was ultimately cloned.


isIdealFitness

public final boolean isIdealFitness()
Description copied from interface: Fitness
Should return true if this is a good enough fitness to end the run
Specified by:
isIdealFitness in interface Fitness

equivalentTo

public final boolean equivalentTo(Fitness _fitness)
Description copied from interface: Fitness
Should return true if this fitness is in the same equivalence class as _fitness, that is, neither is clearly bettter or worse than the other. You may assume that _fitness is of the same class as yourself. worseThan(), equivalentTo() and betterThan() should be disjoint sets.
Specified by:
equivalentTo in interface Fitness

betterThan

public final boolean betterThan(Fitness _fitness)
Description copied from interface: Fitness
Should return true if this fitness is clearly better than _fitness; You may assume that _fitness is of the same class as yourself. worseThan(), equivalentTo() and betterThan() should be disjoint sets.
Specified by:
betterThan in interface Fitness

printFitness

public final void printFitness(EvolutionState state,
                               int log,
                               int verbosity)
Description copied from interface: Fitness
Should print the fitness out in a computer-readable fashion, using state.output.println(...,verbosity,log). You should use ec.util.Code to encode fitness values.
Specified by:
printFitness in interface Fitness

printFitness

public final void printFitness(EvolutionState state,
                               int thread,
                               java.io.PrintWriter writer)
Description copied from interface: Fitness
Should print the fitness out in a computer-readable fashion, using state.output.println(...,verbosity,log). You should use ec.util.Code to encode fitness values. Usually you should try to use printFitness(state,log,verbosity) instead -- use this method only if you can't print through the Output facility for some reason.
Specified by:
printFitness in interface Fitness

printFitnessForHumans

public final void printFitnessForHumans(EvolutionState state,
                                        int log,
                                        int verbosity)
Description copied from interface: Fitness
Should print the fitness out in a pleasing way to humans, using state.output.println(...,verbosity,log)
Specified by:
printFitnessForHumans in interface Fitness

readFitness

public final void readFitness(EvolutionState state,
                              int thread,
                              java.io.LineNumberReader reader)
                       throws java.io.IOException,
                              java.lang.CloneNotSupportedException
Description copied from interface: Fitness
Reads in the fitness from a form printed by printFitness().
Specified by:
readFitness in interface Fitness