Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.93 KB | None | 0 0
  1. /**
  2. * F29AI - Artificial Intelligence and Intelligent Agents
  3. *
  4. * Coursework - Part I - A* Search
  5. *
  6. * This is the main part of the program, which extends
  7. * the starter code given to us as part of the
  8. * coursework assignment.
  9. *
  10. * @author Ryan Shah
  11. * @author Francisco Javier Chiyah Garcia
  12. */
  13.  
  14. #include "astar.cpp"
  15.  
  16. #include <iostream>
  17. #include <stdio.h>
  18. #include <string>
  19.  
  20. /**
  21. * State definitions based on energy cost
  22. */
  23. const double STATE_EMPTY = 1.0;
  24. const double STATE_TRAP = 5.0;
  25.  
  26. struct Robot {
  27.     int x;
  28.     int y;
  29.  
  30.     Robot(int x, int y) {
  31.         x = x;
  32.         y = y;
  33.     }
  34. };
  35.  
  36. struct Graph {
  37.  
  38.     struct State {
  39.         double energy_cost;
  40.         bool accessible;
  41.         int x;
  42.         int y;
  43.  
  44.         State() {
  45.         }
  46.  
  47.         State(int xPos, int yPos) {
  48.             energy_cost = STATE_EMPTY;
  49.             accessible = true;
  50.             x = xPos;
  51.             y = yPos;
  52.         }
  53.  
  54.         State(double cost, bool access, int xPos, int yPos) {
  55.             energy_cost = cost;
  56.             accessible = access;
  57.             x = xPos;
  58.             y = yPos;
  59.         }
  60.  
  61.         double getEnergyCost() {
  62.             if (accessible) {
  63.                 return energy_cost;
  64.             }
  65.             return 100;
  66.         }
  67.  
  68.         bool operator==(const State& s) const {
  69.             return ((x == s.x) && (y == s.y));
  70.         }
  71.  
  72.         bool operator!=(const State& s) const {
  73.             return !((x == s.x) && (y == s.y));
  74.         }
  75.  
  76.         bool operator<(const State& s) const {
  77.             if(y < s.y)
  78.                 return true;
  79.             else if(y > s.y)
  80.                 return false;
  81.             else {
  82.                 if(x < s.x)
  83.                     return true;
  84.                 return false;
  85.             }
  86.         }
  87.     };
  88.  
  89.     int rows, cols;
  90.     vector<vector<State>> gridMatrix;
  91.  
  92.     Graph(int r, int c) {
  93.         rows = r;
  94.         cols = c;
  95.  
  96.         for(int i = 0; i < rows; i++) {
  97.             vector<State> temp;
  98.             for(int j = 0; j < cols; j++) {
  99.                 temp.push_back(State(i, j));
  100.             }
  101.             gridMatrix.push_back(temp);
  102.         }
  103.  
  104.         //traps
  105.         gridMatrix[2][3].energy_cost = STATE_TRAP;
  106.         //walls
  107.         gridMatrix[1][3].accessible = false;
  108.         gridMatrix[1][4].accessible = false;
  109.         gridMatrix[2][0].accessible = false;
  110.         gridMatrix[4][2].accessible = false;
  111.         gridMatrix[3][3].accessible = false;
  112.         gridMatrix[4][3].accessible = false;
  113.         gridMatrix[3][5].accessible = false;
  114.         gridMatrix[3][7].accessible = false;
  115.  
  116.     }
  117.  
  118.     /**
  119.     * Returns a vector containing the
  120.     * neighbouring States for a given
  121.     * State in order: N - E - S - W
  122.     *
  123.     * @param s A State
  124.     * @return A vector containing all neighbouring States for the given State
  125.     */
  126.     vector<State> neighbours(State s) {
  127.         vector<State> neighbour_vec;
  128.  
  129.         // Get each neighbour by coordinates
  130.         if (s.x - 1 >= 0) { // West
  131.             neighbour_vec.push_back(gridMatrix[s.x - 1][s.y]);
  132.         }
  133.         if (s.y + 1 < rows) { // South
  134.             neighbour_vec.push_back(gridMatrix[s.x][s.y + 1]);
  135.         }
  136.         if (s.x + 1 < cols) { // East
  137.             neighbour_vec.push_back(gridMatrix[s.x + 1][s.y]);
  138.         }
  139.         if (s.y - 1 >= 0) { // North
  140.             neighbour_vec.push_back(gridMatrix[s.x][s.y - 1]);
  141.         }
  142.  
  143.         return(neighbour_vec);
  144.     }
  145.  
  146.     /**
  147.     * Returns the cost of moving from one
  148.     * State to another.
  149.     *
  150.     * @param  a The initial State
  151.     * @param  b The destination State
  152.     * @return The cost of moving from the initial to the destination State
  153.     */
  154.     double cost(State a, State b) {
  155.         // This will not work if one of the states is not in the matrix
  156.         return b.energy_cost;
  157.     }
  158. };
  159.  
  160. typedef typename Graph::State State;
  161.  
  162. /**
  163. * Returns the heuristic value of transitioning
  164. * from State a to State b using the Best-First
  165. * heuristic search function.
  166. *
  167. * @param  a The initial State
  168. * @param  b The destination State
  169. * @return The heuristic value of transitioning from the initial to the destination State
  170. */
  171. double heuristic_best_first(State a, State b) {
  172.     State curState;
  173.  
  174.     /*if (a == b)
  175.     return(0);
  176.     else
  177.     curState = a;
  178.  
  179.     return(-1);*/
  180.  
  181.     return(-1);
  182. }
  183.  
  184. /**
  185.  * Returns the heuristic value of transitioning
  186.  * from State a to State b using the Manhattan Distance
  187.  * heuristic function.
  188.  *
  189.  * @param  a The initial State
  190.  * @param  b The destination State
  191.  * @return The heuristic value of transitioning from the initial to the destination State
  192.  */
  193. double heuristic_manhattan(State a, State b) {
  194.     return abs((a.x - b.x) + (a.y - b.y));
  195.  
  196.     /*
  197.         It should be obvious that if there are no obstacles, then moving |x - x_goal|
  198.         steps right or left, then |y - y_goal| steps up or down (or y, then x) cannot be
  199.         longer than the actual shortest path to the goal with obstacles, therefore the
  200.         heuristic is admissible.
  201.      */
  202. }
  203.  
  204. void display_matrix(Graph g) {
  205.     for (int y = 0; y < g.rows; y++) {
  206.         for (int x = 0; x < g.cols; x++) {
  207.             State temp = g.gridMatrix[y][x];
  208.             if(temp.accessible)
  209.                 std::cout << "[" << temp.energy_cost << "]";
  210.             else
  211.                 std::cout << "[" << "W" << "]";
  212.         }
  213.         std::cout << "\n";
  214.     }
  215. }
  216.  
  217. void display_vector(vector<State> states) {
  218.     std::vector<State>::iterator it;
  219.  
  220.     for (it = states.begin(); it < states.end(); it++) {
  221.         std::cout << "[" << (*it).x << ", " << (*it).y << "] ";
  222.     }
  223. }
  224.  
  225. /**
  226. * Display the solution path.
  227. *
  228. * @param g The graph of the solution
  229. * @param path The solution path vector
  230. */
  231. void display_solution(Graph g, vector<State> path) {
  232.     vector<vector<Graph::State> > matrix = g.gridMatrix;
  233.     vector<vector<std::string> > str_matrix;
  234.  
  235.     for(int y = 0; y < g.rows; y++) {
  236.         vector<std::string> row;
  237.  
  238.         for(int x = 0; x < g.cols; x++) {
  239.             Graph::State temp = matrix[y][x];
  240.             if(temp.accessible)
  241.                 row.push_back((temp.energy_cost == STATE_EMPTY ? "1" : "T"));
  242.             else
  243.                 row.push_back("W");
  244.         }
  245.         str_matrix.push_back(row);
  246.     }
  247.  
  248.     for(int i = 0; i < path.size(); i++) {
  249.        Graph::State temp = path[i];
  250.         if(i == 0)
  251.             str_matrix[temp.y][temp.x] = "S";
  252.         else if(i == path.size() - 1)
  253.             str_matrix[temp.y][temp.x] = "G";
  254.         else
  255.             str_matrix[temp.y][temp.x] = "->";
  256.     }
  257.  
  258.     for(int y = 0; y < g.rows; y++) {
  259.         for(int x = 0; x < g.cols; x++) {
  260.             std::cout << "[" << str_matrix[y][x] << "]";
  261.         }
  262.         std::cout << "\n";
  263.     }
  264. }
  265.  
  266. int main() {
  267.     Graph g = Graph(5, 8);
  268.     display_matrix(g);
  269.  
  270.     State initial_state = g.gridMatrix[0][0];
  271.     State destination_state = g.gridMatrix[2][0];
  272.  
  273.     //display_vector(g.neighbours(State(1,1)));
  274.  
  275.     std::cout << "\nPath:\n";
  276.     //Call search
  277.     vector<State> path;
  278.     a_star_search(g, initial_state, destination_state, heuristic_manhattan, path);
  279.  
  280.     display_vector(path);
  281.     std::cout << "\n\nSolution:\n";
  282.     //Display result
  283.     display_solution(g, path);
  284.  
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement