-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add run function to index (WIP - with rangeerror)
- Loading branch information
1 parent
ad45c37
commit 4946b85
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { operations } from './utils/operations'; | ||
import { State } from "./utils/state"; | ||
import { NodeInfo } from "./classes/Node"; | ||
import { generateNodeList, generateNode, readNode } from './functions/node'; | ||
import { areEqual, includes } from './functions/state'; | ||
|
||
const initialState: State = [[4,5,8], [5,4,6], [7, null, 8]]; | ||
const finalState: State = [[1,2,3], [4,5,6], [7, 8, null]]; | ||
|
||
const firstNode = generateNode(initialState, operations.none, finalState, 0); | ||
|
||
let frontier: NodeInfo[] = [firstNode]; | ||
let expandedStates: State[] = []; | ||
|
||
|
||
|
||
const finalNode = run(finalState, frontier, expandedStates); | ||
readNode(finalNode); | ||
|
||
function run(goalState: State, frontier: NodeInfo[], expandedStates: State[]): NodeInfo{ | ||
const actualNode = frontier.shift(); | ||
if(actualNode){ | ||
if (areEqual(actualNode.state, goalState)){ | ||
return actualNode; | ||
} | ||
else{ | ||
if(!includes(actualNode.state, expandedStates)){ //Check if actual state wasn't already openned | ||
const children = generateNodeList(actualNode, goalState); | ||
for (let c of children){ | ||
frontier.push(c); | ||
} | ||
expandedStates.push(actualNode.state); | ||
} | ||
|
||
frontier.sort((a, b) => (a.evaluationFunctionValue.f < b.evaluationFunctionValue.f) ? -1 : 1); // Order frontier according to heuristic value of nodes | ||
|
||
return run(goalState, frontier, expandedStates); | ||
} | ||
}else{ | ||
throw new Error('Empty frontier'); | ||
} | ||
} |