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

Agent

Extends:

events~EventEmitter → Agent

Direct Subclass:

Trader

Agent with Poisson-distributed opportunities to act, with period managment, optional inventory, unit values and costs, and end-of-period production and consumption to satisfy trades

Constructor Summary

Public Constructor
public

constructor(options: Object)

creates an Agent with clone of specified options and initializes with .init().

Member Summary

Public Members
public

period: *

public

time, in JS ms since epoch, of agent wake

Method Summary

Public Methods
public

ends current period, causing agent to undertake end-of-period tasks such as production and redemption of units

public

init(newSettings: Object)

initialize an agent to new settings

public

initPeriod(period: number | Object)

re-initialize agent to the beginning of a new simulation period

public

percent of period used

public

guess at number of random Poisson wakes remaining in period

public

produces units in negative inventory with configured costs, usually called automatically at end-of-period.

public

redeem()

redeems units in positive inventory with configured values, usually called automatically at end-of-period.

public

transfer(myTransfers: Object, memo: Object)

increases or decreases agent's inventories of one or more goods and/or money

public

unitCostFunction(good: String, hypotheticalInventory: Object): number

agent's marginal cost of producing next unit

public

unitValueFunction(good: String, hypotheticalInventory: Object): number

agent's marginal value for redeeming next unit

public

wake(info: Object)

wakes agent so it can act, emitting wake, and sets next wakeTime from this.nextWake() unless period.endTime exceeded

Public Constructors

public constructor(options: Object) source

creates an Agent with clone of specified options and initializes with .init(). Option properties are stored directly on the created agent's this.

Params:

NameTypeAttributeDescription
options Object

Agent creation options

options.description string
  • optional

text description of agent, optional

options.inventory Object
  • optional
  • default: {}

initial inventory, as object with good names as keys and levels as values

options.money string
  • optional
  • default: 'money'

Good used as money by this agent

options.values Object
  • optional
  • default: {}

marginal value table of agent for goods that are redeemed at end-of-period, as object with goods as keys and numeric arrays as values

options.costs Object
  • optional
  • default: {}

marginal cost table of agent for goods that are produced at end-of-period, as object with goods as keys and numeric arrays as values

options.wakeTime number
  • optional
  • default: 0

initial wake-up time for agent, adjusted by this.init() to first poisson-based wake with .nextWake()

options.rate number
  • optional
  • default: 1

Poisson-arrival rate of agent wake events

options.nextWake function(): number
  • optional
  • default: poissonWake

calculates next Agent wake-up time

Public Members

public period: * source

public wakeTime: number source

time, in JS ms since epoch, of agent wake

Public Methods

public endPeriod() source

ends current period, causing agent to undertake end-of-period tasks such as production and redemption of units

Emit:

post-period

when period ends, always, but after first completing any production/redemption

public init(newSettings: Object) source

initialize an agent to new settings

Params:

NameTypeAttributeDescription
newSettings Object
  • optional

see constructor

public initPeriod(period: number | Object) source

re-initialize agent to the beginning of a new simulation period

Params:

NameTypeAttributeDescription
period number | Object

A period initialization object, or a number indicating a new period using the previous period's initialization object

period.number number

A number, usually sequential, identifying the next period, e.g. 1,2,3,4,5,...

period.equalDuration boolean
  • optional
  • default: false

with positive period.duration, autogenerates startTime and endTime as n or n+1 times period.duration

period.duration number
  • optional

the length of the period, used with period.equalDuration

period.startTime number
  • optional

period begins, manual setting for initial time value for agent wakeTime

period.endTime number
  • optional

period ends, no agent wake events will be emitted for this period after this time

period.init Object
  • optional

initializer for other agent properties, passed to .init()

Emit:

pre-period

when initialization to new period is complete

Example:

myAgent.initPeriod({number:1, duration:1000, equalDuration: true});
myAgent.initPeriod(2);

public pctPeriod(): number source

percent of period used

Return:

number

proportion of period time used as a number from 0.0, at beginning of period, to 1.0 at end of period.

public poissonWakesRemainingInPeriod(): number source

guess at number of random Poisson wakes remaining in period

Return:

number

"expected" number of remaining random Poisson wakes, calculated as (this.period.endTime-this.wakeTime)*rate

public produce() source

produces units in negative inventory with configured costs, usually called automatically at end-of-period. transfer uses memo object {isProduce:1}

Emit:

pre-redeem(transferAmounts)

before calling .transfer, can modify transferAmounts

post-redeem(transferAmounts)

after calling .transfer

public redeem() source

redeems units in positive inventory with configured values, usually called automatically at end-of-period. transfer uses memo object {isRedeem:1}

Emit:

pre-redeem(transferAmounts)

before calling .transfer, can modify transferAmounts

post-redeem(transferAmounts)

after calling .transfer

public transfer(myTransfers: Object, memo: Object) source

increases or decreases agent's inventories of one or more goods and/or money

Params:

NameTypeAttributeDescription
myTransfers Object

object with goods as keys and changes in inventory as number values

memo Object
  • optional

optional memo passed to event listeners

Emit:

pre-transfer(myTransfers, memo)

before transfer takes place, modifications to myTransfers will change transfer

post-transfer(myTransfers, memo)

after transfer takes place

public unitCostFunction(good: String, hypotheticalInventory: Object): number source

agent's marginal cost of producing next unit

Params:

NameTypeAttributeDescription
good String

(e.g. "X", "Y")

hypotheticalInventory Object

object with goods as keys and values as numeric levels of inventory

Return:

number

marginal unit cost of next unit, at given (negative) hypothetical inventory, using agent's configured costs

public unitValueFunction(good: String, hypotheticalInventory: Object): number source

agent's marginal value for redeeming next unit

Params:

NameTypeAttributeDescription
good String

(e.g. "X", "Y")

hypotheticalInventory Object

object with goods as keys and values as numeric levels of inventory

Return:

number

marginal unit value of next unit, at given (positive) hypothetical inventory, using agent's configured values

public wake(info: Object) source

wakes agent so it can act, emitting wake, and sets next wakeTime from this.nextWake() unless period.endTime exceeded

Params:

NameTypeAttributeDescription
info Object
  • optional

optional info passed to this.emit('wake', info)

Emit:

wake(info)

immediately