UNPKG

traffic-traversal

Version:

Calculate the weights between each vertex node and help you find the fastest route.

131 lines (125 loc) 4.05 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/Helpers/VertexEncoder.ts var VertexEncoder_exports = {}; __export(VertexEncoder_exports, { VertexEncoder: () => VertexEncoder }); module.exports = __toCommonJS(VertexEncoder_exports); // src/Utils/Array.ts function copy(array) { return [].concat(array); } // src/Utils/Hashmap.ts function useHashmap() { const _map = /* @__PURE__ */ new Map(); const set = (key, data) => { _map.set(key, data); return data; }; const has = (key) => _map.has(key); const get = (key) => _map.get(key); const ensure = (key, callback) => { if (has(key)) { return get(key); } return set(key, callback()); }; return { set, has, get, ensure }; } // src/Utils/Object.ts function deepCopy(obj) { return JSON.parse(JSON.stringify(obj)); } // src/Helpers/VertexEncoder.ts var VertexEncoder = class _VertexEncoder { _trafficGraph; _cNumbers; _cRecordNumber; _cRecordNumbers; static Create(...args) { return new _VertexEncoder(...args); } /** * Create an instance to get the encoder utility functions from the graph. It takes a `graph.state` instance as a parameter. * @param trafficGraphState */ constructor(trafficGraphState) { this._trafficGraph = trafficGraphState; this._cNumbers = useHashmap(); this._cRecordNumber = useHashmap(); this._cRecordNumbers = useHashmap(); } /** * Exports a zero vector with all elements zero. The length of the vector is equal to the number of vertices in use in the graph. * This is good for use with the `oneHot` method. */ zeroHot() { const raw = this._cNumbers.ensure("zeroHot", () => { return new Array(this._trafficGraph.vertices.length).fill(0); }); return copy(raw); } /** * Convert each vertex to a one-hot vector and export it. The length of each vector is equal to the number of all vertices used in the graph. */ oneHot() { const raw = this._cRecordNumbers.ensure("oneHot", () => { const encoding = {}; const vertices = this._trafficGraph.vertices; const len = vertices.length; for (const v of vertices) { const i = this._trafficGraph.data.embedded.indexOf(v); if (i === -1) { throw new Error(`The '${v}' vertex exists in the 'state.vertices' array, but not in the 'state.data.embedded' array.`); } encoding[v] = new Array(len).fill(0); encoding[v][i] = 1; } return encoding; }); const clone = deepCopy(raw); return clone; } /** * Convert each vertex to a label integer and export it. * @param startFrom The starting value of the label integer. The default is `0`. */ label(startFrom = 0) { const raw = this._cRecordNumber.ensure(`label start from '${startFrom}'`, () => { const encoding = {}; const vertices = this._trafficGraph.vertices; for (const v of vertices) { const i = this._trafficGraph.data.embedded.indexOf(v); if (i === -1) { throw new Error(`The '${v}' vertex exists in the 'state.vertices' array, but not in the 'state.data.embedded' array.`); } encoding[v] = startFrom + i; } return encoding; }); const clone = deepCopy(raw); return clone; } };