Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Whitelist - Evaluate Single Insertions #161

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export enum Direction {
Backward
}

export type oneToManyResult = {
export type OneToManyResult = {
duration: number;
distance: number;
};
Expand All @@ -155,7 +155,7 @@ export const oneToMany = async (
one: Coordinates,
many: Coordinates[],
direction: Direction
): Promise<oneToManyResult[]> => {
): Promise<OneToManyResult[]> => {
const dir = direction == Direction.Forward ? 'Forward' : 'Backward';
const response = await fetch('https://europe.motis-project.de/', {
headers: {
Expand Down
19 changes: 19 additions & 0 deletions src/lib/compositionTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@ import type { Capacities } from './capacities';
import type { Interval } from './interval';
import type { Coordinates } from './location';

export type Company = {
id: number;
coordinates: Coordinates;
vehicles: Vehicle[];
zoneId: number;
};

export type Vehicle = {
id: number;
capacities: Capacities;
events: Event[];
availabilities: Interval[];
};

export type Event = {
capacities: Capacities;
is_pickup: boolean;
time: Interval;
id: number;
coordinates: Coordinates;
tourId: number;
arrival: Date;
departure: Date;
communicated: Date;
durationFromPrev: number;
durationToNext: number;
};
4 changes: 4 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ export const MAX_TRAVEL_DURATION = hoursToMs(1);
export const MAX_PASSENGER_WAITING_TIME_PICKUP = minutesToMs(10);
export const MAX_PASSENGER_WAITING_TIME_DROPOFF = minutesToMs(10);
export const SRID = 4326;
export const PASSENGER_CHANGE_TIME = minutesToMs(2);
export const TAXI_DRIVING_TIME_COST_FACTOR = 1;
export const TAXI_WAITING_TIME_COST_FACTOR = 0.2;
export const PASSENGER_TIME_COST_FACTOR = 1;
21 changes: 21 additions & 0 deletions src/lib/interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ export class Interval {
);
}

shrink(postponeStart: number, preponeEnd: number) {
return new Interval(
new Date(this.startTime.getTime() + postponeStart),
new Date(this.endTime.getTime() - preponeEnd)
);
}

isMergeable(other: Interval): boolean {
return this.overlaps(other) || this.touches(other);
}
Expand All @@ -91,4 +98,18 @@ export class Interval {
merged.push(unmerged.pop()!);
return merged;
};

intersect(other: Interval): Interval | undefined {
if (this.overlaps(other)) {
return new Interval(
new Date(Math.max(this.startTime.getTime(), other.startTime.getTime())),
new Date(Math.min(this.endTime.getTime(), other.endTime.getTime()))
);
}
return undefined;
}

covers(time: Date): boolean {
return this.startTime <= time && this.endTime >= time;
}
}
4 changes: 4 additions & 0 deletions src/lib/time_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export function minutesToMs(minutes: number) {
export function hoursToMs(hours: number) {
return hours * 3600000;
}

export function yearsToMs(years: number) {
return years * 365 * 3600000 * 24;
}
2 changes: 2 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ export interface EventTable {
tour: number;
customer: string;
request: number;
durationToNext: number;
durationFromPrev: number;
}

export type Event = Selectable<EventTable>;
Expand Down
11 changes: 8 additions & 3 deletions src/routes/api/booking/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { MAX_TRAVEL_DURATION, MIN_PREP_MINUTES } from '$lib/constants.js';
import { sql } from 'kysely';
import { getFareEstimation } from './fare-estimation/fare_estimation.js';
import { covers } from '$lib/sqlHelpers.js';
import type { RequestEvent } from './$types';

const startAndTargetShareZone = async (from: Coordinates, to: Coordinates) => {
const zoneContainingStartAndDestination = await db
Expand All @@ -18,7 +19,7 @@ const startAndTargetShareZone = async (from: Coordinates, to: Coordinates) => {
return zoneContainingStartAndDestination != undefined;
};

export const POST = async (event) => {
export const POST = async (event: RequestEvent) => {
const customer = event.locals.user;
if (!customer) {
return error(403);
Expand Down Expand Up @@ -427,7 +428,9 @@ export const POST = async (event) => {
address: startAddress.id,
request: requestId!,
tour: tourId!,
customer: customerId
customer: customerId,
durationFromPrev: 0,
durationToNext: 0
},
{
is_pickup: false,
Expand All @@ -438,7 +441,9 @@ export const POST = async (event) => {
address: targetAddress.id,
request: requestId!,
tour: tourId!,
customer: customerId
customer: customerId,
durationFromPrev: 0,
durationToNext: 0
}
])
.execute();
Expand Down
7 changes: 6 additions & 1 deletion src/routes/api/whitelist/capacitySimulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ function createEventCapacitiesOnly(capacities: Capacities, is_pickup: boolean):
time: new Interval(now, now),
id: 1,
coordinates: new Coordinates(1, 1),
tourId: 1
tourId: 1,
arrival: new Date(),
departure: new Date(),
communicated: new Date(),
durationFromPrev: 0,
durationToNext: 0
};
}

Expand Down
115 changes: 115 additions & 0 deletions src/routes/api/whitelist/evaluateSingleInsertions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { describe, it, expect } from 'vitest';
import { type Range } from './capacitySimulation';
import { Coordinates } from '$lib/location';
import { type RoutingResults } from './routing';
import { computeTravelDurations } from './insertions';
import type { Vehicle, Company, Event } from '$lib/compositionTypes';
import { Interval } from '$lib/interval';
import { hoursToMs } from '$lib/time_utils';

const BASE_MS = new Date(Date.now() + hoursToMs(5)).getTime();

const createDate = (h: number) => {
return new Date(BASE_MS + hoursToMs(h));
};

const createCompany = (vehicles: Vehicle[], coordinates: Coordinates): Company => {
return {
id: 1,
coordinates: coordinates,
vehicles,
zoneId: 1
};
};

const createVehicle = (id: number, events: Event[]): Vehicle => {
return {
id,
capacities: { passengers: 0, bikes: 0, wheelchairs: 0, luggage: 0 },
events,
availabilities: []
};
};

const createEvent = (coordinates: Coordinates, time: number): Event => {
return {
capacities: { passengers: 0, bikes: 0, wheelchairs: 0, luggage: 0 },
is_pickup: true,
time: new Interval(createDate(4), createDate(5)),
id: 1,
coordinates,
tourId: 1,
communicated: createDate(time),
arrival: createDate(6),
departure: createDate(3),
durationFromPrev: 0,
durationToNext: 0
};
};

describe('compute Intervals for single insertions test', () => {
it('TODO', () => {
let eventLatLng = 70;
let companyLatLng = 5;
const companies = [
createCompany(
[
createVehicle(1, [
createEvent(new Coordinates(eventLatLng, eventLatLng++), 4),
createEvent(new Coordinates(eventLatLng, eventLatLng++),5),
createEvent(new Coordinates(eventLatLng, eventLatLng++),6)
])
],
new Coordinates(companyLatLng, companyLatLng++)
)
];
companies[0].vehicles.forEach((v)=>v.events.forEach((e)=>{e.arrival=createDate(2);e.departure=createDate(7);}));
const insertions = new Map<number, Range[]>();
insertions.set(1, [{ earliestPickup: 0, latestDropoff: 3 }]);
const travelDurations = [50];
const routingResults: RoutingResults = {
busStops: [
{
fromCompany: [{ duration: 10, distance: 0 }],
toCompany: [{ duration: 10, distance: 0 }],
fromPrevEvent: [
{ duration: 10, distance: 0 },
{ duration: 10, distance: 0 },
{ duration: 10, distance: 0 }
],
toNextEvent: [
{ duration: 10, distance: 0 },
{ duration: 10, distance: 0 },
{ duration: 10, distance: 0 }
]
}
],
userChosen: {
fromCompany: [{ duration: 10, distance: 0 }],
toCompany: [{ duration: 10, distance: 0 }],
fromPrevEvent: [
{ duration: 10, distance: 0 },
{ duration: 10, distance: 0 },
{ duration: 10, distance: 0 }
],
toNextEvent: [
{ duration: 10, distance: 0 },
{ duration: 10, distance: 0 },
{ duration: 10, distance: 0 }
]
}
};
const busStopTimes = [[new Interval(createDate(5), createDate(6))]];
const result = computeTravelDurations(
companies,
insertions,
routingResults,
travelDurations,
true,
busStopTimes,
[[true]]
);
console.log("res", result);
expect(1).toBe(1);
});
});
Loading
Loading