1

Here is my problem statement:

I have a cube with specified dimensions. as time passes, some balls are generated in this cube. I have simulated the the coordinates (x,y,z location), time of generation and size of the balls. Now I need to find the shortest path of overlapped balls that connect upper side of the cube to the lower and find the time that this path is completed.

What I thought up to now is to find the pair euclidean distance between all the points and the pair sum of the ball radius. Then compare the distance to the sum to find the overlapping matrix. Then find all objects at the top that have z-size less than 0 and z+size greater than depth of the cube and then I should find the path. I appreciate any help and idea in advance.

For example consider the following data and the code that I've developed up to now:

offspr_x = [1 3 5 1 2]
offspr_y = [3 3 1 8 2]
offspr_z = [1 4 5 3 2]
size = [2 1 4 6 3]
time = [2 5 6 3 8]

Pos= [offspr_x' offspr_y' offspr_z']
dd=pdist(Pos,'euclidean')
ddm = squareform(dd)


% compute similar matrix based on sum of object sizes (assumes size is radius)

drad = meshgrid(size)+ meshgrid(size)';
dadj = ddm.*(ddm <= drad);

Now I need to convert the overlap matrix to a graph object and try to find the shortest path between those points that offspr_z-size < 0 (all objects at the top have z-size less than 0) and offspr_z+size > 5 (objects at bottom have z+size greater than 5):

starts = find(offspr_z-size < 0)
ends = find(offspr_z+size > 5)

UPDATE: As @beaker suggested, I also tried Floyd–Warshall and here is the code that I used:

function D = FastFloyd(D)

n = size(D, 1); 

for k=1:n

    i2k = repmat(D(:,k), 1, n);
    k2j = repmat(D(k,:), n, 1);

    D = min(D, i2k+k2j);

end

end

Therefore, for:

 dadj(dadj==0)=Inf
 D = FastFloyd(dadj)

I got the following result:

D =

3.4641    4.1815    6.0000    5.3852    1.7321
4.1815    4.8990    3.0000    5.4772    2.4495
6.0000    3.0000    6.0000    8.3066    4.3589
5.3852    5.4772    8.3066   10.7703    6.1644
1.7321    2.4495    4.3589    6.1644    3.4641

But I need to find the shortest (soonest) path between starts and ends vectors (those points that overlap the upper and lower surface of the cube). I say soonest because I'm not interested in the least distance but in the least time of generation ...

Ninaa
  • 13
  • 4
  • 1
    Welcome to Stack Overflow! Open-ended "How to" questions are difficult to answer, and tend to generate follow-up discussions. To improve your chances of getting a helpful answer, please [edit] your question to provide greater focus on the specific problem you're facing. – IKavanagh Sep 25 '15 at 18:45
  • @IKavanagh I tried to make it more specific.. – Ninaa Sep 25 '15 at 21:28
  • @Ninaa This is entirely different. Edits should only clarify and give more detail to the original question, not to ask follow-up questions. Please roll back the edit and put the new information in a new question. – beaker Sep 28 '15 at 18:30
  • @beaker I understand but I am not allowed to post a new question. It says: "You have reached your question limit" ... – Ninaa Sep 28 '15 at 18:56
  • I suppose you've deleted some questions that weren't received very well. So it's probably not a good idea to jeopardize this one as well. That being said, why on earth would you take the reciprocal of the elements of your adjacency matrix? – beaker Sep 28 '15 at 19:15
  • @beaker No it's my first question. I created this account 3 days ago! I take the reciprocal of the matrix so that all non-adjacent pairs get a value of Inf. I found this code here: http://www.mathworks.com/matlabcentral/fileexchange/25776-vectorized-floyd-warshall/content/FastFloyd.m – Ninaa Sep 28 '15 at 19:32
  • Try `dadj(dadj==0)=Inf`. – beaker Sep 28 '15 at 19:35
  • @beaker I still have problem with this question. We didn't specify the depth of the cube in order to recognize the points that touch the upper and lower surface... I really appreciate if you can help me to find the solution.. – Ninaa Sep 30 '15 at 15:57
  • @Ninaa As I said previously, this is an entirely new question and will have to be dealt with in a separate post. – beaker Sep 30 '15 at 16:06
  • @beaker It seems I should wait for 3 days in order to be allowed to post another question! – Ninaa Sep 30 '15 at 19:05

1 Answers1

0

You're off to a good start:

offspr_x = [1 3 5 1 2]
offspr_y = [3 3 1 8 2]
offspr_z = [1 4 5 3 2]
size = [2 1 4 6 3]
time = [2 5 6 3 8]

Pos= [offspr_x' offspr_y' offspr_z']
dd=pdist(Pos,'euclidean')

pdist gives you a vector of distances. To change it into something recognizable, use squareform:

ddm = squareform(dd)

Now we use your calculation for the radius matrix and then on to the adjacency matrix:

% compute similar matrix based on sum of object sizes (assumes size is radius)

drad = meshgrid(size)+ meshgrid(size)';
adj = ddm.*(ddm <= drad);

This finds the values in ddm that are less than their respective radius distances and uses that as a mask to get values from ddm.

Here's the output for your test case:

adj =

   0.00000   0.00000   6.00000   5.38516   1.73205
   0.00000   0.00000   3.00000   5.47723   2.44949
   6.00000   3.00000   0.00000   8.30662   4.35890
   5.38516   5.47723   8.30662   0.00000   6.16441
   1.73205   2.44949   4.35890   6.16441   0.00000
beaker
  • 16,331
  • 3
  • 32
  • 49
  • thank you so much. So now, do you know how can I find the shortest path that connect the upper surface to the lower one? I think I should covert the overlap matrix to a graph object and then find those points that offspr_z-offspr_z < 0 and offspr_z+offspr_z > 4.4 and then find the path between them... – Ninaa Sep 28 '15 at 16:33
  • Did you mean `offspr_z-size` and `offspr_z+size`? If so, then I think this is a sound approach. Though in practice it might be easier to find the all-pairs shortest paths using something like Floyd-Warshall and extract from that the paths that connect the upper and lower surfaces. – beaker Sep 28 '15 at 17:07
  • Sorry, yes I meant offspr_z-size and offspr_z+size. I am not familiar with Floyd-Warshall but I'll check it out. thanks – Ninaa Sep 28 '15 at 17:28
  • @Ninaa You can adapt pretty much any shortest-path algorithm you have available (Dijkstra's algorithm, for example, with modifications to the graph), but Floyd-Warshall is probably the most straightforward way. – beaker Sep 28 '15 at 17:31
  • I tried Floyd-Warshall method. I updated the question. Could you please help me with interpreting the results? – Ninaa Sep 28 '15 at 18:16