Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Simulation

Abstract base class for simulations.

Simulation objects are basically schedulers. They act as a stage where the actors are Entity objects and have props represented by Queue objects.

Simulations keep a list of active entities and conditions these entities are waiting for within their Entity.script methods, which execute asynchronously. As these conditions are satisfied along the simulated time, the Entity objects resume executing their scripts. When the simulation runs out of active entities, that means all entities have finished their scripts and the simulation ends.

To create simulations follow these steps:

  1. Create one or more classes that extend the Entity class and override the Entity.script method to describe the entity's actions within the simulation.

  2. Create a class that extends Simulation to use the entities you defined in step 1.

  3. Override the simulation's onStarting method to create and activate one or more of the entities you defined in step 1.

For example:

// define the BarberShop simulation 
export class BarberShop extends Simulation {
    qJoe = new Queue('Joe', 1);
    qWait = new Queue('Wait Area');

    // generate Customer entities with inter-arrival times of 18 min for 8 hours
    onStarting() {
        super.onStarting();
        this.qWait.grossDwell.binSize = 1;
        this.generateEntities(Customer, new Uniform(18 - 6, 18 + 6));
    }
}

// define the Customer entity used by the Barbershop simulation
class Customer extends Entity<BarberShop> {
    service = new Uniform(15 - 3, 15 + 3);
    async script() {
        const shop = this.simulation;
        await this.enterQueue(shop.qWait); // enter the line
        await this.enterQueue(shop.qJoe); // seize Joe the barber
        this.leaveQueue(shop.qWait); // leave the line
        await this.delay(this.service.sample()); // get a haircut
        this.leaveQueue(shop.qJoe); // free Joe        
    }
}

Hierarchy

  • Simulation

Index

Constructors

constructor

  • Initializes a new instance of the Simulation class.

    Override the onStarting method to generate entities when the simulation starts running.

    Parameters

    • Optional options: any

      Object with parameters used to initialize the simulation (properties and event handlers).

    Returns Simulation

Properties

Readonly finished

finished: Event<Simulation, EventArgs> = ...

Event that occurs after the simulation finishes executing.

Readonly finishing

finishing: Event<Simulation, EventArgs> = ...

Event that occurs before the simulation finishes executing.

Readonly started

started: Event<Simulation, EventArgs> = ...

Event that occurs after the simulation starts executing.

Readonly starting

starting: Event<Simulation, EventArgs> = ...

Event that occurs before the simulation starts executing.

Readonly stateChanged

stateChanged: Event<Simulation, EventArgs> = ...

Event that occurs after the simulation's state property changes.

For example:

sim.stateChanged.addEventListener(s => {
    console.log('the simulation state changed to', SimulationState[s.state]);
});

Readonly stateChanging

stateChanging: Event<Simulation, EventArgs> = ...

Event that occurs before the simulation's state property changes.

Readonly timeNowChanged

timeNowChanged: Event<Simulation, EventArgs> = ...

Event that occurs after the simulation's timeNow property changes.

For example:

sim.timeChanged.addEventListener(s => {
    console.log('the simulation time advanced to', s.timeNow);
});

Readonly timeNowChanging

timeNowChanging: Event<Simulation, EventArgs> = ...

Event that occurs before the simulation's timeNow property changes.

Accessors

frameDelay

  • get frameDelay(): number
  • set frameDelay(value: number): void
  • Gets or sets the duration, in milliseconds of each simulated time advance.

    This property is useful to slow down animated simulations.

    For example, if you set frameDelay to 100, each animation frame will be displayed for 100ms before being updated.

    The default value for this property is 0, which means no frame delays should be applied and the animation will run as quickly as possible.

    See also the maxTimeStep property.

    Returns number

  • Gets or sets the duration, in milliseconds of each simulated time advance.

    This property is useful to slow down animated simulations.

    For example, if you set frameDelay to 100, each animation frame will be displayed for 100ms before being updated.

    The default value for this property is 0, which means no frame delays should be applied and the animation will run as quickly as possible.

    See also the maxTimeStep property.

    Parameters

    • value: number

    Returns void

maxTimeStep

  • get maxTimeStep(): number
  • set maxTimeStep(value: number): void
  • Gets or sets the maximum time step allowed by the simulation.

    The default value for this property is 0, which allows the simulation to advance the time in steps of any size.

    This property can be useful to slow down and set the pace for animated simulations. This will prevent animations from becoming too jerky and hard to follow.

    See also the frameDelay property.

    Returns number

  • Gets or sets the maximum time step allowed by the simulation.

    The default value for this property is 0, which allows the simulation to advance the time in steps of any size.

    This property can be useful to slow down and set the pace for animated simulations. This will prevent animations from becoming too jerky and hard to follow.

    See also the frameDelay property.

    Parameters

    • value: number

    Returns void

name

  • get name(): string
  • set name(value: string): void
  • Gets or sets a string that represents the simulation name.

    This value used by the getStatsTable method when creating output tables. It does not have any effect on the simulation results.

    This property is set to an empty string by default, which causes the getStatsTable method to use the class name as the simulation name. This is adequate in most cases, except if the code is minified.

    Returns string

  • Gets or sets a string that represents the simulation name.

    This value used by the getStatsTable method when creating output tables. It does not have any effect on the simulation results.

    This property is set to an empty string by default, which causes the getStatsTable method to use the class name as the simulation name. This is adequate in most cases, except if the code is minified.

    Parameters

    • value: string

    Returns void

queues

  • Gets an array with the Queue objects in use by this Simulation.

    Returns Queue[]

state

  • Gets the simulation state.

    Use the start and stop methods to change the simulation state.

    Returns SimulationState

timeElapsed

  • get timeElapsed(): number
  • Gets the actual simulation time in milliseconds.

    Returns number

timeEnd

  • get timeEnd(): number
  • set timeEnd(value: number): void
  • Gets or sets the simulation end time.

    The default value for this property is null, which causes the simulation to run until the stop method is called or until it runs out of things to do.

    Returns number

  • Gets or sets the simulation end time.

    The default value for this property is null, which causes the simulation to run until the stop method is called or until it runs out of things to do.

    Parameters

    • value: number

    Returns void

timeNow

  • get timeNow(): number
  • Gets the current simulated time in simulation time units.

    Returns number

timeUnit

  • get timeUnit(): string
  • set timeUnit(value: string): void
  • Gets or sets a string that represents the simulation time unit.

    This value used by the getStatsTable method when creating output tables. It does not have any effect on the simulation results.

    This property is set to 'Sim Time' by default. Set it to a string such as 's', 'min', or 'hours' to get better looking output from the getStatsTable method.

    Returns string

  • Gets or sets a string that represents the simulation time unit.

    This value used by the getStatsTable method when creating output tables. It does not have any effect on the simulation results.

    This property is set to 'Sim Time' by default. Set it to a string such as 's', 'min', or 'hours' to get better looking output from the getStatsTable method.

    Parameters

    • value: string

    Returns void

yieldInterval

  • get yieldInterval(): number
  • set yieldInterval(value: number): void
  • Gets or sets a value that determines how often the Simulation will release control of the thread so the brower remains responsive.

    The default value for this property is 100 ms, which is enough to keep the UI responsive. Use higher values to increase the simulation speed at the expense of UI responsiveness.

    Returns number

  • Gets or sets a value that determines how often the Simulation will release control of the thread so the brower remains responsive.

    The default value for this property is 100 ms, which is enough to keep the UI responsive. Use higher values to increase the simulation speed at the expense of UI responsiveness.

    Parameters

    • value: number

    Returns void

Methods

activate

  • Activates an Entity, causing it to enter the simulation and start executing its Entity.script method.

    Parameters

    Returns Promise<number>

generateEntities

  • generateEntities(entityType: any, interArrival?: number | RandomVar, max?: number, startTime?: number, endTime?: number): void
  • Generates and activates Entity objects of a given type according to a schedule.

    For example, the code below shows a Simulation that overrides the onStarting method to generate entities of type Customer with inter-arrival times of 18+/-6 minutes:

    export class BarberShop extends Simulation {
        qJoe = new Queue('Joe', 1);
        qWait = new Queue('Wait Area');
    
        // generate Customer entities with inter-arrival times of 18+/-6 min
        // for 8 hours * 7 days
        onStarting() {
            super.onStarting();
            this.timeEnd = 60 * 8 * 7; // 8 hours * 7 days
            this.generateEntities(Customer, new Uniform(18 - 6, 18 + 6));
        }
    }
    class Customer extends Entity<BarberShop> {
        async script() {
            // do what the customers do...
        }
    }
    

    Parameters

    • entityType: any

      Type of Entity to generate.

    • Optional interArrival: number | RandomVar

      RandomVar that returns the inter-arrival time, or a number that represents a fixed interval, or null to generate a single entity.

    • Optional max: number

      Maximum number of entities to generate.

    • Optional startTime: number

      Time to start generating entities.

    • Optional endTime: number

      Time to stop generating entities.

    Returns void

getStatsTable

  • getStatsTable(showNetValues?: boolean): string
  • Creates a table with the Simulation statistics.

    For example, here is the table generated after running the BarberShop sample simulation:

     BarberShop
     Finish Time           3,380.37
     Elapsed Time (s)          0.01
     Populations
       Queue           Min      Avg      Max    StDev   Capy     Utz
       Joe            0.00     0.82     1.00     0.38      1     82%
       Wait Area      0.00     0.03     1.00     0.17      *
     Dwell Times
       Queue           Min      Avg      Max    StDev    Cnt
       Joe           12.11    14.91    17.99     1.76    185
       Wait Area      0.00     0.54     6.81     1.30    185
    

    The table shows the simulated finish time, the actual elapsed time, and a list of the Queue objects used in the simulation, along with their population (number of entities in the queue) and dwell time (time they spent in the queue) statistics.

    Parameters

    • showNetValues: boolean = false

      Whether to include net statistics (non-zero Queue dwell times and populations).

    Returns string

    An HTML string defining a table with statistics for all Queue objects in the simulation.

onFinished

  • Raises the finished event.

    Parameters

    Returns void

onFinishing

  • Raises the finishing event.

    Parameters

    Returns void

onStarted

  • Raises the started event.

    Parameters

    Returns void

onStarting

  • Raises the starting event.

    Classes that extend Simulation typically override this method to create the entities that will drive the simulation.

    For example:

    export class BarberShop extends Simulation {
        qJoe = new Queue('Joe', 1);
        qWait = new Queue('Wait Area');
    
        // generate entities with inter-arrival times of 18 min for 8 hours
        onStarting() {
            this.qWait.grossDwell.binSize = 1;
            this.generateEntities(Customer, new Uniform(18 - 6, 18 + 6));
        }
    }
    

    Parameters

    Returns void

onStateChanged

onStateChanging

onTimeNowChanged

onTimeNowChanging

start

  • start(reset?: boolean): Promise<void>
  • Starts the Simulation or resumes the execution of a stopped simulation.

    Parameters

    • reset: boolean = false

      Whether to restart the simulation or resume execution from where it stopped.

    Returns Promise<void>

stop

  • stop(reset?: boolean): void
  • Stops the Simulation.

    Calling this method causes the SimulationState to change from Running to Paused.

    Use the the start method to resume or re-start the simulation when it is paused.

    Parameters

    • reset: boolean = false

      Whether to reset the simulation state after stopping it.

    Returns void

Generated using TypeDoc