Back to godoc.org

Package bo

v0.0.0-...-fd6249b
Latest Go to latest

The highest tagged major version is .

Published: Mar 30, 2020 | License: MIT | Module: github.com/changkun/gobase

Index

Constants

const (
	// DefaultRounds is the default number of rounds to run.
	DefaultRounds = 20
	// DefaultRandomRounds is the default number of random rounds to run.
	DefaultRandomRounds = 5
	// DefaultMinimize is the default value of minimize.
	DefaultMinimize = true

	// NumRandPoints is the maximum allowed number of evaluations.
	NumRandPoints = 100000
	// NumGradPoints is the number of random points of gradient descent
	NumGradPoints = 256
)

Variables

var (
	// DefaultExploration uses UCB with 95 confidence interval.
	DefaultExploration = UCB{Kappa: 1.96}
	// DefaultBarrierFunc sets the default barrier function to use.
	DefaultBarrierFunc = LogBarrier{}
)
var SampleTries = 1000

SampleTries is the number of tries a sample function should try before truncating the samples to the boundaries.

func BasicBarrier

func BasicBarrier(x []float64, params []Param) float64

BasicBarrier returns -Inf if an x value is outside the param range.

type BarrierFunc

type BarrierFunc interface {
	Val(x []float64, params []Param) float64
	Grad(x []float64, params []Param) []float64
}

BarrierFunc returns a value that is added to the value to bound the optimization.

type BoundsMethod

type BoundsMethod struct {
	Method optimize.Method
	Bounds []Param
}

BoundsMethod is a hack that contains the target space.

func (BoundsMethod) Init

func (m BoundsMethod) Init(dims, tasks int) int

Init ...

func (BoundsMethod) Run

func (m BoundsMethod) Run(operation chan<- optimize.Task, result <-chan optimize.Task, tasks []optimize.Task)

Run ...

func (BoundsMethod) Status

func (m BoundsMethod) Status() (optimize.Status, error)

Status ...

func (BoundsMethod) Uses

func (m BoundsMethod) Uses(has optimize.Available) (uses optimize.Available, err error)

Uses ...

type Cov

type Cov interface {
	Cov(a, b []float64) float64
	Grad(a, b []float64) []float64
}

Cov calculates the covariance between a and b.

type Exploration

type Exploration interface {
	Estimate(gp *GP, minimize bool, x []float64) (float64, error)
}

Exploration is the strategy to use for exploring the Gaussian process.

type ExponentialParam

type ExponentialParam struct {
	Name     string
	Max, Min float64
	Rate     float64
}

ExponentialParam is an exponentially distributed parameter between 0 and in the range (0, +math.MaxFloat64] whose rate parameter (lambda) is Rate and whose mean is 1/lambda (1). The Max and Min parameters use discard sampling to find a point between them. Set them to be math.Inf(1) and math.Inf(-1) to disable the bounds.

func (ExponentialParam) GetMax

func (p ExponentialParam) GetMax() float64

GetMax implements Param.

func (ExponentialParam) GetMin

func (p ExponentialParam) GetMin() float64

GetMin implements Param.

func (ExponentialParam) GetName

func (p ExponentialParam) GetName() string

GetName implements Param.

func (ExponentialParam) Sample

func (p ExponentialParam) Sample() float64

Sample implements Param.

type GP

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

GP represents a gaussian process.

func NewGP

func NewGP(cov Cov, noise float64) *GP

NewGP creates a new Gaussian process with the specified covariance function (cov) and noise level (variance).

func (*GP) Add

func (gp *GP) Add(x []float64, y float64)

Add bulk adds XY pairs.

func (GP) Dims

func (gp GP) Dims() int

Dims ...

func (*GP) Estimate

func (gp *GP) Estimate(x []float64) (float64, float64, error)

Estimate returns the mean and standard deviation at the point x.

func (*GP) Gradient

func (gp *GP) Gradient(x []float64) ([]float64, error)

Gradient returns the gradient of the mean at the point x.

func (*GP) Maximum

func (gp *GP) Maximum() (x []float64, y float64)

Maximum returns the maximum value logged.

func (*GP) Minimum

func (gp *GP) Minimum() (x []float64, y float64)

Minimum returns the minimum value logged.

func (GP) Name

func (gp GP) Name(i int) string

Name ...

func (GP) OutputName

func (gp GP) OutputName() string

OutputName ...

func (GP) RawData

func (gp GP) RawData() ([][]float64, []float64)

RawData ...

func (*GP) SetNames

func (gp *GP) SetNames(inputs []string, output string)

SetNames ...

type LogBarrier

type LogBarrier struct{}

LogBarrier implements a logarithmic barrier function.

func (LogBarrier) Grad

func (LogBarrier) Grad(x []float64, params []Param) []float64

Grad returns the gradient of the barrier function.

func (LogBarrier) Val

func (LogBarrier) Val(x []float64, params []Param) float64

Val returns the value of the barrier function.

type MaternCov

type MaternCov struct{}

MaternCov calculates the covariance between a and b. nu = 2.5 https://en.wikipedia.org/wiki/Mat%C3%A9rn_covariance_function#Simplification_for_.CE.BD_half_integer

func (MaternCov) Cov

func (MaternCov) Cov(a, b []float64) float64

Cov implements Conv interface and calculates the covariance between a and b.

func (MaternCov) Grad

func (MaternCov) Grad(a, b []float64) []float64

Grad computes the gradient of the matern covariance between a and b with respect to a. nu = 2.5.

type NormalParam

type NormalParam struct {
	Name         string
	Max, Min     float64
	Mean, StdDev float64
}

NormalParam is a normally distributed parameter with Mean and StdDev. The Max and Min parameters use discard sampling to find a point between them. Set them to be math.Inf(1) and math.Inf(-1) to disable the bounds.

func (NormalParam) GetMax

func (p NormalParam) GetMax() float64

GetMax implements Param.

func (NormalParam) GetMin

func (p NormalParam) GetMin() float64

GetMin implements Param.

func (NormalParam) GetName

func (p NormalParam) GetName() string

GetName implements Param.

func (NormalParam) Sample

func (p NormalParam) Sample() float64

Sample implements Param.

type Optimizer

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

Optimizer is a blackbox gaussian process optimizer.

func NewOptimizer

func NewOptimizer(params []Param, opts ...OptimizerOption) *Optimizer

NewOptimizer creates a new optimizer with the specified optimizable parameters and options.

func (*Optimizer) ExplorationErr

func (o *Optimizer) ExplorationErr() error

ExplorationErr returns the error of exploration

func (*Optimizer) GP

func (o *Optimizer) GP() *GP

GP returns the underlying gaussian process. Primary for use with plotting behavior.

func (*Optimizer) Log

func (o *Optimizer) Log(x map[Param]float64, y float64)

Log adds given x and y to the gaussian process

func (*Optimizer) Next

func (o *Optimizer) Next() (x map[Param]float64, parallel bool, err error)

Next returns the next best x values to explore. If more than rounds have elapsed, nil is returned. If parallel is true, that round can happen in parallel to other rounds.

func (*Optimizer) Predict

func (o *Optimizer) Predict(X []map[Param]float64, Y []float64) (x map[Param]float64, err error)

Predict consumes all historical X and Y and predict the next x

func (*Optimizer) Rounds

func (o *Optimizer) Rounds() int

Rounds is the number of rounds that have been run.

func (*Optimizer) Run

func (o *Optimizer) Run(f func(map[Param]float64) float64) (x map[Param]float64, y float64, err error)

Run will call f the fewest times as possible while trying to maximize the output value. It blocks until all rounds have elapsed, or Stop is called.

func (*Optimizer) RunSerial

func (o *Optimizer) RunSerial(f func(map[Param]float64) float64) (x map[Param]float64, y float64, err error)

RunSerial will call f sequentially without parallelism. It blocks until all rounds have elapsed, or Stop is called.

func (*Optimizer) Running

func (o *Optimizer) Running() bool

Running returns whether or not the optimizer is running.

func (*Optimizer) Stop

func (o *Optimizer) Stop()

Stop stops Optimize.

type OptimizerOption

type OptimizerOption func(*Optimizer)

OptimizerOption sets an option on the optimizer.

func WithBarrierFunc

func WithBarrierFunc(bf BarrierFunc) OptimizerOption

WithBarrierFunc sets the barrier function to use.

func WithExploration

func WithExploration(exploration Exploration) OptimizerOption

WithExploration sets the exploration function to use.

func WithMinimize

func WithMinimize(minimize bool) OptimizerOption

WithMinimize sets whether or not to minimize. Passing false, maximizes instead.

func WithOutputName

func WithOutputName(name string) OptimizerOption

WithOutputName sets the outputs name. Only really matters if you're planning on using gp/plot.

func WithRandomRounds

func WithRandomRounds(rounds int) OptimizerOption

WithRandomRounds sets the number of random rounds to run.

func WithRounds

func WithRounds(rounds int) OptimizerOption

WithRounds sets the total number of rounds to run.

type Param

type Param interface {
	// GetName returns the name of the parameter.
	GetName() string
	// GetMax returns the maximum value.
	GetMax() float64
	// GetMin returns the minimum value.
	GetMin() float64
	// Sample returns a random point within the bounds. It doesn't have to be
	// uniformly distributed.
	Sample() float64
}

Param represents a parameter that can be optimized.

type RejectionParam

type RejectionParam struct {
	Param

	F func(x float64) float64
}

RejectionParam samples from Param and then uses F to decide whether or not to reject the sample. This is typically used with a UniformParam. F should output a value between 0 and 1 indicating the proportion of samples this point should be accepted. If F always outputs 0, Sample will get stuck in an infinite loop.

func (RejectionParam) Sample

func (p RejectionParam) Sample() float64

Sample implements Param.

type UCB

type UCB struct {
	Kappa float64
}

UCB implements upper confidence bound exploration.

func (UCB) Estimate

func (e UCB) Estimate(gp *GP, minimize bool, x []float64) (float64, error)

Estimate implements Exploration.

type UniformParam

type UniformParam struct {
	Name     string
	Max, Min float64
}

UniformParam is a uniformly distributed parameter between Max and Min.

func (UniformParam) GetMax

func (p UniformParam) GetMax() float64

GetMax implements Param.

func (UniformParam) GetMin

func (p UniformParam) GetMin() float64

GetMin implements Param.

func (UniformParam) GetName

func (p UniformParam) GetName() string

GetName implements Param.

func (UniformParam) Sample

func (p UniformParam) Sample() float64

Sample implements Param.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier