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

61 lines (60 loc) 3.27 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 { PRESERVE_ORDER, REOPTIMIZE } from "../../../../models"; import { RequirementHelper } from "../base"; import { RouteViolationValidator } from "../../validations"; import { JobRemovePreserveOrderStrategy } from "./job-remove-preserve-order-strategy"; import { JobRemoveReoptimizeStrategy } from "./job-remove-reoptimize-strategy"; /** * Strategy that reoptimizes only the target agent when assigning jobs */ export class JobAssignReoptimizeStrategy { execute(context, agentIndex, jobIndexes, options) { return __awaiter(this, void 0, void 0, function* () { yield this.removeJobsFromCurrentAgents(context, jobIndexes, options); const targetJobIndexes = new Set(context.getAgentJobs(agentIndex)); jobIndexes.forEach((jobIndex) => targetJobIndexes.add(jobIndex)); const targetShipmentIndexes = new Set(context.getAgentShipments(agentIndex)); const inputData = context.cloneInputData(); const targetAgent = inputData.agents[agentIndex]; if (!targetAgent) { return false; } RequirementHelper.extendAgentTimeWindows(targetAgent); this.clearTimeWindowsForAssignedJobs(inputData.jobs || [], jobIndexes); inputData.agents = [targetAgent]; RequirementHelper.restrictAssignmentsToTargetSet(inputData.jobs || [], targetJobIndexes); RequirementHelper.restrictAssignmentsToTargetSet(inputData.shipments || [], targetShipmentIndexes); const result = yield context.executeAgentPlan(agentIndex, inputData); RouteViolationValidator.validate(context, agentIndex); return result; }); } removeJobsFromCurrentAgents(context, jobIndexes, options) { var _a; return __awaiter(this, void 0, void 0, function* () { const removeStrategyType = (_a = options.removeStrategy) !== null && _a !== void 0 ? _a : PRESERVE_ORDER; const removeStrategy = removeStrategyType === REOPTIMIZE ? new JobRemoveReoptimizeStrategy() : new JobRemovePreserveOrderStrategy(); const removeOptions = { strategy: removeStrategyType }; yield removeStrategy.execute(context, jobIndexes, removeOptions); }); } clearTimeWindowsForAssignedJobs(jobs, assignedJobIndexes) { for (const jobIndex of assignedJobIndexes) { const job = jobs[jobIndex]; if (!job) { continue; } delete job.time_windows; } } }