Professional Documents
Culture Documents
#include <vector>
#include <stack>
#include <iostream>
#include <stdexcept>
#include "rectangularvectors.h"
using Random = util::Random;
using Stack = util::Stack;
//heuristic function f(h) is lowest possible f (n) from current state
class Eight_Puzzle
{
{2, 0, 6},
{1, 4, 3},
{8, 7, 5}
//nice backtrack not solved in local maxima
{2, 6, 3},
{1, 0, 4},
{8, 7, 5}
*/
/* one no backtrack local maxima test input example
{1, 4, 0},
{8, 3, 2},
{7, 6, 5}
*/
/* one impossible local maxima test input example
{1, 2, 0},
{8, 3, 4},
{7, 6, 5}
int min_fn = 0;
Node *min_fn_node;
//initializations
virtual ~Eight_Puzzle()
{
delete min_fn_node;
delete random;
}
private:
Node *getLowestPossible_fn_node();
//----------------------------
//return number of misplaced tiles for left state
Node *get_fn_left();
//takes parent removed, sorted node array and add states to stack in high to
low order
void addToStackState(std::vector<Node*> &nodeArray);
//find out the new empty tile position for current state
void locateEmptyTilePosition();
return newVector;
}
};
#include "snippet.h"
void Eight_Puzzle::initializations()
{
void Eight_Puzzle::hill_climbing_search()
{
while (true)
{
std::wcout << L">========================================<" <<
std::endl;
std::wcout << L"cost/steps: " << (++stepCounter) << std::endl;
std::wcout << L"-------------" << std::endl;
//Priority.preState = game_board;//change pre state
Node *lowestPossible_fn_node = getLowestPossible_fn_node();
addToStackState(Priority::neighbors_nodeArray); //add neighbors to
stack in high to low order fn
//backtracking
for (int i = 0; i < Priority::neighbors_nodeArray->length; i++)
{
if (stack_state != nullptr)
{
std::wcout << L"pop " << (i + 1) << std::endl;
stack_state.pop();
}
else
{
std::wcout << L"empty stack inside loop" <<
std::endl;
}
}
if (stack_state != nullptr)
{
Node *gameNode = stack_state.pop();
game_board = gameNode->state; //update game board
Priority->preState = gameNode->parent; //update prestate
locateEmptyTilePosition(); //locate empty tile for updated
state
if (lowestPossible_fn_node->fn == 0)
{ //no misplaced found
std::wcout << L"-------------------------" << std::endl;
std::wcout << L"8-Puzzle has been solved!" << std::endl;
std::wcout << L"-------------------------" << std::endl;
std::wcout << L"Total cost/steps to reach the goal: " <<
stepCounter << std::endl;
std::wcout << L"-------------------------------------" <<
std::endl;
break;
}
if (stack_state != nullptr)
{
Node *gameNode = stack_state.pop();
game_board = gameNode->state; //update game board
Priority->preState = gameNode->parent; //update
prestate
locateEmptyTilePosition(); //locate empty tile for
updated state
}
else
{
std::wcout << L"---stuck in local maxima---" << std::endl;
std::wcout << L"getting higher, not possible" << std::endl;
//break;
//backtracking
for (int i = 0; i < Priority::neighbors_nodeArray->length;
i++)
{
if (stack_state != nullptr)
{
//System.out.println("pop " + (i + 1));
stack_state.pop();
}
else
{
std::wcout << L"empty stack inside loop" <<
std::endl;
}
}
if (stack_state != nullptr)
{
Node *Eight_Puzzle::getLowestPossible_fn_node()
{
}
else if (emptyTile_row == 0 && emptyTile_col == 1)
{ //0,1 position is empty tile
//System.out.println("Empty 0,1");
std::vector<Node*> fn_array = {get_fn_left(), get_fn_down(),
get_fn_right()};
Node *lowest_fn_node = Priority::sort(fn_array);
return lowest_fn_node;