@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
86 lines (85 loc) • 4.45 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 { InvalidParameter } from "../../../models";
import { IndexConverter } from "../../../helpers/index-converter";
import { RequirementHelper } from "../strategies/base/requirement-helper";
import { RouteViolationValidator } from "../validations";
export class AgentReoptimizeHelper {
static execute(context, agentIdOrIndex, options = {}) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
if (agentIdOrIndex === undefined || agentIdOrIndex === null) {
throw new InvalidParameter("agentIdOrIndex is required", "agentIdOrIndex");
}
const agentIndex = IndexConverter.convertAgentToIndex(context.getRawData(), agentIdOrIndex, true);
const targetJobIndexes = new Set(context.getAgentJobs(agentIndex));
const targetShipmentIndexes = new Set(context.getAgentShipments(agentIndex));
if (options.includeUnassigned) {
this.addUnassignedIndexes(targetJobIndexes, (_a = context.getRawData().properties.issues) === null || _a === void 0 ? void 0 : _a.unassigned_jobs);
this.addUnassignedIndexes(targetShipmentIndexes, (_b = context.getRawData().properties.issues) === null || _b === void 0 ? void 0 : _b.unassigned_shipments);
}
if (targetJobIndexes.size === 0 && targetShipmentIndexes.size === 0) {
return true;
}
const inputData = context.cloneInputData();
const targetAgent = inputData.agents[agentIndex];
if (!targetAgent) {
return true;
}
if (options.allowViolations) {
this.removeViolationsFromInput(targetAgent, inputData.jobs || [], inputData.shipments || [], targetJobIndexes, targetShipmentIndexes);
}
inputData.agents = [targetAgent];
RequirementHelper.restrictAssignmentsToTargetSet(inputData.jobs || [], targetJobIndexes);
RequirementHelper.restrictAssignmentsToTargetSet(inputData.shipments || [], targetShipmentIndexes);
yield context.executeAgentPlan(agentIndex, inputData);
RouteViolationValidator.validate(context, agentIndex);
context.updateIssues();
return true;
});
}
static addUnassignedIndexes(targetIndexes, unassignedIndexes) {
if (!(unassignedIndexes === null || unassignedIndexes === void 0 ? void 0 : unassignedIndexes.length)) {
return;
}
for (const index of unassignedIndexes) {
targetIndexes.add(index);
}
}
static removeViolationsFromInput(targetAgent, jobs, shipments, targetJobIndexes, targetShipmentIndexes) {
RequirementHelper.extendAgentTimeWindows(targetAgent);
delete targetAgent.breaks;
if (Object.prototype.hasOwnProperty.call(targetAgent, "pickup_capacity")) {
targetAgent.pickup_capacity = this.MAX_CAPACITY;
}
if (Object.prototype.hasOwnProperty.call(targetAgent, "delivery_capacity")) {
targetAgent.delivery_capacity = this.MAX_CAPACITY;
}
for (const jobIndex of targetJobIndexes) {
const job = jobs[jobIndex];
if (job) {
delete job.time_windows;
}
}
for (const shipmentIndex of targetShipmentIndexes) {
const shipment = shipments[shipmentIndex];
if (!shipment) {
continue;
}
if (shipment.pickup) {
delete shipment.pickup.time_windows;
}
if (shipment.delivery) {
delete shipment.delivery.time_windows;
}
}
}
}
AgentReoptimizeHelper.MAX_CAPACITY = 2147483647;