UNPKG

@geoapify/route-planner-sdk

Version:

TypeScript SDK for the Geoapify Route Planner API. Supports route optimization, delivery planning, and timeline visualization in browser and Node.js

178 lines (177 loc) 8.93 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { RouteResultEditorBase } from "./route-result-editor-base"; export class RouteResultShipmentEditor extends RouteResultEditorBase { assignShipments(agentIndex, shipmentIndexes, newPriority) { return __awaiter(this, void 0, void 0, function* () { this.validateAgent(agentIndex); this.validateShipments(shipmentIndexes, agentIndex); for (const shipmentIndex of shipmentIndexes) { this.setShipmentPriority(shipmentIndex, newPriority); yield this.assignShipment(shipmentIndex, agentIndex); } return true; }); } removeShipments(shipmentIndexes) { return __awaiter(this, void 0, void 0, function* () { this.validateShipments(shipmentIndexes); for (const shipmentIndex of shipmentIndexes) { yield this.removeShipment(shipmentIndex); } return true; }); } addNewShipments(agentIndex, shipments) { return __awaiter(this, void 0, void 0, function* () { let shipmentsRaw = shipments.map(shipment => shipment.getRaw()); this.validateAgent(agentIndex); this.validateNewShipments(shipmentsRaw); yield this.addNewShipmentsToAgent(agentIndex, shipmentsRaw); return true; }); } assignShipment(shipmentIndex, agentIndex) { return __awaiter(this, void 0, void 0, function* () { let shipmentInfo = this.result.getShipmentInfoByIndex(shipmentIndex); let newAgentSolution = this.result.getAgentSolutionByIndex(agentIndex); if (newAgentSolution && shipmentInfo) { yield this.removeShipmentFromExistingAgent(shipmentInfo); yield this.addShipmentToExistingAgent(agentIndex, shipmentIndex); } if (newAgentSolution && !shipmentInfo) { yield this.addShipmentToExistingAgent(agentIndex, shipmentIndex); } if (!newAgentSolution && shipmentInfo) { yield this.removeShipmentFromExistingAgent(shipmentInfo); yield this.addShipmentToNonExistingAgent(agentIndex, shipmentIndex); } if (!newAgentSolution && !shipmentInfo) { yield this.addShipmentToNonExistingAgent(agentIndex, shipmentIndex); } }); } removeShipment(shipmentIndex) { return __awaiter(this, void 0, void 0, function* () { let shipmentInfo = this.result.getShipmentInfoByIndex(shipmentIndex); if (shipmentInfo) { yield this.removeShipmentFromExistingAgent(shipmentInfo); } else { this.result.getRawData().properties.issues.unassigned_shipments = this.result.getRawData().properties.issues.unassigned_shipments.filter((shipmentIndex) => shipmentIndex !== shipmentIndex); } }); } addNewShipmentsToAgent(agentIndex, shipments) { return __awaiter(this, void 0, void 0, function* () { let existingAgentSolution = this.result.getAgentSolutionByIndex(agentIndex); let initialShipmentsCount = this.result.getRawData().properties.params.shipments.length; this.result.getRawData().properties.params.shipments.push(...shipments); let newAgentInput = this.addShipmentsToAgent(agentIndex, shipments.map((shipment, index) => initialShipmentsCount + index), existingAgentSolution); let optimizedRouterPlan = yield this.optimizeRoute(newAgentInput); this.updateAgent(optimizedRouterPlan, agentIndex); }); } addShipmentToNonExistingAgent(agentIndex, shipmentIndex) { return __awaiter(this, void 0, void 0, function* () { let newAgentInput = this.addShipmentsToAgent(agentIndex, [shipmentIndex]); let optimizedRouterPlan = yield this.optimizeRoute(newAgentInput); this.updateAgent(optimizedRouterPlan, agentIndex); }); } addShipmentToExistingAgent(agentIndex, shipmentIndex) { return __awaiter(this, void 0, void 0, function* () { let existingAgentSolution = this.result.getAgentSolutionByIndex(agentIndex); let newAgentInput = this.addShipmentsToAgent(agentIndex, [shipmentIndex], existingAgentSolution); let optimizedRouterPlan = yield this.optimizeRoute(newAgentInput); this.updateAgent(optimizedRouterPlan, agentIndex); }); } removeShipmentFromExistingAgent(shipmentInfo) { return __awaiter(this, void 0, void 0, function* () { let existingAgentSolution = shipmentInfo.getAgent(); let newAgentInput = this.removeShipmentFromAgent(existingAgentSolution, shipmentInfo.getActions()[0].getShipmentIndex()); this.addUnassignedShipment(shipmentInfo); if (newAgentInput.agentShipmentIndexes.size == 0 && newAgentInput.agentJobIndexes.size == 0) { this.removeAgent(existingAgentSolution.getAgentIndex()); } else { let optimizedRouterPlan = yield this.optimizeRoute(newAgentInput); this.updateAgent(optimizedRouterPlan, shipmentInfo.getAgent().getAgentIndex()); } }); } addShipmentsToAgent(agentIndex, shipmentIndexes, existingAgent) { let optimizedAgentInput = this.generateOptimizeAgentInput(agentIndex, existingAgent); shipmentIndexes.forEach(shipmentIndex => { optimizedAgentInput.agentShipmentIndexes.add(shipmentIndex); }); return optimizedAgentInput; } removeShipmentFromAgent(existingAgent, shipmentIndex) { let optimizedAgentInput = this.generateOptimizeAgentInput(existingAgent.getAgentIndex(), existingAgent); optimizedAgentInput.agentShipmentIndexes.delete(shipmentIndex); return optimizedAgentInput; } validateShipments(shipmentIndexes, agentIndex) { if (shipmentIndexes.length == 0) { throw new Error("No shipments provided"); } if (!this.checkIfArrayIsUnique(shipmentIndexes)) { throw new Error("Shipments are not unique"); } shipmentIndexes.forEach((shipmentIndex) => { let shipmentInfo = this.result.getShipmentInfoByIndex(shipmentIndex); if (shipmentInfo == undefined) { this.validateShipmentExists(shipmentIndex); } if (agentIndex != undefined) { if ((shipmentInfo === null || shipmentInfo === void 0 ? void 0 : shipmentInfo.getAgent().getAgentIndex()) == agentIndex) { throw new Error(`Shipment with index ${shipmentIndex} already assigned to agent with index ${agentIndex}`); } } }); } validateShipmentExists(shipmentIndex) { let shipmentFound = this.getShipmentByIndex(shipmentIndex); if (!shipmentFound) { throw new Error(`Shipment with index ${shipmentIndex} not found`); } else { let isUnassignedShipment = this.result.getRawData().properties.issues.unassigned_shipments.includes(shipmentIndex); if (!isUnassignedShipment) { throw new Error(`Shipment with index ${shipmentIndex} is invalid`); } } } validateNewShipments(shipments) { if (shipments.length == 0) { throw new Error("No shipments provided"); } if (!this.checkIfArrayIsUnique(shipments)) { throw new Error("Shipments are not unique"); } shipments.forEach((job) => { if (job.id == undefined) { throw new Error("Shipment id is undefined"); } }); } addUnassignedShipment(shipmentInfo) { this.generateEmptyUnassignedShipmentsIfNeeded(); this.result.getRawData().properties.issues.unassigned_shipments.push(shipmentInfo.getActions()[0].getShipmentIndex()); } setShipmentPriority(jobIndex, newPriority) { if (newPriority != undefined) { this.result.getRawData().properties.params.shipments[jobIndex].priority = newPriority; } } }