-
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.
Update index (separate a star loop function)
- Loading branch information
1 parent
bb3273d
commit 04f0e77
Showing
2 changed files
with
52 additions
and
33 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,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 } |
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 |
---|---|---|
@@ -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 } |