Niche {galgo} | R Documentation

## The representation of a set of chromosomes for genetic algorithms

### Description

Niche represents a set of chromosomes for the genetic algorithm. The niche can generate a progeny that may be more adapted to certains tasks (or enviroment, see Goldberg). To decide which chromosomes are more suitable to be chosen as “parents”, every chromosome in the niche is evaluated using a “fitness” function. The selected chromosomes are mated using crossover to produce diversity. Finally the chromosomes are mutated and the new progeny is ready for next generation.

The basic idea to generate a progeny is a random selection biased toward the best chromosomes (see Goldberg). We implented this idea as a weighted probability for a chromosome to be selected using the formula:

p = scale * max(0,fitness - mean * mean(fitness))\^\ power

where scale, mean and power are the properties of the niche (`offspringScaleFactor, offspringMeanFactor and offspringPowerFactor` respectively). The default values were selected to be reasonably bias when the variance in the fitness are both high (at early generations) and low (in late generatios).

The crossover mechanism needs to know the positions whose chromosomes can actually mate (`crossoverPoints`). The number of crossovers can be customized with `crossoverFunc` (`*crossover()`).

The elitism mechanism (`elitism` variable) are implemented replacing a random chromosome from the niche at the end of the progeny process (`*progeny()`).

The Niche object keeps a record of the number of generations, the maximum chromosome in the niche, and the best chromosome ever known (see `*best()` for an example).

The length of the niche is static. Nevertheless this behaviour (and any other) can be customised overwriting original methods (like progeny or crossover) methods. However, this is intend to be used only for experienced users.

The niche is considered a “closed population”, this means mating with chromosomes within the same niche. Migration mechanism uses niches to exchange chromosomes between them, which is implemented in `World` object (see `World`).

### Usage

    Niche(id=0,
    	chromosomes=list(),
    	offspringScaleFactor=1,
    	offspringMeanFactor=0.85,
    	offspringPowerFactor=2,
    	crossoverPoints=0,
    	mutationsFunc=function(.O) length(.O),
    	crossoverFunc=function(.O) length(.O)/2,
    	elitism=1,
    	generation=0,
    	fitness=0,
    	maxFitness=0,
    	bestFitness=0,
    	maxChromosome=NULL,
    	bestChromosome=NULL,
    	...)

### Arguments

`id` | A way to identify the object.
`chromosomes` | A list of defined chromosomes composing the niche.
`offspringScaleFactor` | The `offspringScaleFactor` parameter. See description.
`offspringMeanFactor` | The `offspringMeanFactor` parameter. See description.
`offspringPowerFactor` | The `offspringPowerFactor` parameter. See description.
`crossoverPoints` | Specific positions at which the chromosomes can be mated. Should be from 2 to minimum possible length of any chromosome in the niche.

`mutationsFunc`

A function returning the final number of mutations in the niche. It receives the `Niche` object as parameter. To implement “probability of mutation” instead, add a variable like `pMutation` in the constructor and multiply by the length of the niche and the length of the chromosome in the function (`function(niche) niche$pMutation * length(niche) * length(niche$chromosomes[[1]])`).

`crossoverFunc` | A function returning the final number of crossovers in the niche. It receives the `Niche` object as parameter. To implement “probability of crossover” instead, add a variable like `pCrossOver` in the constructor and multiply by the length of the niche in the function. (`function(niche) niche$pCrossOver * length(niche)`).

`elitism`

Controls the elitism mechanism. Elitism is desired to find solutions quicker, but it may be a nuisance when it is trapped in strong attractors. Therefore, in general, it may be a probability. Furthermore, it can be a vector of probabilities where the index is controlled by generation. If the current generation is greather than the length of this vector, a cycled version is used (starting from the first value).

`fitness` | The current fitness. It should be 0 initially, but it is included for generalization.
`bestFitness` | The best fitness ever visited. It should be 0 initially. Included for generalization.
`maxFitness` | The maximum fitness from the current chromosomes. It should be 0 initially, but it is included for generalization.
`maxChromosome` | The chromosome whose fitness is maximum from the current chromosomes. It should be NULL initially, but it is included for generalization.
`bestChromosome` | The chromosome whose fitness is maximum visited ever. It should be NULL initially, but it is included for generalization.
`generation` | For internal uses only.
`...` | Other user named values to include in the object (like pMutation, pCrossover or any other).

### Class

Package: galgo
Class Niche

`Object`
`~~|`
`~~+--``Niche`

Directly known subclasses:

public static class Niche
extends Object

### Fields and Methods

Methods:

| `as.double` | Converts the chromosome values (genes) to a vector.
| `as.matrix` | Converts the chromosome values (genes) to a matrix.
| `best` | Returns the best chromosome of the niche.
| `bestFitness` | Returns the fitness of the best chromosome in the niche.
| `clone` | Clones itself and its chromosomes.
| `crossover` | Performs crossover between chromosomes of the niche.
| `evaluate` | Evaluates the chromosome using a fitness function.
| `generateRandom` | Generates random values for all genes contained in all chromosomes in the niche.
| `getFitness` | Returns the fitness vector related to chromosomes.
| `length` | Gets the number of chromosomes defined in the niche.
| `max` | Returns the chromosome in the niche whose current fitness is maximum.
| `maxFitness` | Returns the fitness of the maximum chromosome in the niche.
| `mutate` | Mutates a niche calling mutate method for all chromosomes.
| `newCollection` | Generates a list of cloned niches.
| `newRandomCollection` | Creates a list of cloned niches with its internal values generated by random.
| `offspring` | Overwrites the new niche selecting a new population from the best chromosomes.
| `plot` | Plots information about niche object.
| `print` | Prints the representation of a niche object.
| `progeny` | Performs offspring, crossover, mutation, and elitism mechanism to generate the ``evolved'' niche.
| `refreshStats` | Updates the internal values from the current population.
| `reInit` | Erases all internal values in order to re-use the object.
| `scaling` | Assigns a weight for every chromosome to be selected for the next generation.
| `summary` | Prints the representation and statistics of the niche object.

Methods inherited from Object:
as.list, unObject, $, $<-, [[, [[<-, as.character, attach, clone, detach, equals, extend, finalize, getFields, getInstanciationTime, getStaticInstance, hasField, hashCode, ll, load, objectSize, print, save

### Author(s)

Victor Trevino. Francesco Falciani Group. University of Birmingham, U.K. http://www.bip.bham.ac.uk/bioinf

### References

Goldberg, David E. 1989 Genetic Algorithms in Search, Optimization and Machine Learning. Addison-Wesley Pub. Co. ISBN: 0201157675

### See Also

`Gene`, `Chromosome`, `World`, `Galgo`, `BigBang`.

### Examples

       cr <- Chromosome(genes=newCollection(Gene(shape1=1, shape2=100),5))
       ni <- Niche(chromosomes=newRandomCollection(cr, 10))
       ni

       ## in average, one of 10 genes can be mutated
       mf <- function(niche) niche$pMutations * length(niche) * length(niche$chromosomes[[1]])
       ni2 <- Niche(chromosomes=newRandomCollection(cr, 10),
              mutationsFunc=mf,
    		   pMutations=1/10)
       ni2    # random initial niche
       mutate(ni2) # returns the chromosomes indexes that were mutated
       ni2    # mutated niche

[Package galgo version 1.4 Index]