Skip to content

Commit

Permalink
show steps and auto re-run algo on change of params
Browse files Browse the repository at this point in the history
  • Loading branch information
npretto committed Jan 13, 2019
1 parent e70d39a commit 9063367
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 49 deletions.
92 changes: 51 additions & 41 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class App extends Component {

async componentDidMount() {
await this.generateNodes(8, 70)
this.generateLinks(100)
await this.generateLinks(100)

this.test()
}

generateNodes(size, dist) {
Expand All @@ -47,39 +49,36 @@ class App extends Component {
}

generateLinks(maxDist) {
// return new Promise((resolve, reject) => {
// this.setState(
// produce(this.state, state => {
// console.log(JSON.stringify(state))
const nodes = JSON.parse(JSON.stringify(this.state.nodes))
let links = newEntries()

const addLink = (i, l) => {
links.byId[i] = l
links.allIds.push(i)
}

let index = 0
for (let i = 0; i < nodes.allIds.length; i++) {
for (let j = i + 1; j < nodes.allIds.length; j++) {
const a = nodes.byId[i]
const b = nodes.byId[j]

if (dist(a, b) < maxDist) {
nodes.byId[i].neighbors.push(j)
nodes.byId[j].neighbors.push(i)
addLink(index, { from: i, to: j })

index++
return new Promise((resolve, reject) => {
// this.setState(
// produce(this.state, state => {
// console.log(JSON.stringify(state))
const nodes = JSON.parse(JSON.stringify(this.state.nodes)) //grrr
let links = newEntries()

const addLink = (i, l) => {
links.byId[i] = l
links.allIds.push(i)
}

let index = 0
for (let i = 0; i < nodes.allIds.length; i++) {
for (let j = i + 1; j < nodes.allIds.length; j++) {
const a = nodes.byId[i]
const b = nodes.byId[j]

if (dist(a, b) < maxDist) {
nodes.byId[i].neighbors.push(j)
nodes.byId[j].neighbors.push(i)
addLink(index, { from: i, to: j })

index++
}
}
}
}

this.setState({ nodes, links })
// }),
// () => resolve()
// )
// })
this.setState({ nodes, links }, () => resolve())
})
}

test = () => {
Expand All @@ -93,10 +92,13 @@ class App extends Component {

const pf = createPathFinder(queue, heuristic)

console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
console.log("pf with", queue, heuristic)

const findPath = pf(this.state.nodes, this.state.links)
this.setState({ pathSteps: [], step: 0, hasDonePath: false }, () => {
for (let pathStep of findPath(35, 0)) {
console.log(pathStep)
for (let pathStep of findPath(51, 0)) {
// console.log(pathStep)
this.setState(
produce(state => {
state.pathSteps.push(pathStep)
Expand All @@ -118,14 +120,14 @@ class App extends Component {
queue === "fifo"
? "Breadth-first search"
: heuristic === "omogeneus"
? "Djstra"
? "Dijkstra"
: "A*"

return (
<div className="mainContainer">
<div className="left">
<div className="top">
<button onClick={this.test}> TEST </button>{" "}
<button onClick={this.test}> TEST </button>
{hasDonePath && (
<input
style={{ width: "400px" }}
Expand All @@ -135,9 +137,12 @@ class App extends Component {
value={step}
step={1}
onInput={e => this.setState({ step: e.target.value })}
onChange={e => this.setState({ step: e.target.value })}
onChange={e => {
this.setState({ step: e.target.value })
}}
/>
)}
{hasDonePath && `${step}/${pathSteps.length}`}
</div>
<div className="canvasContainer">
<MyCanvas {...this.state} />
Expand All @@ -151,8 +156,10 @@ class App extends Component {
Queue:
<br />
<select
value={this.state.queue}
onChange={e => this.setState({ queue: e.target.value })}
value={queue}
onChange={e => {
this.setState({ queue: e.target.value }, () => this.test())
}}
>
<option value="fifo">First In First Out</option>
<option value="priority">Priority Queue</option>
Expand All @@ -164,18 +171,21 @@ class App extends Component {
Heuristic:
<br />
<select
value={this.state.heuristic}
disabled={queue === "fifo"}
value={heuristic}
onChange={e => {
console.log(e, e.target.value)
this.setState({ heuristic: e.target.value })
this.setState({ heuristic: e.target.value }, () => this.test())
}}
>
<option value="omogeneus">h(x) = 1</option>
<option value="euclidean">Eucledian Distance</option>
</select>
</label>
<br />
<p> This looks like : {algoName}</p>
<p>
This looks like : <strong>{algoName}</strong>
</p>
</div>
</div>
)
Expand Down
16 changes: 8 additions & 8 deletions src/algo/createPathFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const createPathFinder = (queue, heuristic = (from, to) => 1) => (
while (!frontier.isEmpty()) {
const current = frontier.getNext()

console.log("current", current)
// console.log("current", current)
// console.log("nodes.byId[current]", nodes.byId[current])
// console.log("cameFrom", cameFrom)

Expand All @@ -39,12 +39,12 @@ export const createPathFinder = (queue, heuristic = (from, to) => 1) => (
) {
//node not yet seen
costsSoFar[next] = cost
console.log(
"adding ",
next,
" with cost ",
cost + heuristic(nodes.byId[next], nodes.byId[goal])
)
// console.log(
// "adding ",
// next,
// " with cost ",
// cost + heuristic(nodes.byId[next], nodes.byId[goal])
// )
frontier.add({
id: next,
cost: cost + heuristic(nodes.byId[next], nodes.byId[goal])
Expand All @@ -58,7 +58,7 @@ export const createPathFinder = (queue, heuristic = (from, to) => 1) => (
const prev = cameFrom[node]
path.push(prev)
node = prev
console.log(prev, node)
// console.log(prev, node)
}
}
}
Expand Down

0 comments on commit 9063367

Please sign in to comment.