We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › logical mistake
Page Index Toggle Pages: 1
logical mistake? (Read 782 times)
logical mistake?
Jan 6th, 2010, 11:31am
 
I seem to be blind, because I don´t find the logical mistake in this code.
That`s what I want to do:
Several particles (each an instance of the particle-class of traer-physics-library) are on the screen.
I choose one an try to find its next three neighbours.
Of course I could get all distances and sort them, but that`s to much work to get only three elements.
So my solution (not working correctly):
I dont bother about the particle being its own best neighbour - sort out later.
I take the first four elements, put them in an array nb[][] an then loop through the
other particles,if there are better neighbours. Therefor in each loop  I have to find the "worst" neighbour under these four.
I only compare this one, if the next particle is "better". Than replace it and find the new maximum. And so on.

This works well sometimes, but sometimes I get an strange behaviour I don`t understand.
There are two "good" neighbours and one "wrong" neighbour. Here is the output I get:



Example with 12 particles:
console-output via prntln:
(the index of the particle and its distance to the choosen one)
 nb 0 0: 0     nb 1 0 : 21.209055
nb 0 1: 1     nb 1 1 : 58.19002
nb 0 2: 2     nb 1 2 : 130.75015
 nb 0 3: 3     nb 1 3 : 79.68597
maximum value:2
(starting with the first four particles and getting nr. 2 correctly as greatest value)

21.209055  58.19002  46.24385  79.68597
maximum value:3
(replacing correctly nr. 2 with a smaller value, now nr. 3 is maximum)

21.209055  58.19002  46.24385  79.68597
maximum value:3
(next particle more distant than these four, no change)

21.209055  58.19002  46.24385  79.68597
maximum value:3
(next particle more distant than these four, no change)

21.209055  58.19002  46.24385  47.40248
maximum value:1
(replacing correctly nr. 3 with a smaller value)

21.209055  58.19002  46.24385  47.40248
maximum value:1
(next particle more distant than these four, no change)

21.209055  0.0  46.24385  47.40248
maximum value:3
(replacing correctly nr. 1 with the element itself -distance zero)

21.209055  0.0  46.24385  64.40618
maximum value:3
(now thats odd - replacing nr. 3 with a greater value!)

21.209055  0.0  46.24385  64.40618(all 12 particles checked)

Here is my code:
Code:
void nachbarn(int a)//------------------------nachbarn (finding the three nearest neighbours of a particle with index a)
{
 if(a<0){// no object choosen
   return;  
 }
 float abb;//distance of the actually checked particle
 int maxi;//index in nb[][] of the element with greatest distance
 float[][] nb=new float[2][4];//first index 0 - objectnumber   1 - distance    //second index: the four next neighbours including a itself
 String dat1;
 String titel="nachbarn"+str(a)+".txt";
 String[] datlist=new String[5];//preparation for writing the neighbours to a file

 for (int i=0;i<4;i++){//starting with the four first objects  without regard of their distance
   nb[0][i]=i;
   nb[1][i]=abst(i,a);//
println("first four  nb 0 "+i+": "+nb[0][i] +"temp  nb 1 "+i+" : "+nb[1][i]);
 }
 if(anzahl>4){//anzahl=number of particles

   for(int j=4;j<anzahl;j++){//if an object is nearer than the most distant object in the array nb, replace it
maxi=findmax(nb);//find the most distant object in nb[][]  
println("maximum value:"+maxi+"");  
   println("");  
abb=abst(j,a);
if(abb<abst(maxi,a)){
 nb[0][maxi]=j;
 nb[1][maxi]=abb;
}

 println(nb[1][0]+"  "+nb[1][1]+"  "+nb[1][2]+"  "+nb[1][3]);
   }
   //--now the output to file and console
   //println("Nachbarn von "+a+": ");
   // dat1="Nachbarn von "+str(a)+": ";
   // datlist[0]=dat1;
   if(int(nb[0][0])!=a){//ignore the particle itself as its own neighbour
//println(int(nb[0][0])+": "+round(nb[1][0]));
//  dat1=str(int(nb[0][0]))+": "+str(round(nb[1][0]));
//  datlist[1]=dat1;
   }
   if(int(nb[0][1])!=a){
//println(nb[0][1]+": "+round(nb[1][1]));
//  dat1=str(int(nb[0][1]))+": "+str(round(nb[1][1]));
//  datlist[2]=dat1;
   }
   if(int(nb[0][2])!=a){
//println(nb[0][2]+": "+round(nb[1][2]));
//  dat1=str(int(nb[0][2]))+": "+str(round(nb[1][2]));
//  datlist[3]=dat1;
   }
   if(int(nb[0][3])!=a){
//println(nb[0][3]+": "+round(nb[1][3]));
//  dat1=str(int(nb[0][3]))+": "+str(round(nb[1][3]));
//  datlist[4]=dat1;
   }
   // saveStrings(titel,datlist);

 }
}

float abst(int i, int j){//-----------------------------------------------------------------------distance between two particles with index i and j
 return dist(particles[i].position().x(),particles[i].position().y(),particles[j].position().x(),particles[j].position().y());
}

int findmax(float[][] nb)//----------------------------------------------------------------------------findmax
{
 float maxi;
 int ret_ind=0;
 if(nb[1][0]<nb[1][1])
 {
   ret_ind=1;
 }
 if(nb[1][ret_ind]<nb[1][2])
 {
   ret_ind=2;
 }
 if(nb[1][ret_ind]<nb[1][3])
 {
   ret_ind=3;
 }
 return ret_ind;
}

Re: logical mistake?
Reply #1 - Jan 7th, 2010, 2:41pm
 
Are you trying to find four particles among 12 particles that are most clustered together? I'm having a hard time understanding what you want to do.
Re: logical mistake?
Reply #2 - Jan 7th, 2010, 3:07pm
 
Hi, that´s exactly what I want to do - the four ones, that are nearest a previously choosen particle. By now I did it with a different approach , but I am still very interested in the search for my mistake.
With a small number of particles the efficiency is not so importat, but I thought the way I described would be nice, compairing distances no more than 3 times the number of all particles.
Have you got any idea?
Re: logical mistake?
Reply #3 - Jan 8th, 2010, 8:28am
 
Seems to be difficult.... I apologize for the formating of the code - it wasn´t written to be shown here and the small window gave it some more line-feeds producing some broken lines.
As a last try I give here a pseudo-code to show,what the programm tries to do:

There are some particles with index 0 to number.
Particle with index a is looking for its nearest neighbours.
I put the first four partikels with index 0 to 3  in an array.

find under these four that one with greatest distance from particle a, thats called max.

for(i=4;i=<number of particles;i++){
 
   is distance (particle a,particle i) greater
   than the max- value stored in the array?
   
    if (distance is smaller){
               replace the old particle with max-value
               by the new found particle.
               find again the new max-value under the
               four particles in the array
           }
   }

//after the for-loop there should be the four particles nearest to //particle a  in the array, of course unsorted and including particle a,
//which is its own nearest neighbour.

...and of course I say Thank You to all the people having a look at my problem and trying to find an answer.
Page Index Toggle Pages: 1