Commit b9651954 authored by Carlos's avatar Carlos

applying djikstra algo

parent 2230c3e0
import express from 'express'
import Graph from 'node-dijkstra';
const route = new Graph()
const app = express()
// Node graph (store floor plan) with edges, nodes and distance.
route.addNode('SP5', { C1C2:1 })
route.addNode('C1C2', { SP5:1, C1:1, SP6SP5:1 })
route.addNode('C1', { C1C2:1 })
route.addNode('SP6SP5', { C1C2:1, C1B1:1, C2B2:1 })
route.addNode('C1B1', { SP6SP5:1, SP6:1, B1:1 })
route.addNode('SP6', { C1B1:1 })
route.addNode('B1', { C1B1:1 })
route.addNode('C2B2', { SP6SP5:1, C2:1, B2:1, C3B3:1 })
route.addNode('B2', { C2B2:1 })
route.addNode('C2', { C2B2:1 })
route.addNode('C3B3', { C2B2:1, C3:1, B3:1, SP3SP4:1 })
route.addNode('B3', { C3B3:1 })
route.addNode('C3', { C3B3:1 })
route.addNode('SP3SP4', { C3C4:1, C4B5:1, C3B3:1, B4B5:1 })
route.addNode('C3C4', { SP3SP4:1, C4:1, SP4:1 })
route.addNode('SP4', { C3C4:1 })
route.addNode('C4', { C3C4:1 })
route.addNode('C4B5', { SP3:1, SP3SP4:1, B5:1 })
route.addNode('B5', { C4B5:1 })
route.addNode('SP3', { C4B5:1 })
route.addNode('B4B5', { SP3SP4:1, B4:1, SP2B4B5:1 })
route.addNode('B4', { B4B5:1 })
route.addNode('SP2B4B5', { B4B5:1, B5A3:1, B2A2:2 })
route.addNode('B5A3', { SP2B4B5:1, A3:1, SP2:1 })
route.addNode('A3', { B5A3:1 })
route.addNode('SP2', { B5A3:1 })
route.addNode('B2A2', { A2:1, SP2B4B5:2, B1A1:2 })
route.addNode('A2', { B2A2:1 })
route.addNode('B1A1', { B2A2:2, A1:2, SP1:1 })
route.addNode('A1', { B1A1:1 })
route.addNode('SP1', { B1A1:1 })
// coordinates
const coordinates = {
SP5: [101.75, 5],
C1C2: [101.75, 4.3],
C1: [101.4, 4.3],
C1B1: [100.5, 3.3],
B1: [100.5, 3.0],
SP6: [100, 3.3],
SP6SP5: [101.75, 3.3],
SP3SP4: [104.85, 3.3],
C2B2: [102.7, 3.3],
C2: [102.7, 3.6],
B2: [102.7, 3.0],
C3B3: [103.7, 3.3],
C3: [103.7, 3.6],
B3: [103.7, 3.0],
C4B5: [106.2, 3.3],
SP3: [107, 3.3],
B5: [106.2, 3.0],
C3C4: [104.85, 4.3],
C4: [105.2, 4.3],
SP4: [104.85, 5],
B4B5: [104.85, 2],
B4 : [104.50, 2],
SP2B4B5: [104.85, 1.35],
B5A3: [105.5, 1.35],
A3: [105.5, 1.0],
SP2: [107, 1.35],
B2A2: [102.5, 1.35],
A2: [102.5, 1.0],
B1A1: [100.5, 1.35],
A1: [100.5, 1.0],
SP1: [100, 1.35],
}
const getCoordinates = (shortestPath) => {
const c = shortestPath.map((locationId)=>{
return coordinates[locationId];
})
return c;
}
app.get('/getShortestPath', function (req, res) {
console.log(req.query.productCoordinates);
console.log(req.query.startingPointCoordinates);
console.log(req.query.productLocationId);
console.log(req.query.startingPointId);
// consume the dijkstra lib to get the path
res.send([
[100, 1.35],
[104.85, 1.35],
[104.85, 2],
[104.5,2]
])
const shortestPath = route.path(req.query.startingPointId, req.query.productLocationId);
console.log('shortesPath:', shortestPath);
const coordinatesPath = getCoordinates(shortestPath);
console.log('coordinatesPath:', coordinatesPath);
// res.send([
// [100, 1.35],
// [104.85, 1.35],
// [104.85, 2],
// [104.5,2]
// ])
res.send(coordinatesPath);
})
console.log("listening in port: 3000")
app.listen(3000)
\ No newline at end of file
......@@ -9,7 +9,8 @@
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"express": "^4.18.2"
"express": "^4.18.2",
"node-dijkstra": "^2.5.0"
}
},
"node_modules/accepts": {
......@@ -423,6 +424,11 @@
"node": ">= 0.6"
}
},
"node_modules/node-dijkstra": {
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/node-dijkstra/-/node-dijkstra-2.5.0.tgz",
"integrity": "sha512-2REYb1lo8yDRAY1gsdhjwQdGVSh47VI5Z5wS8sCqydO/P1OVAKOwLuV1fDxNLvbrtMspW7k2UZ2JKIKP06hl+A=="
},
"node_modules/object-inspect": {
"version": "1.13.1",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.1.tgz",
......
......@@ -9,6 +9,7 @@
"author": "Carlos Alvarez",
"license": "ISC",
"dependencies": {
"express": "^4.18.2"
"express": "^4.18.2",
"node-dijkstra": "^2.5.0"
}
}
......@@ -6,15 +6,7 @@ import useFetch from "./hooks/useFetch";
import ProductListItem from "./components/productListItem";
import { useEffect, useState } from "react";
import Graph from 'node-dijkstra';
const route = new Graph()
route.addNode('A', { B:1 })
route.addNode('B', { A:1, C:2, D: 4 })
route.addNode('C', { B:2, D:1 })
route.addNode('D', { C:1, B:4 })
function App() {
const {
......@@ -38,7 +30,6 @@ function App() {
} = useFetch(`${import.meta.env.VITE_API_ENDPOINT}/todos`);
const [currentProductId, setCurrentProductId] = useState<null | number>(null);
console.log(route.path('A', 'D')); // => [ 'A', 'B', 'C', 'D' ]
useEffect(() => {
if (results.length > 0) {
......
......@@ -29,7 +29,7 @@ def hello_world():
print('hee')
# pre Step: getting the path lineString coordinates
api_url = "http://localhost:3000/getShortestPath?productCoordinates=[104.5,%202]&startingPointCoordinates=[100,%201.35]"
api_url = "http://localhost:3000/getShortestPath?productLocationId=SP5&startingPointId=A1"
response = requests.get(api_url)
coordinates = response.json()
......
// Creating grahp, nodes and edges
route.addNode('SP5', { C1C2:1 })
route.addNode('C1C2', { SP5:1, C1:1, SP6SP5:1 })
route.addNode('C1', { C1C2:1 })
route.addNode('SP6SP5', { C1C2:1, C1B1:1, C2B2:1 })
route.addNode('C1B1', { SP6SP5:1, SP6:1, B1:1 })
route.addNode('SP6', { C1B1:1 })
route.addNode('B1', { C1B1:1 })
route.addNode('C2B2', { SP6SP5:1, C2:1, B2:1, C3B3:1 })
route.addNode('B2', { C2B2:1 })
route.addNode('C2', { C2B2:1 })
route.addNode('C3B3', { C2B2:1, C3:1, B3:1, SP3SP4:1 })
route.addNode('B3', { C3B3:1 })
route.addNode('C3', { C3B3:1 })
route.addNode('SP3SP4', { C3C4:1, C4B5:1, C3B3:1, B4B5:1 })
route.addNode('C3C4', { SP3SP4:1, C4:1, SP4:1 })
route.addNode('SP4', { C3C4:1 })
route.addNode('C4', { C3C4:1 })
route.addNode('C4B5', { SP3:1, SP3SP4:1, B5:1 })
route.addNode('B5', { C4B5:1 })
route.addNode('SP3', { C4B5:1 })
route.addNode('B4B5', { SP3SP4:1, B4:1, SP2B4B5:1 })
route.addNode('B4', { B4B5:1 })
route.addNode('SP2B4B5', { B4B5:1, B5A3:1, B2A2:2 })
route.addNode('B5A3', { SP2B4B5:1, A3:1, SP2:1 })
route.addNode('A3', { B5A3:1 })
route.addNode('SP2', { B5A3:1 })
route.addNode('B2A2', { A2:1, SP2B4B5:2, B1A1:2 })
route.addNode('A2', { B2A2:1 })
route.addNode('B1A1', { B2A2:2, A1:2, SP1:1 })
route.addNode('A1', { B1A1:1 })
route.addNode('SP1', { B1A1:1 })
// Coordinates in the plot
SP5: [101.75, 5],
C1C2: [101.75, 4.3],
......@@ -12,7 +50,7 @@ C1B1: [100.5, 3.3],
B1: [100.5, 3.0],
SP6: [100, 3.3],
SP6SP5: [101.75, 3.3],
SP3PS4: [104.85, 3.3]
SP3SP4: [104.85, 3.3]
C2B2: [102.7, 3.3],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment