lgp

package
v0.0.0-...-f34b45d Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SPECIAL_REGISTERS = 1
	LAST_WRITTEN      = -1
)

Variables

View Source
var (
	BaseActions = []Action{
		{Neg, "neg", 1},

		{Add, "add", 3},
		{Subtract, "sub", 3},
		{Multiply, "mult", 3},

		{Bnez, "bnez", 1},
		{Bgz, "bgz", 1},
		{Jmp, "jmp", 1},
		{Randv, "rand", 1},
		{ConstInt(0), "0", 1},
		{ConstInt(1), "1", 1},
	}
	MinActions = []Action{
		{Bnez, "bnez", 1},
		{Bgz, "bgz", 1},
		{Jmp, "jmp", 1},
	}
	EnvActions = []Action{
		{GetEnv, "env", 2},

		{EnvLen, "envLen", 1},
	}
	PowSumActions = []Action{
		{GetEnv, "env", 2},
		{Add, "add", 3},
		{Multiply, "mult", 3},
		{Divide, "div", 3},
		{ConstInt(0), "0", 1},
		{ConstInt(1), "1", 1},
		{ConstInt(2), "2", 1},
	}
	TartarusActions = []Action{
		{Neg, "neg", 1},
		{Add, "add", 3},
		{Subtract, "sub", 3},
		{Multiply, "mult", 3},
		{Bnez, "bnez", 1},
		{Bgz, "bgz", 1},
		{Jmp, "jmp", 1},
		{Randv, "rand", 1},
		{ConstInt(0), "0", 1},
		{ConstInt(1), "1", 1},
		{ConstInt(2), "2", 1},
	}
	SortActions = []Action{
		{Bnez, "bnez", 1},
		{Bgz, "bgz", 1},
		{Bgeq, "bgeq", 2},
		{Jmp, "jmp", 1},
		{GetEnv, "env", 2},
		{EnvLen, "envLen", 1},
		{Divide, "div", 3},
		{Subtract, "sub", 3},
		{ConstInt(0), "0", 1},
		{ConstInt(1), "1", 1},
		{MemSwap, "swap", 2},
	}
)

Functions

func Add

func Add(gp *LGP, xs ...int)

func Bgeq

func Bgeq(gp *LGP, xs ...int)

func Bgz

func Bgz(gp *LGP, xs ...int)

func Bnez

func Bnez(gp *LGP, xs ...int)

Branch functions skip the following instruction if true.

func ConstInt

func ConstInt(i int) func(gp *LGP, xs ...int)

func Divide

func Divide(gp *LGP, xs ...int)

func EnvFitness

func EnvFitness(g *LGP, 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 EnvLen

func EnvLen(gp *LGP, xs ...int)

func GeneratePopulation

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

func GetEnv

func GetEnv(gp *LGP, xs ...int)

func Init

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

func Jmp

func Jmp(gp *LGP, xs ...int)

func MatchEnvFitness

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

func MatchMemFitness

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

func Mem0Fitness

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

func MemSwap

func MemSwap(gp *LGP, xs ...int)

func Mod

func Mod(gp *LGP, xs ...int)

func Multiply

func Multiply(gp *LGP, xs ...int)

func Neg

func Neg(gp *LGP, xs ...int)

func OptInit

func OptInit(opt interface{})

func Pow

func Pow(gp *LGP, xs ...int)

func Pow2

func Pow2(gp *LGP, xs ...int)

func Pow3

func Pow3(gp *LGP, xs ...int)

func PrintActions

func PrintActions()

func Randv

func Randv(gp *LGP, xs ...int)

func ResetCumActionWeights

func ResetCumActionWeights()

func SetEnv

func SetEnv(gp *LGP, xs ...int)

func Subtract

func Subtract(gp *LGP, xs ...int)

Types

type Action

type Action struct {
	Op   Operator
	Name string
	Args int
}

The effect of a given action is internally defined by the action, and GPs need to learn what actions are useful, when. Unlike Tree-GPs, LGPs do not care about the number of arguments their actions take unless mutating or generating them.

type FitnessFunc

type FitnessFunc func(gp *LGP, 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 InitOptions

type InitOptions struct {
	GenOpt           Options
	Env              *env.I
	Mem              *env.I
	Cross            LGPCrossover
	Act              []Action
	BaseActionWeight float64
	Fitness          FitnessFunc
	QuitEarly        int
}

type Instruction

type Instruction struct {
	Act  Action
	Args []int
}

func (*Instruction) String

func (i *Instruction) String() string

type LGP

type LGP struct {
	Instructions []Instruction
	Mem          *env.I
	MemStart     *env.I
	Env          *env.I
	// contains filtered or unexported fields
}

A linear genetic program

func GenerateLGP

func GenerateLGP(genOpt Options) *LGP

func (*LGP) CanCrossover

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

func (*LGP) Copy

func (gp *LGP) Copy() *LGP

func (*LGP) Crossover

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

Crossover types (multi)point crossover uniform crossover these should be brought out and used for all list-like structures

func (*LGP) ExpandMutate

func (gp *LGP) ExpandMutate()

func (*LGP) Fitness

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

func (*LGP) GetInstruction

func (gp *LGP) GetInstruction() Instruction

func (*LGP) GetReg

func (gp *LGP) GetReg(r1 int) (r2 int)

func (*LGP) MemMutate

func (gp *LGP) MemMutate()

func (*LGP) Mutate

func (gp *LGP) Mutate()

Mutation types: swap mutate, swapping instructions at two locations value mutate, changing the values baked into an instruction shrink/expand mutate, removing or adding instructions from random locations environment mutate, changing the initial environment values

func (*LGP) Print

func (gp *LGP) Print()

func (*LGP) RegVal

func (gp *LGP) RegVal(r1 int) int

func (*LGP) Run

func (gp *LGP) Run()

func (*LGP) SetReg

func (gp *LGP) SetReg(r1, v int)

func (*LGP) ShrinkMutate

func (gp *LGP) ShrinkMutate()

func (*LGP) SwapMutate

func (gp *LGP) SwapMutate()

func (*LGP) ValueMutate

func (gp *LGP) ValueMutate()

type LGPCrossover

type LGPCrossover interface {
	Crossover(a, b *LGP) *LGP
}

type Operator

type Operator func(*LGP, ...int)

type Options

type Options struct {
	MinActionCount  int
	MaxActionCount  int
	MaxStartActions int
	MinStartActions int

	SwapMutationChance   float64
	ValueMutationChance  float64
	ShrinkMutationChance float64
	ExpandMutationChance float64
	MemMutationChance    float64
}

type PointCrossover

type PointCrossover struct {
	NumPoints int
}

func (PointCrossover) Crossover

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

type UniformCrossover

type UniformCrossover struct {
	ChosenProportion float64
}

This looks like it doesn't work

func (UniformCrossover) Crossover

func (uc UniformCrossover) Crossover(a, b *LGP) *LGP

Jump to

Keyboard shortcuts

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