Package ec

# Class Fitness

java.lang.Object
ec.Fitness

All Implemented Interfaces:
  `Prototype`, `Setup`, `Serializable`, `Cloneable`, `Comparable<Object>`

Direct Known Subclasses:
  `KozaFitness`, `MultiObjectiveFitness`, `SimpleFitness`

public abstract class Fitness extends Object implements Prototype, Comparable<Object>
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:

comparison | method
a > b | a.betterThan(b)
a >= b | a.betterThan(b) || a.equivalentTo(b)
a = b | a.equivalentTo(b)

This applies even to multiobjective pareto-style dominance, eg:

- a dominates b :: a > b
- a and b do not dominate each other :: a = b
- b dominates a :: a < b

Parameter bases

`fit` | default fitness base

See Also:
  - Serialized Form

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `Individual[]`
  `context`
  Auxiliary variable, used by coevolutionary processes, to store the individuals involved in producing this given Fitness value.
  `static final String`
  `FITNESS_PREAMBLE`
  Basic preamble for printing Fitness values out
  `static final String`
  `P_FITNESS`
  base parameter for defaults
  `ArrayList`
  `trials`
  Auxiliary variable, used by coevolutionary processes, to compute the number of trials used to compute this Fitness value.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `Fitness()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `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.
  `Object`
  `clone()`
  Creates a new individual cloned from a prototype, and suitable to begin use in its own evolutionary context.
  `int`
  `compareTo(Object o)`
  Returns -1 if I am FITTER than the other Fitness, 1 if I am LESS FIT than the other Fitness, and 0 if we are equivalent.
  `boolean`
  `contextIsBetterThan(Fitness other)`
  Given another Fitness, returns true if the trial which produced my current context is "better" in fitness than the trial which produced his current context, and thus should be retained in lieu of his.
  `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 double`
  `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).
  `String`
  `fitnessToString()`
  Print to a string the fitness in a fashion intended to be parsed in again via readFitness(...).
  `String`
  `fitnessToStringForHumans()`
  Print to a string the fitness in a fashion readable by humans, and not intended to be parsed in again.
  `Individual[]`
  `getContext()`
  Treat the Individual[] you receive from this as read-only.
  `abstract boolean`
  `isIdealFitness()`
  Should return true if this is a good enough fitness to end the run
  `void`
  `merge(EvolutionState state, Fitness other)`
  Merges the other fitness into this fitness.
  `void`
  `printFitness(EvolutionState state, int log)`
  Should print the fitness out in a computer-readable fashion, with a verbosity of Output.V_NO_GENERAL.
  `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, PrintWriter writer)`
  Should print the fitness out in a computer-readable fashion, using writer.println(...).
  `void`
  `printFitnessForHumans(EvolutionState state, int log)`
  Should print the fitness out fashion pleasing for humans to read, with a verbosity of Output.V_NO_GENERAL.
  `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, DataInput dataInput)`
  Reads the binary form of an individual from a DataInput.
  `void`
  `readFitness(EvolutionState state, LineNumberReader reader)`
  Reads in the fitness from a form outputted by fitnessToString() and thus printFitnessForHumans(...).
  `void`
  `readTrials(EvolutionState state, DataInput dataInput)`
  Reads trials in from DataInput.
  `void`
  `setContext(Individual[] cont)`
  `void`
  `setContext(Individual[] cont, int index)`
  `void`
  `setToBestOf(EvolutionState state, Fitness[] fitnesses)`
  Sets the fitness to be the same value as the best of the provided fitnesses.
  `void`
  `setToMeanOf(EvolutionState state, Fitness[] fitnesses)`
  Sets the fitness to be the same value as the mean of the provided fitnesses.
  `void`
  `setToMedianOf(EvolutionState state, Fitness[] fitnesses)`
  Sets the fitness to be the median of the provided fitnesses.
  `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, DataOutput dataOutput)`
  Writes the binary form of an individual out to a DataOutput.
  `void`
  `writeTrials(EvolutionState state, DataOutput dataOutput)`
  Writes trials out to 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 Details

  - ### trials

    public ArrayList trials
    Auxiliary variable, used by coevolutionary processes, to compute the number of trials used to compute this Fitness value. By default trials=null and stays that way. If you set this variable, all of the elements of the ArrayList must be immutable -- once they're set they never change internally.

  - ### context

    public Individual[] context
    Auxiliary variable, used by coevolutionary processes, to store the individuals involved in producing this given Fitness value. By default context=null and stays that way. Note that individuals stored here may possibly not themselves have Fitness values to avoid circularity when cloning.

  - ### P_FITNESS

    public static final String P_FITNESS
    base parameter for defaults
    See Also:
      - Constant Field Values

  - ### FITNESS_PREAMBLE

    public static final String FITNESS_PREAMBLE
    Basic preamble for printing Fitness values out
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### Fitness

    public Fitness()

- ## Method Details

  - ### setContext

    public void setContext(Individual[] cont, int index)

  - ### setContext

    public void setContext(Individual[] cont)

  - ### getContext

    public Individual[] getContext()
    Treat the Individual[] you receive from this as read-only.

  - ### fitness

    public abstract double 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)
    Should print the fitness out fashion pleasing for humans to read, with a verbosity of Output.V_NO_GENERAL.

  - ### 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(), adds context (collaborators) if any, and printlns the resultant string.

  - ### printFitness

    public void printFitness(EvolutionState state, int log)
    Should print the fitness out in a computer-readable fashion, with a verbosity of Output.V_NO_GENERAL.

  - ### 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, 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, LineNumberReader reader) throws 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:
      `IOException`

  - ### fitnessToStringForHumans

    public 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 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. When overriding, you may wish to check to see if the 'trials' variable is non-null, and issue an error if so.

  - ### writeFitness

    public void writeFitness(EvolutionState state, DataOutput dataOutput) throws 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, and be certain to also write the 'trials' variable as well.
    Throws:
      `IOException`

  - ### writeTrials

    public void writeTrials(EvolutionState state, DataOutput dataOutput) throws IOException
    Writes trials out to DataOutput
    Throws:
      `IOException`

  - ### readFitness

    public void readFitness(EvolutionState state, DataInput dataInput) throws 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, and be certain to also write the 'trials' variable as well.
    Throws:
      `IOException`

  - ### readTrials

    public void readTrials(EvolutionState state, DataInput dataInput) throws IOException
    Reads trials in from DataInput.
    Throws:
      `IOException`

  - ### contextIsBetterThan

    public boolean contextIsBetterThan(Fitness other)
    Given another Fitness, returns true if the trial which produced my current context is "better" in fitness than the trial which produced his current context, and thus should be retained in lieu of his. This method by default assumes that trials are Doubles, and that higher Doubles are better. If you are using distributed evaluation and coevolution and your tirals are otherwise, you need to override this method.

  - ### merge

    public void merge(EvolutionState state, Fitness other)
    Merges the other fitness into this fitness. May destroy the other Fitness in the process. This method is typically called by coevolution in combination with distributed evauation where the Individual may be sent to various different sites to have trials performed on it, and the results must be merged together to form a relevant fitness. By default merging occurs as follows. First, the trials arrays are concatenated. Then whoever has the best trial has his context retained: this Fitness is determined by calling contextIsBetterThan(other). By default that method assumes that trials are Doubles, and that higher values are better. You will wish to override that method if trials are different. In coevolution nothing else needs to be merged usually, though you may need to override this to handle other things specially.

    This method only works properly if the other Fitness had its trials deleted before it was sent off for evaluation on a remote machine: thus all of the trials are new and can be concatenated in. This is what sim.eval.Job presently does in its method copyIndividualsForward().

  - ### 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 `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`

  - ### compareTo

    public int compareTo(Object o)
    Returns -1 if I am FITTER than the other Fitness, 1 if I am LESS FIT than the other Fitness, and 0 if we are equivalent.
    Specified by:
      `compareTo` in interface `Comparable<Object>`

  - ### setToBestOf

    public void setToBestOf(EvolutionState state, Fitness[] fitnesses)
    Sets the fitness to be the same value as the best of the provided fitnesses. This method calls setToMeanOf(...), so if that method is unimplemented, this method will also fail.

  - ### setToMeanOf

    public void setToMeanOf(EvolutionState state, Fitness[] fitnesses)
    Sets the fitness to be the same value as the mean of the provided fitnesses. The default version of this method exits with an "unimplemented" error; you should override this.

  - ### setToMedianOf

    public void setToMedianOf(EvolutionState state, Fitness[] fitnesses)
    Sets the fitness to be the median of the provided fitnesses. This method calls setToMeanOf(...), so if that method is unimplemented, this method will also fail.