@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
66 lines (65 loc) • 3.57 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 { PRESERVE_ORDER, REOPTIMIZE } from "../../../../models";
import { RequirementHelper } from "../base";
import { ShipmentRemovePreserveOrderStrategy } from "./shipment-remove-preserve-order-strategy";
import { ShipmentRemoveReoptimizeStrategy } from "./shipment-remove-reoptimize-strategy";
import { RouteViolationValidator } from "../../validations";
/**
* Strategy that reoptimizes only the target agent when assigning shipments
*/
export class ShipmentAssignReoptimizeStrategy {
execute(context, agentIndex, shipmentIndexes, options) {
return __awaiter(this, void 0, void 0, function* () {
yield this.removeShipmentsFromCurrentAgents(context, shipmentIndexes, options);
const targetShipmentIndexes = new Set(context.getAgentShipments(agentIndex));
shipmentIndexes.forEach((shipmentIndex) => targetShipmentIndexes.add(shipmentIndex));
const targetJobIndexes = new Set(context.getAgentJobs(agentIndex));
const inputData = context.cloneInputData();
const targetAgent = inputData.agents[agentIndex];
if (!targetAgent) {
return false;
}
RequirementHelper.extendAgentTimeWindows(targetAgent);
this.clearTimeWindowsForAssignedShipments(inputData.shipments || [], shipmentIndexes);
inputData.agents = [targetAgent];
RequirementHelper.restrictAssignmentsToTargetSet(inputData.jobs || [], targetJobIndexes);
RequirementHelper.restrictAssignmentsToTargetSet(inputData.shipments || [], targetShipmentIndexes);
yield context.executeAgentPlan(agentIndex, inputData);
RouteViolationValidator.validate(context, agentIndex);
return true;
});
}
removeShipmentsFromCurrentAgents(context, shipmentIndexes, 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 ShipmentRemoveReoptimizeStrategy()
: new ShipmentRemovePreserveOrderStrategy();
const removeOptions = { strategy: removeStrategyType };
yield removeStrategy.execute(context, shipmentIndexes, removeOptions);
});
}
clearTimeWindowsForAssignedShipments(shipments, assignedShipmentIndexes) {
for (const shipmentIndex of assignedShipmentIndexes) {
const shipment = shipments[shipmentIndex];
if (!shipment) {
continue;
}
if (shipment.pickup) {
delete shipment.pickup.time_windows;
}
if (shipment.delivery) {
delete shipment.delivery.time_windows;
}
}
}
}