JGAP

examples.knapsack
Class KnapsackFitnessFunction

java.lang.Object
  extended by org.jgap.FitnessFunction
      extended by examples.knapsack.KnapsackFitnessFunction
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, ICloneable

public class KnapsackFitnessFunction
extends FitnessFunction

Fitness function for the knapsack example.

Since:
2.3
See Also:
Serialized Form

Field Summary
static double MAX_BOUND
           
 
Fields inherited from class org.jgap.FitnessFunction
DELTA, NO_FITNESS_VALUE
 
Constructor Summary
KnapsackFitnessFunction(double a_knapsackVolume)
           
 
Method Summary
protected  double computeItemNumberPenalty(double a_maxFitness, int a_items)
          Calculates the penalty to apply to the fitness value based on the amount of items in the solution.
 double evaluate(IChromosome a_subject)
          Determine the fitness of the given Chromosome instance.
static int getNumberOfItemsAtGene(IChromosome a_potentialSolution, int a_position)
          Retrieves the number of items represented by the given potential solution at the given gene position.
static int getTotalNumberOfItems(IChromosome a_potentialSolution)
          Returns the total number of items represented by all of the genes in the given potential solution.
static double getTotalVolume(IChromosome a_potentialSolution)
          Calculates the total amount of change (in cents) represented by the given potential solution and returns that amount.
protected  double volumeDifferenceBonus(double a_maxFitness, double a_volumeDifference)
          Bonus calculation of fitness value.
 
Methods inherited from class org.jgap.FitnessFunction
clone, getFitnessValue, getLastComputedFitnessValue
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_BOUND

public static final double MAX_BOUND
See Also:
Constant Field Values
Constructor Detail

KnapsackFitnessFunction

public KnapsackFitnessFunction(double a_knapsackVolume)
Method Detail

evaluate

public double evaluate(IChromosome a_subject)
Determine the fitness of the given Chromosome instance. The higher the return value, the more fit the instance. This method should always return the same fitness value for two equivalent Chromosome instances.

Specified by:
evaluate in class FitnessFunction
Parameters:
a_subject - the Chromosome instance to evaluate
Returns:
a positive double reflecting the fitness rating of the given Chromosome
Since:
2.3

volumeDifferenceBonus

protected double volumeDifferenceBonus(double a_maxFitness,
                                       double a_volumeDifference)
Bonus calculation of fitness value.

Parameters:
a_maxFitness - maximum fitness value appliable
a_volumeDifference - volume difference in ccm for the items problem
Returns:
bonus for given volume difference
Since:
2.3

computeItemNumberPenalty

protected double computeItemNumberPenalty(double a_maxFitness,
                                          int a_items)
Calculates the penalty to apply to the fitness value based on the amount of items in the solution.

Parameters:
a_maxFitness - upper boundary for fitness value possible
a_items - number of items in the solution
Returns:
a penalty for the fitness value based on the number of items
Since:
2.3

getTotalVolume

public static double getTotalVolume(IChromosome a_potentialSolution)
Calculates the total amount of change (in cents) represented by the given potential solution and returns that amount.

Parameters:
a_potentialSolution - the potential solution to evaluate
Returns:
the total amount of change (in cents) represented by the given solution
Since:
2.3

getNumberOfItemsAtGene

public static int getNumberOfItemsAtGene(IChromosome a_potentialSolution,
                                         int a_position)
Retrieves the number of items represented by the given potential solution at the given gene position.

Parameters:
a_potentialSolution - the potential solution to evaluate
a_position - the gene position to evaluate
Returns:
the number of items represented by the potential solution at the given gene position
Since:
2.3

getTotalNumberOfItems

public static int getTotalNumberOfItems(IChromosome a_potentialSolution)
Returns the total number of items represented by all of the genes in the given potential solution.

Parameters:
a_potentialSolution - the potential solution to evaluate
Returns:
the total number of items represented by the given Chromosome
Since:
2.3

JGAP