psokit

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: 14 Imported by: 0

Documentation

Overview

Package psokit is a toolkit for running a collection of, at the moment, experimental Set-based Particle Swarm Optimisers (SPSOs) in the package setpso. These SPSOs are used to find good and often optimal cost minimising subsets where the cost is a function of subsets of a finite collection of items, where the cost is a big integer so that in principle large and combinatorial difficult problems can be looked at. Please read https://github.com/mathrgo/setpso to give background information followed by the godoc documentation.

The main toolkit interface is ManPso; its instance is usually referred to as man which manages the sequence of runs to get some impression of how the chosen SPSO performs with the chosen cost function with Actions that are available for monitoring the runs. All these components (SPSO,Function,Actions) are chosen by name using various Select methods. There are short descriptions against each name and the toolkit provides methods for listing the names and their associated descriptions.

As well as inbuilt components there is an interface to generate new component creators together with their names and brief descriptions which can be moved to the inbuilt components at a later date. In this way a list of ready made components can be built up to deepen the test space for competing algorithms and monitoring methods.

An example of its use is given in the setpso subdirectory

setpso/example/runkit1

This includes the command line option reader Action. When run without arguments command line option help is displayed together with the list of available Actions. to do a single run try

go run runkit1.go -nrun 1

in the runkit1 directory.

Index

Examples

Constants

View Source
const DefaultFun = "subsetsum-0"

DefaultFun is the default cost function.

View Source
const DefaultPso = "gpso-0"

DefaultPso is the default SPSO.

Variables

This section is empty.

Functions

func FixLinAxis

func FixLinAxis(a *plot.Axis)

FixLinAxis attempts to give better min/max bounds for linear axis

func FloatingpointDifference

func FloatingpointDifference(x, y *big.Int) float64

FloatingpointDifference gives a measure of the difference of the integers x,y as sets and returns the result as a floating point value of the Hamming distance between x and y.

Types

type Act

type Act interface{}

Act is for arbitrary Action, which is placed in the appropriate slots based on its methods.

type ActData

type ActData interface{ DataUpdate(man *ManPso) }

ActData is the interface for data input/output actions that occur at fixed intervals of NThink() iterations. This is used to reduce the communication bandwidth for memory demanding actions such as plotting the progress of the swarm.

type ActInit

type ActInit interface{ Init(man *ManPso) }

ActInit is the interface for pre runs initializing Action. This is expected to be run before the cost-function and SPSO instances are available; it is used to configure man before the run commences; it typically is used to provide a command line interface or even include new things that are not part of the installed options.

type ActResult

type ActResult interface{ Result(man *ManPso) }

ActResult is the interface for post run Action.

type ActRunInit

type ActRunInit interface{ RunInit(man *ManPso) }

ActRunInit is the interface for a run initializing Action. It can change the cost-function and SPSO instances and even swap them if you are not interested in the runs being statistically independent!

type ActSummary

type ActSummary interface{ Summary(man *ManPso) }

ActSummary is the interface for post runs Action.

type ActUpdate

type ActUpdate interface{ Update(man *ManPso) }

ActUpdate is the interface for post Update per iteration Action. This may be used for ultra fine logging of data during a debug dump, update a variance calculation, stop the run before it reaches a maximum number of iterations or even change the nature of the function being optimized.

type CmdOptions

type CmdOptions struct{}

CmdOptions is the implementation of the Action, use-cmd-options. It provides the ability to change some of the man options using the command line. if no CmdOptions are chosen it prints out a list of defaults together with a list of Action names.

func (*CmdOptions) Init

func (cmd *CmdOptions) Init(man *ManPso)

Init reads the command options.

type CreateFun

type CreateFun interface{ Create(sd int64) Fun }

CreateFun is the interface for creating new instances of cost-functions.

type CreatePso

type CreatePso interface {
	Create(p0 *setpso.Pso, hu ...*setpso.PsoHeuristics) PsoInterface
}

CreatePso is the interface for creating new instances of SPSO.

type Fun

type Fun = setpso.Fun

Fun is the cost function interface from setpso.

type ManPso

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

ManPso manages the runs.

func NewMan

func NewMan() *ManPso

NewMan creates a default instance of ManPso.

Example
var fc myFun
man := NewMan()
// try adding creator to existing cost-function
if err := man.AddFun("subsetsum-0", "subsetsum of 50 elements", &fc); err != nil {
	fmt.Println(err)
}
// try selecting a non existent Fun
if err := man.SelectFun("subsetsum-1"); err != nil {
	fmt.Println(err)
}
//try deleting a cost-function that has not been added
if err := man.DelFun("subsetsum-0"); err != nil {
	fmt.Println(err)
}
// this should be the default
fmt.Println("\n==default man==")
fmt.Print(man)
// add a new  cost function
if err := man.AddFun("subsetsum-1", "subsetsum of 50 elements", &fc); err != nil {
	fmt.Println(err)
}
// select it
if err := man.SelectFun("subsetsum-1"); err != nil {
	fmt.Println(err)
}
// it is now managed by man
fmt.Println("\n===man with new cost-function==")
fmt.Print(man)
fmt.Print(man.FunDescription())

//delete a function that is added and selected for the runs
if err := man.DelFun("subsetsum-1"); err != nil {
	fmt.Println(err)
}
// it is now not managed by man which uses the default
fmt.Println("\n===man with default cost-function==")
fmt.Print(man)
fmt.Print(man.PsoDescription())
/* Output:
 *//
Output:

func (*ManPso) ActDescription

func (man *ManPso) ActDescription() string

ActDescription gives a description of Action by name.

func (*ManPso) AddAct

func (man *ManPso) AddAct(name, desc string, a Act) error

AddAct adds an Action instance creator to man for it to use later on. the Action has the name name and description desc. If the Action already exists it is not added and an error message is returned.

func (*ManPso) AddFun

func (man *ManPso) AddFun(name, desc string, f CreateFun) error

AddFun adds a cost function instance f with an assigned name to reference it by where desc is the description of the function. Note one cannot here reuse instance names. It returns true if successful. However, if there is a need to reuse a name that has been added, call DelFun() to remove it.

func (*ManPso) AddPso

func (man *ManPso) AddPso(name, desc string, p CreatePso) error

AddPso adds a SPSO instance creator p with an assigned name to reference it by where desc is the description of it. Note one cannot here reuse instance names. However, if there is a need to reuse a name that has been added, call DelFun() to remove it.

func (*ManPso) CreateFun

func (man *ManPso) CreateFun(name string) (f Fun)

CreateFun returns the cost-function instance based on its name and is called by man at the beginning of each run, so there is no need to call this if using man to execute the run sequence; you can use SelectFun() instead to prime man before a run.

If cost-function not found the the event is logged and it returns nil otherwise it sets up man to use the cost-function.

func (*ManPso) CreatePso

func (man *ManPso) CreatePso(name string) (p PsoInterface)

CreatePso returns the SPSO instance based on its name and is called by man at the beginning of a run, so there is no need to call this if using man to execute the run sequence; you can use SelectPso() instead.

If SPSO is not found the the event is logged and it returns nil otherwise it sets up man to use it . This also assumes the cost-function has been created for man beforehand using CreateFun().

func (*ManPso) Datalength

func (man *ManPso) Datalength() int

Datalength is the maximum number of data events per run used mainly for plotting.

func (*ManPso) DebugDump

func (man *ManPso) DebugDump() bool

DebugDump returns true when detailed output to file is used.

func (*ManPso) DelFun

func (man *ManPso) DelFun(name string) error

DelFun can be used to delete an added cost function thus freeing resources. it logs the event if not found. As a temporary measure man is set to use the Default cost function if name has been selected.

func (*ManPso) DelPso

func (man *ManPso) DelPso(name string) error

DelPso can be used to delete an added SPSO instance creator thus freeing resources.

func (*ManPso) Diter

func (man *ManPso) Diter() int

Diter returns the data output count during a run.

func (*ManPso) F

func (man *ManPso) F() Fun

F returns the instance of the cost-function in use for Actions during a run

func (*ManPso) FunCase

func (man *ManPso) FunCase() string

FunCase returns the cost-function case name.

func (*ManPso) FunDescription

func (man *ManPso) FunDescription() string

FunDescription gives a description of cost-function by name.

func (*ManPso) FunSeed

func (man *ManPso) FunSeed() (sd0, sd1 int64)

FunSeed returns the random generator seed components of the cost-function where seed=sd0+sd1*RunId()

func (*ManPso) Init

func (man *ManPso) Init()

Init sets up man with instance of cost-function and SPSO based on its settings. This is automatically called at the beginning of each run so is normally not explicitly called; it is exportable for those that need it. For instance it is used in some test examples where there is no need to run a case.

func (*ManPso) Iter

func (man *ManPso) Iter() int

Iter returns the iteration count during a run.

func (*ManPso) Npart

func (man *ManPso) Npart() int

Npart returns number of particles in SPSO instance.

func (*ManPso) Nrun

func (man *ManPso) Nrun() int

Nrun returns number of runs.

func (*ManPso) Nthink

func (man *ManPso) Nthink() int

Nthink is the number of thinking iterations between data output.

func (*ManPso) P

func (man *ManPso) P() PsoInterface

P returns the instance of the SPSO in use for Actions during a run

func (*ManPso) PsoCase

func (man *ManPso) PsoCase() string

PsoCase returns the SPSO case name.

func (*ManPso) PsoDescription

func (man *ManPso) PsoDescription() string

PsoDescription gives a description of SPSO by name.

func (*ManPso) PsoSeed

func (man *ManPso) PsoSeed() (sd0, sd1 int64)

PsoSeed returns the random generator seed components of SPSO where seed=sd0+sd1*RunId().

func (*ManPso) Run

func (man *ManPso) Run()

Run runs the chosen SPSO using the chosen cost-function and settings in man for Nrun() runs. Each run starts with a new cost-function and SPSO with different but computed random number seeds to aim at making each run independent of other runs in the sequence thus making it easy to generate moderately unbiased performance statistics.

During the runs chosen Actions are activated according to their interfaces.

func (*ManPso) RunID

func (man *ManPso) RunID() int

RunID returns run Number.

func (*ManPso) SelectActs

func (man *ManPso) SelectActs(ac ...string) error

SelectActs selects a list of actions by name to be added for used by man. Each Action is slotted into the runs where they have a capability to act.

Example
man := NewMan()
if err := man.SelectActs("print-result"); err != nil {
	fmt.Println(err)
}
// ensure there are instances to apply print-result
man.Init()
// go under the hood to see if SelectActs() has installed the action
r := man.actResult[0]
r.Result(man)
/* Output:
 *//
Output:

func (*ManPso) SelectFun

func (man *ManPso) SelectFun(name string) error

SelectFun informs man to consider using the named cost-function instance, name. it checks that there exists an instance creator and returns an error if it does not exist. There is no need to add a cost-function if you are using an inbuilt instance name.

func (*ManPso) SelectPso

func (man *ManPso) SelectPso(name string) error

SelectPso primes man to consider using the named SPSO instance creator, name, for the runs. It checks that there exists an instance creator and returns an error if it does not exist.There is no need to add a PSO using AddPso if you are using an inbuilt instance creator name.

func (*ManPso) SetDebugDump

func (man *ManPso) SetDebugDump(db bool)

SetDebugDump sets DebugDump flag.

func (*ManPso) SetFunCase

func (man *ManPso) SetFunCase(name string)

SetFunCase sets the cost-function case name.

func (*ManPso) SetFunSeed

func (man *ManPso) SetFunSeed(sd0, sd1 int64)

SetFunSeed sets the cost_function seed components.

func (*ManPso) SetNpart

func (man *ManPso) SetNpart(n int)

SetNpart sets Npart.

func (*ManPso) SetNrun

func (man *ManPso) SetNrun(n int)

SetNrun sets Nrun.

func (*ManPso) SetNthink

func (man *ManPso) SetNthink(n int)

SetNthink sets Nthink.

func (*ManPso) SetPsoCase

func (man *ManPso) SetPsoCase(name string)

SetPsoCase sets the SPSo case name.

func (*ManPso) SetPsoSeed

func (man *ManPso) SetPsoSeed(sd0, sd1 int64)

SetPsoSeed sets the SPSO seed components.

func (*ManPso) SetStopAt

func (man *ManPso) SetStopAt(n int)

SetStopAt sets StopAt.

func (*ManPso) Setdatalength

func (man *ManPso) Setdatalength(n int)

Setdatalength sets datalength.

func (*ManPso) StopAt

func (man *ManPso) StopAt() int

StopAt returns iteration number to stop at when debug = true.

func (*ManPso) String

func (man *ManPso) String() string

String gives a description of the man settings

type PlotPersonalBest

type PlotPersonalBest struct {
	*ResultsArray
}

PlotPersonalBest plots the personal best costs of each Particle during a run. It implements the plot-personal-best Action.

func (*PlotPersonalBest) DataUpdate

func (pl *PlotPersonalBest) DataUpdate(man *ManPso)

DataUpdate loads personal best costs into plot

func (*PlotPersonalBest) Result

func (pl *PlotPersonalBest) Result(man *ManPso)

Result generates the plots of the personal best and puts it into the file of the form:

plotFbits(Cost)<run ID>.pdf

func (*PlotPersonalBest) RunInit

func (pl *PlotPersonalBest) RunInit(man *ManPso)

RunInit setup plotting arrays for the run

type Printheading

type Printheading struct{}

Printheading used as the Action, print-headings

func (*Printheading) Init

func (a *Printheading) Init(man *ManPso)

Init prints man settings

func (*Printheading) RunInit

func (a *Printheading) RunInit(man *ManPso)

RunInit outputs information about the cost-function. If the seed does not vary between runs it only outputs on the first run

type Printresult

type Printresult struct{}

Printresult used as the Action, print-result

func (*Printresult) Result

func (a *Printresult) Result(man *ManPso)

Result just prints the run result as cost and decoded subset

type PsoInterface

type PsoInterface = setpso.PsoInterface

PsoInterface is the PsoInterface from setpso.

type ResultsArray

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

ResultsArray is a structure for storing and plotting results from Each particle

func NewResultsArray

func NewResultsArray(datalength, dimension int) *ResultsArray

NewResultsArray creates a Results Array and returns a pointer to it. It consists of datalength data entries for dimension values per entry.

func (*ResultsArray) NewPlot

func (r *ResultsArray) NewPlot(yaxisname, title string, runid int)

NewPlot creates a basic plot. yaxisname is the Y-axis label; title is the plot title; runid is the run ID.

func (*ResultsArray) ResUpdate

func (r *ResultsArray) ResUpdate(val float64, dataID, valID, iterID int)

ResUpdate puts val into the plotting results array for value index valueID and data slot dataID where valID is the number of iterations so far in a run.

type RunProgress

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

RunProgress output progress statements during the run

func (*RunProgress) DataUpdate

func (a *RunProgress) DataUpdate(man *ManPso)

DataUpdate checks progress and prints out a change in progress

func (*RunProgress) RunInit

func (a *RunProgress) RunInit(man *ManPso)

RunInit initializes the progres counter

Jump to

Keyboard shortcuts

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