engine

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: 6 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Elites

func Elites(n int) func(*Engine) error

Elites defines the number of candidates preserved via elitism for the engine. By default it is set to 0, no elitism is applied.

In elitism, a subset of the population with the best fitness scores is preserved, unchanged, and placed into the successive generation. Candidate solutions that are preserved unchanged through elitism remain eligible for selection for breeding the remainder of the next generation. This value must be non-negative and less than the population size or Evolve will return en error

func EndOn

func EndOn(cond evolve.Condition) func(*Engine) error

EndOn adds a termination condition to the engine. The engine stops after one or more condition is met.

func Observe

func Observe(o Observer) func(*Engine) error

Observe adds an observer of the evolution process.

func Rand

func Rand(rng *rand.Rand) func(*Engine) error

Rand sets rng as the source of randomness of the engine.

func Seeds

func Seeds(seeds []interface{}) func(*Engine) error

Seeds provides the engine with a set of candidates to seed the starting population with. Successive calls to Seeds will replace the set of seed candidates set in the previous call.

Types

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine runs an evolutionary algorithm, following all the steps of evolution, from the creation of the initial population to the end of evolution.

func New

func New(gen evolve.Generator, eval evolve.Evaluator, epoch evolve.Epocher, options ...func(*Engine) error) (*Engine, error)

New creates an evolution engine.

gen generates new random candidates solutions. eval evaluates fitness scores of candidates. epoch transforms a whole population into the next generation.

Example

Create an engine evolving bit strings, in which the generator simply counts the number of ones. See full example in "evolve/example/bits/main.go"

var epocher Generational

gen := generator.Bitstring(2)

eval := evolve.EvaluatorFunc(
	true, // natural fitness (higher is better)
	func(cand interface{}, pop []interface{}) float64 {
		// our evaluator counts the ones in the bitstring
		return float64(cand.(*bitstring.Bitstring).OnesCount())
	})

New(gen, eval, &epocher)

func (*Engine) AddObserver

func (e *Engine) AddObserver(o Observer)

AddObserver adds an observer of the evolution process.

func (*Engine) Evolve

func (e *Engine) Evolve(popsize int, options ...func(*Engine) error) (evolve.Population, []evolve.Condition, error)

Evolve runs the evolutionary algorithm until one of the termination conditions is met, then return the entire population present during the final generation.

size is the number of candidate in the population. They whole population is generated for the first generation, unless some seed candidates are provided with Seeds. size must be at least 1 or Evolve will return en error.

At least one termination condition must be defined with EndOn, or Evolve will return an error.

func (*Engine) RemoveObserver

func (e *Engine) RemoveObserver(o Observer)

RemoveObserver removes an observer of the evolution process.

type Generational

type Generational struct {
	Op   evolve.Operator
	Eval evolve.Evaluator
	Sel  evolve.Selection
}

Generational implements a general-purpose engine for generational evolutionary algorithm.

It supports optional concurrent fitness evaluations to take full advantage of multi-processor, multi-core and hyper-threaded machines through the concurrent evaluation of candidate's fitness.

If multi-threading is enabled, evolution (mutation, crossover, etc.) occurs on the request goroutine but fitness evaluations are delegated to a pool of worker threads. All of the host's available processing units are used (i.e. on a quad-core machine there will be four fitness evaluation worker threads).

If multi-threading is disabled, all work is performed synchronously on the request thread. This strategy is suitable for restricted/managed environments where it is not permitted for applications to manage their own threads. If there are no restrictions on concurrency, applications should enable multi-threading for improved performance.

func (*Generational) Epoch

func (e *Generational) Epoch(pop evolve.Population, nelites int, rng *rand.Rand) evolve.Population

Epoch performs a single step/iteration of the evolutionary process.

pop is the population at the beginning of the process. nelites is the number of the fittest individuals that must be preserved.

Returns the updated population after the evolutionary process has proceeded by one step/iteration.

type Observer

type Observer interface {
	Observe(*evolve.PopulationStats)
}

An Observer monitors the evolution of a population.

Once registered within the evolution engine, an observer gets notified, after each completed epoch, with the population statistics (i.e once for every completed epoch).

func ObserverFunc

func ObserverFunc(f func(*evolve.PopulationStats)) Observer

The ObserverFunc type is an adapter to allow the use of ordinary functions as evolution observers. If f is a function with the appropriate signature, ObserverFunc(f) is an Observer that calls f.

Jump to

Keyboard shortcuts

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