futil

package
v0.0.0-...-f1b6caa Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2022 License: LGPL-2.1 Imports: 4 Imported by: 0

Documentation

Overview

Package futil provides some usefull utilities for cost functions

Index

Examples

Constants

W is assumed size of word used in big int array

Variables

This section is empty.

Functions

This section is empty.

Types

type CmpMode

type CmpMode int

CmpMode indicates modes for Cmp.

const (
	//CostMode :Cmp compares costs
	CostMode CmpMode = iota
	//TriesMode : Cmp compares tries with best try
	TriesMode
)

type FloatFun

type FloatFun interface {
	Fun
	// calculates the cost of the try using the decoded data, returning  the cost
	Cost(data TryData) float64
}

FloatFun is the interface for big int costed function

type FloatFunStub

type FloatFunStub struct {
	FloatFun
}

FloatFunStub uses FloatFun interface to create the setpso.Fun interface

func NewFloatFunStub

func NewFloatFunStub(f FloatFun) *FloatFunStub

NewFloatFunStub creates an instance of the FloatFunStub ready for use as the interface setpso.Fun

func (*FloatFunStub) Cmp

func (f *FloatFunStub) Cmp(x, y Try, mode CmpMode) float64

Cmp compares the tries

func (*FloatFunStub) Copy

func (f *FloatFunStub) Copy(dest, src Try)

Copy copies src to dest

func (*FloatFunStub) Fun

func (f *FloatFunStub) Fun() FloatFun

Fun retrieves the internal cost function

func (*FloatFunStub) NewTry

func (f *FloatFunStub) NewTry() Try

NewTry creates a try as an FloatTry

func (*FloatFunStub) SetTry

func (f *FloatFunStub) SetTry(t Try, z *big.Int)

SetTry sets try to a new parameter z

func (*FloatFunStub) ToConstraint

func (f *FloatFunStub) ToConstraint(pre Try, hint *big.Int) bool

ToConstraint uses the previous try pre and the updating hint parameter to attempt to produce an update to pre which satisfies solution constraints it returns valid = True if succeeds, otherwise pre remains un changed and returns false

func (*FloatFunStub) UpdateCost

func (f *FloatFunStub) UpdateCost(t Try)

UpdateCost recalculates the try cost

type FloatTry

type FloatTry struct {
	TryData
	// contains filtered or unexported fields
}

FloatTry is the data type used to store floating point costed try where the cost is a function of the parameter x.

func NewFloatTry

func NewFloatTry(z *big.Int, data TryData) *FloatTry

NewFloatTry is a convenience function for generating an new floating point costed try.

func (*FloatTry) Cmp

func (t *FloatTry) Cmp(s *FloatTry) float64

Cmp compares the cost of t with s where t is of type *FloatTry

func (*FloatTry) Cost

func (t *FloatTry) Cost() string

Cost returns a human readable cost description

func (*FloatTry) CostValue

func (t *FloatTry) CostValue() float64

CostValue returns the stored cost value

func (*FloatTry) Data

func (t *FloatTry) Data() TryData

Data returns the decoded data

func (*FloatTry) Decode

func (t *FloatTry) Decode() string

Decode gives a human readable description of decoded try data

func (*FloatTry) Fbits

func (t *FloatTry) Fbits() float64

Fbits gives a floating point measure of number of bits in the cost that takes on non integer values to help represent big integer cost size for plotting. it approximates to the log of the big integer.

func (*FloatTry) Parameter

func (t *FloatTry) Parameter() *big.Int

Parameter reads the try value

func (*FloatTry) SetCostValue

func (t *FloatTry) SetCostValue(c float64)

SetCostValue is used to set the cost value.

type Fun

type Fun interface {
	// creates an empty try data store for the decoded part of the try
	CreateData() TryData
	// returns a default parameter which should satisfy constraints
	DefaultParam() *big.Int
	// copies try data from src to dest
	CopyData(dest, src TryData)
	// maximum number of bits used in the try parameter
	MaxLen() int
	//description of the function
	About() string
	//attempts to update hint to give a constraint satisfying try parameter possibly with the help of pre which is assumed to be constraint satisfying. Note it is rare that pre is needed and pre should be un modified by this function
	Constraint(pre TryData, hint *big.Int) (valid bool)
	// Delete hints to the function to remove/replace the ith item
	Delete(i int) bool

	// Idecode decodes the parameter z and stores the result in data.
	IDecode(data TryData, z *big.Int)
}

Fun is the common cost function interface for manipulating TryData

type IntFun

type IntFun interface {
	Fun
	// calculates the cost of the try using the decoded data, returning  the result in cost
	Cost(data TryData, cost *big.Int)
}

IntFun is the interface for big int costed function

type IntFunStub

type IntFunStub struct {
	IntFun
	// contains filtered or unexported fields
}

IntFunStub uses IntFun interface to create the setpso.Fun interface

func NewIntFunStub

func NewIntFunStub(f IntFun) *IntFunStub

NewIntFunStub creates an instance of the IntFunStub ready for use as the interface setpso.Fun

func (*IntFunStub) Cmp

func (f *IntFunStub) Cmp(x, y Try, mode CmpMode) float64

Cmp compares the tries

func (*IntFunStub) Copy

func (f *IntFunStub) Copy(dest, src Try)

Copy copies src to dest

func (*IntFunStub) NewTry

func (f *IntFunStub) NewTry() Try

NewTry creates a try as an IntTry

func (*IntFunStub) SetTry

func (f *IntFunStub) SetTry(t Try, z *big.Int)

SetTry sets try to a new parameter z

func (*IntFunStub) ToConstraint

func (f *IntFunStub) ToConstraint(pre Try, hint *big.Int) bool

ToConstraint uses the previous try pre and the updating hint parameter to attempt to produce an update to pre which satisfies solution constraints it returns valid = True if succeeds, otherwise pre remains un changed and returns false

func (*IntFunStub) UpdateCost

func (f *IntFunStub) UpdateCost(t Try)

UpdateCost recalculates the try cost

type IntTry

type IntTry struct {
	TryData
	// contains filtered or unexported fields
}

IntTry is the data type used to store big integer costed try.

func NewIntTry

func NewIntTry(z *big.Int, data TryData) *IntTry

NewIntTry is a convenience function for generating an new big int costed try.

func (*IntTry) Cmp

func (t *IntTry) Cmp(s *IntTry) float64

Cmp compares the cost of x with c where x is of type *IntTry

func (*IntTry) Cost

func (t *IntTry) Cost() string

Cost returns a human readable cost description

func (*IntTry) CostValue

func (t *IntTry) CostValue() *big.Int

CostValue returns the stored cost value

func (*IntTry) Data

func (t *IntTry) Data() TryData

Data returns the decoded data

func (*IntTry) Decode

func (t *IntTry) Decode() string

Decode gives a human readable description of decoded try data

func (*IntTry) Fbits

func (t *IntTry) Fbits() float64

Fbits gives a floating point measure of number of bits in the cost that takes on non integer values to help represent big integer cost size for plotting. it approximates to the log of the big integer.

func (*IntTry) Parameter

func (t *IntTry) Parameter() *big.Int

Parameter reads the try value

func (*IntTry) SetCostValue

func (t *IntTry) SetCostValue(c *big.Int)

SetCostValue is used to set the cost value.

type S0FloatCostValue

type S0FloatCostValue struct {

	// Time constant
	Tc float64
	// contains filtered or unexported fields
}

S0FloatCostValue is an old data type that is no longer used to store Float cost values based on simple statistics. Internally it maintains an mean as the cost plus the

variance of the cost, which is used to determine if one cost is bigger than
another. This is communicated using the Cmp function. As well as this it uses an updating weight that incorporates a forgetting process that allows for
gradual change to the cost function itself.

func NewS0FloatCostValue

func NewS0FloatCostValue(Tc float64) *S0FloatCostValue

NewS0FloatCostValue returns a pointer to S0FloatCostValue type initialized with a time constant to stats change of Tc.

func (*S0FloatCostValue) Cmp

Cmp compares with x

func (*S0FloatCostValue) Copy

func (c *S0FloatCostValue) Copy(c1 *S0FloatCostValue)

Copy takes a copy of c1

func (*S0FloatCostValue) Fbits

func (c *S0FloatCostValue) Fbits() float64

Fbits scales the cost value by taking sign(x.mean)log2(1+|x.mean|)

func (*S0FloatCostValue) Set

func (c *S0FloatCostValue) Set(x float64)

Set is used to set c from x

func (*S0FloatCostValue) String

func (c *S0FloatCostValue) String() string

String gives human readable description

func (*S0FloatCostValue) Update

func (c *S0FloatCostValue) Update(x float64)

Update adds the mean of x as raw data to calculate an updated stats of the cost value

type SFloatCostValue

type SFloatCostValue struct {

	// Time constant
	Tc float64
	// contains filtered or unexported fields
}

SFloatCostValue is the data type used to store Float cost values based

on simple statistics.
Internally it maintains an mean as the cost plus a count of  number of times the mean has been sampled as well as a success count  , which is used to determine how two costs compare.
Such things are communicated using the Cmp function.
As well as this it uses an updating weight that incorporates a forgetting process that allows for gradual change to the cost function itself.

func NewSFloatCostValue

func NewSFloatCostValue(Tc float64) *SFloatCostValue

NewSFloatCostValue returns a pointer to SFloatCostValue type initialized with a time constant to stats change of Tc.

Example
Tc := 100.0
c := NewSFloatCostValue(Tc)
c.Set(10.0)
c.Update(15)
fmt.Printf("c = %v fbits=%f\n", c, c.Fbits())
/* Output:
 *//
Output:

func (*SFloatCostValue) Cmp

func (c *SFloatCostValue) Cmp(c1 *SFloatCostValue, mode CmpMode) float64

Cmp compares with x

func (*SFloatCostValue) Copy

func (c *SFloatCostValue) Copy(c1 *SFloatCostValue)

Copy takes a copy of c1

func (*SFloatCostValue) Fbits

func (c *SFloatCostValue) Fbits() float64

Fbits scales the cost value by taking sign(x.mean)log2(1+|x.mean|)

func (*SFloatCostValue) Set

func (c *SFloatCostValue) Set(x float64)

Set is used to set c from x

func (*SFloatCostValue) String

func (c *SFloatCostValue) String() string

String gives human readable description

func (*SFloatCostValue) Update

func (c *SFloatCostValue) Update(x float64)

Update adds the mean of x as raw data to calculate an updated stats of the cost value

type SFloatFun

type SFloatFun interface {
	Fun
	// calculates the cost of the try using the decoded data, returning  the cost
	Cost(data TryData) float64
}

SFloatFun is the interface for big int costed function

type SFloatFunStub

type SFloatFunStub struct {
	SFloatFun
	// initial time  constant of try cost updates
	Tc float64
	//comparison margin in sigmas
	SigmaMargin float64
}

SFloatFunStub uses SFloatFun interface to create the setpso.Fun interface

func NewSFloatFunStub

func NewSFloatFunStub(f SFloatFun, Tc, SigmaMargin float64) *SFloatFunStub

NewSFloatFunStub creates an instance of the SFloatFunStub ready for use as the interface setpso.Fun. Tc is the initial try cost update time constant.

func (*SFloatFunStub) Cmp

func (f *SFloatFunStub) Cmp(x, y Try, mode CmpMode) float64

Cmp compares the tries

func (*SFloatFunStub) Copy

func (f *SFloatFunStub) Copy(dest, src Try)

Copy copies src to dest

func (*SFloatFunStub) Fun

func (f *SFloatFunStub) Fun() SFloatFun

Fun retrieves the internal cost function

func (*SFloatFunStub) NewTry

func (f *SFloatFunStub) NewTry() Try

NewTry creates a try as an SFloatTry

func (*SFloatFunStub) SetTry

func (f *SFloatFunStub) SetTry(t Try, z *big.Int)

SetTry sets try to a new parameter z

func (*SFloatFunStub) ToConstraint

func (f *SFloatFunStub) ToConstraint(pre Try, hint *big.Int) bool

ToConstraint uses the previous try pre and the updating hint parameter to attempt to produce an update to pre which satisfies solution constraints it returns valid = True if succeeds, otherwise pre remains un changed and returns false

func (*SFloatFunStub) UpdateCost

func (f *SFloatFunStub) UpdateCost(t Try)

UpdateCost recalculates the try cost

type SFloatTry

type SFloatTry struct {
	TryData
	// contains filtered or unexported fields
}

SFloatTry is the data type used to store floating point costed try where the cost is a function of the parameter x.

func NewSFloatTry

func NewSFloatTry(z *big.Int, data TryData, Tc float64) *SFloatTry

NewSFloatTry is a convenience function for generating an new floating point costed try. Tc is the cost update timeconstant in iterations

func (*SFloatTry) Cmp

func (t *SFloatTry) Cmp(s *SFloatTry, mode CmpMode) float64

Cmp compares the cost of t with s where t is of type *SFloatTry

func (*SFloatTry) Cost

func (t *SFloatTry) Cost() string

Cost returns a human readable cost description

func (*SFloatTry) Data

func (t *SFloatTry) Data() TryData

Data returns the decoded data

func (*SFloatTry) Decode

func (t *SFloatTry) Decode() string

Decode gives a human readable description of decoded try data

func (*SFloatTry) Fbits

func (t *SFloatTry) Fbits() float64

Fbits gives a floating point measure of number of bits in the cost that takes on non integer values to help represent big integer cost size for plotting. it approximates to the log of the big integer.

func (*SFloatTry) Parameter

func (t *SFloatTry) Parameter() *big.Int

Parameter reads the try value

func (*SFloatTry) SetCostValue

func (t *SFloatTry) SetCostValue(c float64)

SetCostValue is used to set the cost value.

type Splitter

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

Splitter is used to split a positive big int up into an array of big ints parts formed from the big int. For Computational simplicity the original integer absolute value is split up into sub slices of big.Word (assumed to be of uint); the sign of the original big int is not used.

func NewSplitter

func NewSplitter(bits ...int) *Splitter

NewSplitter creates a new Splitter. Bits is an array of number of used bits for each split element. T

Example
s := NewSplitter(2, 64, 67)
fmt.Printf("%v\n", s)
fmt.Println("----MaxBits() test-----")
fmt.Printf("max number of bits: %d\n", s.MaxBits())
/* Output:
 *//
Output:

func (*Splitter) Join

func (sp *Splitter) Join(parts []*big.Int, x *big.Int) (err error)

Join creates a positive big int from a list of word aligned parts using splitter bit sizes and writes the result into x. it returns an error if the parts are too large to fit or number of parts is not compatible. Also when there is an error x remains unchanged.

Example
parts := make([]*big.Int, 3)
parts[0] = big.NewInt(5)
parts[1] = big.NewInt(10)
parts[2] = big.NewInt(255)
x := big.NewInt(2)
sp := NewSplitter(4, 4, 8)
err := sp.Join(parts, x)
fmt.Printf("x: %x\n", x)
if err != nil {
	fmt.Print(err)
}
/* Output:
 *//
Output:

func (*Splitter) MaxBits

func (sp *Splitter) MaxBits() int

MaxBits returns the maximum bits used by the splitter.

func (*Splitter) Split

func (sp *Splitter) Split(x *big.Int, parts []*big.Int) []*big.Int

Split takes a copy of the absolute value of x and splits it up into positive big int parts in place ensuring the parts are word aligned. While doing this it modifies the copy to match the splitting so that each part is a sub slice of the modified copy. To do this unused padding bits are set to zero. .The parts array is overwritten and is mapped into this copy.

Example
var parts []*big.Int
x := big.NewInt(7)
sp := NewSplitter(2, 5)
parts = sp.Split(x, parts)
fmt.Printf("x: %x\n", x)
fmt.Println("parts:")
for i := range parts {
	fmt.Printf("%v \n", parts[i])
}
/* Output:
 *//
Output:

func (*Splitter) String

func (sp *Splitter) String() string

String gives a readable description of internal state mainly for diagnosis.

type Try

type Try interface {
	// returns floating point representation of
	// of number of bits needed to represent cost as if an integer.
	Fbits() float64
	// subset parameter used by Try
	Parameter() *big.Int
	// this  gives a human readable interpretation of Try based on the internal decoding of the Parameter
	Decode() string
	//this gives human readable cost details such as variance ....
	Cost() string
	// decoded data part
	Data() TryData
}

Try is an interface that is used by a structure to store a big integer parameter, internal decoding of the parameter, and current evaluated cost of a particular try at finding a good solution to the optimization.The interface is usually used to store data that depends on the parameter and is mainly manipulated by the cost function.

type TryData

type TryData interface {
	// Decode gives a human readable description of TryData content
	Decode() string
}

TryData is used to store internal decoded data of a try.

Jump to

Keyboard shortcuts

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