Skip to content

Commit

Permalink
Update index (separate a star loop function)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreKavalerski committed Jul 7, 2020
1 parent bb3273d commit 04f0e77
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 33 deletions.
37 changes: 37 additions & 0 deletions src/functions/AStar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { State } from "../utils/state";
import { NodeInfo } from "../classes/Node";
import { areEqual, includes, isSolvable } from "./state";
import { generateNodeList } from "./node";
import Result from "../classes/Result";


function runAStarLoop(goalState: State, frontier: NodeInfo[], expandedStates: State[]){
let actualNode = frontier.shift();
let iterations = 0;
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
if(isSolvable(c.state)){ //Only add to frontier solvable states
frontier.push(c);
}
expandedStates.push(actualNode.state);
}
}

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

const aux = frontier.shift();
iterations += 1;
if(aux){
actualNode = aux;
}else{
throw new Error('Empty frontier');
}
}
return new Result(actualNode.evaluationFunctionValue.g, iterations, expandedStates.length, actualNode);
}
}

export { runAStarLoop as run }
48 changes: 15 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,31 @@
import { operations } from './utils/operations';
import { State } from "./utils/state";
import { NodeInfo } from "./classes/Node";
import { generateNodeList, generateNode } from './functions/node';
import { areEqual, includes, isSolvable } from './functions/state';
import { State, StateAsList } from "./utils/state";
import { generateNode } from './functions/node';
import { isSolvable, convertArrayInState, convertStateInArray } from './functions/state';
import { Frontier } from './utils/frontier';
import { run } from './functions/AStar';
import { getSolutionFromNode } from './functions/solution';

function AStar(initialState: State, finalState?: State){
function index(state: StateAsList, finalState?: State){
const initialState = convertArrayInState(state);
const goalState: State = finalState ? finalState : [[1,2,3],[4,5,6],[7, 8,0]];
if(isSolvable(initialState)){
const firstNode = generateNode(initialState, operations.none, goalState, 0);
let frontier: Frontier = [firstNode];
let expandedStates: State[] = [];

const finalNode = runAStarLoop(goalState, frontier, expandedStates);
return finalNode;
const result = run(goalState, frontier, expandedStates);
if(result){
result.solution = getSolutionFromNode(result.finalNode, []);
return result;
}else{
throw new Error('Unexpected error while processing search');
}
}else{
throw new Error('Initial state is not solvable');
}
}

function runAStarLoop(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
if(isSolvable(c.state)){ //Only add to frontier solvable states
frontier.push(c);
}
expandedStates.push(actualNode.state);
}
}

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

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

export {AStar as solvePuzzle}
export { index as solvePuzzle, convertArrayInState, convertStateInArray, isSolvable }

0 comments on commit 04f0e77

Please sign in to comment.