diff --git a/action.h b/action.h new file mode 100644 index 0000000..584bf7f --- /dev/null +++ b/action.h @@ -0,0 +1,23 @@ +#ifndef ACTION_H +#define ACTION_H + +#include + +#include "state.h" + +using namespace std; + +struct Action { + string act; + + Action(State boarding, State unboarding) { + act = "From " + stateToString(boarding) + " to " + stateToString(unboarding); + } + + Action(State begin) { + act = "Begin in " + stateToString(begin); + } +}; + + +#endif // ACTION_H diff --git a/main.cpp b/main.cpp index 1639dfa..70ff870 100644 --- a/main.cpp +++ b/main.cpp @@ -1,359 +1,11 @@ #include -using namespace std; - -enum State { arad = 0, zerind, oradea, sibiu, fagaras, rimnicu, pitesti, craiova, drobeta, mehadia, lugoj, - timisoara, bucharest, giurgiu, urziceni, hirsova, eforie, vaslui, iasi, neamt }; - -string stateToString(State c) { - switch(c) { - case arad: - return "Arad"; - case zerind: - return "Zerind"; - case oradea: - return "Oradeia"; - case sibiu: - return "Sibiu"; - case fagaras: - return "Fagaras"; - case rimnicu: - return "Rimnincu Vilcea"; - case pitesti: - return "Pitesti"; - case craiova: - return "Craiova"; - case drobeta: - return "Drobeta"; - case mehadia: - return "Mehadia"; - case lugoj: - return "Logoj"; - case timisoara: - return "Timisoara"; - case bucharest: - return "Bucharest"; - case giurgiu: - return "Giurgiu"; - case urziceni: - return "Urzicent"; - case hirsova: - return "Hisova"; - case eforie: - return "Eforie"; - case vaslui: - return "Vaslui"; - case iasi: - return "Iasi"; - case neamt: - return "Neamt"; - default: - return "no entry"; - } -} - -State stringToState(string s) { - if(s == "Arad" || s == "arad") - return arad; - if(s == "Zerind" || s == "zerind") - return zerind; - if(s == "Oradea" || s == "oradea") - return oradea; - if(s == "Sibiu" || s == "sibiu") - return sibiu; - if(s == "Fagaras" || s == "fagaras") - return fagaras; - if(s == "Rimnicu Vilcea" || s == "rimnicu") - return rimnicu; - if(s == "Pitesti" || s == "pitesti") - return pitesti; - if(s == "Cravoia" || s == "cravoia") - return craiova; - if(s == "Drobeta" || s == "drobeta") - return drobeta; - if(s == "Mehadia" || s == "mehadia") - return mehadia; - if(s == "Lugoj" || s == "lugoj") - return lugoj; - if(s == "Timisoara" || s == "timisoara") - return timisoara; - if(s == "Bucharest" || s == "bucharest") - return bucharest; - if(s == "Giurgiu" || s == "giurgiu") - return giurgiu; - if(s =="Urziceni" || s == "urziceni") - return urziceni; - if(s == "Hirsova" || s == "hirsova") - return hirsova; - if(s == "Eforie" || s == "eforie") - return eforie; - if(s == "Vaslui" || s == "vaslue") - return vaslui; - if(s == "Iasi" || s == "iasi") - return iasi; - if(s == "Neamt" || s == "neamt") - return neamt; - - cout << "entrada inválida." << endl; - exit(0); -} - -void printTowns(vector> v) { - for(pair p : v) - cout << "Cidade: " << stateToString(p.first) << endl << "Distância: " << p.second << endl; - return; -} - -void printTown(pair p) { - cout << "Cidade: " << stateToString(p.first) << endl << "Distância: " << p.second << endl; - return; -} - -struct Action { - string act; - - Action(State boarding, State unboarding) { - act = "From " + stateToString(boarding) + " to " + stateToString(unboarding); - } - - Action(State begin) { - act = "Begin in " + stateToString(begin); - } -}; - -struct Node { - Node *dad; - State s; - Action *a; - int costPath; - int hCostPath; - - Node(Node *_dad, State _s, Action *_a, int _cost, int _hCostPath) { - dad = _dad; - s = _s; - a = _a; - costPath = _cost; - hCostPath = _hCostPath; - } - - ~Node() { - delete a; - } - -}; - -struct Compar { - bool operator() (const Node *x, const Node *y) const { - return x->costPath > y->costPath; - } -}; - -struct HCompar { - bool operator() (const Node *x, const Node *y) const { - return x->hCostPath > y->hCostPath; - } -}; - -struct Model { - State boarding, unboarding; - map>> * cities; - unordered_map *sld; - - Model(State _boarding, State _unboarding, map>> * _cities, unordered_map *_sld) { - boarding = _boarding; - unboarding = _unboarding; - cities = _cities; - sld = _sld; - } - - ~Model() { - delete cities; - } - - bool wasVisited(vector *t, State s) { - for(State elem : (*t)) { - if(elem == s) - return true; - } - return false; - } - - Node * dfs() { - - stack edg; - vector expl; - - edg.push(new Node( nullptr, - boarding, - new Action(boarding), - 0, - 0)); // the heuristic campus isn't used in this search - - while(!edg.empty()) { - Node *current = edg.top(); - expl.push_back(current->s); - edg.pop(); - - for(pair each : cities->at(current->s)) { - Node *n = new Node(current, - each.first, - new Action(current->s, each.first), - each.second + current->costPath, - 0); // the heuristic campus isn't used in this search - - if(n->s == unboarding) { - return n; - } - else { - if(!wasVisited(&expl, n->s)) { - edg.push(n); - } - } - } - } - - return nullptr; - } - - Node * bfs() { - - queue edg; - vector expl; - - edg.push( new Node( nullptr, - boarding, - new Action(boarding), - 0, - 0)); // the heuristic campus isn't used in this search - - while(!edg.empty()) { - Node * current = edg.front(); - edg.pop(); - - expl.push_back(current->s); - - if(current->s == unboarding) - return current; - - for(pair each : cities->at(current->s)) { - if(!wasVisited(&expl, each.first)) { - edg.push( new Node( current, - each.first, - new Action(current->s, each.first), - each.second + current->costPath, - 0)); // the heuristic campus isn't used in this search - } - - } - } - - return nullptr; - } - - Node * ucs() { - priority_queue, Compar > edg; - vector expl; - - edg.push( new Node( nullptr, - boarding, - new Action(boarding), - 0, - 0)); - - while(!edg.empty()) { - Node * current = edg.top(); - edg.pop(); - - expl.push_back(current->s); - - if(current->s == unboarding) - return current; - - for(pair each : cities->at(current->s)) { - if(!wasVisited(&expl, each.first)) { - edg.push( new Node( current, - each.first, - new Action(current->s, each.first), - each.second + current->costPath, 0)); - } - - } - } - - return nullptr; - } - - Node * searchGreedy() { - - priority_queue< Node *, vector, HCompar> edg; - vector expl; - - edg.push( new Node( nullptr, - boarding, - new Action(boarding), - 0, - sld->at(boarding))); - - while(!edg.empty()) { - Node * current = edg.top(); - edg.pop(); - - expl.push_back(current->s); - - if(current->s == unboarding) - return current; - - for(pair each : cities->at(current->s)) { - if(!wasVisited(&expl, each.first)) { - edg.push( new Node( current, - each.first, - new Action(current->s, each.first), - each.second + current->costPath, - sld->at(current->s))); - } - - } - } - return nullptr; - } - - - Node * starSearch() { - - priority_queue< Node *, vector, HCompar> edg; - vector expl; - - edg.push( new Node( nullptr, - boarding, - new Action(boarding), - 0, - sld->at(boarding))); - - while(!edg.empty()) { - Node * current = edg.top(); - edg.pop(); - - expl.push_back(current->s); - - if(current->s == unboarding) - return current; - - for(pair each : cities->at(current->s)) { - if(!wasVisited(&expl, each.first)) { - edg.push( new Node( current, - each.first, - new Action(current->s, each.first), - each.second + current->costPath, - each.second + sld->at(current->s))); - } - - } - } - return nullptr; - } - -}; +#include "state.h" +#include "action.h" +#include "node.h" +#include "model.h" +using namespace std; void printPath(Node *n) { diff --git a/model.h b/model.h new file mode 100644 index 0000000..5c9833e --- /dev/null +++ b/model.h @@ -0,0 +1,220 @@ +#ifndef MODEL_H +#define MODEL_H + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "node.h" +#include "state.h" +#include "action.h" + +using namespace std; + +struct Model { + State boarding, unboarding; + map>> * cities; + unordered_map *sld; + + Model(State _boarding, State _unboarding, map>> * _cities, unordered_map *_sld) { + boarding = _boarding; + unboarding = _unboarding; + cities = _cities; + sld = _sld; + } + + ~Model() { + delete cities; + } + + bool wasVisited(vector *t, State s) { + for(State elem : (*t)) { + if(elem == s) + return true; + } + return false; + } + + Node * dfs() { + + stack edg; + vector expl; + + edg.push(new Node( nullptr, + boarding, + new Action(boarding), + 0, + 0)); // the heuristic campus isn't used in this search + + while(!edg.empty()) { + Node *current = edg.top(); + expl.push_back(current->s); + edg.pop(); + + for(pair each : cities->at(current->s)) { + Node *n = new Node(current, + each.first, + new Action(current->s, each.first), + each.second + current->costPath, + 0); // the heuristic campus isn't used in this search + + if(n->s == unboarding) { + return n; + } + else { + if(!wasVisited(&expl, n->s)) { + edg.push(n); + } + } + } + } + + return nullptr; + } + + Node * bfs() { + + queue edg; + vector expl; + + edg.push( new Node( nullptr, + boarding, + new Action(boarding), + 0, + 0)); // the heuristic campus isn't used in this search + + while(!edg.empty()) { + Node * current = edg.front(); + edg.pop(); + + expl.push_back(current->s); + + if(current->s == unboarding) + return current; + + for(pair each : cities->at(current->s)) { + if(!wasVisited(&expl, each.first)) { + edg.push( new Node( current, + each.first, + new Action(current->s, each.first), + each.second + current->costPath, + 0)); // the heuristic campus isn't used in this search + } + + } + } + + return nullptr; + } + + Node * ucs() { + priority_queue, Compar > edg; + vector expl; + + edg.push( new Node( nullptr, + boarding, + new Action(boarding), + 0, + 0)); + + while(!edg.empty()) { + Node * current = edg.top(); + edg.pop(); + + expl.push_back(current->s); + + if(current->s == unboarding) + return current; + + for(pair each : cities->at(current->s)) { + if(!wasVisited(&expl, each.first)) { + edg.push( new Node( current, + each.first, + new Action(current->s, each.first), + each.second + current->costPath, 0)); + } + + } + } + + return nullptr; + } + + Node * searchGreedy() { + + priority_queue< Node *, vector, HCompar> edg; + vector expl; + + edg.push( new Node( nullptr, + boarding, + new Action(boarding), + 0, + sld->at(boarding))); + + while(!edg.empty()) { + Node * current = edg.top(); + edg.pop(); + + expl.push_back(current->s); + + if(current->s == unboarding) + return current; + + for(pair each : cities->at(current->s)) { + if(!wasVisited(&expl, each.first)) { + edg.push( new Node( current, + each.first, + new Action(current->s, each.first), + each.second + current->costPath, + sld->at(current->s))); + } + + } + } + return nullptr; + } + + + Node * starSearch() { + + priority_queue< Node *, vector, HCompar> edg; + vector expl; + + edg.push( new Node( nullptr, + boarding, + new Action(boarding), + 0, + sld->at(boarding))); + + while(!edg.empty()) { + Node * current = edg.top(); + edg.pop(); + + expl.push_back(current->s); + + if(current->s == unboarding) + return current; + + for(pair each : cities->at(current->s)) { + if(!wasVisited(&expl, each.first)) { + edg.push( new Node( current, + each.first, + new Action(current->s, each.first), + each.second + current->costPath, + each.second + sld->at(current->s))); + } + + } + } + return nullptr; + } + +}; + + +#endif // MODEL_H diff --git a/node.h b/node.h new file mode 100644 index 0000000..ec43355 --- /dev/null +++ b/node.h @@ -0,0 +1,41 @@ +#ifndef NODE_H +#define NODE_H + +#include "state.h" +#include "action.h" + +struct Node { + Node *dad; + State s; + Action *a; + int costPath; + int hCostPath; + + Node(Node *_dad, State _s, Action *_a, int _cost, int _hCostPath) { + dad = _dad; + s = _s; + a = _a; + costPath = _cost; + hCostPath = _hCostPath; + } + + ~Node() { + delete a; + } + +}; + +struct Compar { + bool operator() (const Node *x, const Node *y) const { + return x->costPath > y->costPath; + } +}; + +struct HCompar { + bool operator() (const Node *x, const Node *y) const { + return x->hCostPath > y->hCostPath; + } +}; + + +#endif // NODE_H diff --git a/path.pro b/path.pro index e4b5a40..3e26c02 100644 --- a/path.pro +++ b/path.pro @@ -4,3 +4,9 @@ CONFIG -= app_bundle CONFIG -= qt SOURCES += main.cpp + +HEADERS += \ + state.h \ + action.h \ + node.h \ + model.h diff --git a/state.h b/state.h new file mode 100644 index 0000000..3e2a9d6 --- /dev/null +++ b/state.h @@ -0,0 +1,117 @@ +#ifndef STATE_H +#define STATE_H + +#include +#include + +using namespace std; + +enum State { arad = 0, zerind, oradea, sibiu, fagaras, rimnicu, pitesti, craiova, drobeta, mehadia, lugoj, + timisoara, bucharest, giurgiu, urziceni, hirsova, eforie, vaslui, iasi, neamt }; + +string stateToString(State c) { + switch(c) { + case arad: + return "Arad"; + case zerind: + return "Zerind"; + case oradea: + return "Oradeia"; + case sibiu: + return "Sibiu"; + case fagaras: + return "Fagaras"; + case rimnicu: + return "Rimnincu Vilcea"; + case pitesti: + return "Pitesti"; + case craiova: + return "Craiova"; + case drobeta: + return "Drobeta"; + case mehadia: + return "Mehadia"; + case lugoj: + return "Logoj"; + case timisoara: + return "Timisoara"; + case bucharest: + return "Bucharest"; + case giurgiu: + return "Giurgiu"; + case urziceni: + return "Urzicent"; + case hirsova: + return "Hisova"; + case eforie: + return "Eforie"; + case vaslui: + return "Vaslui"; + case iasi: + return "Iasi"; + case neamt: + return "Neamt"; + default: + return "no entry"; + } +} + +State stringToState(string s) { + if(s == "Arad" || s == "arad") + return arad; + if(s == "Zerind" || s == "zerind") + return zerind; + if(s == "Oradea" || s == "oradea") + return oradea; + if(s == "Sibiu" || s == "sibiu") + return sibiu; + if(s == "Fagaras" || s == "fagaras") + return fagaras; + if(s == "Rimnicu Vilcea" || s == "rimnicu") + return rimnicu; + if(s == "Pitesti" || s == "pitesti") + return pitesti; + if(s == "Cravoia" || s == "cravoia") + return craiova; + if(s == "Drobeta" || s == "drobeta") + return drobeta; + if(s == "Mehadia" || s == "mehadia") + return mehadia; + if(s == "Lugoj" || s == "lugoj") + return lugoj; + if(s == "Timisoara" || s == "timisoara") + return timisoara; + if(s == "Bucharest" || s == "bucharest") + return bucharest; + if(s == "Giurgiu" || s == "giurgiu") + return giurgiu; + if(s =="Urziceni" || s == "urziceni") + return urziceni; + if(s == "Hirsova" || s == "hirsova") + return hirsova; + if(s == "Eforie" || s == "eforie") + return eforie; + if(s == "Vaslui" || s == "vaslue") + return vaslui; + if(s == "Iasi" || s == "iasi") + return iasi; + if(s == "Neamt" || s == "neamt") + return neamt; + + cout << "entrada inválida." << endl; + exit(0); +} + +void printTowns(vector> v) { + for(pair p : v) + cout << "Cidade: " << stateToString(p.first) << endl << "Distância: " << p.second << endl; + return; +} + +void printTown(pair p) { + cout << "Cidade: " << stateToString(p.first) << endl << "Distância: " << p.second << endl; + return; +} + + +#endif // STATE_H