@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
51 lines (50 loc) • 2.69 kB
JavaScript
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 { RequirementHelper } from "../base";
import { RouteViolationValidator } from "../../validations";
/**
* Strategy that reoptimizes the route after removing shipments
*/
export class ShipmentRemoveReoptimizeStrategy {
execute(context, shipmentIndexes, options) {
return __awaiter(this, void 0, void 0, function* () {
const shipmentsByAgent = new Map();
for (const shipmentIndex of shipmentIndexes) {
const agentIndex = context.getAgentIndexForShipment(shipmentIndex);
if (agentIndex === undefined) {
continue;
}
if (!shipmentsByAgent.has(agentIndex)) {
shipmentsByAgent.set(agentIndex, []);
}
shipmentsByAgent.get(agentIndex).push(shipmentIndex);
}
for (const [agentIndex, removedShipmentIndexes] of shipmentsByAgent.entries()) {
const removedSet = new Set(removedShipmentIndexes);
const remainingShipments = context
.getAgentShipments(agentIndex)
.filter((index) => !removedSet.has(index));
const targetShipmentIndexes = new Set(remainingShipments);
const inputData = context.cloneInputData();
const targetJobIndexes = new Set(context.getAgentJobs(agentIndex));
const targetAgent = inputData.agents[agentIndex];
if (!targetAgent) {
continue;
}
inputData.agents = [targetAgent];
RequirementHelper.restrictAssignmentsToTargetSet(inputData.jobs || [], targetJobIndexes);
RequirementHelper.restrictAssignmentsToTargetSet(inputData.shipments || [], targetShipmentIndexes);
yield context.executeAgentPlan(agentIndex, inputData);
RouteViolationValidator.validate(context, agentIndex);
}
return true;
});
}
}