Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thanks in advance #8

Open
sfp932705 opened this issue Oct 5, 2018 · 11 comments
Open

Thanks in advance #8

sfp932705 opened this issue Oct 5, 2018 · 11 comments

Comments

@sfp932705
Copy link

Hey guys!
I have used you indexing function indices(). Works great!
One question though, is there any way to use it for a 2 dimensional array?

@Korijn
Copy link
Collaborator

Korijn commented Oct 5, 2018

Can you share a minimal piece of code that demonstrates what you want to accomplish?

@sfp932705
Copy link
Author

Sure.
I am implementing A* to solve N-Puzzle, and need to calculate manhattan distance between 2 states. A state is an nxn 2D array. I have done this using loops, as follows

...
for i in xrange(np.size(Mxy[0])):
Gxy[0][i] = np.where(goal == matrix[Mxy][i])[0]
Gxy[1][i] = np.where(goal == matrix[Mxy][i])[1]
Cxy = abs(Mxy[0]-Gxy[0]) + abs(Mxy[1]-Gxy[1])
return np.sum(Cxy)

where Mxy holds the tiles that are differently placed in both states, and Gxy is just an empty array with same dimensions. It works okay, but as n grows it takes more time. And i have defined a linear conflict function, that needs to calculate manhattan distance first. I want to speed things up a little bit.
I used numpy_indexed.indices(goal,matrix) for a flat array version of my states and it works great. However i need to use it in the actual two dimensional array in order to get the actual manhattan distance.
I just want to get rid of the python loop, and have found several ways but only with a flattened array. I tried the scipy cdist but didn't get what i expected.
Any help is appreciated!!!

@Korijn
Copy link
Collaborator

Korijn commented Oct 5, 2018

Have you tried browsing the unit tests of the indices function? https://github.com/EelcoHoogendoorn/Numpy_arraysetops_EP/blob/master/tests/test_indexed.py#L80

@sfp932705
Copy link
Author

Actually no, but what should i do? Look through to see if any function is of interest to my approach?

@Korijn
Copy link
Collaborator

Korijn commented Oct 5, 2018

Yeah. I'm afraid I'm not too sure how to deal with your example code. Hopefully @EelcoHoogendoorn can answer your question directly.

@sfp932705
Copy link
Author

ok thanks!

@Korijn
Copy link
Collaborator

Korijn commented Oct 5, 2018

Actually, do you mind providing an example input & output, so I can test my solution? I might be able to figure it out.

@sfp932705
Copy link
Author

This is kind of what i want:
Given a goal state,
[[1,2,3,4]
[5,6,7,8]
[9,10,11,12]
[13,14,15,0]]
And a current state,
[[4,1,5,6]
[2,3,8,7]
[9,10,11,12]
[14,15,13,0]]

I would like to know the index for every element of the current state in the goal state, in some sort of vectorized way that involves no looping. So i would expect getting:
[[[0,3],[0,0],[1,0],[1,1]],..] and so on, because 4 which is the first element in current state is element [0,3] in goal state,and 1 is element [0,0,] and so on.
Thank you!

@EelcoHoogendoorn
Copy link
Owner

Hi;

There are various graph-like algorithms that you can reasonably efficiently implement using the techniques in this library; at least compared to any python for loops; but its not going to be very fast, compared to the very optimized graph libraries available in python for A* specifically. But certainly interesting as an exercise in vectorized graph algorithms.

It is not clear to me what the code you posted thus far is trying to accomplish though; as per stackoverflow, a self-contained example would help a lot. Speaking of which; its a better format for these type of questions.

@EelcoHoogendoorn
Copy link
Owner

Ah posted that last comment without refreshing my page; thats a lot more of a self-contained problem!

Indeed you could do something like:

idx = npi.indices(G.flatten(), C.flatten())
idx = np.unravel_index(idx, C.shape)

on a higher level, I really doubt this is the most efficient way to approach A* using numpy_indexed though...

@sfp932705
Copy link
Author

ok thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants