mutation

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2019 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidMutationCount = errors.New("mutation count must be in the [0,MaxInt32] range")

ErrInvalidMutationCount is the error returned when trying to set an invalid mutation count

View Source
var ErrInvalidMutationProb = errors.New("mutation probability must be in the [0,1] range")

ErrInvalidMutationProb is the error returned when trying to set an invalid mutation probability

Functions

func NewString

func NewString(alphabet string) *stringMutater

NewString creates an operator that mutates individual characters in a string according to some probability.

Zero or more characters may be modified. The probability of any given character being modified is governed by the mutation probability.

Types

type Bitstring

type Bitstring struct {
	*Mutation
	// contains filtered or unexported fields
}

Bitstring is a an evolutionary operator that mutates individual bits in a bitstring.Bitstring according to some probability.

func NewBitstring

func NewBitstring() *Bitstring

NewBitstring creates a new BitString mutation operator, pre-configured with a probability of mutation of 0.01 (1%) and mutation count of 1 bit.

The mutation probability is the (possibly variable) probability of a candidate bit string being mutated at all. It can be modified with SetProb and SetProbRange. The mutation count is the (possibly variable) number of bits that will be flipped on any candidate bit string that is selected for mutation. It can be modified with SetMutations and SetMutationsRange.

func (*Bitstring) Mutate

func (op *Bitstring) Mutate(c interface{}, rng *rand.Rand) interface{}

Mutate mutates a single bit string. Zero or more bits may be flipped.

The probability of any given bit being flipped is governed by the probability generator configured for this mutation operator.

func (*Bitstring) SetMutations

func (op *Bitstring) SetMutations(nmut int) error

SetMutations sets the number of mutations (i.e the number of bits that will be flipped if the bitstring candidate is selected for mutation).

If nmut is not in the [0,MaxInt32] range SetMutations will return ErrInvalidMutationCount.

func (*Bitstring) SetMutationsRange

func (op *Bitstring) SetMutationsRange(min, max int) error

SetMutationsRange sets the range of possible number of mutations (i.e the numnber of bits that will be flipped if the bitstring candidate is selected for mutation).

The specific number of mutations will be randomly chosen with the pseudo random number generator argument of Apply, by linearly converting from [0,MaxInt32) to [min,max).

If min and max are not bounded by [0,MaxInt32] SetMutationsRange will return ErrInvalidMutationCount.

type Mutater

type Mutater interface {

	// Mutate performs mutation on a candidate.
	//
	// The original candidate is let untouched while the mutant is returned.
	Mutate(interface{}, *rand.Rand) interface{}
}

Mutater is the interface that wraps the Mutate method.

type Mutation

type Mutation struct {
	Mutater
	// contains filtered or unexported fields
}

Mutation implements a mutation operator.

It supports all mutation processes that operate on an unique candidate. The mutation probability is configurable, its effect depends on the specific mutation implementation, where it will be documented.

Note: unless you are implementing your own mutation operator, you generally don't need to directly instantiate a Mutation as specific mutation operators like BitStringMutation, StringMutation, etc. already create and embed a Mutation.

func New

func New(mut Mutater) *Mutation

New creates a Mutation operator with rhe provided Mutater.

The returned Mutation is preconfigured with a 0.01 mutation probability.

func (*Mutation) Apply

func (op *Mutation) Apply(sel []interface{}, rng *rand.Rand) []interface{}

Apply applies the mutation operator to each entry in the list of selected candidates.

func (*Mutation) SetProb

func (op *Mutation) SetProb(prob float64) error

SetProb sets the mutation probability,

If prob is not in the [0,1] range SetProb will return ErrInvalidMutationCount.

func (*Mutation) SetProbRange

func (op *Mutation) SetProbRange(min, max float64) error

SetProbRange sets the range of possible mutation probabilities.

The specific mutation probability will be randomly chosen with the pseudo random number generator argument of Apply, by linearly converting from [0,1) to [min,max).

If min and max are not bounded by [0,1] SetProbRange will return ErrInvalidMutationProb.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL