Skip to content

Commit

Permalink
Add run function to index (WIP - with rangeerror)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreKavalerski committed Jul 4, 2020
1 parent ad45c37 commit 4946b85
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/index.ts
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');
}
}

0 comments on commit 4946b85

Please sign in to comment.