Home Reference Source Test Repository
import {Pool} from 'market-agents/src/index.js'
public class | source

Pool

Pool for managing a collection of agents.
Agents may belong to multiple pools.

Constructor Summary

Public Constructor
public

Member Summary

Public Members
public

agents: *[]

public
public

Method Summary

Public Methods
public

distribute(field: string, good: good, aggregateArray: number[] | string)

distribute an aggregate setting of buyer Values or seller Costs to a pool of sellers, by giving each agent a successive value from the array without replacement

public

calls .endPeriod for all agents in the Pool

public

finds latest period.endTime of all agent in Pool

public

calls .initPeriod for all agents in the Pool

public

finds agent from Pool with lowest wakeTime

public

push(agent: Object)

Add an agent to the Pool

public

runAsPromise(untilTime: number, batch: number): Promise<Object, Error>

Repeatedly wake agents in Pool, until simulation time "untilTime" is reached.

public

syncRun(untilTime: number, limitCalls: number)

Repeatedly wake agents in Pool, until simulation time "untilTime" or "limitCalls" agent wake calls are reached.

public

trade(tradeSpec: Object)

adjusts Pool agents inventories, via agent.transfer(), in response to one or more trades

public

wake()

wakes agent in Pool with lowest wakeTime

Public Constructors

public constructor() source

Public Members

public agents: *[] source

public agentsById: {} source

public nextCache: * source

Public Methods

public distribute(field: string, good: good, aggregateArray: number[] | string) source

distribute an aggregate setting of buyer Values or seller Costs to a pool of sellers, by giving each agent a successive value from the array without replacement

Params:

NameTypeAttributeDescription
field string

"values" or "costs"

good good

name of good for agents inventories.

aggregateArray number[] | string

list of numeric values or costs reflecting the aggregate pool values or costs

Throw:

Error

when field is invalid or aggregateArray is wrong type

public endPeriod() source

calls .endPeriod for all agents in the Pool

public endTime(): number source

finds latest period.endTime of all agent in Pool

Return:

number

max of agents period.endTime

public initPeriod(param: Object | number) source

calls .initPeriod for all agents in the Pool

Params:

NameTypeAttributeDescription
param Object | number

passed to each agent's .initPeriod()

public next(): Object source

finds agent from Pool with lowest wakeTime

Return:

Object

public push(agent: Object) source

Add an agent to the Pool

Params:

NameTypeAttributeDescription
agent Object

to add to pool. Should be instanceof Agent, including subclasses.

public runAsPromise(untilTime: number, batch: number): Promise<Object, Error> source

Repeatedly wake agents in Pool, until simulation time "untilTime" is reached. For a synchronous equivalent, see syncRun(untilTime, limitCalls)

Params:

NameTypeAttributeDescription
untilTime number

Stop time for this run

batch number

Batch size of number of agents to wake up synchronously before surrendering to event loop

Return:

Promise<Object, Error>

returns promise resolving to pool, with caught errors passed to reject handler.

public syncRun(untilTime: number, limitCalls: number) source

Repeatedly wake agents in Pool, until simulation time "untilTime" or "limitCalls" agent wake calls are reached. This method runs synchronously. It returns only when finished.

Params:

NameTypeAttributeDescription
untilTime number

Stop time for this run

limitCalls number
  • optional

Stop run once this number of agent wake up calls have been executed.

public trade(tradeSpec: Object) source

adjusts Pool agents inventories, via agent.transfer(), in response to one or more trades

Params:

NameTypeAttributeDescription
tradeSpec Object

Object providing specifics of trades.

tradeSpec.bs string

'b' for buy trade, 's' for sell trade. In a buy trade, buyQ, buyId are single element arrays. In a sell trade, sellQ, sellId are single element arrays,

tradeSpec.goods string

the name of the goods, as stored in agent inventory object

tradeSpec.money string

the name of money used for payment, as stored in agent inventory object

tradeSpec.prices number[]

the price of each trade

tradeSpec.buyId number[]

the agent id of a buyer in a trade

tradeSpec.buyQ number[]

the number bought by the corresponding agent in .buyId

tradeSpec.sellId number[]

the agent id of a seller in a trade

tradeSPec.sellQ number[]

the number bought by he corresponding agent in .sellId

Throw:

Error

when accounting identities do not balance or trade invalid

public wake() source

wakes agent in Pool with lowest wakeTime