pso

package
v0.0.0-...-94a9b73 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

README

This sample workflow demos a long iterative math optimization process using particle swarm optimization (PSO).

The workflow first does some data structure initialization and then runs many iterations using a child workflow. The child workflow runs 10 iterations and then uses ContinueAsNew to avoid to store too long history in the Temporal database. In case of recovery the whole history has to be replayed to reconstruct the workflow state. So if history is too large the recover can take very long time. Each particle is processed in parallel using worflow.Go and the math grunt work is done in the activites. Since the data structure that maintains the optimization state has to be passed to the child workflow and the activities, a custom DataConverter has been implemented to take care of serialization/deserialization. Also the query API is supported to get the current state of running workflow.

Steps to run this sample:

  1. You need a Temporal service running. See details in README.md
  2. Run the following command multiple times on different console window. This is to simulate running workers on multiple different machines.
go run pso/worker/main.go
  1. Run the following command to submit a start request for this PSO workflow.
go run pso/starter/main.go
  1. Query the call stack for the workflow with
go run pso/query/main.go -w <workflow_id from step 3> -r <run_id from step 3>

Documentation

Index

Constants

View Source
const (
	InitParticleActivityName   = "initParticleActivityName"
	UpdateParticleActivityName = "updateParticleActivityName"
)

*

  • Sample activities used by file processing sample workflow.
View Source
const ContinueAsNewStr = "CONTINUEASNEW"

Variables

View Source
var ActivityOptions = workflow.ActivityOptions{
	StartToCloseTimeout: 10 * time.Minute,
	HeartbeatTimeout:    2 * time.Second,
	RetryPolicy: &temporal.RetryPolicy{
		InitialInterval:    time.Second,
		BackoffCoefficient: 2.0,
		MaximumInterval:    time.Minute,
		MaximumAttempts:    5,
	},
}

ActivityOptions can be reused

View Source
var Griewank = ObjectiveFunction{

	Goal:     1e-5,
	Evaluate: EvalGriewank,
	// contains filtered or unexported fields
}
View Source
var Rosenbrock = ObjectiveFunction{

	Goal:     1e-5,
	Evaluate: EvalRosenbrock,
	// contains filtered or unexported fields
}
View Source
var Sphere = ObjectiveFunction{

	Goal:     1e-5,
	Evaluate: EvalSphere,
	// contains filtered or unexported fields
}

Functions

func CalculateSwarmSize

func CalculateSwarmSize(dim, max_size int) int

func EvalGriewank

func EvalGriewank(vec []float64) float64

func EvalRosenbrock

func EvalRosenbrock(vec []float64) float64

func EvalSphere

func EvalSphere(vec []float64) float64

func NewJSONDataConverter

func NewJSONDataConverter() converter.DataConverter

NewJSONDataConverter creates a json data converter

func PSOWorkflow

func PSOWorkflow(ctx workflow.Context, functionName string) (string, error)

PSOWorkflow workflow definition

Types

type ObjectiveFunction

type ObjectiveFunction struct {
	Goal     float64                     // optimization goal (error threshold)
	Evaluate func(vec []float64) float64 // the objective function
	// contains filtered or unexported fields
}

func FunctionFactory

func FunctionFactory(functionName string) ObjectiveFunction

type Particle

type Particle struct {
	Position *Position
	Pbest    *Position
	Velocity Vector
}

func InitParticleActivity

func InitParticleActivity(ctx context.Context, swarm Swarm) (Particle, error)

func NewParticle

func NewParticle(swarm *Swarm) *Particle

func UpdateParticleActivity

func UpdateParticleActivity(ctx context.Context, swarm Swarm, particleIdx int) (Particle, error)

func (*Particle) UpdateFitness

func (particle *Particle) UpdateFitness(swarm *Swarm)

func (*Particle) UpdateLocation

func (particle *Particle) UpdateLocation(swarm *Swarm)

type ParticleResult

type ParticleResult struct {
	Position
	Step int
}

type Position

type Position struct {
	Location Vector
	Fitness  float64
}

func NewPosition

func NewPosition(dim int) *Position

func RandomPosition

func RandomPosition(function ObjectiveFunction, rng *rand.Rand) *Position

func (*Position) Copy

func (position *Position) Copy() *Position

func (*Position) IsBetterThan

func (position *Position) IsBetterThan(other *Position) bool

type Swarm

type Swarm struct {
	Settings  *SwarmSettings
	Gbest     *Position
	Particles []*Particle
}

func NewSwarm

func NewSwarm(ctx workflow.Context, settings *SwarmSettings) (*Swarm, error)

func (*Swarm) Run

func (swarm *Swarm) Run(ctx workflow.Context, step int) (ParticleResult, error)

type SwarmSettings

type SwarmSettings struct {
	FunctionName string

	// swarm size (number of particles)
	Size int
	// ... N steps (set to 0 for no output)
	PrintEvery int
	// Steps after issuing a ContinueAsNew, to reduce history size
	ContinueAsNewEvery int
	// maximum number of iterations
	Steps int
	// cognitive coefficient
	C1 float64
	// social coefficient
	C2 float64
	// max inertia weight value
	InertiaMax float64
	// min inertia weight value
	InertiaMin float64
	// whether to keep particle position within defined bounds (TRUE)
	// or apply periodic boundary conditions (FALSE)
	ClampPosition bool

	Inertia float64 // current inertia weight value
	// contains filtered or unexported fields
}

func PSODefaultSettings

func PSODefaultSettings(functionName string) *SwarmSettings

type Vector

type Vector []float64

type WorkflowResult

type WorkflowResult struct {
	Msg     string // Uppercase the members otherwise serialization won't work!
	Success bool
}

func PSOChildWorkflow

func PSOChildWorkflow(ctx workflow.Context, swarm Swarm, startingStep int) (WorkflowResult, error)

PSOChildWorkflow workflow definition Returns true if the optimization has converged

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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