Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Network

Represents a network defined by an array of INode and an array of ILink elements along which entities may move.

Networks can be used to simulate streets, rails, rivers, or any other system with nodes connected by links.

Use networks to select the shortest path between two points in the simulation, them move entities along the path using the entity's Entity.delay method.

For an example, please see the shortestPath method.

Networks are defined by arrays of INode and ILink objects.

Both INode and ILink objects may optionally include Queue objects. These queues are not used by the Network class, but may be used by calling simulations to control the flow of entities along the network.

For an example of using link queues to account for congestion, please see the getLinkDistance method.

Hierarchy

  • Network

Index

Constructors

constructor

  • new Network(options?: any): Network
  • Initializes a new instance of the Network class.

    Parameters

    • Optional options: any

      Object with parameters used to initialize the Network.

    Returns Network

Properties

_links

_links: ILink[]

_nodes

_nodes: INode[]

Accessors

links

  • get links(): ILink[]
  • set links(value: ILink[]): void
  • Gets or sets the network links.

    Returns ILink[]

  • Gets or sets the network links.

    Parameters

    Returns void

nodes

  • get nodes(): INode[]
  • set nodes(value: INode[]): void
  • Gets or sets the network nodes.

    Returns INode[]

  • Gets or sets the network nodes.

    Parameters

    Returns void

Methods

_getLinkAngle

  • _getLinkAngle(link: ILink): number
  • Parameters

    Returns number

_getPathPart

  • _getPathPart(visited: PathPart[], node: INode): PathPart
  • Parameters

    • visited: PathPart[]
    • node: INode

    Returns PathPart

getLinkDistance

  • getLinkDistance(link: ILink, prevLink?: ILink): number
  • Gets the distance (cost) associated with a link.

    The function returns the link's distance value, if specified, or the distance between the link's from and to nodes.

    You can override this method to account for congestion along links, turning costs, or temporary flow restrictions.

    For example, the code below creates a CongestionNetwork class that accounts for congestion by increasing the calculated distance based on the link's current population:

    // network with congestion cost
    // assumes the simulation adds entities to the link's queue 
    // while they move along each link.
    class CongestionNetwork extends Network {
        getLinkDistance(link: ILink, prevLink?: ILink): number {
    
            // regular distance
            let dist = super.getLinkDistance(link, prevLink);
    
            // add congestion cost
            dist += dist * link.queue.pop * 0.5;
    
            // add turning cost
            if (prevLink) {
                const
                    a1 = this._getLinkAngle(link),
                    a2 = this._getLinkAngle(prevLink);
                let angle = Math.abs(a1 - a2); // turn in radians
                distance += someFunction(angle);
            }
    
            // done
            return dist;
        }
        _getLinkAngle(link: ILink): number {
            return Point.angle(link.from.position, link.to.position, true);
        }
    }
    

    Parameters

    • link: ILink

      Link whose distance is being evaluated.

    • Optional prevLink: ILink

      Previous link in the path (used to calculate turning costs).

    Returns number

    The distance (cost) associated with the link.

getNode

  • getNode(id: any): INode
  • Gets the node with a given ID.

    Parameters

    • id: any

      ID of the node to get.

    Returns INode

mergePath

  • Merges a path defined by an array of ILink objects into an array of Queue objects.

    This makes it possible for entities to traverse the whole path with a single delay. For example:

    // select from/to nodes
    const from = sim.network.nodes[0];
    const to = sim.network.nodes[10];
    
    // get shortest path
    const path = sim.network.shortestPath(from, to);
    
    // merge the path into a list of queues
    const [queues, distance] = sim.network.mergePath(path);
    
    // traverse the whole path with a single delay
    await this.delay(distance / sim.speed.sample(), {
        queues: queues,
        tension: 0.5
    });
    

    Parameters

    • path: ILink[]

      Array of ILink objects that defines the path.

    Returns [Queue[], number]

    An array where the first element is an array of Queue objects to be visited and the second is the total path distance.

shortestPath

  • Computes the shortest path between two nodes on the network.

    The shortestPath method returns an array of ILink objects that represent the shortest path between two network ILink objects.

    If a path between the nodes cannot be found, the method returns an empty array.

    The example below shows how you to move an Entity between to nodes on a Network:

    class Vehicle extends Entity {
        async script() {
    
            // get a reference to the simulation
            const sim = this.simulation as NetworkIntro;
    
            // select from/to nodes
            const from = sim.network.nodes[0];
            const to = sim.network.nodes[10];
    
            // calculate the shortest path
            const path = sim.network.shortestPath(from, to);
            assert(path.length > 0, 'cannot reach destination');
    
            // move along the path one link at a time
            for (let i = 0; i < path.length; i++) {
                const link = path[i];
                const distance = sim.network.getLinkDistance(link, i > 0 ? path[i - 1] : null);
                const time = distance / sim.serviceVehicleSpeed.sample();
                await this.delay(time, {
                    queues: [link.from.queue, link.to.queue] // used in animations
                });
            }
        }
    }
    

    You can also use the mergePath method to get a list of queues that make up the path and the total path distance, and use those elements to travel the entire path with a single delay:

    // calculate the shortest path
    const path = sim.network.shortestPath(from, to);
    assert(path.length > 0, 'cannot reach destination');
    
    // merge the path into a list of queues
    const [queues, distance] = sim.network.mergePath(path);
    
    // traverse the whole path with a single delay
    await this.delay(distance / sim.speed.sample(), {
        queues: queues,
        tension: 0.5
    });
    

    Parameters

    Returns ILink[]

    An array of ILink objects that describe the shortest path from start to finish, or an empty array if a path could not be found.

Generated using TypeDoc