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 - gather coordinates for one to many routing #155

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
4e43ce0
format
nilspenzel Sep 19, 2024
1dea051
wip
nilspenzel Sep 19, 2024
5976f36
wip
nilspenzel Sep 19, 2024
c8d720f
Merge branch 'master' into gather
nilspenzel Sep 19, 2024
4537cc3
wip
nilspenzel Sep 19, 2024
d326dc5
wip
nilspenzel Sep 20, 2024
55d93bf
wip
nilspenzel Sep 20, 2024
de81c52
wip
nilspenzel Sep 20, 2024
cb2ad94
Remove unsed functions
nilspenzel Sep 20, 2024
d103439
wip
nilspenzel Sep 20, 2024
549f4c0
wip
nilspenzel Sep 24, 2024
1607734
Format
nilspenzel Sep 24, 2024
59f08bf
wip
nilspenzel Sep 27, 2024
b0506dd
wip
nilspenzel Sep 27, 2024
0107310
wip
nilspenzel Sep 27, 2024
3dd888e
wip
nilspenzel Sep 29, 2024
6f77181
wip
nilspenzel Oct 8, 2024
a2e70a1
wip
nilspenzel Oct 8, 2024
ecbfbd9
wip
nilspenzel Oct 8, 2024
92fa21d
wip
nilspenzel Oct 8, 2024
83bd7ba
wip
nilspenzel Oct 8, 2024
acd1862
Rename CheckBookingValidityParameters -> WhiteListRequest
nilspenzel Oct 22, 2024
301e25a
Move busStopCompanyFilter into Company
nilspenzel Oct 22, 2024
c019ab1
Use alias type to clarify that type is used for vehicle ids
nilspenzel Oct 22, 2024
ca74dd8
wip
nilspenzel Oct 25, 2024
0cafb6f
Merge master
nilspenzel Nov 21, 2024
89eac37
Alter order of deleting columns in e2e tests
nilspenzel Nov 21, 2024
d5db993
Merge branch 'master' into gather
nilspenzel Nov 21, 2024
bc44522
wip
nilspenzel Dec 4, 2024
dc9ff45
wip
nilspenzel Dec 4, 2024
cfab605
wip
nilspenzel Dec 4, 2024
ac4a0b4
wip
nilspenzel Dec 4, 2024
7408408
wip
nilspenzel Dec 4, 2024
e50da66
wip
nilspenzel Dec 4, 2024
d262020
wip
nilspenzel Dec 4, 2024
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
21 changes: 21 additions & 0 deletions src/lib/compositionTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ 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;
tours: Tour[];
availabilities: Interval[];
};

export type Tour = {
departure: Date;
arrival: Date;
id: number;
events: Event[];
};

export type Event = {
capacities: Capacities;
is_pickup: boolean;
Expand Down
113 changes: 113 additions & 0 deletions src/routes/api/whitelist/gatherCoordinates.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { describe, it, expect } from 'vitest';
import { type Range } from './capacitySimulation';
import { Coordinates } from '$lib/location';
import type { Vehicle, Company, Tour, Event } from '$lib/compositionTypes';
import { Interval } from '$lib/interval';
import { gatherRoutingCoordinates } from './routing';

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

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

const createTour = (events: Event[]): Tour => {
return {
departure: new Date(),
arrival: new Date(),
id: 1,
events
};
};

const createEvent = (coordinates: Coordinates): Event => {
return {
capacities: { passengers: 0, bikes: 0, wheelchairs: 0, luggage: 0 },
is_pickup: true,
time: new Interval(new Date(), new Date()),
id: 1,
coordinates,
tourId: 1
};
};

const createBusStop = () => {
return { coordinates: new Coordinates(1, 1), times: [] };
};

describe('gather coordinates test', () => {
it('TODO', () => {
let eventLatLng = 100;
let companyLatLng = 5;
const companies = [
createCompany(
[
createVehicle(1, [
createTour([
createEvent(new Coordinates(eventLatLng, eventLatLng++)),
createEvent(new Coordinates(eventLatLng, eventLatLng++))
])
])
],
new Coordinates(companyLatLng, companyLatLng++)
),
createCompany(
[
createVehicle(2, [
createTour([
createEvent(new Coordinates(eventLatLng, eventLatLng++)),
createEvent(new Coordinates(eventLatLng, eventLatLng++))
])
])
],
new Coordinates(companyLatLng, companyLatLng++)
)
];
const busStops = [createBusStop(), createBusStop()];
const insertions = new Map<number, Range[]>();
insertions.set(1, [{ earliestPickup: 0, latestDropoff: 2 }]);
insertions.set(2, [{ earliestPickup: 0, latestDropoff: 2 }]);
const coordinates = gatherRoutingCoordinates(companies, busStops, insertions);
expect(coordinates.busStopMany).toHaveLength(2);
expect(coordinates.userChosenMany).toHaveLength(6);
busStops.forEach((_, idx) => {
expect(coordinates.busStopMany[idx]).toHaveLength(6);
});
// Company coordinates
expect(coordinates.userChosenMany[0].lat).toBe(5);
expect(coordinates.busStopMany[0][0].lat).toBe(5);
expect(coordinates.busStopMany[1][0].lat).toBe(5);
expect(coordinates.userChosenMany[1].lat).toBe(6);
expect(coordinates.busStopMany[0][1].lat).toBe(6);
expect(coordinates.busStopMany[1][1].lat).toBe(6);

// Event coordinates
expect(coordinates.userChosenMany[2].lat).toBe(100);
expect(coordinates.busStopMany[0][2].lat).toBe(100);
expect(coordinates.busStopMany[1][2].lat).toBe(100);

expect(coordinates.userChosenMany[3].lat).toBe(101);
expect(coordinates.busStopMany[0][3].lat).toBe(101);
expect(coordinates.busStopMany[1][3].lat).toBe(101);

expect(coordinates.userChosenMany[4].lat).toBe(102);
expect(coordinates.busStopMany[0][4].lat).toBe(102);
expect(coordinates.busStopMany[1][4].lat).toBe(102);

expect(coordinates.userChosenMany[5].lat).toBe(103);
expect(coordinates.busStopMany[0][5].lat).toBe(103);
expect(coordinates.busStopMany[1][5].lat).toBe(103);
});
});
68 changes: 68 additions & 0 deletions src/routes/api/whitelist/routing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { BusStop } from '$lib/busStop';
import type { Company, Event } from '$lib/compositionTypes';
import { Coordinates } from '$lib/location';
import type { Range } from './capacitySimulation';

type RoutingCoordinates = {
busStopMany: Coordinates[][];
userChosenMany: Coordinates[];
};

function iterateAllInsertions(
companies: Company[],
insertions: Map<number, Range[]>,
insertionFn: (events: Event[], insertionIdx: number, companyPos: number, eventPos: number) => void
) {
let companyPos = 0;
let eventPos = companies.length;
companies.forEach((company) => {
company.vehicles.forEach((vehicle) => {
const events = vehicle.tours.flatMap((t) => t.events);
insertions.get(vehicle.id)!.forEach((insertion) => {
for (
let insertionIdx = insertion.earliestPickup;
insertionIdx != insertion.latestDropoff;
++insertionIdx
) {
insertionFn(events, insertionIdx, companyPos, eventPos++);
}
});
});
companyPos++;
});
}

export function gatherRoutingCoordinates(
companies: Company[],
busStops: BusStop[],
insertionsByVehicle: Map<number, Range[]>
): RoutingCoordinates {
const eventInsertionCount = companies
.flatMap((c) => c.vehicles)
.flatMap((v) => insertionsByVehicle.get(v.id)!)
.reduce((sum, current) => sum + current.latestDropoff - current.earliestPickup, 0);
const insertionCount = companies.length + eventInsertionCount;
const busStopMany = new Array<Coordinates[]>(busStops.length);
for (let i = 0; i != busStopMany.length; ++i) {
busStopMany[i] = new Array<Coordinates>(insertionCount);
}
const userChosenMany = new Array<Coordinates>(insertionCount);
companies.forEach((company, companyPos) => {
for (let busStopIdx = 0; busStopIdx != busStops.length; ++busStopIdx) {
busStopMany[busStopIdx][companyPos] = company.coordinates;
}
userChosenMany[companyPos] = company.coordinates;
});
iterateAllInsertions(companies, insertionsByVehicle, (events, insertionIdx, _, eventPos) => {
const eventCoordinates = events[insertionIdx].coordinates;
for (let busStopIdx = 0; busStopIdx != busStops.length; ++busStopIdx) {
busStopMany[busStopIdx][eventPos] = eventCoordinates;
}
userChosenMany[eventPos] = eventCoordinates;
});
console.log(userChosenMany);
return {
busStopMany,
userChosenMany
};
}