Skip to content

Commit

Permalink
Refactor heuristic value (add consistence to h value)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreKavalerski committed Jul 5, 2020
1 parent 59f6052 commit 9a980a1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
8 changes: 3 additions & 5 deletions src/classes/HeuristicValue.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
export default class HeuristicValue {
g: number;
h: number;
f: number;

constructor(g: number, h: number){
constructor(g: number, h: number, f: number){
this.g = g;
this.h = h;
}

get f(){
return this.g + this.h;
this.f = f;
}
}
7 changes: 6 additions & 1 deletion src/functions/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ function generateAndTest(op: operations, node: NodeInfo, goalState: State, gValu

function generateNode(state: State, op: operations, goalState: State, gValue: number, previousNode?: NodeInfo): NodeInfo{
const hValue = calcHValue(state, goalState);
const heuristicValue = new HeuristicValue(gValue, hValue);
let heuristicValue = new HeuristicValue(gValue, hValue, (gValue + hValue));
if(previousNode){
if(heuristicValue.h > previousNode.evaluationFunctionValue.h){
heuristicValue.f = previousNode.evaluationFunctionValue.f; //Add consistence to h value
}
}
return new NodeInfo(heuristicValue, op, state, previousNode);
}

Expand Down
28 changes: 14 additions & 14 deletions tests/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Node Smoke Tests', () => { // the tests container

it('should return an Array when call generateNodeList function', () => {
const state: State = [[4,5,8], [null,1,6], [7, 2, 3]];
const heuristicValue = new HeuristicValue(0, 14);
const heuristicValue = new HeuristicValue(0, 14, 14);
const node: NodeInfo = new NodeInfo(heuristicValue, operations.none, state);

expect(generateNodeList(node, state)).to.be.an('Array');
Expand All @@ -35,7 +35,7 @@ describe('Testing returns of generateNode function', () => {
it('should return this NodeInfo obj when call generateNode function', () => {
const state: State = [[4,5,8], [null,1,6], [7, 2, 3]];
const finalState: State = [[1,2,3], [4,5,6], [7, 8, null]];
const heuristicValue = new HeuristicValue(0, 14);
const heuristicValue = new HeuristicValue(0, 14, 14);

const expectedNode = new NodeInfo(heuristicValue, operations.none, state);
const gValue = 0;
Expand All @@ -45,7 +45,7 @@ describe('Testing returns of generateNode function', () => {
it('should return this NodeInfo obj when call generateNode function', () => {
const state: State = [[4,5,8], [1,null,6], [7, 2, 3]];
const finalState: State = [[1,2,3], [4,5,6], [7, 8, null]];
const heuristicValue = new HeuristicValue(1, 12);
const heuristicValue = new HeuristicValue(1, 12, 13);

const expectedNode = new NodeInfo(heuristicValue, operations.left, state);
const gValue = 1;
Expand All @@ -55,10 +55,10 @@ describe('Testing returns of generateNode function', () => {
it('should return this NodeInfo obj when call generateNode function', () => {
const state: State = [[4,5,8], [1,2,6], [7, 3, null]];
const finalState: State = [[1,2,3], [4,5,6], [7, 8, null]];
const heuristicValuePrevious = new HeuristicValue(0, 0);
const heuristicValuePrevious = new HeuristicValue(0, 0, 0);

const mockedPreviousNode = new NodeInfo(heuristicValuePrevious, operations.none, state);
const heuristicValueExpected = new HeuristicValue(3, 10);
const heuristicValueExpected = new HeuristicValue(3, 10, 0);
const expectedNode = new NodeInfo(heuristicValueExpected, operations.up, state, mockedPreviousNode);
const gValue = 3;
expect(generateNode(state, operations.up, finalState, gValue, mockedPreviousNode)).to.eql(expectedNode);
Expand All @@ -70,19 +70,19 @@ describe('Testing returns of generateNodeList function', () => {
it('should return this NodeInfo array when call generateNodeList function', () => {
const state: State = [[4,5,8], [null,1,6], [7, 2, 3]];
const goalState: State = [[1,2,3], [4,5,6], [7, 8, null]];
const heuristicValueRoot = new HeuristicValue(0, 14);
const heuristicValueRoot = new HeuristicValue(0, 14, 14);
const rootNode = new NodeInfo(heuristicValueRoot, operations.none, state);

const stateChild1: State = [[null,5,8], [4,1,6], [7, 2, 3]];
const heuristicValue1 = new HeuristicValue(1, 14);
const heuristicValue1 = new HeuristicValue(1, 14, 15);
const expectedChild1 = new NodeInfo(heuristicValue1, operations.up, stateChild1, rootNode);

const stateChild2: State = [[4,5,8], [1,null,6], [7, 2, 3]];
const heuristicValue2 = new HeuristicValue(1, 12);
const heuristicValue2 = new HeuristicValue(1, 12, 13);
const expectedChild2 = new NodeInfo(heuristicValue2, operations.right, stateChild2, rootNode);

const stateChild3: State = [[4,5,8], [7,1,6], [null, 2, 3]];
const heuristicValue3 = new HeuristicValue(1, 14);
const heuristicValue3 = new HeuristicValue(1, 14, 15);
const expectedChild3 = new NodeInfo(heuristicValue3, operations.down, stateChild3, rootNode);

const childrenList = [expectedChild1, expectedChild2 , expectedChild3];
Expand All @@ -93,23 +93,23 @@ describe('Testing returns of generateNodeList function', () => {
it('should return this NodeInfo array when call generateNodeList function', () => {
const state: State = [[4,5,8], [1,null,6], [7, 2, 3]];
const goalState: State = [[1,2,3], [4,5,6], [7, 8, null]];
const heuristicValueRoot = new HeuristicValue(1, 12);
const heuristicValueRoot = new HeuristicValue(1, 12, 13);
const rootNode = new NodeInfo(heuristicValueRoot, operations.right, state);

const stateChild1: State = [[4,null,8], [1,5,6], [7, 2, 3]];
const heuristicValue1 = new HeuristicValue(2, 12);
const heuristicValue1 = new HeuristicValue(2, 12, 14);
const expectedChild1 = new NodeInfo(heuristicValue1, operations.up, stateChild1, rootNode);

const stateChild2: State = [[4,5,8], [1,6,null], [7, 2, 3]];
const heuristicValue2 = new HeuristicValue(2, 12);
const heuristicValue2 = new HeuristicValue(2, 12, 14);
const expectedChild2 = new NodeInfo(heuristicValue2, operations.right, stateChild2, rootNode);

const stateChild3: State = [[4,5,8], [1,2,6], [7, null, 3]];
const heuristicValue3 = new HeuristicValue(2, 10);
const heuristicValue3 = new HeuristicValue(2, 10, 12);
const expectedChild3 = new NodeInfo(heuristicValue3, operations.down, stateChild3, rootNode);

const stateChild4: State = [[4,5,8], [null,1,6], [7, 2, 3]];
const heuristicValue4 = new HeuristicValue(2, 14);
const heuristicValue4 = new HeuristicValue(2, 14, 13);
const expectedChild4 = new NodeInfo(heuristicValue4, operations.left, stateChild4, rootNode);

const childrenList = [expectedChild1, expectedChild2, expectedChild3, expectedChild4];
Expand Down

0 comments on commit 9a980a1

Please sign in to comment.