|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import "./GraphTraversalVisualiser.css" |
| 3 | +import { UCS } from './graphAlgorithms/ucs'; |
| 4 | +import { BFS } from './graphAlgorithms/bfs'; |
| 5 | +import { DFS } from './graphAlgorithms/dfs'; |
| 6 | +import { conflict } from './graphAlgorithms/helper'; |
| 7 | + |
| 8 | +const MatrixVisualization = () => { |
| 9 | + const [matrix, setMatrix] = useState([]); |
| 10 | + const [start, setStart] = useState({ row: 0, col: 0 }); |
| 11 | + const [objectives, setObjectives] = useState([]); |
| 12 | + // const [weights, setWeights] = useState([]); |
| 13 | + const [obstacles, setObstacles] = useState([]); |
| 14 | + const [algorithm, setAlgorithm] = useState('Reset'); |
| 15 | + const [button, setButton] = useState(false); |
| 16 | + |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + generateMatrix(); |
| 20 | + }, []); |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + // generate a new matrix |
| 26 | + const generateMatrix = () => { |
| 27 | + const n = 30 |
| 28 | + const newMatrix = Array.from({ length: n }, () => Array(n).fill(" ")); // generate array |
| 29 | + const objectivesArray = []; // array to hold bojectives |
| 30 | + const obstaclesArray = []; // array to hold obstacles |
| 31 | + const indices = {}; // dictionary to hold indicies to avoid overlap |
| 32 | + const numObjectives = 2; // number of objectives |
| 33 | + const numObstacles = 100; // number of obstacles |
| 34 | + let x = Math.floor(Math.random() * (newMatrix.length)); // a random x cord |
| 35 | + let y = Math.floor(Math.random() * (newMatrix[0].length)); // a randon y cord |
| 36 | + setStart({ row: x, col: y }); // set the starting point |
| 37 | + indices[`${x},${y}`] = 0; // add starting point to indices |
| 38 | + |
| 39 | + // generate the map |
| 40 | + while (Object.keys(indices).length < numObjectives + numObstacles) { |
| 41 | + x = Math.floor(Math.random() * (newMatrix.length)); // new x cord |
| 42 | + y = Math.floor(Math.random() * (newMatrix[0].length)); // new y cord |
| 43 | + |
| 44 | + if (indices.hasOwnProperty(`${x},${y}`)) { // if its an index we have been do ignore it |
| 45 | + continue; |
| 46 | + } else { |
| 47 | + if (Object.keys(indices).length > numObjectives - 1) { |
| 48 | + if (conflict(x, y, newMatrix)) { // if there is a conflic ignore the index |
| 49 | + continue; |
| 50 | + } else { |
| 51 | + obstaclesArray.push({ row: x, col: y }); // add to obstacles |
| 52 | + indices[`${x},${y}`] = 0; |
| 53 | + newMatrix[x][y] = "w"; |
| 54 | + } |
| 55 | + } else { |
| 56 | + objectivesArray.push({ row: x, col: y }); // add to objectives |
| 57 | + indices[`${x},${y}`] = 0; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + // set the approriate obstacles, objectives and new matrix/grid |
| 62 | + setObjectives(objectivesArray); |
| 63 | + setObstacles(obstaclesArray); |
| 64 | + // setWeights(weightsArray); |
| 65 | + setMatrix(newMatrix); |
| 66 | + }; |
| 67 | + |
| 68 | + // reset the matrix |
| 69 | + const resetMatrix = () => { |
| 70 | + clearPath(); // remove the previously drawn path |
| 71 | + generateMatrix(); // generate a new matrix |
| 72 | + }; |
| 73 | + |
| 74 | + const colorNodes = (path, expanded) => { |
| 75 | + path.forEach(node => { |
| 76 | + const [row, col] = node; |
| 77 | + const nodeElement = document.querySelector(`.matrix-row-${row} .col-index-${col}`); |
| 78 | + if (nodeElement) { |
| 79 | + if (!(nodeElement.classList.contains('objective')) && !(nodeElement.classList.contains('start'))){ |
| 80 | + nodeElement.classList.add('path'); |
| 81 | + } |
| 82 | + }}); |
| 83 | + |
| 84 | + if (expanded){ |
| 85 | + expanded.forEach(node => { |
| 86 | + const [row, col] = node; |
| 87 | + const nodeElement = document.querySelector(`.matrix-row-${row} .col-index-${col}`); |
| 88 | + if (nodeElement) { |
| 89 | + if (!(nodeElement.classList.contains('path')) && !(nodeElement.classList.contains('objective')) && !(nodeElement.classList.contains('start'))){ |
| 90 | + nodeElement.classList.add('expanded'); |
| 91 | + } |
| 92 | + }}); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // clear the old path |
| 97 | + const clearPath = () => { |
| 98 | + const pathNodes = document.querySelectorAll('.path'); |
| 99 | + pathNodes.forEach((node) => { |
| 100 | + node.classList.remove('path'); |
| 101 | + }); |
| 102 | + const expandedNodes = document.querySelectorAll('.expanded'); |
| 103 | + expandedNodes.forEach((node) => { |
| 104 | + node.classList.remove('expanded'); |
| 105 | + }); |
| 106 | + }; |
| 107 | + |
| 108 | + const handleSubmit = (event) => { |
| 109 | + let method = algorithm; |
| 110 | + if (method === 'astar') { |
| 111 | + // |
| 112 | + } else if (method === 'BFS') { |
| 113 | + // console.log(objectives) |
| 114 | + const result = BFS(matrix, [start.row, start.col], [objectives[0].row, objectives[0].col]); |
| 115 | + colorNodes(result.path, result.expanded); |
| 116 | + } else if (method === 'Reset') { |
| 117 | + resetMatrix(); |
| 118 | + } else if (method === 'DFS') { |
| 119 | + const result = DFS(matrix,[start.row, start.col], [objectives[0].row, objectives[0].col]); |
| 120 | + colorNodes(result.path, result.expanded); |
| 121 | + } else if (method === 'UCS') { |
| 122 | + const result = UCS(matrix, [start.row, start.col], [objectives[0].row, objectives[0].col]); |
| 123 | + console.log(result.expanded); |
| 124 | + colorNodes(result.path, result.expanded); |
| 125 | + } |
| 126 | + |
| 127 | + event.preventDefault(); |
| 128 | + }; |
| 129 | + |
| 130 | + const handleChange = (event) => { |
| 131 | + setAlgorithm(event.target.value); |
| 132 | + }; |
| 133 | + |
| 134 | + |
| 135 | + return ( |
| 136 | + <div className='GraphTraversalVisualiser'> |
| 137 | + <div className="matrix-container"> |
| 138 | + {matrix.map((row, rowIndex) => ( |
| 139 | + <div key={rowIndex} className={"matrix-row matrix-row-"+rowIndex }> |
| 140 | + {row.map((cell, colIndex) => ( |
| 141 | + <div |
| 142 | + key={colIndex} |
| 143 | + className={`matrix-cell ${start.row === rowIndex && start.col === colIndex ? 'start' : ''} |
| 144 | + ${objectives.some(obj => obj.row === rowIndex && obj.col === colIndex) ? 'objective' : ''} |
| 145 | + ${obstacles.some(obs => obs.row === rowIndex && obs.col === colIndex) ? 'obstacle' : ''} col-index-${colIndex}`}> |
| 146 | + {cell} |
| 147 | + </div> |
| 148 | + ))} |
| 149 | + </div> |
| 150 | + ))} |
| 151 | + </div> |
| 152 | + <div className='legend'> |
| 153 | + <ul> |
| 154 | + <li className='start'>Start</li> |
| 155 | + <li className='objective'>Objective</li> |
| 156 | + <li className='path'>Path</li> |
| 157 | + <li className='obstacle white-text'>Obstacle</li> |
| 158 | + <li className='expanded'>Expanded Nodes</li> |
| 159 | + <li className='weight'>Weighted (ignored for bfs and dfs)</li> |
| 160 | + </ul> |
| 161 | + </div> |
| 162 | + <form onSubmit={handleSubmit}> |
| 163 | + <label htmlFor='algorithm' className='label'> |
| 164 | + Algorithms: |
| 165 | + <select |
| 166 | + className='menu' |
| 167 | + id='menu' |
| 168 | + value={algorithm} |
| 169 | + onChange={handleChange}> |
| 170 | + <option value='Reset'>Reset Array</option> |
| 171 | + <option value='BFS'>BFS</option> |
| 172 | + <option value='DFS'>DFS</option> |
| 173 | + <option value='UCS'>UCS</option> |
| 174 | + <option value='Astar'>Astar</option> |
| 175 | + </select> |
| 176 | + </label> |
| 177 | + <input |
| 178 | + style={{ color: 'black' }} |
| 179 | + className="btn" |
| 180 | + type="submit" |
| 181 | + value="Submit" |
| 182 | + disabled={button} |
| 183 | + /> |
| 184 | + </form> |
| 185 | + |
| 186 | + </div> |
| 187 | + ); |
| 188 | +}; |
| 189 | + |
| 190 | +export default MatrixVisualization; |
0 commit comments