@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.61 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 jobs
*/
export class JobRemoveReoptimizeStrategy {
execute(context, jobIndexes, options) {
return __awaiter(this, void 0, void 0, function* () {
const jobsByAgent = new Map();
for (const jobIndex of jobIndexes) {
const agentIndex = context.getAgentIndexForJob(jobIndex);
if (agentIndex === undefined) {
continue;
}
if (!jobsByAgent.has(agentIndex)) {
jobsByAgent.set(agentIndex, []);
}
jobsByAgent.get(agentIndex).push(jobIndex);
}
for (const [agentIndex, removedJobIndexes] of jobsByAgent.entries()) {
const removedSet = new Set(removedJobIndexes);
const remainingJobs = context
.getAgentJobs(agentIndex)
.filter((index) => !removedSet.has(index));
const targetJobIndexes = new Set(remainingJobs);
const inputData = context.cloneInputData();
const targetShipmentIndexes = new Set(context.getAgentShipments(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;
});
}
}