ec
Class Fitness

java.lang.Object
  extended by ec.Fitness
All Implemented Interfaces:
Prototype, Setup, java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
KozaFitness, MultiObjectiveFitness, SimpleFitness

public abstract class Fitness
extends java.lang.Object
implements Prototype

Fitness is a prototype which describes the fitness of an individual. Every individual contains exactly one Fitness object. Fitness objects are compared to each other with the equivalentTo() and betterThan(), etc. methods.

Rules:

comparisonmethod
a > ba.betterThan(b)
a >= ba.betterThan(b) || a.equivalentTo(b)
a = ba.equivalentTo(b)
This applies even to multiobjective pareto-style dominance, eg:

Parameter bases

fit default fitness base

Version:
1.0
Author:
Sean Luke
See Also:
Serialized Form

Field Summary
static java.lang.String FITNESS_PREAMBLE
          Basic preamble for printing Fitness values out
static java.lang.String P_FITNESS
          base parameter for defaults
 
Constructor Summary
Fitness()
           
 
Method Summary
abstract  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.
 java.lang.Object clone()
          Creates a new individual cloned from a prototype, and suitable to begin use in its own evolutionary context.
abstract  boolean equivalentTo(Fitness _fitness)
          Should return true if this fitness is in the same equivalence class as _fitness, that is, neither is clearly better or worse than the other.
abstract  float fitness()
          Should return an absolute fitness value ranging from negative infinity to infinity, NOT inclusive (thus infinity, negative infinity, and NaN are NOT valid fitness values).
 java.lang.String fitnessToString()
          Print to a string the fitness in a fashion intended to be parsed in again via readFitness(...).
 java.lang.String fitnessToStringForHumans()
          Print to a string the fitness in a fashion readable by humans, and not intended to be parsed in again.
abstract  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, java.io.PrintWriter writer)
          Should print the fitness out in a computer-readable fashion, using writer.println(...).
 void printFitnessForHumans(EvolutionState state, int log, int verbosity)
          Should print the fitness out fashion pleasing for humans to read, using state.output.println(...,verbosity,log).
 void readFitness(EvolutionState state, java.io.DataInput dataInput)
          Reads the binary form of an individual from a DataInput.
 void readFitness(EvolutionState state, java.io.LineNumberReader reader)
          Reads in the fitness from a form outputted by fitnessToString() and thus printFitnessForHumans(...).
 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.
 void writeFitness(EvolutionState state, java.io.DataOutput dataOutput)
          Writes the binary form of an individual out to a DataOutput.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface ec.Prototype
defaultBase
 

Field Detail

P_FITNESS

public static final java.lang.String P_FITNESS
base parameter for defaults

See Also:
Constant Field Values

FITNESS_PREAMBLE

public static final java.lang.String FITNESS_PREAMBLE
Basic preamble for printing Fitness values out

See Also:
Constant Field Values
Constructor Detail

Fitness

public Fitness()
Method Detail

fitness

public abstract float fitness()
Should return an absolute fitness value ranging from negative infinity to infinity, NOT inclusive (thus infinity, negative infinity, and NaN are NOT valid fitness values). This should be interpreted as: negative infinity is worse than the WORST possible fitness, and positive infinity is better than the IDEAL fitness.

You are free to restrict this range any way you like: for example, your fitness values might fall in the range [-5.32, 2.3]

Selection methods relying on fitness proportionate information will assume the fitness is non-negative and should throw an error if it is not. Thus if you plan on using FitProportionateSelection, BestSelection, or GreedyOverselection, for example, your fitnesses should assume that 0 is the worst fitness and positive fitness are better. If you're using other selection methods (Tournament selection, various ES selection procedures, etc.) your fitness values can be anything.

Similarly, if you're writing a selection method and it needs positive fitnesses, you should check for negative values and issue an error; and if your selection method doesn't need an absolute fitness value, it should use the equivalentTo() and betterThan() methods instead.

If your fitness scheme does not use a metric quantifiable to a single positive value (for example, MultiObjectiveFitness), you should perform some reasonable translation.


isIdealFitness

public abstract boolean isIdealFitness()
Should return true if this is a good enough fitness to end the run


equivalentTo

public abstract boolean equivalentTo(Fitness _fitness)
Should return true if this fitness is in the same equivalence class as _fitness, that is, neither is clearly better or worse than the other. You may assume that _fitness is of the same class as yourself. For any two fitnesses fit1 and fit2 of the same class, it must be the case that fit1.equivalentTo(fit2) == fit2.equivalentTo(fit1), and that only one of fit1.betterThan(fit2), fit1.equivalentTo(fit2), and fit2.betterThan(fit1) can be true.


betterThan

public abstract 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. For any two fitnesses fit1 and fit2 of the same class, it must be the case that fit1.equivalentTo(fit2) == fit2.equivalentTo(fit1), and that only one of fit1.betterThan(fit2), fit1.equivalentTo(fit2), and fit2.betterThan(fit1) can be true.


printFitnessForHumans

public void printFitnessForHumans(EvolutionState state,
                                  int log,
                                  int verbosity)
Should print the fitness out fashion pleasing for humans to read, using state.output.println(...,verbosity,log). The default version of this method calls fitnessToStringForHumans() and println's the resultant string.


printFitness

public void printFitness(EvolutionState state,
                         int log,
                         int verbosity)
Should print the fitness out in a computer-readable fashion, using state.output.println(...,verbosity,log). You might use ec.util.Code to encode fitness values. The default version of this method calls fitnessToString() and println's the resultant string.


printFitness

public void printFitness(EvolutionState state,
                         java.io.PrintWriter writer)
Should print the fitness out in a computer-readable fashion, using writer.println(...). You might use ec.util.Code to encode fitness values. The default version of this method calls fitnessToString() and println's the resultant string.


readFitness

public void readFitness(EvolutionState state,
                        java.io.LineNumberReader reader)
                 throws java.io.IOException
Reads in the fitness from a form outputted by fitnessToString() and thus printFitnessForHumans(...). The default version of this method exits the program with an "unimplemented" error.

Throws:
java.io.IOException

fitnessToStringForHumans

public java.lang.String fitnessToStringForHumans()
Print to a string the fitness in a fashion readable by humans, and not intended to be parsed in again. The default form simply calls toString(), but you'll probably want to override this to something else.


fitnessToString

public java.lang.String fitnessToString()
Print to a string the fitness in a fashion intended to be parsed in again via readFitness(...). 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.


writeFitness

public void writeFitness(EvolutionState state,
                         java.io.DataOutput dataOutput)
                  throws java.io.IOException
Writes the binary form of an individual out to a DataOutput. This is not for serialization: the object should only write out the data relevant to the object sufficient to rebuild it from a DataInput. The default version exits the program with an "unimplemented" error; you should override this.

Throws:
java.io.IOException

readFitness

public void readFitness(EvolutionState state,
                        java.io.DataInput dataInput)
                 throws java.io.IOException
Reads the binary form of an individual from a DataInput. This is not for serialization: the object should only read in the data written out via printIndividual(state,dataInput). The default version exits the program with an "unimplemented" error; you should override this.

Throws:
java.io.IOException

clone

public java.lang.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:

Implementations.

Specified by:
clone in interface Prototype
Overrides:
clone in class java.lang.Object

setup

public 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.

Specified by:
setup in interface Prototype
Specified by:
setup in interface Setup