gp

package
v0.0.0-...-b9e61c6 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	OneArgActions = []Action{
		{getEnv, "env"},
	}
	TwoArgActions = []Action{
		{add, "add"},

		{multiply, "mult"},
		{divide, "div"},
	}
	ThreeArgActions = []Action{
		{neZero, "!0?"},
	}
	ZeroArgActions = []Action{

		{zero, "0"},
		{one, "1"},
		{two, "2"},
	}
	Pow8Actions = [][]Action{
		{
			{randv, "rand"},
			{zero, "0"},
			{one, "1"},
			{two, "2"},
		},
		{
			{neg, "neg"},
			{getEnv, "env"},
		},
		{
			{add, "add"},
			{subtract, "sub"},
			{multiply, "multiply"},
		},
		{
			{isPositive, "+?"},
			{ifRand, "rand?"},
			{neZero, "!0?"},
		},
	}
	BaseActions = [][]Action{
		ZeroArgActions,
		OneArgActions,
		TwoArgActions,
		ThreeArgActions,
	}
	TartarusActions = [][]Action{
		{
			{randv, "rand"},
			{zero, "0"},
			{one, "1"},
			{two, "2"}},
		{
			{neg, "neg"}},
		{
			{add, "add"},
			{subtract, "sub"},
			{multiply, "multiply"},
			{do2, "do2"}},
		{
			{isPositive, "+?"},
			{ifRand, "rand?"},
			{do3, "do3"},
			{neZero, "!0?"}},
	}
)

This is severely limited so as to weigh all actions the same. We want to be able to give certain actions different weights for being picked!

Functions

func AddStorage

func AddStorage(spaces int, baseWeight float64)

func CalculateCumulativeActionWeights

func CalculateCumulativeActionWeights(args ...int) []float64

func EnvFitness

func EnvFitness(g *GP, inputs, outputs [][]float64) int

An example fitness function which treats the output as a environment to compare a modified environment by the GP to.

func Eval

func Eval(n *Node) int

This is more or less shorthand

func GeneratePopulation

func GeneratePopulation(opt interface{}, popSize int) []pop.Individual

func Init

func Init(genOpt Options, e *env.I, cross GPCrossover,
	act [][]Action, baseActionWeight float64, f FitnessFunc)

func MatchEnvFitness

func MatchEnvFitness(g *GP, inputs, outputs [][]float64) int

func MatchMemFitness

func MatchMemFitness(g *GP, inputs, outputs [][]float64) int

func Mem0Fitness

func Mem0Fitness(g *GP, inputs, outputs [][]float64) int

func ModifyActionWeight

func ModifyActionWeight(action string, newWeight float64) bool

Returns whether the modification was successful (whether an action with that name was found)

func OptInit

func OptInit(opt interface{})

func OutputFitness

func OutputFitness(g *GP, inputs, outputs [][]float64) int

An example fitness which treats the output of the GP as a value to compare against the single expected output

Types

type Action

type Action struct {
	Op   Operator
	Name string
}

type Actions

type Actions [][]Action

The effect of a given action is internally defined by the action, and GPs need to learn what actions are useful, when. The first slice here represents how many arguments each action takes.

type FitnessFunc

type FitnessFunc func(gp *GP, inputs, outputs [][]float64) int

func ComplexityFitness

func ComplexityFitness(f FitnessFunc, mod float64) FitnessFunc

func TimeFitness

func TimeFitness(f FitnessFunc, threshold int, timeLimit int) FitnessFunc

type GP

type GP struct {
	First *Node
	Env   *env.I
	Mem   *env.I
	Nodes int
}

The principal Individual implementation for the gp package

func GenerateGP

func GenerateGP(genOpt Options) *GP

func (*GP) CanCrossover

func (gp *GP) CanCrossover(other pop.Individual) bool

func (*GP) Crossover

func (gp *GP) Crossover(other pop.Individual) pop.Individual

func (*GP) Fitness

func (gp *GP) Fitness(input, expected [][]float64) int

func (*GP) Mutate

func (gp *GP) Mutate()

func (*GP) Print

func (gp *GP) Print()

func (*GP) ShrinkMutate

func (gp *GP) ShrinkMutate()

func (*GP) SwapMutate

func (gp *GP) SwapMutate()

type GPCrossover

type GPCrossover interface {
	Crossover(a, b *GP) *GP
}

type InitOptions

type InitOptions struct {
	GenOpt           Options
	Env              *env.I
	Cross            GPCrossover
	Act              [][]Action
	BaseActionWeight float64
	Fitness          FitnessFunc
	StorageCount     int
	StorageWeight    float64
}

type Node

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

func NewNode

func NewNode(a Action, children int, gp *GP) *Node

func (*Node) Copy

func (n *Node) Copy(gp *GP) *Node

func (*Node) GenerateTree

func (n *Node) GenerateTree(depth, nodeLimit int) int

func (*Node) GetAllNodes

func (n *Node) GetAllNodes() []*Node

func (*Node) GetRandomNode

func (n *Node) GetRandomNode() (*Node, *Node)

func (*Node) Print

func (n *Node) Print(prefix string, isTail bool)

http://stackoverflow.com/questions/4965335/how-to-print-binary-tree-diagram Original credit, as per above source, to whoever wrote tree for linux

func (*Node) RandChild

func (n *Node) RandChild() *Node

func (*Node) Size

func (n *Node) Size() int

type Operator

type Operator func(*GP, ...*Node) int

type Options

type Options struct {
	MaxNodeCount  int
	MaxStartDepth int
	MaxDepth      int
	// These mutation chances are rolled once per GP
	//
	// Swap mutation is a swap of one node's action
	// to another action from the pool with the same
	// number of arguments.
	// (This is based off of GPJPP. Question: why is
	// there no mutation swap that can change the number
	// of arguments?)
	SwapMutationChance float64
	// Shrink Mutation picks some leaf node and replaces
	// its parent with it.
	ShrinkMutationChance float64
}

type PointCrossover

type PointCrossover struct{}

func (PointCrossover) Crossover

func (pc PointCrossover) Crossover(a, b *GP) *GP

Jump to

Keyboard shortcuts

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