Skip to content

Commit

Permalink
Update index (WIP - solved rangeerror)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreKavalerski committed Jul 4, 2020
1 parent 0874665 commit b6f4be0
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,40 @@ 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 initialState: State = [[1,2,3], [4,5,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);
if(finalNode){
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){
function run(goalState: State, frontier: NodeInfo[], expandedStates: State[]){
let actualNode = frontier.shift();
if(actualNode){
while(!areEqual(actualNode.state, goalState)){
const children = generateNodeList(actualNode, goalState);
for (let c of children){
if(!includes(c.state, expandedStates)){//Only add to frontier states not openned yet
frontier.push(c);
expandedStates.push(actualNode.state);
}
expandedStates.push(actualNode.state);
}

frontier.sort((a, b) => (a.evaluationFunctionValue.f < b.evaluationFunctionValue.f) ? -1 : ((a.evaluationFunctionValue.f === b.evaluationFunctionValue.f) ? ((a.evaluationFunctionValue.g > b.evaluationFunctionValue.g) ? -1 : 1): 1)); // Order frontier according to heuristic value of nodes

return run(goalState, frontier, expandedStates);

const aux = frontier.shift();
if(aux){
actualNode = aux;
}else{
throw new Error('Empty frontier');
}
}
}else{
throw new Error('Empty frontier');
return actualNode;
}
}

0 comments on commit b6f4be0

Please sign in to comment.