1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <iostream>
#include <list>
#include <vector>
#include <string>
#include <algorithm>
#include "State.hpp"
#include "Node.hpp"
using namespace std;
bool compare_cost(const Node& n1, const Node& n2)
{
return(n1.getCost() <= n2.getCost());
}
bool compare_heuristic(const Node& n1, const Node& n2)
{
return(n1.getHeuristic() <= n2.getHeuristic());
}
bool compare_total_cost(const Node& n1, const Node& n2)
{
return((n1.getCost()+n1.getHeuristic()) <= (n2.getCost()+n2.getHeuristic()));
}
bool goalTest(const State& s1, const State& s2)
{
if(s1 == s2)
{
return true;
}
else
{
return false;
}
}
int main()
{
string beginning, finish;
cout << "Enter the starting city and the ending city: " << endl;
cout << "Starting: ";
getline(cin, beginning);// cin >> beginning;
cout << "Ending: ";
getline(cin, finish);// cin >> finish;
State initialState(beginning);// = new State(beginning);
State goalState(finish);// = new State(finish);
//list<Node>::iterator start;
char a;
cout << "which method of sort do you want? c for cost, h for heuristic: ";
a = getchar();
// Open nodes - a list of nodes currently available to expand
// TODO determine structure of open nodes list
vector<Node> openNodes;
// Initialize the problem
// Add a node representing the initial state
Node start_Node(initialState);
// openNodes.add(new Node(initialState))
openNodes.push_back(start_Node);
while (openNodes.size() > 0)
{
//start = openNodes.begin();
Node bestNode(openNodes[0]);
bestNode.print_states();
cout << endl;
//int last = bestNode.states.size() - 1;
bool goalReached = goalTest(bestNode.getLastState(), goalState);
if(goalReached)
{
cout << "Successful" << endl;
return 0;
}
else
{
openNodes.erase(openNodes.begin());
vector<Node> children = bestNode.expand();
int children_size = children.size() -1;
//add children to openNodes
for(int i = 0; i < children_size; i++)
{
openNodes.push_back(children[i]);
}
}
//sort openNodes by whatever criteria we're using
//openNodes.sort(compare);
if(a == 'C' || a == 'c')
{
sort (openNodes.begin(), openNodes.end(), compare_total_cost);
}
else
{
sort (openNodes.begin(), openNodes.end(), compare_heuristic);
}
}
}
|