From a512b05cab770f681e0f20a63d1ae43f4f4fdb2c Mon Sep 17 00:00:00 2001 From: elysiacasandra Date: Mon, 18 Sep 2023 12:06:04 +1000 Subject: [PATCH 1/3] Bidirectional Roads are now named --- src/components/Canvas/BidirectionalRoad.tsx | 47 ++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/components/Canvas/BidirectionalRoad.tsx b/src/components/Canvas/BidirectionalRoad.tsx index 2f9f171..5a3771e 100644 --- a/src/components/Canvas/BidirectionalRoad.tsx +++ b/src/components/Canvas/BidirectionalRoad.tsx @@ -1,4 +1,4 @@ -import { Arrow } from 'react-konva'; +import { Arrow, Text } from 'react-konva'; import { Edge } from '~/types/Network'; import { useNetworkStore } from '~/zustand/useNetworkStore'; @@ -22,8 +22,53 @@ export function BidirectionalRoad({ const from = networkStore.nodes[edge.from]; const to = networkStore.nodes[edge.to]; + // Printing road name : Angle and Placement + // Calculate the difference in X and Y coordinates + const deltaX = to.x - from.x; + const deltaY = to.y - from.y; + + // Use Math.atan2 to calculate the angle in radians + const radians = Math.atan2(deltaY, deltaX); + + // Convert radians to degrees + const degrees = (radians * 180) / Math.PI; + + // Ensure the result is a positive angle between 0 and 360 degrees + const positiveDegrees = (degrees + 360) % 360; + + let degreeDisplay; + let xName; + let yName; + + if (positiveDegrees < 45 || positiveDegrees >= 270) { + degreeDisplay= positiveDegrees + xName=from.x + 25 + yName=from.y-50 + } + else if (45 <= positiveDegrees && positiveDegrees < 90) { + degreeDisplay = positiveDegrees + xName= from.x +50 + yName= from.y + } + else if (positiveDegrees >= 90 && positiveDegrees < 270) { + degreeDisplay = positiveDegrees - 180 + xName= from.x - 100 + yName= from.y - 50 + + } + console.log(degreeDisplay) + return ( <> + Date: Mon, 18 Sep 2023 13:26:55 +1000 Subject: [PATCH 2/3] Console.logs analytics once a simulation has ended --- src/api/network.ts | 41 ++++++++++++++++++++++++++++ src/components/FloatingPlayPause.tsx | 4 ++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/api/network.ts b/src/api/network.ts index 1cc2774..ccb76b4 100644 --- a/src/api/network.ts +++ b/src/api/network.ts @@ -50,6 +50,47 @@ export async function getSimulationOutput( return await response.json(); } +export async function getSimulationAnalytics( + simulationAnalytics: SimulationOutput, +): Promise { + const tripInfo = simulationAnalytics.tripInfo; + const netState = simulationAnalytics.netstate; +// Run time: Measured in simulation seconds + +// Average duration: The average time each vehicle needed to accomplish the route in simulation seconds + // Extract all durations into an array + const durations = tripInfo.map((trip) => trip.duration); + + // Calculate the average duration + const totalDuration = durations.reduce((acc, duration) => acc + duration, 0); + const averageDuration = totalDuration / tripInfo.length; + +// Waiting time: The average time in which vehicles speed was below or equal 0.1 m/s in simulation seconds + // Extract all durations into an array + const waitingTime = tripInfo.map((trip) => trip.waitingTime); + + // Calculate the average duration + const totalWaiting = waitingTime.reduce((acc, time) => acc + time, 0); + const averageWaiting = totalWaiting / tripInfo.length; + +// Time loss: The time lost due to driving below the ideal speed. (ideal speed includes the individual speedFactor; slowdowns due to intersections etc. will incur timeLoss, scheduled stops do not count) in simulation seconds +// Extract all durations into an array +const timeLoss = tripInfo.map((trip) => trip.timeLoss); + +// Calculate the average duration +const totalTimeLoss = timeLoss.reduce((acc, time) => acc + time, 0); +const averageTimeLoss = totalTimeLoss / tripInfo.length; + + +// Total number of cars that reached their destination. Can work this out with vaporised variable +const noFinish = tripInfo.filter((trip) => trip.vaporized === true).length; +const totalNumberOfCarsThatCompleted = tripInfo.length - noFinish; + + +// Return the averages as an array + return [averageDuration, averageWaiting, averageTimeLoss, totalNumberOfCarsThatCompleted]; +} + export async function getSimulationInfo( simulationId: string, ): Promise { diff --git a/src/components/FloatingPlayPause.tsx b/src/components/FloatingPlayPause.tsx index 627ba2b..268c17e 100644 --- a/src/components/FloatingPlayPause.tsx +++ b/src/components/FloatingPlayPause.tsx @@ -3,7 +3,7 @@ import { CircleLoader } from 'react-spinners'; import { PlayIcon, StopIcon } from '@heroicons/react/24/outline'; -import { getSimulationOutput, uploadNetwork } from '~/api/network'; +import { getSimulationAnalytics, getSimulationOutput, uploadNetwork } from '~/api/network'; import { extractCarsFromSumoMessage } from '~/helpers/sumo'; import { useSimulation } from '~/hooks/useSimulation'; import { @@ -112,6 +112,7 @@ export const FloatingPlayPause = () => { } const simOutput = await getSimulationOutput(player.simulationId); + const simAnalytics = await getSimulationAnalytics(simOutput); if (startTime && simulationInfo) { simulationHistory.updateHistory({ @@ -125,6 +126,7 @@ export const FloatingPlayPause = () => { } console.log({ simOutput }); + console.log(simAnalytics) } catch (error: unknown) { console.error(error); } finally { From 0dd094a1e1363b87ffd5ecd1b3d1c8248648fc60 Mon Sep 17 00:00:00 2001 From: elysiacasandra Date: Mon, 18 Sep 2023 13:30:04 +1000 Subject: [PATCH 3/3] Remove road naming changes from this PR --- src/components/Canvas/BidirectionalRoad.tsx | 49 +-------------------- 1 file changed, 2 insertions(+), 47 deletions(-) diff --git a/src/components/Canvas/BidirectionalRoad.tsx b/src/components/Canvas/BidirectionalRoad.tsx index 5a3771e..58215aa 100644 --- a/src/components/Canvas/BidirectionalRoad.tsx +++ b/src/components/Canvas/BidirectionalRoad.tsx @@ -1,4 +1,4 @@ -import { Arrow, Text } from 'react-konva'; +import { Arrow } from 'react-konva'; import { Edge } from '~/types/Network'; import { useNetworkStore } from '~/zustand/useNetworkStore'; @@ -22,53 +22,8 @@ export function BidirectionalRoad({ const from = networkStore.nodes[edge.from]; const to = networkStore.nodes[edge.to]; - // Printing road name : Angle and Placement - // Calculate the difference in X and Y coordinates - const deltaX = to.x - from.x; - const deltaY = to.y - from.y; - - // Use Math.atan2 to calculate the angle in radians - const radians = Math.atan2(deltaY, deltaX); - - // Convert radians to degrees - const degrees = (radians * 180) / Math.PI; - - // Ensure the result is a positive angle between 0 and 360 degrees - const positiveDegrees = (degrees + 360) % 360; - - let degreeDisplay; - let xName; - let yName; - - if (positiveDegrees < 45 || positiveDegrees >= 270) { - degreeDisplay= positiveDegrees - xName=from.x + 25 - yName=from.y-50 - } - else if (45 <= positiveDegrees && positiveDegrees < 90) { - degreeDisplay = positiveDegrees - xName= from.x +50 - yName= from.y - } - else if (positiveDegrees >= 90 && positiveDegrees < 270) { - degreeDisplay = positiveDegrees - 180 - xName= from.x - 100 - yName= from.y - 50 - - } - console.log(degreeDisplay) - return ( <> - ); -} +} \ No newline at end of file