Pages

Saturday, December 7, 2013

Dijkstra's algorithm : Grid Search implementation Arduino


This tutorial contains an implementation of Dijkstra's algorithm using an Arduino to search for the shortest path in a grid. Here I have used 4*4 grid to demonstrate the implementation


Step 1 : Write the grid nodes to the EEPROM of the Arduino


Sample grid


1     1     1     0

1     0     1     1

1     1     1     1

1     0     1     1

Note that is a square grid matrix where 1 indicates accessible nodes and 0 indicates nodes to be avoided. I wrote this to Arduino EEPROM by a slight modification to the EEPROMWrite in Arduino example set.

Code:


#include <EEPROM.h>
void setup()
{
 
   EEPROM.write(0,1);
   EEPROM.write(1,1);
   EEPROM.write(2,1);
   EEPROM.write(3,0);
   EEPROM.write(4,1);
   EEPROM.write(5,0);
   EEPROM.write(6,1);
   EEPROM.write(7,1);
   EEPROM.write(8,1);
  EEPROM.write(9,1);
  EEPROM.write(10,1);
  EEPROM.write(11,1);
  EEPROM.write(12,1);
 EEPROM.write(13,0);
 EEPROM.write(14,1);
 EEPROM.write(15,1);
}
void loop()
{
}



Step 2 : Running DIJKSTRA's Algorithm


Below is the code of Dijkstra's implementation using Arduino. Its quite simple even though the chunk of code may suggest otherwise. Objective is to create two matrices. One which contains the cost of travelling from each node to all other nodes. The other matrix contains the shortest path in doing so for each node. So it is clear for our example you should create two 16*16 matrices for the above two purposes since we got 16 nodes.
 If you run this you can see through the Serial Monitor of Arduino IDE chunk of data appearing. You will see somewhere at the beginning a line containing '1''s and '0's, in fact 16  them. That's the nodes in order. Then below that will be a matrix which is our cost matrix I mentioned followed by the matrix containing the shortest path from each node to all other nodes. You can use the information in this matrix to calculate the path you should follow to get from one node to another or else check whether it is possible to get from one node to another(that is if the value of a certain matrix position is -1)

Since the code is 139 lines longer it would look ugly if I included it here. So I will upload it as a text file. 



16 comments:

  1. Dear sir,

    i can't seem to compile the code? It got some error...

    if(pathMatrix[j][k]==-1) pathMatrix[j][k] = String(trimString(pathMatrix[j [i])+","+pathMatrix[i][k]);

    ->invalid conversion from 'int' to 'const char*'..

    How i can solve this? please help thanks

    Best Regards.

    ReplyDelete
    Replies
    1. Change "if(pathMatrix[j][k]==-1)" to "if(pathMatrix[j][k]==String(-1))"

      It should help :)

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi,

    Thanks for sharing your code. However, I can't seem to get anything in the serial monitor. I have an arduino uno connected. I uploaded the EEPROM code first and then the second code. Any idea what I might be doing wrong?

    Thanks in advance,

    James

    ReplyDelete
    Replies
    1. Arduinos can only hold one program at a time sorry. :(

      Delete
  4. I can see the cost matrix and the shortest path matrix arrays in the serial monitor..... then how do I tell the arduino to solve the grid by following the shortest path? can you please help me?

    ReplyDelete
    Replies
    1. basically in the cost matrix, 9999 denotes impossible path. 1,2,3.... represents the number of steps required to get to the destination... and those chunks of data represent shortest path between every point with every point. ie, first point to all the points. then second point with all of the points. the number in the chunk with comma in between represents the points we need to connect to reach our goal..

      Delete
  5. The code works fine...... most of the values of the path matrix are legit..... but some of these values are jumbled..... something like this:

    http://i58.tinypic.com/o7m814.jpg

    Can you help me about this?

    ReplyDelete
    Replies
    1. basically in the cost matrix, 9999 denotes impossible path. 1,2,3.... represents the number of steps required to get to the destination... and those chunks of data represent shortest path between every point with every point. ie, first point to all the points. then second point with all of the points. the number in the chunk with comma in between represents the points we need to connect to reach our goal..

      Delete
  6. I can see the cost matrix and the shortest path matrix arrays in the serial monitor..... then how do I tell the arduino to solve the grid by following the shortest path? can you please help me?

    ReplyDelete
    Replies
    1. basically in the cost matrix, 9999 denotes impossible path. 1,2,3.... represents the number of steps required to get to the destination... and those chunks of data represent shortest path between every point with every point. ie, first point to all the points. then second point with all of the points. the number in the chunk with comma in between represents the points we need to connect to reach our goal..

      Delete
  7. Oh, so basically in the cost matrix, 9999 denotes impossible path. 1,2,3.... represents the number of steps required to get to the destination... and those chunks of data represent shortest path between every point with every point. ie, first point to all the points. then second point with all of the points. the number in the chunk with comma in between represents the points we need to connect to reach our goal.. cool. thanks :)

    ReplyDelete
  8. I have this doubt. in the solutions of the matrix, there are solutions in which there are multiple paths coming to solve the matrix. even with multiple paths, some paths are incomplete as well ! what can be done for that !

    ReplyDelete
  9. I am trying to run code in Arduino Uno. But it is showing error stating that there is no enough memory.

    ReplyDelete
  10. How do I read this? I don't understand how to actually determine the shortest path will the following output information:


    ****************************************
    Nodes
    105 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255

    Cost Matrix
    9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999
    9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999
    9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999
    9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999
    9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999
    9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999
    9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999
    9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999
    9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999
    9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999
    9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999
    9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999
    9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999
    9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999
    9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999
    9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999 9999


    Shortest Path Matrix
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

    ReplyDelete
  11. Error getGrid was not declared in this scope

    how to fix this case? thank you..

    ReplyDelete