Message Boards Message Boards

0
|
6925 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Help with ManhattanDistance function

Posted 10 years ago

Hello friends I'm working on a mini project (8-puzzle) for which I need the distance manhattan and have programmed it in the following way

valabs[a_, b_] := If [a > b, a - b, b - a]
manhattan[tablero_List] := Module[{i, j, x, y, cuenta = 0},
  For[i = 1, i <= 3, i++,
   For[j = 1, j <= 3, j++,
     If[tablero[[i, j]] == 0
       ,
       cuenta += 0;
       ,
       x = Quotient[tablero[[i, j]] - 1, 3] + 1;
       y = Mod[tablero[[i, j]] - 1, 3] + 1;
       cuenta = cuenta + valabs[x, i] + valabs[y, j];
       ];
     ];
   ];
  cuenta
  ]

Remember that you should not count zero, wanting to do the same with function ManhattanDistance get a different value, here is an example. With my function

In[1]:= manhattan[{{2, 1, 5}, {6, 3, 4}, {8, 0, 7}}]

Out[1]= 13

Now with ManhattanDistance

In[2]:= ManhattanDistance[{{2, 1, 5}, {6, 3, 4}, {8, 0, 7}}, {{1, 2,    3}, {4, 5, 6}, {7, 8, 0}}]

Out[2]= 11  

Someone could say why am I having such a result, I think that there is some trick to be able to do the same, it would be of much help some guidance, because I plan to keep using ManhattanDistance later in my code, greetings to all.

POSTED BY: Luis Ledesma
4 Replies
Posted 10 years ago

I would like to apologize for being so brief in my explanation, but I still insist that the function ManhattanDistances gives me a different result, in my function manhattan, the distance manhattan is calculated between the State that serves as an argument to the function and the state goal { {1,2,3 } , {4,5,6 } , {7,8,0 }} so I make the comparison with ManhattanDistances[{{2, 1, 5}, {6, 3, 4}, {8, 0, 7}},{ {1,2,3 } , {4,5,6 } , {7,8,0 }} ], so will look the result that is different in both cases.Or I could suggest some improvements to the function I les shared, as wanting to change the for table problems arise me, I hope to have explained well my idea and I hope your comments.In the section of Consistency and Admissibility the next article explains what I need http://en.wikipedia.org/wiki/Heuristic_function

POSTED BY: Luis Ledesma
Posted 10 years ago

I would like to apologize for being so brief in my explanation, but I still insist that the function ManhattanDistances gives me a different result, in my function manhattan, the distance manhattan is calculated between the State that serves as an argument to the function and the state goal { {1,2,3 } , {4,5,6 } , {7,8,0 }} so I make the comparison with ManhattanDistances[{{2, 1, 5}, {6, 3, 4}, {8, 0, 7}},{ {1,2,3 } , {4,5,6 } , {7,8,0 }} ], so will look the result that is different in both cases.Or I could suggest some improvements to the function I les shared, as wanting to change the for table problems arise me, I hope to have explained well my idea and I hope your comments.

POSTED BY: Luis Ledesma

ManhattanDistance, applied to matrices, produces the l1 operator norm of their difference.

 Norm[{{2, 1, 5}, {6, 3, 4}, {8, 0, 7}} - {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}}, 1]

(* Out[317]= 11 *)

I was not able to figure out what manhattan is computing.

POSTED BY: Daniel Lichtblau

Norm[expr,1] gives the l1 norm of expr. That's the Manhattan distance.

POSTED BY: Frank Kampas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract