simulator

package
v0.0.0-...-4ec6d13 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 18 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var File_app_src_dashboard_state_proto protoreflect.FileDescriptor

Functions

This section is empty.

Types

type ConstantTimestepFunction

type ConstantTimestepFunction struct {
	Stepsize float64
}

ConstantTimestepFunction iterates the timestep by a constant stepsize.

func (*ConstantTimestepFunction) SetNextIncrement

func (t *ConstantTimestepFunction) SetNextIncrement(
	timestepsHistory *CumulativeTimestepsHistory,
) *CumulativeTimestepsHistory

type ConstantValuesIteration

type ConstantValuesIteration struct {
}

ConstantValuesIteration leaves the values set by the initial conditions unchanged for all time.

func (*ConstantValuesIteration) Configure

func (c *ConstantValuesIteration) Configure(
	partitionIndex int,
	settings *Settings,
)

func (*ConstantValuesIteration) Iterate

func (c *ConstantValuesIteration) Iterate(
	params *OtherParams,
	partitionIndex int,
	stateHistories []*StateHistory,
	timestepsHistory *CumulativeTimestepsHistory,
) []float64

type CumulativeTimestepsHistory

type CumulativeTimestepsHistory struct {
	NextIncrement     float64
	Values            *mat.VecDense
	CurrentStepNumber int
	StateHistoryDepth int
}

CumulativeTimestepsHistory is a windowed history of cumulative timestep values which includes the next value to increment time by and number of steps taken.

type DashboardPartitionState

type DashboardPartitionState struct {
	CumulativeTimesteps float64   `protobuf:"fixed64,1,opt,name=cumulative_timesteps,json=cumulativeTimesteps,proto3" json:"cumulative_timesteps,omitempty"`
	PartitionIndex      int64     `protobuf:"varint,2,opt,name=partition_index,json=partitionIndex,proto3" json:"partition_index,omitempty"`
	State               []float64 `protobuf:"fixed64,3,rep,packed,name=state,proto3" json:"state,omitempty"`
	// contains filtered or unexported fields
}

func (*DashboardPartitionState) Descriptor deprecated

func (*DashboardPartitionState) Descriptor() ([]byte, []int)

Deprecated: Use DashboardPartitionState.ProtoReflect.Descriptor instead.

func (*DashboardPartitionState) GetCumulativeTimesteps

func (x *DashboardPartitionState) GetCumulativeTimesteps() float64

func (*DashboardPartitionState) GetPartitionIndex

func (x *DashboardPartitionState) GetPartitionIndex() int64

func (*DashboardPartitionState) GetState

func (x *DashboardPartitionState) GetState() []float64

func (*DashboardPartitionState) ProtoMessage

func (*DashboardPartitionState) ProtoMessage()

func (*DashboardPartitionState) ProtoReflect

func (x *DashboardPartitionState) ProtoReflect() protoreflect.Message

func (*DashboardPartitionState) Reset

func (x *DashboardPartitionState) Reset()

func (*DashboardPartitionState) String

func (x *DashboardPartitionState) String() string

type EmbeddedSimulationRunIteration

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

EmbeddedSimulationRunIteration facilitates running an embedded sub-simulation to termination inside of an iteration of another simulation for each step of the latter simulation.

func NewEmbeddedSimulationRunIteration

func NewEmbeddedSimulationRunIteration(
	settings *Settings,
	implementations *Implementations,
) *EmbeddedSimulationRunIteration

NewEmbeddedSimulationRunIterationFromConfigs creates a new EmbeddedSimulationRunIteration from settings and implementations configs.

func (*EmbeddedSimulationRunIteration) Configure

func (e *EmbeddedSimulationRunIteration) Configure(
	partitionIndex int,
	settings *Settings,
)

func (*EmbeddedSimulationRunIteration) Iterate

func (e *EmbeddedSimulationRunIteration) Iterate(
	params *OtherParams,
	partitionIndex int,
	stateHistories []*StateHistory,
	timestepsHistory *CumulativeTimestepsHistory,
) []float64

type EveryNStepsOutputCondition

type EveryNStepsOutputCondition struct {
	N int
	// contains filtered or unexported fields
}

EveryStepOutputCondition calls the OutputFunction once for every N steps that occur.

func (*EveryNStepsOutputCondition) IsOutputStep

func (c *EveryNStepsOutputCondition) IsOutputStep(
	partitionIndex int,
	state []float64,
	cumulativeTimesteps float64,
) bool

type EveryStepOutputCondition

type EveryStepOutputCondition struct{}

EveryStepOutputCondition calls the OutputFunction at every step.

func (*EveryStepOutputCondition) IsOutputStep

func (c *EveryStepOutputCondition) IsOutputStep(
	partitionIndex int,
	state []float64,
	cumulativeTimesteps float64,
) bool

type ExponentialDistributionTimestepFunction

type ExponentialDistributionTimestepFunction struct {
	Mean float64
	Seed uint64
	// contains filtered or unexported fields
}

ExponentialDistributionTimestepFunction iterates the timestep by a new sample drawn from an exponential distribution with hyperparameters set by Mean and Seed.

func NewExponentialDistributionTimestepFunction

func NewExponentialDistributionTimestepFunction(
	mean float64,
	seed uint64,
) *ExponentialDistributionTimestepFunction

New ExponentialDistributionTimestepFunction creates a new ExponentialDistributionTimestepFunction given a mean and seed.

func (*ExponentialDistributionTimestepFunction) SetNextIncrement

type ImplementationStrings

type ImplementationStrings struct {
	Partitions           []PartitionStrings `yaml:"partitions"`
	OutputCondition      string             `yaml:"output_condition"`
	OutputFunction       string             `yaml:"output_function"`
	TerminationCondition string             `yaml:"termination_condition"`
	TimestepFunction     string             `yaml:"timestep_function"`
}

ImplementationStrings is the yaml-loadable config which consists of string type names to insert into templating.

type Implementations

type Implementations struct {
	Partitions           []Partition
	OutputCondition      OutputCondition
	OutputFunction       OutputFunction
	TerminationCondition TerminationCondition
	TimestepFunction     TimestepFunction
}

Implementations defines all of the types that must be implemented in order to configure a stochastic process defined by the stochadex.

type Iteration

type Iteration interface {
	Configure(partitionIndex int, settings *Settings)
	Iterate(
		params *OtherParams,
		partitionIndex int,
		stateHistories []*StateHistory,
		timestepsHistory *CumulativeTimestepsHistory,
	) []float64
}

Iteration is the interface that must be implemented for any stochastic phenomenon within the stochadex. Its .Iterate method reads in an OtherParams struct, a int partitionIndex, the full current history of the process defined by a slice []*StateHistory and a CumulativeTimestepsHistory reference and outputs an updated state history row in the form of a float64 slice.

type IteratorInputMessage

type IteratorInputMessage struct {
	StateHistories   []*StateHistory
	TimestepsHistory *CumulativeTimestepsHistory
}

IteratorInputMessage defines the message which is passed from the PartitionCoordinator to a StateIterator of a given partition when the former is requesting the latter to perform a job.

type JsonLogEntry

type JsonLogEntry struct {
	PartitionIndex      int       `json:"partition_index"`
	State               []float64 `json:"state"`
	CumulativeTimesteps float64   `json:"time"`
}

JsonLogEntry is the format in which the logs are serialised when using the JsonLogOutputFunction.

type JsonLogOutputFunction

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

JsonLogOutputFunction outputs data to a log of json packets from the simulation.

func NewJsonLogOutputFunction

func NewJsonLogOutputFunction(
	filePath string,
) *JsonLogOutputFunction

NewJsonLogOutputFunction creates a new JsonLogOutputFunction.

func (*JsonLogOutputFunction) Output

func (j *JsonLogOutputFunction) Output(
	partitionIndex int,
	state []float64,
	cumulativeTimesteps float64,
)

type MemoryIteration

type MemoryIteration struct {
	Data *StateHistory
}

MemoryIteration provides a stream of data which is already know from a separate data source and is held in memory.

func NewMemoryIterationFromCsv

func NewMemoryIterationFromCsv(
	filePath string,
	stateColumns []int,
	skipHeaderRow bool,
) *MemoryIteration

NewMemoryIterationFromCsv creates a new MemoryIteration based on data that is read in from the provided csv file and some specified columns for time and state.

func (*MemoryIteration) Configure

func (m *MemoryIteration) Configure(
	partitionIndex int,
	settings *Settings,
)

func (*MemoryIteration) Iterate

func (m *MemoryIteration) Iterate(
	params *OtherParams,
	partitionIndex int,
	stateHistories []*StateHistory,
	timestepsHistory *CumulativeTimestepsHistory,
) []float64

type MemoryTimestepFunction

type MemoryTimestepFunction struct {
	Data *CumulativeTimestepsHistory
}

MemoryTimestepFunction provides a stream of timesteps which already known from a separate data source and is held in memory.

func NewMemoryTimestepFunctionFromCsv

func NewMemoryTimestepFunctionFromCsv(
	filePath string,
	timeColumn int,
	skipHeaderRow bool,
) *MemoryTimestepFunction

NewMemoryTimestepFunctionFromCsv creates a new MemoryTimestepFunction based on data that is read in from the provided csv file and some specified columns for time and state.

func (*MemoryTimestepFunction) SetNextIncrement

func (m *MemoryTimestepFunction) SetNextIncrement(
	timestepsHistory *CumulativeTimestepsHistory,
) *CumulativeTimestepsHistory

type NilOutputCondition

type NilOutputCondition struct{}

NilOutputCondition never outputs.

func (*NilOutputCondition) IsOutputStep

func (c *NilOutputCondition) IsOutputStep(
	partitionIndex int,
	state []float64,
	cumulativeTimesteps float64,
) bool

type NilOutputFunction

type NilOutputFunction struct{}

NilOutputFunction outputs nothing from the stochastic process.

func (*NilOutputFunction) Output

func (f *NilOutputFunction) Output(
	partitionIndex int,
	state []float64,
	cumulativeTimesteps float64,
)

type NumberOfStepsTerminationCondition

type NumberOfStepsTerminationCondition struct {
	MaxNumberOfSteps int
}

NumberOfStepsTerminationCondition terminates the process when the overall number of steps performed has reached MaxNumberOfSteps.

func (*NumberOfStepsTerminationCondition) Terminate

func (t *NumberOfStepsTerminationCondition) Terminate(
	stateHistories []*StateHistory,
	timestepsHistory *CumulativeTimestepsHistory,
) bool

type OtherParams

type OtherParams struct {
	FloatParams     map[string][]float64 `yaml:"float_params"`
	IntParams       map[string][]int64   `yaml:"int_params"`
	FloatParamsMask map[string][]bool    `yaml:"float_params_mask,omitempty"`
	IntParamsMask   map[string][]bool    `yaml:"int_params_mask,omitempty"`
}

OtherParams is a yaml-loadable struct to put any additional parameters needed to configure the stochastic process.

type OutputCondition

type OutputCondition interface {
	IsOutputStep(partitionIndex int, state []float64, cumulativeTimesteps float64) bool
}

OutputCondition is the interface that must be implemented to define when the stochastic process calls the OutputFunction.

type OutputFunction

type OutputFunction interface {
	Output(partitionIndex int, state []float64, cumulativeTimesteps float64)
}

OutputFunction is the interface that must be implemented for any function which can be used to outputs data from the stochastic process when the provided OutputCondition is met.

type Partition

type Partition struct {
	Iteration                   Iteration
	ParamsFromUpstreamPartition map[string]int
	ParamsFromSlice             map[string][]int
}

Partition is the config which defines an iteration which acts on a partition of the the global simulation state and its upstream partitions which may provide params for it.

type PartitionCoordinator

type PartitionCoordinator struct {
	Iterators            []*StateIterator
	StateHistories       []*StateHistory
	TimestepsHistory     *CumulativeTimestepsHistory
	TimestepFunction     TimestepFunction
	TerminationCondition TerminationCondition
	// contains filtered or unexported fields
}

PartitionCoordinator coordinates the assignment of iteration work to separate StateIterator objects on separate goroutines and when to enact these updates on the state history.

func NewPartitionCoordinator

func NewPartitionCoordinator(
	settings *Settings,
	implementations *Implementations,
) *PartitionCoordinator

NewPartitionCoordinator creates a new PartitionCoordinator given a StochadexConfig.

func (*PartitionCoordinator) ReadyToTerminate

func (c *PartitionCoordinator) ReadyToTerminate() bool

ReadyToTerminate returns whether or not the process has met the TerminationCondition.

func (*PartitionCoordinator) RequestMoreIterations

func (c *PartitionCoordinator) RequestMoreIterations(wg *sync.WaitGroup)

RequestMoreIterations spawns a goroutine for each state partition to carry out a ReceiveAndIteratePending job.

func (*PartitionCoordinator) Run

func (c *PartitionCoordinator) Run()

Run runs multiple Step calls up until the TerminationCondition has been met.

func (*PartitionCoordinator) Step

func (c *PartitionCoordinator) Step(wg *sync.WaitGroup)

Step is the main method call of PartitionCoordinator - call this proceeding a new configuration of the latter to run the desired process for a single step.

func (*PartitionCoordinator) UpdateHistory

func (c *PartitionCoordinator) UpdateHistory(wg *sync.WaitGroup)

RequestMoreIterations spawns a goroutine for each state partition to carry out an UpdateHistory job.

type PartitionStrings

type PartitionStrings struct {
	Iteration                   string           `yaml:"iteration"`
	ParamsFromUpstreamPartition map[string]int   `yaml:"params_from_upstream_partition,omitempty"`
	ParamsFromSlice             map[string][]int `yaml:"params_from_slice,omitempty"`
}

PartitionStrings is the yaml-loadable config for a Partition.

type Settings

type Settings struct {
	OtherParams           []*OtherParams `yaml:"other_params"`
	InitStateValues       [][]float64    `yaml:"init_state_values"`
	InitTimeValue         float64        `yaml:"init_time_value"`
	Seeds                 []uint64       `yaml:"seeds"`
	StateWidths           []int          `yaml:"state_widths"`
	StateHistoryDepths    []int          `yaml:"state_history_depths"`
	TimestepsHistoryDepth int            `yaml:"timesteps_history_depth"`
}

Settings is the yaml-loadable config which defines all of the settings that can be set for a stochastic process defined by the stochadex.

func LoadSettingsFromYaml

func LoadSettingsFromYaml(path string) *Settings

LoadSettingsFromYaml creates a new Settings struct from a provided yaml path.

type StateHistory

type StateHistory struct {
	// each row is a different state in the history, by convention,
	// starting with the most recent at index = 0
	Values *mat.Dense
	// should be of length = StateWidth
	NextValues        []float64
	StateWidth        int
	StateHistoryDepth int
}

StateHistory represents the information contained within a windowed history of State structs.

type StateIterator

type StateIterator struct {
	Iteration       Iteration
	Params          *OtherParams
	PartitionIndex  int
	ValueChannels   StateValueChannels
	OutputCondition OutputCondition
	OutputFunction  OutputFunction
}

StateIterator handles iterations of a given state partition on a separate goroutine and reads/writes data from/to the state history.

func (*StateIterator) Iterate

func (s *StateIterator) Iterate(
	stateHistories []*StateHistory,
	timestepsHistory *CumulativeTimestepsHistory,
) []float64

Iterate takes the state and timesteps history and outputs an updated State struct using an implemented Iteration interface.

func (*StateIterator) ReceiveAndIteratePending

func (s *StateIterator) ReceiveAndIteratePending(
	inputChannel <-chan *IteratorInputMessage,
)

ReceiveAndIteratePending listens for input messages sent to the input channel, runs Iterate when an IteratorInputMessage has been received on the provided inputChannel and then updates an internal pending state update object.

func (*StateIterator) UpdateHistory

func (s *StateIterator) UpdateHistory(inputChannel <-chan *IteratorInputMessage)

UpdateHistory should always follow a call to ReceiveAndIteratePending as it enacts the internal pending state update on the StateHistory object passed over the provided inputChannel.

type StateValueChannels

type StateValueChannels struct {
	UpstreamByParams    map[string](chan []float64)
	SliceByParams       map[string][]int
	Downstream          chan []float64
	NumberOfDownstreams int
}

StateValueChannels defines the methods by which separate StateIterators can communicate with each other by sending the values of upstream iterators to downstream parameters via channels.

func (*StateValueChannels) BroadcastDownstream

func (s *StateValueChannels) BroadcastDownstream(stateValues []float64)

func (*StateValueChannels) UpdateUpstreamParams

func (s *StateValueChannels) UpdateUpstreamParams(params *OtherParams)

type StdoutOutputFunction

type StdoutOutputFunction struct{}

StdoutOutputFunction outputs the state to the terminal.

func (*StdoutOutputFunction) Output

func (s *StdoutOutputFunction) Output(
	partitionIndex int,
	state []float64,
	cumulativeTimesteps float64,
)

type TerminationCondition

type TerminationCondition interface {
	Terminate(
		stateHistories []*StateHistory,
		timestepsHistory *CumulativeTimestepsHistory,
	) bool
}

TerminationCondition is the interface that must be implemented in order to create a new condition for ending the stochastic process.

type TimeElapsedTerminationCondition

type TimeElapsedTerminationCondition struct {
	MaxTimeElapsed float64
}

TimeElapsedTerminationCondition terminates the process when the overall time elapsed has reached MaxTimeElapsed.

func (*TimeElapsedTerminationCondition) Terminate

func (t *TimeElapsedTerminationCondition) Terminate(
	stateHistories []*StateHistory,
	timestepsHistory *CumulativeTimestepsHistory,
) bool

type TimestepFunction

type TimestepFunction interface {
	SetNextIncrement(
		timestepsHistory *CumulativeTimestepsHistory,
	) *CumulativeTimestepsHistory
}

TimestepFunction is the interface that must be implemented for a function which gets the next increment to the time variable of the stochastic process.

type VariableStoreOutputFunction

type VariableStoreOutputFunction struct {
	Store [][][]float64
}

VariableStoreOutputFunction stores the data from the stochastic process in a provided Store variable on the steps when the OutputCondition is met

func (*VariableStoreOutputFunction) Output

func (f *VariableStoreOutputFunction) Output(
	partitionIndex int,
	state []float64,
	cumulativeTimesteps float64,
)

type WebsocketOutputFunction

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

WebsocketOutputFunction serialises the state of each partition of the simulation and sends this data via a websocket connection.

func NewWebsocketOutputFunction

func NewWebsocketOutputFunction(
	connection *websocket.Conn,
	mutex *sync.Mutex,
) *WebsocketOutputFunction

NewWebsocketOutputFunction creates a new WebsocketOutputFunction given a connection struct and mutex to protect concurrent writes to the connection.

func (*WebsocketOutputFunction) Output

func (w *WebsocketOutputFunction) Output(
	partitionIndex int,
	state []float64,
	cumulativeTimesteps float64,
)

Jump to

Keyboard shortcuts

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