creatur-2.0.12: Framework for artificial life experiments.

Portabilityportable
Stabilityexperimental
Maintaineramy@nualeargais.ie
Safe HaskellNone

ALife.Creatur.Genetics.CodeInternal

Description

Encoding schemes for genes.

Synopsis

Documentation

data Code a b Source

An encoding scheme.

Constructors

Code 

Fields

cSize :: Int
 
cTable :: [(a, [b])]
 

Instances

(Show a, Show b) => Show (Code a b) 

encode :: Eq a => Code a b -> a -> Maybe [b]Source

Encodes a value as a sequence of bits.

encodeNext :: Eq a => (Code a b, a) -> [b] -> [b]Source

decode :: Eq b => Code a b -> [b] -> Maybe aSource

Returns the value corresponding to a sequence of bits.

decodeNext :: Eq b => Code a b -> [b] -> (Maybe a, [b])Source

asBits :: [Bool] -> StringSource

Convert a list of bits to a string of 0s and 1s.

mkGrayCode :: [a] -> Code a BoolSource

Constructs a Gray code for the specified values, using the minimum number of bits required to encode all of the values.

If the number of values provided is not a perfect square, some codes will not be used; calling decode with those values will return Nothing. You can find out if this will be the case by calling excessGrayCodes. For example mkGrayCode ['a','b','c'] would assign the code 00 to a, 01 to b, and 11 to c, leaving 10 unassigned. To avoid having unassigned codes, you can repeat a value in the input list so the example above could be modified to mkGrayCode ['a','a','b','c'], which would assign the codes 00 and 01 to a, 11 to b, and 10 to c.

A Gray code maps values to codes in a way that guarantees that the codes for two consecutive values will differ by only one bit. This feature can be useful in evolutionary programming because the genes resulting from a crossover operation will be similar to the inputs. This helps to ensure that offspring are similar to their parents, as any radical changes from one generation to the next are the result of mutation alone.

grayCodeLength :: Int -> IntSource

grayCodeLength n returns the number of bits required to encode n values.

grayCodeCapacity :: Int -> IntSource

grayCodeCapacity n returns the number of values that can be encoded using n bits. The number of values that can be encoded in n bits is 2^n.