Skip to content
Draft
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ driver-app/app/google-services.json
static/uploads

#Simulation statistics
simulation-stats.jsonl
/simulation-stats.jsonl
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"test:e2e": "playwright test",
"test:unit": "./setup-db.sh && vitest --fileParallelism false",
"test:gen": "./setup-db.sh && vitest src/lib/server/booking/taxi/tests/generatedTests/gen.test.ts --fileParallelism false",
"test:one": "./setup-db.sh && vitest src/lib/server/availabilityCompensation/availabilityCompensation.test.ts --fileParallelism false",
"test:one": "./setup-db.sh && vitest src/lib/server/simulation/simulation.test.ts --fileParallelism false",
"sim": "pnpx vite-node --options.transformMode.ssr='/.*/' scripts/simulation/script.ts",
"sim:health": "pnpx vite-node --options.transformMode.ssr='/.*/' scripts/simulation/script.ts -- --ongoing --health --restrict --bu --wl",
"sim:nohealth": "pnpx vite-node --options.transformMode.ssr='/.*/' scripts/simulation/script.ts -- --ongoing --restrict --bu",
Expand Down
29 changes: 29 additions & 0 deletions src/lib/server/simulation/simulation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { beforeAll, describe, it } from 'vitest';
import { simulation, type SimulationParams } from './simulation';
import { MINUTE } from '$lib/util/time';
import { clearDatabase } from '$lib/testHelpers';

beforeAll(async () => {
await clearDatabase();
});

const duration = 9 * MINUTE;

describe(
'simulation as test',
() => {
it('simulation', async () => {
const p: SimulationParams = {
restrict: true,
finishTime: Date.now() + duration,
mode: 'taxi',
healthChecks: true,
full: true,
companies: 3,
vehiclesPerCompany: 3
};
await simulation(p);
});
},
MINUTE + duration
);
17 changes: 10 additions & 7 deletions src/lib/server/simulation/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ enum Action {
PUBLIC_TRANSPORT
}

type SimulationMode = 'taxi' | 'taxionly' | 'rs' | 'pt';
type ActionMode = 'ODM' | 'RIDE_SHARING';

type ActionType = {
action: Action;
probability: number;
Expand All @@ -61,7 +64,7 @@ type ActionType = {
customerId: number,
coordinates: Coordinates[],
restrictedCoordinates?: Coordinates[],
mode?: string,
mode?: ActionMode,
compareCosts?: boolean,
doWhitelist?: boolean
) => Promise<ActionResponse>;
Expand Down Expand Up @@ -129,7 +132,7 @@ const getAction = (r: number) => {
return undefined;
};

type Params = {
export type SimulationParams = {
backups?: boolean;
healthChecks?: boolean;
restrict?: boolean;
Expand All @@ -138,13 +141,13 @@ type Params = {
finishTime?: number;
whitelist?: boolean;
cost?: boolean;
mode?: string;
mode?: SimulationMode;
full: boolean;
companies: number;
vehiclesPerCompany: number;
};

async function setup(params: Params) {
async function setup(params: SimulationParams) {
adjustToParams(params);
const coordinates = await readCoordinates();
// The following coordinates are used to restrict either start or target (which is chosen at random) in each booking.
Expand Down Expand Up @@ -182,7 +185,7 @@ async function addCompanyLocal(vehicles: number, c: Coordinates) {
}
}

function adjustToParams(params: Params) {
function adjustToParams(params: SimulationParams) {
const probabilitySum = actionProbabilities.reduce((sum, curr) => sum + curr.probability, 0);
if (Math.abs(probabilitySum - 1) > 0.00000001) {
console.log('The probabilities in actionProbabilies must add to 1 exactly. ', {
Expand All @@ -206,7 +209,7 @@ function adjustToParams(params: Params) {
}
}

export async function simulation(params: Params): Promise<boolean> {
export async function simulation(params: SimulationParams): Promise<boolean> {
async function mainLoop(i: number) {
const r = Math.random();
console.log('RANDOM API ITERATION: ', i, ' with random value: ', r);
Expand Down Expand Up @@ -326,7 +329,7 @@ function lastActionWasRideShare(idx: number) {
);
}

function setActionProbabilities(mode: string) {
function setActionProbabilities(mode: SimulationMode) {
function setActionProbability(probability: number, action: Action) {
actionProbabilities[actionProbabilities.findIndex((a) => a.action === action)].probability =
probability;
Expand Down
Loading