Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iostream>
- #include <string>
- #include <vector>
- struct Node
- {
- int stepPathCost, numberOfChildren, heuristics, perviousPathCost;
- std::string name, path;
- std::vector<Node*> childNodes;
- };
- struct openSetAndclosedSetNode
- {
- std::string name, path;
- int stepPathCost;
- Node *referedNode;
- };
- Node* createNode(int stepPathCost, int numberOfChildren, int heuristics, std::string name, int perviousPathCost, std::string path, std::vector<Node*> childNodes)
- {
- Node *pnn = new Node();
- pnn->stepPathCost = stepPathCost;
- pnn->numberOfChildren = numberOfChildren;
- pnn->heuristics = heuristics;
- pnn->name = name;
- pnn->path = path;
- pnn->perviousPathCost = perviousPathCost;
- pnn->childNodes = childNodes;
- return pnn;
- }
- void searchForExistance(Node *pnn, std::string name, Node *&temp)
- {
- if (pnn->name == name) {
- temp = pnn;
- return;
- }
- if (pnn->childNodes.empty() == true) return;
- for (int i = 0; i<pnn->numberOfChildren; i++) {
- //std::cout<<pnn->name<<"\n";
- searchForExistance(pnn->childNodes.at(i), name, temp);
- if (pnn->childNodes.at(i + 1) == NULL) return;
- }
- }
- void removeNullValues(std::vector<Node*> &pnn) {
- for (int k = 0; k < pnn.size(); k++) {
- if (pnn[k] == NULL) pnn.erase(pnn.begin()+k);
- }
- }
- void InsertChild(Node* pnn, int stepPathCost, int numberOfChildren, int heuristics, std::string name, int perviousPathCost, std::string path, Node *root)
- {
- Node *temp, *temp2;
- for (int i = 0; i<pnn->numberOfChildren; i++)
- {
- temp2 = NULL;
- std::cin >> name;
- std::cin >> stepPathCost;
- path = pnn->path + name;
- perviousPathCost = pnn->stepPathCost + pnn->perviousPathCost;
- searchForExistance(root, name, temp2);
- if (temp2 == NULL) {
- std::cin >> numberOfChildren >> heuristics;
- std::vector<Node*> childNodes;
- childNodes.empty();
- temp = new Node();
- temp = createNode(stepPathCost, numberOfChildren, heuristics, name, perviousPathCost, path, childNodes);
- pnn->childNodes.insert(pnn->childNodes.begin() + i, temp);
- //std::cout<<"Child Added\n";
- pnn->childNodes.push_back(NULL);
- InsertChild(temp, stepPathCost, numberOfChildren, heuristics, name, perviousPathCost, path, root);
- }
- else {
- temp = new Node();
- temp = createNode(stepPathCost, temp2->numberOfChildren, temp2->heuristics, temp2->name, perviousPathCost, path, temp2->childNodes);
- pnn->childNodes.insert(pnn->childNodes.begin() + i, temp);
- //std::cout<<"Child Added\n";
- pnn->childNodes.push_back(NULL);
- }
- }
- }
- void sorting(std::vector<openSetAndclosedSetNode> &v) {
- for (int i = 1; i<v.size(); ++i) {
- for (int j = 0; j<v.size() - 1; ++j) {
- if (v[j].stepPathCost > v[i].stepPathCost) std::swap(v[j], v[i]);
- }
- }
- }
- void calculateAstar(Node *pnn, std::vector<openSetAndclosedSetNode> openSet, std::vector<std::string> closedSet, std::string theGoal, int &path, std::string &AStarPath)
- {
- openSetAndclosedSetNode x;
- bool isExist = false;
- auto i = openSet.begin();
- while (!openSet.empty()) {
- if (i->name == theGoal) {
- path = i->stepPathCost;
- AStarPath = i->path;
- return;
- }
- else {
- pnn = openSet.at(0).referedNode;
- removeNullValues(pnn->childNodes);
- i = openSet.begin();
- closedSet.push_back(i->name);
- openSet.erase(openSet.begin(), openSet.begin() + 1);
- for (int j = 0; j<pnn->numberOfChildren; j++) {
- x.stepPathCost = pnn->childNodes.at(j)->stepPathCost + pnn->childNodes.at(j)->perviousPathCost + pnn->childNodes.at(j)->heuristics;
- x.name = pnn->childNodes.at(j)->name;
- x.path = pnn->path + x.name;
- x.referedNode = pnn->childNodes.at(j);
- for (auto k = openSet.begin(); k != openSet.end(); k++) {
- if (k->name == x.name) {
- if (k->stepPathCost > x.stepPathCost) {
- for (int ff = 0; ff<x.referedNode->numberOfChildren; ff++) {
- x.referedNode->childNodes.at(ff)->perviousPathCost = x.stepPathCost;
- x.referedNode->childNodes.at(ff)->path = x.path + x.referedNode->childNodes.at(ff)->name;
- }
- openSet.erase(k);
- openSet.push_back(x);
- isExist = true;
- break;
- }
- else isExist = true;
- }
- }
- if (isExist == false) openSet.push_back(x);
- isExist = false;
- }
- sorting(openSet);
- i = openSet.begin();
- }
- }
- std::cout << "No Goal Achieved";
- }
- void UniformCostSearch(Node *pnn, std::vector<openSetAndclosedSetNode> openSet, std::vector<std::string> closedSet, std::string theGoal, int &path, std::string &UniformCostSearchPath)
- {
- openSetAndclosedSetNode x;
- bool isExist = false;
- auto i = openSet.begin();
- while (!openSet.empty()) {
- if (i->name == theGoal) {
- path = i->stepPathCost;
- UniformCostSearchPath = i->path;
- return;
- }
- else {
- pnn = openSet.at(0).referedNode;
- removeNullValues(pnn->childNodes);
- i = openSet.begin();
- closedSet.push_back(i->name);
- openSet.erase(openSet.begin(), openSet.begin() + 1);
- for (int j = 0; j<pnn->numberOfChildren; j++) {
- x.stepPathCost = pnn->childNodes.at(j)->stepPathCost + pnn->childNodes.at(j)->perviousPathCost;
- x.name = pnn->childNodes.at(j)->name;
- x.path = pnn->path + x.name;
- x.referedNode = pnn->childNodes.at(j);
- for (auto k = openSet.begin(); k != openSet.end(); k++) {
- if (k->name == x.name) {
- if (k->stepPathCost > x.stepPathCost) {
- for (int ff = 0; ff<x.referedNode->numberOfChildren; ff++) {
- x.referedNode->childNodes.at(ff)->perviousPathCost = x.stepPathCost;
- x.referedNode->childNodes.at(ff)->path = x.path + x.referedNode->childNodes.at(ff)->name;
- }
- openSet.erase(k);
- openSet.push_back(x);
- isExist = true;
- break;
- }
- else isExist = true;
- }
- }
- if (isExist == false) openSet.push_back(x);
- isExist = false;
- }
- sorting(openSet);
- i = openSet.begin();
- }
- }
- std::cout << "No Goal Achieved";
- }
- void GreedyFirstSearch(Node *pnn, std::vector<openSetAndclosedSetNode> openSet, std::vector<std::string> closedSet, std::string theGoal, int &path, std::string &GreedyFirstSearchPath)
- {
- openSetAndclosedSetNode x;
- bool isExist = false;
- auto i = openSet.begin();
- while (!openSet.empty()) {
- if (i->name == theGoal) {
- path = i->stepPathCost;
- GreedyFirstSearchPath = i->path;
- return;
- }
- else {
- pnn = openSet.at(0).referedNode;
- removeNullValues(pnn->childNodes);
- i = openSet.begin();
- closedSet.push_back(i->name);
- openSet.erase(openSet.begin(), openSet.begin() + 1);
- for (int j = 0; j<pnn->numberOfChildren; j++) {
- x.stepPathCost = pnn->childNodes.at(j)->heuristics;
- x.name = pnn->childNodes.at(j)->name;
- x.path = pnn->path + x.name;
- x.referedNode = pnn->childNodes.at(j);
- for (auto k = openSet.begin(); k != openSet.end(); k++) {
- if (k->name == x.name) {
- if (k->stepPathCost > x.stepPathCost) {
- for (int ff = 0; ff<x.referedNode->numberOfChildren; ff++) {
- x.referedNode->childNodes.at(ff)->perviousPathCost = x.stepPathCost;
- x.referedNode->childNodes.at(ff)->path = x.path + x.referedNode->childNodes.at(ff)->name;
- }
- openSet.erase(k);
- openSet.push_back(x);
- isExist = true;
- break;
- }
- else isExist = true;
- }
- }
- if (isExist == false) openSet.push_back(x);
- isExist = false;
- }
- sorting(openSet);
- i = openSet.begin();
- }
- }
- std::cout << "No Goal Achieved";
- }
- std::string DepthFirstSearch(Node *pnn, std::vector<openSetAndclosedSetNode> openSet, std::vector<std::string> closedSet, std::string theGoal)
- {
- openSetAndclosedSetNode x;
- auto i = openSet.begin();
- bool isExist = false;
- while (!openSet.empty()) {
- if (i->name == theGoal) {
- return i->path;
- }
- else {
- pnn = openSet.at(0).referedNode;
- removeNullValues(pnn->childNodes);
- i = openSet.begin();
- closedSet.push_back(i->name);
- openSet.erase(openSet.begin(), openSet.begin() + 1);
- for (int j = pnn->numberOfChildren - 1; j >= 0; j--) {
- x.name = pnn->childNodes.at(j)->name;
- x.path = pnn->path + x.name;
- x.referedNode = pnn->childNodes.at(j);
- for (auto k = openSet.begin(); k!=openSet.end(); k++) {
- if (k->name == x.name)isExist = true;
- }
- if (isExist == false)openSet.insert(openSet.begin(), x);
- isExist = false;
- }
- i = openSet.begin();
- }
- }
- std::cout << "No Goal Achieved";
- return "";
- }
- std::string BredthFirstSearch(Node *pnn, std::vector<openSetAndclosedSetNode> openSet, std::vector<std::string> closedSet, std::string theGoal)
- {
- openSetAndclosedSetNode x;
- auto i = openSet.begin();
- bool isExist = false;
- while (!openSet.empty()) {
- if (i->name == theGoal) {
- return i->path;
- }
- else {
- pnn = openSet.at(0).referedNode;
- removeNullValues(pnn->childNodes);
- i = openSet.begin();
- closedSet.push_back(i->name);
- openSet.erase(openSet.begin(), openSet.begin() + 1);
- for (int j = 0; j<pnn->numberOfChildren; j++) {
- x.name = pnn->childNodes.at(j)->name;
- x.path = pnn->path + x.name;
- x.referedNode = pnn->childNodes.at(j);
- for (auto k = openSet.begin(); k!=openSet.end(); k++) {
- if (k->name == x.name)isExist = true;
- }
- if (isExist == false) openSet.push_back(x);
- isExist = false;
- }
- i = openSet.begin();
- }
- }
- std::cout << "No Goal Achieved";
- return "";
- }
- int main()
- {
- Node *root = new Node();
- std::string name, theGoal, path = "";
- int numberOfChildren, heuristics, perviousPathCost = 0;
- std::vector<openSetAndclosedSetNode> openSet;
- std::vector<std::string> closedSet;
- std::cout << "Construct the root as follow\nName, NumberOfChildren, Heuristics\n";
- std::cin >> name;
- std::cin >> numberOfChildren >> heuristics;
- std::vector<Node*> childNodes;
- childNodes.empty();
- std::cout << "Construct the Tree as follow\nName, PathCost, NumberOfChildren, Heuristics\nIf the Node already existed\nName, PathCost\n";
- root = createNode(0, numberOfChildren, heuristics, name, perviousPathCost, path, childNodes);
- InsertChild(root, 0, numberOfChildren, heuristics, name, perviousPathCost, path, root);
- std::cout << "Enter the Goal Name\n";
- std::cin >> theGoal;
- openSetAndclosedSetNode x;
- int pathCost = 0;
- std::string searchPaths;
- x.stepPathCost = heuristics;
- x.name = name;
- x.referedNode = root;
- openSet.push_back(x);
- /* Depth First Search */
- std::string DepthFirstSearchPath = DepthFirstSearch(root, openSet, closedSet, theGoal);
- std::cout << "(DepthFirstSearch)" << root->name << DepthFirstSearchPath << "\n";
- /* Bredth First Search */
- std::string BredthFirstSearchPath = BredthFirstSearch(root, openSet, closedSet, theGoal);
- std::cout << "(BredthFirstSearch)" << root->name << BredthFirstSearchPath << "\n";
- /* A* */
- calculateAstar(root, openSet, closedSet, theGoal, pathCost, searchPaths);
- std::reverse(searchPaths.begin(), searchPaths.end());
- std::cout << "(A*)" << searchPaths << root->name << "=" << pathCost << "\n";
- /* Uniform search cost */
- UniformCostSearch(root, openSet, closedSet, theGoal, pathCost, searchPaths);
- std::reverse(searchPaths.begin(), searchPaths.end());
- std::cout << "(UniformSearch)" << searchPaths << root->name << "=" << pathCost << "\n";
- ///* Greedy First Search */
- GreedyFirstSearch(root, openSet, closedSet, theGoal, pathCost, searchPaths);
- std::reverse(searchPaths.begin(), searchPaths.end());
- std::cout << "(Greedy)" << searchPaths << root->name << "=" << pathCost << "\n";
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement