Documentation ¶
Index ¶
- Constants
- Variables
- func CrossoverSliceByMidpoint[T any](s1, s2 []T)
- func CrossoverSliceByRandomPoint[T any](s1, s2 []T)
- func MutateRandomIndex[T any](slice []T, changeFunc func(T) T)
- type ConsistentSizeSliceSolution
- func (c *ConsistentSizeSliceSolution[E]) Clone() Solution[[]E]
- func (c *ConsistentSizeSliceSolution[E]) Describe() string
- func (c *ConsistentSizeSliceSolution[E]) Equals(other Solution[[]E]) bool
- func (c *ConsistentSizeSliceSolution[E]) Score() float64
- func (c *ConsistentSizeSliceSolution[E]) String() string
- func (c *ConsistentSizeSliceSolution[E]) Value() []E
- type Controller
- type Crossover
- type Handler
- type IOWriterRecorder
- func (r *IOWriterRecorder[T]) Crossover(in1, in2, out1, out2 Solution[T])
- func (r *IOWriterRecorder[T]) ExperimentStart(controller Controller[T])
- func (r *IOWriterRecorder[T]) Generation(generation int)
- func (r *IOWriterRecorder[T]) InitialPopulation(population []Solution[T])
- func (r *IOWriterRecorder[T]) Mutate(in, out Solution[T])
- func (r *IOWriterRecorder[T]) Selection(before, after []Solution[T])
- func (r *IOWriterRecorder[T]) Terminate(result Result[T])
- type Mutation
- type PairwiseSelectionStrategy
- type PopulationPredicate
- type PredicateReason
- type Recorder
- type RecorderConfig
- type Result
- type Solution
- type SolutionPredicate
- type SubgroupSelectionStrategy
Constants ¶
const ( PredicateReasonPopulationWasEmpty = "population is empty" PredicateReasonPopulationAllScoresSame = "all scores were equal" PredicateReasonPopulationAllSolutionsEqual = "all solutions were equal" PredicateReasonScoreWasHit = "defined score threshold was hit" )
Variables ¶
var ( RecordEverything = &RecorderConfig{ ExperimentStart: true, InitialPopulation: true, Generation: true, Crossover: true, Mutate: true, Selection: true, Terminate: true, } )
Functions ¶
func CrossoverSliceByMidpoint ¶
func CrossoverSliceByMidpoint[T any](s1, s2 []T)
CrossoverSliceByMidpoint performs an in-place crossover on an exact mid-point split of both slices. It is the caller's responsibility to ensure that the input slices are safe to be mutated.
This function panics if len(s1) != len(s2)
func CrossoverSliceByRandomPoint ¶
func CrossoverSliceByRandomPoint[T any](s1, s2 []T)
CrossoverSliceByRandomPoint performs an in-place crossover on a random point-of-split of both slices. The point-of-split is randomly decided, but is guaranteed to be within the bounds of both slices. It is the caller's responsibility to ensure that the input slices are safe to be mutated.
This function panics if len(s1) != len(s2)
func MutateRandomIndex ¶
func MutateRandomIndex[T any](slice []T, changeFunc func(T) T)
Types ¶
type ConsistentSizeSliceSolution ¶
type ConsistentSizeSliceSolution[E comparable] struct { Slice []E }
func NewConsistentSizeSliceSolution ¶
func NewConsistentSizeSliceSolution[E comparable](initialValue []E) *ConsistentSizeSliceSolution[E]
func (*ConsistentSizeSliceSolution[E]) Clone ¶
func (c *ConsistentSizeSliceSolution[E]) Clone() Solution[[]E]
func (*ConsistentSizeSliceSolution[E]) Describe ¶
func (c *ConsistentSizeSliceSolution[E]) Describe() string
func (*ConsistentSizeSliceSolution[E]) Equals ¶
func (c *ConsistentSizeSliceSolution[E]) Equals(other Solution[[]E]) bool
func (*ConsistentSizeSliceSolution[E]) Score ¶
func (c *ConsistentSizeSliceSolution[E]) Score() float64
func (*ConsistentSizeSliceSolution[E]) String ¶
func (c *ConsistentSizeSliceSolution[E]) String() string
func (*ConsistentSizeSliceSolution[E]) Value ¶
func (c *ConsistentSizeSliceSolution[E]) Value() []E
type Controller ¶
type Controller[T any] struct { Recorder Recorder[T] // GenerationCycles is the number of generation cycles // to run the experiment for GenerationCycles int // PopulationSize represents the number of solutions // in each generation corpus PopulationSize int // MutationProbability is the probability that // each solutions has to mutate on each generation cycle MutationProbability float64 // CrossoverProbability is the probability that // each pair of solutions has to crossover together CrossoverProbability float64 // CrossoverSelectionStrategy is the strategy used to select pairs // for crossover CrossoverSelectionStrategy PairwiseSelectionStrategy[T] // NextGenerationSelectionStrategy is the strategy used in each iteration // to select the next corpus of solutions NextGenerationSelectionStrategy SubgroupSelectionStrategy[T] // TerminationConditions allows for input of conditions // that may allow for early termination of the experiment TerminationConditions []PopulationPredicate[T] }
Controller represents inner structure data about the experiment, as well as strategies for handling experiment execution.
type Handler ¶
type Handler[TUnderlying any] interface { Crossover[TUnderlying] Mutation[TUnderlying] // NewSolution returns a single solutions instance NewSolution() Solution[TUnderlying] NewSolutionFrom(innerValue TUnderlying) Solution[TUnderlying] }
Handler is the interface of the handler used for operating a problem
type IOWriterRecorder ¶
type IOWriterRecorder[T any] struct { // contains filtered or unexported fields }
func NewIOWriterRecorder ¶
func NewIOWriterRecorder[T any](writer io.Writer, config *RecorderConfig) *IOWriterRecorder[T]
NewIOWriterRecorder creates a new IOWriterRecorder
func (*IOWriterRecorder[T]) Crossover ¶
func (r *IOWriterRecorder[T]) Crossover(in1, in2, out1, out2 Solution[T])
func (*IOWriterRecorder[T]) ExperimentStart ¶
func (r *IOWriterRecorder[T]) ExperimentStart(controller Controller[T])
func (*IOWriterRecorder[T]) Generation ¶
func (r *IOWriterRecorder[T]) Generation(generation int)
func (*IOWriterRecorder[T]) InitialPopulation ¶
func (r *IOWriterRecorder[T]) InitialPopulation(population []Solution[T])
func (*IOWriterRecorder[T]) Mutate ¶
func (r *IOWriterRecorder[T]) Mutate(in, out Solution[T])
func (*IOWriterRecorder[T]) Selection ¶
func (r *IOWriterRecorder[T]) Selection(before, after []Solution[T])
func (*IOWriterRecorder[T]) Terminate ¶
func (r *IOWriterRecorder[T]) Terminate(result Result[T])
type PairwiseSelectionStrategy ¶
func SelectBestPairs ¶
func SelectBestPairs[T any]() PairwiseSelectionStrategy[T]
SelectBestPairs returns a k-size slice of pairs of solutions whose scores are elitist, ie. Best and 2nd-best are paired, 3rd-best and 4th-best are paired.
func SelectRandomPairs ¶
func SelectRandomPairs[T any]() PairwiseSelectionStrategy[T]
SelectRandomPairs returns a k-size slice of randomly picked pairs of solutions
type PopulationPredicate ¶
type PopulationPredicate[T any] func([]Solution[T]) (PredicateReason, bool)
func TrueWhenAllScoresSame ¶
func TrueWhenAllScoresSame[T any]() PopulationPredicate[T]
TrueWhenAllScoresSame returns a PopulationPredicate that returns true when all solutions of the input population corpus have the same score
func TrueWhenAllSolutionsEqual ¶
func TrueWhenAllSolutionsEqual[T any]() PopulationPredicate[T]
TrueWhenAllSolutionsEqual returns a PopulationPredicate that returns true when all solutions of the input population corpus are identical
func TrueWhenAtLeastNScores ¶
func TrueWhenAtLeastNScores[T any](n int, scoreThresholdFunc func(float64) bool) PopulationPredicate[T]
TrueWhenAtLeastNScores returns a PopulationPredicate that returns true when at least [n] solutions in the corpus fulfill the [scoreThresholdFunc] (returns true) given.
If n > len(population), the predicate will always return false.
type PredicateReason ¶
type PredicateReason string
type Recorder ¶
type Recorder[T any] interface { // ExperimentStart marks the start of the experiment ExperimentStart(controller Controller[T]) // InitialPopulation records the initial population InitialPopulation(population []Solution[T]) // Generation records whenever a new generation cycle begins Generation(generation int) // Crossover records each crossover event Crossover(in1, in2, out1, out2 Solution[T]) // Mutate records each mutation event Mutate(in, out Solution[T]) // Selection records the selection process at the end of each cycle Selection(before, after []Solution[T]) // Terminate records a termination Terminate(result Result[T]) }
Recorder is a logger interface for visibility into the iteration process. All method implementations must be concurrent-safe.
type RecorderConfig ¶
type Result ¶
type Result[T any] struct { Population []Solution[T] `json:"population"` TerminatedAtGeneration int `json:"terminatedAtGeneration"` TerminationReason string `json:"terminationReason"` }
Result is the output of a single experiment run
func RunN ¶
func RunN[T any](handler Handler[T], controller *Controller[T], n int) *Result[T]
RunN is similar to Run, but only returns the top (by score) n solutions in the final population corpus
type Solution ¶
type Solution[TUnderlying any] interface { fmt.Stringer // Score returns the solutions's individual score Score() float64 // Value returns the underlying value backing the solutions Value() TUnderlying // Equals returns true if the solutions is // identical to the given other solutions Equals(Solution[TUnderlying]) bool // Clone returns a clone that has no shared memory references // with the original Clone() Solution[TUnderlying] // Describe returns textual information describing the solutions. // This may be information to be presented at the end of a run, etc. Describe() string }
Solution is a standard interface for a potential solutions in each corpus
type SolutionPredicate ¶
type SolutionPredicate[T any] func(solution Solution[T]) (PredicateReason, bool)
type SubgroupSelectionStrategy ¶
func SelectBestAndWorstSubgroup ¶
func SelectBestAndWorstSubgroup[T any](bestRatio float64) SubgroupSelectionStrategy[T]
func SelectBestSubgroup ¶
func SelectBestSubgroup[T any]() SubgroupSelectionStrategy[T]
func SelectRandomSubgroup ¶
func SelectRandomSubgroup[T any]() SubgroupSelectionStrategy[T]