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.
Page Index Toggle Pages: 1
Diffuse (Read 901 times)
Diffuse
Nov 18th, 2009, 8:54am
 
Hi. I have written this diffuse sketch which seems to work fine visually but when i print the value of the centre cell to check its working against my maths it doesn't tally - have checked and checked again my maths and sure correct so i can't figure what might be wrong with the code.
Can anyone offer any advice please. Thanks.

Basically the centre cell is setup with a value of 100, everything else is empty. Diffusion is at 0.9. So, the value in centre cell should go:
100, 10, 11.125, etc... (thats the hand maths) but the sketch works out: 100, 10, 9.02.....
also when checking the centre cells neighbours they go 0, 11.25 (which correlates to my maths) but then again at third step goes 'wrong'.
Also i am sure that the values for the corner cells should be equal and the 4 (north, east, south west also) as they have same topology to centre cell - but each cells value is different - which means something must be wrong with the code.

the diffusion function is:
each cell gives away .9 of its value and takes 1/8th of the diffused value of each of its 8 neighbours.

thankyou for any help!

float diffRate = 0.9;

Cell [][] grid;
Cell [][] Limbo;

int numberOfColours = 255;
color[]colours = new color[numberOfColours];
/*........................................................................*/
void setup() {
 size(200,200);
 colorMode(HSB, 50);  

 for(int i=0; i<numberOfColours; i++){
   colours[i] = color((numberOfColours-i), i, 255);    
 }

 grid = new Cell[width][height];
 Limbo = new Cell[width][height];
 for (int i=0; i<width; i++ ) {
   for (int j=0; j<height; j++ ) {
     // Initialize each object
     grid[i][j] = new Cell(i,j,0);
     Limbo[i][j] = new Cell(i,j,0);
   }
 }      
 grid[width/2][height/2].pH = 100;      //test: setup centre cell with pheromone
}
/*......................................................................*/
void draw() {
 frameRate(125);
 println(grid[width/2][height/2].pH);   //check value of test cell
 diffuse();
 for (int i=0; i<width; i++ ) {    
   for (int j=0; j<height; j++ ) {
     grid[i][j].render();
   }
 }
}

void diffuse() {
 for(int i=0; i<width; i++){
   for(int j=0; j<height; j++){
     float neighsPhero = 0;
     float phero = grid[i][j].pH;

     //addup neighbours pheromone values
     neighsPhero =
    (((grid[((i-1) +width) %width] [((j-1) +height) %height].pH *diffRate) /8) +    //Northwest
     ((grid[(i +width) %width] [((j-1) +height) %height].pH *diffRate) /8) +        //North
     ((grid[((i+1) +width) %width] [((j-1) +height) %height].pH *diffRate) /8) +    //Northeast
     ((grid[((i+1) +width) %width] [(j +height) %height].pH *diffRate) /8) +        //East
     ((grid[((i+1) +width) %width] [((j+1) +height) %height].pH *diffRate) /8) +    //Souteast
     ((grid[(i +width) %width] [((j+1) +height) %height].pH *diffRate) /8) +        //South
     ((grid[((i-1) +width) %width] [((j+1) +height) %height].pH *diffRate) /8) +    //Southwest
     ((grid[((i-1) +width) %width] [(j +height) %height].pH *diffRate) /8)) ;       //West
     
     phero = (phero - (phero*diffRate))    //amount distributed to neighbours
       + neighsPhero;                      //amount taken from neighbours

     Limbo[i][j].pH = phero;
   }
 }
 grid = Limbo;
}

class Cell {
 int x,y;                 // x,y location
 float pH;                //pheromone value
 /*...................................................................*/
 Cell(int xpos, int ypos, float pheromone) {
   x = xpos;
   y = ypos;
   pH = pheromone;
 }
 /*....................................................................*/
 void render() {
   //colour the pheromone
   int phero = floor(grid[x][y].pH);   //translate .pH to an int (rounded down) to allow colour scale
   stroke(colours[phero]);
   point(x, y);
 }
}
Re: Diffuse
Reply #1 - Nov 18th, 2009, 3:05pm
 
Sorry - haven't really looked at the code so feel free to ignore this if irrelevant...

You sure this isn't an issue with floating point accuracy problems  It's surprising how often that leads to unexpected results...
Re: Diffuse
Reply #2 - Nov 19th, 2009, 3:07am
 
the problem is here:

Code:

void diffuse() {
   ....
  grid = Limbo;
}


if you do this, you are not copying the values, but switching pointers. Instead do this:

Code:

void diffuse() {
   ....
   for(int i=0; i<width; i++){
       for(int j=0; j<height; j++){
        grid[i][j].pH = Limbo[i][j].pH;
    }
  }
}


(or perhaps someone knows a more elegant way to copy arrays)
Re: Diffuse
Reply #3 - Nov 19th, 2009, 12:46pm
 
arraycopy method

http://processing.org/reference/arraycopy_.html
Re: Diffuse
Reply #4 - Nov 20th, 2009, 8:47am
 
woo-hoo!

Thanks for the help. As you say the problem was that i wasn't copying the new values in Limbo array to the grid array. Thanks for your help.

cheers
Page Index Toggle Pages: 1