Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const data = {
- "algorithmData":{
- "0":{
- "left":{
- "0_0_0_1":{
- "parkingSpaceID":"0_0_0_1",
- "isValid":true,
- "parkingSpaceNumber":1
- },
- "0_0_0_2":{
- "parkingSpaceID":"0_0_0_2",
- "isValid":true,
- "parkingSpaceNumber":2
- },
- "0_0_0_3-invalid_OQzhP":{
- "parkingSpaceID":"0_0_0_3-invalid_OQzhP",
- "isValid":false,
- "parkingSpaceNumber":3
- },
- "0_0_0_4-invalid_25qO6":{
- "parkingSpaceID":"0_0_0_4-invalid_25qO6",
- "isValid":false,
- "parkingSpaceNumber":4
- },
- "0_0_0_5-invalid_NJ2Aw":{
- "parkingSpaceID":"0_0_0_5-invalid_NJ2Aw",
- "isValid":false,
- "parkingSpaceNumber":5
- },
- "0_0_0_6-invalid_Qlh6u":{
- "parkingSpaceID":"0_0_0_6-invalid_Qlh6u",
- "isValid":false,
- "parkingSpaceNumber":6
- },
- "0_0_0_7-invalid_xZDL4":{
- "parkingSpaceID":"0_0_0_7-invalid_xZDL4",
- "isValid":false,
- "parkingSpaceNumber":7
- },
- "0_0_0_8-invalid_msNUp":{
- "parkingSpaceID":"0_0_0_8-invalid_msNUp",
- "isValid":false,
- "parkingSpaceNumber":8
- }
- },
- "right":{
- "0_1_0_1":{
- "parkingSpaceID":"0_1_0_1",
- "isValid":true,
- "parkingSpaceNumber":1
- },
- "0_1_0_2":{
- "parkingSpaceID":"0_1_0_2",
- "isValid":true,
- "parkingSpaceNumber":2
- },
- "0_1_0_3-invalid_9qApW":{
- "parkingSpaceID":"0_1_0_3-invalid_9qApW",
- "isValid":false,
- "parkingSpaceNumber":3
- },
- "0_1_0_4":{
- "parkingSpaceID":"0_1_0_4",
- "isValid":true,
- "parkingSpaceNumber":4
- },
- "0_1_0_5":{
- "parkingSpaceID":"0_1_0_5",
- "isValid":true,
- "parkingSpaceNumber":5
- },
- "0_1_0_6":{
- "parkingSpaceID":"0_1_0_6",
- "isValid":true,
- "parkingSpaceNumber":6
- },
- "0_1_0_7-invalid_zQN3G":{
- "parkingSpaceID":"0_1_0_7-invalid_zQN3G",
- "isValid":false,
- "parkingSpaceNumber":7
- },
- "0_1_0_8":{
- "parkingSpaceID":"0_1_0_8",
- "isValid":true,
- "parkingSpaceNumber":8
- }
- }
- },
- "1":{
- "left":{
- "1_0_0_1":{
- "parkingSpaceID":"1_0_0_1",
- "isValid":true,
- "parkingSpaceNumber":1
- },
- "1_0_0_2":{
- "parkingSpaceID":"1_0_0_2",
- "isValid":true,
- "parkingSpaceNumber":2
- },
- "1_0_0_3-invalid_RxtPW":{
- "parkingSpaceID":"1_0_0_3-invalid_RxtPW",
- "isValid":false,
- "parkingSpaceNumber":3
- },
- "1_0_0_4":{
- "parkingSpaceID":"1_0_0_4",
- "isValid":true,
- "parkingSpaceNumber":4
- },
- "1_0_0_5":{
- "parkingSpaceID":"1_0_0_5",
- "isValid":true,
- "parkingSpaceNumber":5
- },
- "1_0_0_6":{
- "parkingSpaceID":"1_0_0_6",
- "isValid":true,
- "parkingSpaceNumber":6
- },
- "1_0_0_7-invalid_FK3hF":{
- "parkingSpaceID":"1_0_0_7-invalid_FK3hF",
- "isValid":false,
- "parkingSpaceNumber":7
- },
- "1_0_0_8":{
- "parkingSpaceID":"1_0_0_8",
- "isValid":true,
- "parkingSpaceNumber":8
- }
- }
- }
- }
- }
- const graph = {
- ingang: {'0': 0, 'B': 2},
- //Eerste verdieping
- '0': {AW1: 0},
- AW1: {AP1: 5, AP2: 5, AW2: 0},
- AW2: {AP3: 4, AP4: 4, AW3: 0},
- AW3: {AP5: 3, AP6: 3, AW4: 0},
- AW4: {AP7: 2, AP8: 2, AW5: 0},
- AW5: {AP9: 1, AP10: 1},
- AP1: {uitgang: 0},
- AP2: {uitgang: 0},
- AP3: {uitgang: 0},
- AP4: {uitgang: 0},
- AP5: {uitgang: 0},
- AP6: {uitgang: 0},
- AP7: {uitgang: Infinity},
- AP8: {uitgang: Infinity},
- AP9: {uitgang: Infinity},
- AP10: {uitgang: Infinity},
- //Tweede verdieping
- 'B': {BW1: 0},
- BW1: {BP1: 5, BW2: 0},
- BW2: {BP2: 4, BW3: 0},
- BW3: {BP3: 3, BW4: 0},
- BW4: {BP4: 2, BW5: 0},
- BW5: {BP5: 1},
- BP1: {uitgang: 1},
- BP2: {uitgang: 1},
- BP3: {uitgang: 1},
- BP4: {uitgang: 1},
- BP5: {uitgang: 1},
- uitgang: {},
- };
- const tempGraph = {
- 'ingang': {},
- 'uitgang': {}
- }
- const makeGraph = (data) => {
- const parkingSpots = data.algorithmData
- let connectionNodeI = 0
- let floorNodeI = 0
- //Loop through all the floors
- for(const floor in parkingSpots) {
- //For each floor add the floor to the start node
- tempGraph.ingang[floor] = floorNodeI
- //Get the keys for the left right places
- const LR = Object.keys(parkingSpots[floor])
- //Loop through all the spots and added them to the node graph
- let countSpots = 0
- //Count how many spots there are for each floor
- if(LR.length > 1) {
- LR.forEach(function (spot, index) {
- countSpots += Object.keys(parkingSpots[floor][spot]).length / 2
- })
- } else {
- LR.forEach(function (spot, index) {
- countSpots += Object.keys(parkingSpots[floor][spot]).length
- })
- }
- //Add the connection nodes to the graph
- for (const spotList in parkingSpots[floor]){
- console.log(spotList)
- for (var i = 1; i <= countSpots; i++){
- if (!((floor + '_' + 'C' + i) in tempGraph)) {
- if (i >= countSpots/2) {
- tempGraph[floor + '_' + 'C' + i] = {}
- } else if (i === 1) {
- tempGraph[floor + '_' + 'C' + i] = {}
- tempGraph[floor + '_' + 'C' + i][floor + '_' + 'C' + (i+1)] = 0
- tempGraph[floor] = {}
- tempGraph[floor][floor + '_' + 'C' + i] = 0
- } else {
- tempGraph[floor + '_' + 'C' + i] = {}
- tempGraph[floor + '_' + 'C' + i][floor + '_' + 'C' + (i+1)] = 0
- }
- } else {
- continue;
- }
- }
- let iSpot = 1
- for (const spotKey in parkingSpots[floor][spotList]) {
- let spot = parkingSpots[floor][spotList][spotKey];
- if (spot['isValid']) {
- tempGraph[floor + '_' + 'C' + iSpot][spot['parkingSpaceID']] = 0
- tempGraph[spot['parkingSpaceID']] = {'uitgang': 0}
- } else {
- tempGraph[floor + '_' + 'C' + iSpot][spot['parkingSpaceID']] = Infinity
- tempGraph[spot['parkingSpaceID']] = {'uitgang': Infinity}
- }
- iSpot++;
- }
- }
- floorNodeI++;
- }
- console.log('TEMP', tempGraph)
- }
- makeGraph(data)
- const findLowestCostNode = (costs, processed) => {
- const knownNodes = Object.keys(costs);
- return knownNodes.reduce((lowest, node) => {
- if (lowest === null && !processed.includes(node)) {
- lowest = node;
- }
- if (costs[node] < costs[lowest] && !processed.includes(node)) {
- lowest = node;
- }
- return lowest;
- }, null)
- };
- // function that returns the minimum cost and path to reach Finish
- const dijkstra = (graph) => {
- // de kosten van de childnodes van de parent node (bij het begin dus ingang)
- const trackedCosts = Object.assign({uitgang: Infinity}, graph.ingang);
- console.log(trackedCosts)
- // Hier worden de naam van de parent node van de child nodes opgeslagen
- const trackedParents = {uitgang: null};
- for (let child in graph.ingang) {
- trackedParents[child] = 'ingang';
- }
- //De lijst van de langsgelopen nodes
- const processedNodes = [];
- //Vind de laagste kostende node
- let node = findLowestCostNode(trackedCosts, processedNodes);
- while (node) {
- //Hoeveel het kost om naar die node te gaan
- let costToReachNode = trackedCosts[node];
- //De children van de node die nu wordt behandeld
- let childrenOfNode = graph[node];
- for (let child in childrenOfNode) {
- //Hoeveel het cost van de parent node naar de child node te gaan
- let costFromNodetoChild = childrenOfNode[child];
- //De totale kosten om deze route te volgen
- let costToChild = costToReachNode + costFromNodetoChild;
- //Checken of de node al in de lijst van langsgelopen nodes zit of dat de kosten van de loop hoger is dan de kost naar de child
- if (!trackedCosts[child] || trackedCosts[child] > costToChild) {
- //Voeg node toe aan aan de kosten van de langsgelopen nodes
- trackedCosts[child] = costToChild;
- //Voeg parent van de node toe aan de trackedParents lijst
- trackedParents[child] = node;
- }
- }
- //Voeg node toe aan langsgelopen nodes
- processedNodes.push(node);
- //Ga naar volgende node in de lijst
- node = findLowestCostNode(trackedCosts, processedNodes);
- }
- //Hierin komen de nodes voor het meest optimale pad
- let optimalPath = ['uitgang'];
- //Pak de node waarbij de child uitgang is
- let parent = trackedParents.uitgang;
- //Loop door trackedParents tot dat er geen parent node meer is
- while (parent) {
- optimalPath.push(parent);
- parent = trackedParents[parent];
- }
- //Draai de lijst om en haal uitgang weg
- optimalPath.reverse().pop(-1);
- //Return de optimale pad
- return optimalPath;
- };
- // makeGraph(data)
- console.log(dijkstra(tempGraph));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement