com.rubecula.darwin.examples.travelingsalesman
Class RouteFitness

java.lang.Object
  extended by com.rubecula.darwin.domain.fitness.Fitness_
      extended by com.rubecula.darwin.examples.travelingsalesman.RouteFitness
All Implemented Interfaces:
Fitness, HasExpressions, com.rubecula.jexpression.EvalExpression

public class RouteFitness
extends Fitness_
implements HasExpressions

Author:
Robin Hillyard

Field Summary
 
Fields inherited from class com.rubecula.darwin.domain.fitness.Fitness_
LOG
 
Constructor Summary
RouteFitness()
           
RouteFitness(com.rubecula.jexpression.Evaluator evaluator)
          TODO use a real function listener (instead of null)
 
Method Summary
protected  double bandwidth(java.lang.String key)
          Concrete sub-classes must implement this method to indicate how sharp the fitness curve should be with respect to the possible values of the variate.
protected  double calculateFitness(double travelTime, double radius, java.lang.String traitId, java.lang.String factorId)
          Calculate fitness according to the pseudo-Poisson distribution function.
 double getAdjustment()
           
 double getClientFactor()
           
 java.lang.Number getEnvironmentFactor()
           
 ExpressionMap getExpressions()
           
 double getFitness(Trait trait, EcoFactor factor)
          Calculate the fitness for the trait and the factor.
 double getScale()
           
 double getTimeToTravel()
           
 int getWeight(java.lang.String character, EcoFactor factor)
           
 void resetAdjustment(java.lang.Number factor, java.lang.String trait)
          Scale adjustment field by value of factor (if not null).
 void resetEnvironmentFactor(java.lang.String trait, EcoFactor factor)
           
protected  double scaleFactor(java.lang.String key)
          Scale down the factor by the "scale".
protected  double scaleTrait(java.lang.String key)
          Scale down the travel time by the "timeToTravel".
 void setClientFactor(double clientFactor)
          This is a factor which adjusts the for the number of clients in the route.
 void setScale(double scale)
          Set the approximate size of the radius of points of interest in the same distance units as used in the client map.
 void setTimeToTravel(double timeToTravel)
          Set the time to travel a unit distance, where time is in the same units as the times-to-travel eco factor and the distance units are the same as those used for the client positions.
 
Methods inherited from class com.rubecula.darwin.domain.fitness.Fitness_
compare, equals, getExpression, getExpressionTerms, getFitnessFunction, getTolerance, hashCode, setTolerance, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

RouteFitness

public RouteFitness()

RouteFitness

public RouteFitness(com.rubecula.jexpression.Evaluator evaluator)
TODO use a real function listener (instead of null)

Parameters:
evaluator -
Method Detail

getAdjustment

public double getAdjustment()
Returns:
the adjustment

getClientFactor

public double getClientFactor()
Returns:
the client factor as set by setClientFactor(double).

getEnvironmentFactor

public java.lang.Number getEnvironmentFactor()
Specified by:
getEnvironmentFactor in interface Fitness
Overrides:
getEnvironmentFactor in class Fitness_
Returns:
the value that is dependent on the environment
See Also:
Fitness.getEnvironmentFactor()

getExpressions

public ExpressionMap getExpressions()
Specified by:
getExpressions in interface HasExpressions
Returns:
a map of String / EvalExpressionMutable pairs. The String key for each EvalExpressionMutable is the name by which that expression will be known in the user interface.
See Also:
HasExpressions.getExpressions()

getFitness

public double getFitness(Trait trait,
                         EcoFactor factor)
                  throws FitnessException
Calculate the fitness for the trait and the factor.

Specified by:
getFitness in interface Fitness
Returns:
the fitness between 0 (unfit) and 1 (perfect fit).
Throws:
FitnessException
See Also:
Fitness.getFitness(com.rubecula.darwin.domain.helper.Trait, com.rubecula.darwin.domain.helper.EcoFactor)

getScale

public double getScale()
Returns:
the scale property.

getTimeToTravel

public double getTimeToTravel()
Returns:
the timeToTravel property.

getWeight

public int getWeight(java.lang.String character,
                     EcoFactor factor)
Specified by:
getWeight in interface Fitness
Parameters:
character - the identifier (or key) of the phenotypic character whose traits/variants will be measured for fitness against the given factor.
Returns:
the relative weighting for this fitness (0: not a significant fitness match)
See Also:
Fitness.getWeight(String, com.rubecula.darwin.domain.helper.EcoFactor)

resetAdjustment

public void resetAdjustment(java.lang.Number factor,
                            java.lang.String trait)
Scale adjustment field by value of factor (if not null).

Specified by:
resetAdjustment in interface Fitness
Overrides:
resetAdjustment in class Fitness_
Parameters:
factor - if 1 is passed in then nothing changes.
trait - XXX
See Also:
Fitness_.resetAdjustment(java.lang.Number, String)

resetEnvironmentFactor

public void resetEnvironmentFactor(java.lang.String trait,
                                   EcoFactor factor)
Specified by:
resetEnvironmentFactor in interface Fitness
Overrides:
resetEnvironmentFactor in class Fitness_
Parameters:
trait - XXX
factor - XXX
See Also:
Fitness.resetEnvironmentFactor(String, EcoFactor)

setClientFactor

public void setClientFactor(double clientFactor)
This is a factor which adjusts the for the number of clients in the route. The more clients, the less perfect our route is likely to be so we adjust here. The formula for the adjustment is: clientFactor / (N + clientFactor) where N is the number of clients. An appropriate value is around 10 or 12. The higher the value, the closer the adjustment comes to 1.0 and the lower the fitness values for routes with many clients. When fitness values are low, more organisms are culled and we have therefore a smaller gene pool to evolve. OTOH, if this value is too small, the more likely the time to travel the route will actually be too good (less then the expected) possibly favoring some routes that take longer than other routes.

Parameters:
clientFactor -

setScale

public void setScale(double scale)
Set the approximate size of the radius of points of interest in the same distance units as used in the client map.

Parameters:
scale -

setTimeToTravel

public void setTimeToTravel(double timeToTravel)
Set the time to travel a unit distance, where time is in the same units as the times-to-travel eco factor and the distance units are the same as those used for the client positions.

Parameters:
timeToTravel -

bandwidth

protected double bandwidth(java.lang.String key)
Description copied from class: Fitness_
Concrete sub-classes must implement this method to indicate how sharp the fitness curve should be with respect to the possible values of the variate.

Specified by:
bandwidth in class Fitness_
Parameters:
key - the key which determines which bandwidth to get (trait or ecofactor).
Returns:
0.5.
See Also:
Fitness_.bandwidth(String)

calculateFitness

protected double calculateFitness(double travelTime,
                                  double radius,
                                  java.lang.String traitId,
                                  java.lang.String factorId)
                           throws FitnessException
Calculate fitness according to the pseudo-Poisson distribution function. The value used is travelTime/radius/pi, scaled down by the value of getTimeToTravel() which is in theory the time travel one unit of distance and also by (nClients/ getClientFactor() + 1) . The target (an estimate of the perfect value) is 1 scaled down by getScale(). The shape factor is the result of calling bandwidth(String). Ideally, the target should be higher than and very close to 1.0; the value should be slightly greater than the target; If the values are inappropriate, you can adjust them by changing the time to travel setting, the scale setting, the client factor setting, or the bandwidth value. If the resulting fitness values are too low, the population will suffer. The fitness calculated and returned will be:

Overrides:
calculateFitness in class Fitness_
Parameters:
travelTime -
radius -
traitId -
factorId -
Returns:
the fitness, as evaluated by calculateFitness(double, double, String, String) with travelTime/radius/pi/adjustment for the first parameter.
Throws:
FitnessException

scaleFactor

protected double scaleFactor(java.lang.String key)
Scale down the factor by the "scale". This is a fudge factor to bring the comparison with the target to an appropriate value. It depends on the kind of route problem being solved. The factor might be different for a mailman versus a fedex man for example. If you see warnings "Fitness calculation issue...", try increasing the scale (which will result in a smaller return value from this method).

Overrides:
scaleFactor in class Fitness_
Parameters:
key - the key (identifier) of the ecoFactor
Returns:
a scale factor to be applied (to the eco factor value) before the difference is calculated between the trait as a value and the eco factor as a value. By value returned by this base method is 1.0. Specific fitness implementations must override if the value is to be different.
See Also:
Fitness_.scaleFactor(String)

scaleTrait

protected double scaleTrait(java.lang.String key)
Scale down the travel time by the "timeToTravel".

Overrides:
scaleTrait in class Fitness_
Parameters:
key - the key (identifier) of the trait
Returns:
a scale factor to be applied (to the trait value) before the difference is calculated between the trait as a value and the eco factor as a value. By value returned by this base method is 1.0. Specific fitness implementations must override if the value is to be different.
See Also:
Fitness_.scaleTrait(java.lang.String)


Copyright © 2010 Rubecula Software, LLC. All Rights Reserved.