Documentation ¶
Overview ¶
Package futil provides some usefull utilities for cost functions
Index ¶
- Constants
- type CmpMode
- type FloatFun
- type FloatFunStub
- func (f *FloatFunStub) Cmp(x, y Try, mode CmpMode) float64
- func (f *FloatFunStub) Copy(dest, src Try)
- func (f *FloatFunStub) Fun() FloatFun
- func (f *FloatFunStub) NewTry() Try
- func (f *FloatFunStub) SetTry(t Try, z *big.Int)
- func (f *FloatFunStub) ToConstraint(pre Try, hint *big.Int) bool
- func (f *FloatFunStub) UpdateCost(t Try)
- type FloatTry
- func (t *FloatTry) Cmp(s *FloatTry) float64
- func (t *FloatTry) Cost() string
- func (t *FloatTry) CostValue() float64
- func (t *FloatTry) Data() TryData
- func (t *FloatTry) Decode() string
- func (t *FloatTry) Fbits() float64
- func (t *FloatTry) Parameter() *big.Int
- func (t *FloatTry) SetCostValue(c float64)
- type Fun
- type IntFun
- type IntFunStub
- type IntTry
- type S0FloatCostValue
- type SFloatCostValue
- type SFloatFun
- type SFloatFunStub
- func (f *SFloatFunStub) Cmp(x, y Try, mode CmpMode) float64
- func (f *SFloatFunStub) Copy(dest, src Try)
- func (f *SFloatFunStub) Fun() SFloatFun
- func (f *SFloatFunStub) NewTry() Try
- func (f *SFloatFunStub) SetTry(t Try, z *big.Int)
- func (f *SFloatFunStub) ToConstraint(pre Try, hint *big.Int) bool
- func (f *SFloatFunStub) UpdateCost(t Try)
- type SFloatTry
- type Splitter
- type Try
- type TryData
Examples ¶
Constants ¶
const W = bits.UintSize
W is assumed size of word used in big int array
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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) 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 ¶
NewFloatTry is a convenience function for generating an new floating point costed try.
func (*FloatTry) Fbits ¶
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) SetCostValue ¶
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) 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 (*IntTry) Fbits ¶
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) SetCostValue ¶
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 ¶
func (c *S0FloatCostValue) Cmp(c1 *S0FloatCostValue) float64
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) 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) 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 ¶
NewSFloatTry is a convenience function for generating an new floating point costed try. Tc is the cost update timeconstant in iterations
func (*SFloatTry) Fbits ¶
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) SetCostValue ¶
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 ¶
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 ¶
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) Split ¶
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:
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.