Skip to content

Commit

Permalink
Adicionando outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
PapauloMP committed Jan 4, 2023
1 parent 31fe0a9 commit d2a0957
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Headers/SearchTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SearchTree{
bool auxSearch (Node* node, int& id, int& rank);
std::vector<int> solveTempState(int temp);
void auxBacktracking(Node* node, std::vector<Node*>& searchResult, std::stack<Node*>& solutionPath, bool& solution);

void rankDot(std::vector<Node*>& searchResult);

};
#endif //TRABALHOIA_GRUPO9_SEARCHTREE_H
135 changes: 39 additions & 96 deletions MIs/SearchTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,22 @@ bool compareNodes(Node* x, Node* y){
return x->getRank() < y->getRank();
}

void SearchTree::rankDot(std::vector<Node*>& searchResult){
std::sort(searchResult.begin(), searchResult.end(), compareNodes);

outputFile << "\t{rank = same";
for(size_t i = 0; i < searchResult.size(); i++){
if (i != 0){
if(searchResult[i-1]->getRank() != searchResult[i]->getRank())
outputFile << "};" << std::endl << "\t{rank = same";
}
outputFile << "; "<< searchResult[i]->getState();
}


outputFile << "};" << std::endl << "}";
}

std::stack<Node*> SearchTree::breadthSearch() {
outputFile << "strict digraph buscaEmLargura {" << std::endl;
outputFile << "\trankdir=\"TB\";" << std::endl;
Expand Down Expand Up @@ -373,19 +389,7 @@ std::stack<Node*> SearchTree::breadthSearch() {
}

///organizando a árvore pelo rank
std::sort(searchResult.begin(), searchResult.end(), compareNodes);

outputFile << "\t{rank = same";
for(size_t i = 0; i < searchResult.size(); i++){
if (i != 0){
if(searchResult[i-1]->getRank() != searchResult[i]->getRank())
outputFile << "};" << std::endl << "\t{rank = same";
}
outputFile << "; "<< searchResult[i]->getState();
}


outputFile << "};" << std::endl << "}";
rankDot(searchResult);

return solutionPath;
}
Expand Down Expand Up @@ -434,18 +438,7 @@ std::stack<Node*> SearchTree::deepFirstSearch() {
}

///organizando a árvore pelo rank
std::sort(searchResult.begin(), searchResult.end(), compareNodes);

outputFile << "\t{rank = same";
for(size_t i = 0; i < searchResult.size(); i++){
if (i != 0){
if(searchResult[i-1]->getRank() != searchResult[i]->getRank())
outputFile << "};" << std::endl << "\t{rank = same";
}
outputFile << "; "<< searchResult[i]->getState();
}

outputFile << "};" << std::endl << "}";
rankDot(searchResult);

return solutionPath;
}
Expand All @@ -461,18 +454,7 @@ std::stack<Node*> SearchTree::backtrackingSearch() {
auxBacktracking(root, searchResult, solutionPath, solution);

///organizando a árvore pelo rank
std::sort(searchResult.begin(), searchResult.end(), compareNodes);

outputFile << "\t{rank = same";
for(size_t i = 0; i < searchResult.size(); i++){
if (i != 0){
if(searchResult[i-1]->getRank() != searchResult[i]->getRank())
outputFile << "};" << std::endl << "\t{rank = same";
}
outputFile << "; "<< searchResult[i]->getState();
}

outputFile << "};" << std::endl << "}";
rankDot(searchResult);

return solutionPath;

Expand All @@ -494,16 +476,16 @@ void SearchTree::auxBacktracking(Node* node, std::vector<Node*>& searchResult, s
}
return;
}
else
outputFile << "\t" << node->getState() << " -> ";
build(node);
for (Node* n : node->getNextNodes()){ //CRITERIO
//if(!solution) { // para o backtracking apos achar a primeira solução
else {
build(node);
for (Node *n: node->getNextNodes()) { //CRITERIO
if(!solution) { // para o backtracking apos achar a primeira solução
outputFile << "\t" << node->getState() << " -> ";
outputFile << n->getState() << std::endl;
auxBacktracking(n, searchResult, solutionPath, solution);
//}
}
}
}

}


Expand All @@ -523,12 +505,11 @@ std::stack<Node*> SearchTree::greedySearch() {
std::vector<Node*> searchResult;
std::priority_queue<Node*, std::vector<Node*>, compareGreedy> queue; //maxHeap
std::stack<Node*> solutionPath;
searchResult.push_back(root);
queue.push(root);

bool solution = false;
//bool solution = false;

while(!solution && !queue.empty()){
while(!queue.empty()){ //&& !solution
Node* node = queue.top();
queue.pop();
searchResult.push_back(node);
Expand All @@ -539,7 +520,7 @@ std::stack<Node*> SearchTree::greedySearch() {
}
if(node->getId() == 4032){
Node* x = node;
solution = true;
//solution = true;
while (x != nullptr) {
solutionPath.push(x);
if (x->getParent() != nullptr) {
Expand All @@ -555,19 +536,7 @@ std::stack<Node*> SearchTree::greedySearch() {
}

///organizando a árvore pelo rank
std::sort(searchResult.begin(), searchResult.end(), compareNodes);

outputFile << "\t{rank = same";
for(int i = 0; i < searchResult.size(); i++){
if (i != 0){
if(searchResult[i-1]->getRank() != searchResult[i]->getRank())
outputFile << "};" << std::endl << "\t{rank = same";
}
outputFile << "; "<< searchResult[i]->getState();
}


outputFile << "};" << std::endl << "}";
rankDot(searchResult);

return solutionPath;
}
Expand Down Expand Up @@ -598,15 +567,14 @@ std::stack<Node*> SearchTree::uniformSearch() {
std::vector<Node*> searchResult;
std::priority_queue<QueueNode, std::vector<QueueNode>, compareUniform> queue;
std::stack<Node*> solutionPath;
searchResult.push_back(root);

struct QueueNode queueRoot(root, 0);
queue.push(queueRoot);

bool solution = false;
//bool solution = false;

while(!solution && !queue.empty()){
QueueNode qnode = queue.top();
while(!queue.empty()){ // && !solution
QueueNode qnode = queue.top();
queue.pop();
searchResult.push_back(qnode.node);
build(qnode.node);
Expand All @@ -617,7 +585,7 @@ std::stack<Node*> SearchTree::uniformSearch() {
}
if(qnode.node->getId() == 4032){
Node* x = qnode.node;
solution = true;
//solution = true;
while (x != nullptr) {
solutionPath.push(x);
if (x->getParent() != nullptr) {
Expand All @@ -633,19 +601,7 @@ std::stack<Node*> SearchTree::uniformSearch() {
}

///organizando a árvore pelo rank
std::sort(searchResult.begin(), searchResult.end(), compareNodes);

outputFile << "\t{rank = same";
for(int i = 0; i < searchResult.size(); i++){
if (i != 0){
if(searchResult[i-1]->getRank() != searchResult[i]->getRank())
outputFile << "};" << std::endl << "\t{rank = same";
}
outputFile << "; "<< searchResult[i]->getState();
}


outputFile << "};" << std::endl << "}";
rankDot(searchResult);

return solutionPath;
}
Expand Down Expand Up @@ -682,14 +638,13 @@ std::stack<Node*> SearchTree::AStarSearch() {
std::vector<Node*> searchResult;
std::priority_queue<QueueNode, std::vector<QueueNode>, compareAStar> queue;
std::stack<Node*> solutionPath;
searchResult.push_back(root);

struct QueueNode queueRoot(root, 0);
queue.push(queueRoot);

bool solution = false;
//bool solution = false;

while(!solution && !queue.empty()){
while(!queue.empty()){ //&& !solution
QueueNode qnode = queue.top();
queue.pop();
searchResult.push_back(qnode.node);
Expand All @@ -701,7 +656,7 @@ std::stack<Node*> SearchTree::AStarSearch() {
}
if(qnode.node->getId() == 4032){
Node* x = qnode.node;
solution = true;
//solution = true;
while (x != nullptr) {
solutionPath.push(x);
if (x->getParent() != nullptr) {
Expand All @@ -717,19 +672,7 @@ std::stack<Node*> SearchTree::AStarSearch() {
}

///organizando a árvore pelo rank
std::sort(searchResult.begin(), searchResult.end(), compareNodes);

outputFile << "\t{rank = same";
for(int i = 0; i < searchResult.size(); i++){
if (i != 0){
if(searchResult[i-1]->getRank() != searchResult[i]->getRank())
outputFile << "};" << std::endl << "\t{rank = same";
}
outputFile << "; "<< searchResult[i]->getState();
}


outputFile << "};" << std::endl << "}";
rankDot(searchResult);

return solutionPath;
}
Binary file added Outputs/AStarFirstSolution.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Outputs/AStarSolutions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Outputs/GreedyFirstSolution.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Outputs/GreedySolutions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Outputs/UniformFirstSolution.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Outputs/UniformSolutions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d2a0957

Please sign in to comment.