LifeFarm::LifeFarm(int w, int h, Canvas* c, bool randomize) { framecount = 0; listend = 0; width = w; height = h; size = w * h; livelist = new int[size]; int maxsize = 1+h*(1+w); currentstate = new int[maxsize]; for (int i = 0; i < maxsize; ++i) currentstate[i] = 0; nextstate = new int[maxsize]; for (int i = 0; i < maxsize; ++i) nextstate[i] = 0; for (int i = 0; i < size; ++i) livelist[i] = 0; currentstart = currentstate; //Pointers to the beginning of the arrays nextstart = nextstate; alive = new bool*[h](); for (int i = 0; i < h; ++i) { alive[i] = new bool[w]; for (int j = 0; j < w; ++j) alive[i][j] = false; } if (randomize) { for (int i = 9*h/10; i > h/10; --i) { bool newrow = true; for (int j = 9*w/10; j > w/10; --j) { if ((rand() % 2) > 0) { if (newrow) { *currentstate = (i + 1); ++currentstate; newrow = false; } *currentstate = -(j + 1); ++currentstate; addAnt(j,i); } } } *currentstate = 0; currentstate = currentstart; } else initGun(); can = c; neighbors = new int*[h](); for (int i = 0; i < h; ++i) neighbors[i] = new int[w]; bgcolor = c->getBackgroundColor(); drawdead = false; }
/********** Cr� la population **********/ void itsHybridContinuousInteractingAntColony::createAnts() { // r�ervations vecteurs antProbaMemory.reserve( antsNb ); //antMoveRange.reserve( antsNb*SpaceDim() ); antMoveRange.reserve( antsNb ); antCurrentPoint.reserve( antsNb * this->getProblem()->getDimension() ); antCurrentValue.reserve( antsNb ); // trop gourmand : antIncomingPoints.reserve( antsNb*antsNb*SpaceDim() ); antIncomingPoints.reserve( antsNb*antsNb ); antIncomingValues.reserve( antsNb*antsNb ); antState.reserve( antsNb ); for( unsigned int i=0; i < antsNb; i++ ) { // tirage al�toire uniforme sur l'hypercube de l'espace de recherche vector<double> aPoint; /* aPoint.reserve( this->getProblem()->getDimension() ); aPoint = randomUniform( this->getProblem()->boundsMinima(), this->getProblem()->boundsMaxima() ); */ aPoint = this->getSamplePointAddr(i)->getSolution(); // tire suivant N l'amplitude max de mouvement // distribution normale sur la moyenne de l'amplitude de l'espace de recherche double aMoveRange; aMoveRange = abs( randomNormal( mean( substraction(this->getProblem()->boundsMaxima(),this->getProblem()->boundsMinima()) ) * moveRangeMeanRatio, 1/2 * mean( substraction(this->getProblem()->boundsMaxima(),this->getProblem()->boundsMinima()) ) * moveRangeStdRatio ) ); // tire sur N la probabilit�de d�art d'utiliser un type de m�oire double aProbaUseMemory; aProbaUseMemory = randomNormal( probaUseMemoryMean, probaUseMemoryStd ); // corrige les d�assements if ( aProbaUseMemory < 0 ) { aProbaUseMemory = 0; } if ( aProbaUseMemory > 1 ) { aProbaUseMemory = 1; } // ajoute la fourmi addAnt( aPoint, aMoveRange, aProbaUseMemory ); } }
void LifeFarm::initGun() { int w = width/2, h = height/2; currentstart = currentstate; *currentstate++ = h+1; *currentstate++ = -(w+25); addAnt(w+25,h+1); *currentstate++ = h+2; *currentstate++ = -(w+25); *currentstate++ = -(w+23); addAnt(w+25,h+2); addAnt(w+23,h+2); *currentstate++ = h+3; *currentstate++ = -(w+36); *currentstate++ = -(w+35); *currentstate++ = -(w+22); *currentstate++ = -(w+21); *currentstate++ = -(w+14); *currentstate++ = -(w+13); addAnt(w+36,h+3); addAnt(w+35,h+3); addAnt(w+22,h+3); addAnt(w+21,h+3); addAnt(w+14,h+3); addAnt(w+13,h+3); *currentstate++ = h+4; *currentstate++ = -(w+36); *currentstate++ = -(w+35); *currentstate++ = -(w+22); *currentstate++ = -(w+21); *currentstate++ = -(w+16); *currentstate++ = -(w+12); addAnt(w+36,h+4); addAnt(w+35,h+4); addAnt(w+22,h+4); addAnt(w+21,h+4); addAnt(w+16,h+4); addAnt(w+12,h+4); *currentstate++ = h+5; *currentstate++ = -(w+22); *currentstate++ = -(w+21); *currentstate++ = -(w+17); *currentstate++ = -(w+11); *currentstate++ = -(w+2); *currentstate++ = -(w+1); addAnt(w+22,h+5); addAnt(w+21,h+5); addAnt(w+17,h+5); addAnt(w+11,h+5); addAnt(w+2,h+5); addAnt(w+1,h+5); *currentstate++ = h+6; *currentstate++ = -(w+25); *currentstate++ = -(w+23); *currentstate++ = -(w+18); *currentstate++ = -(w+17); *currentstate++ = -(w+15); *currentstate++ = -(w+11); *currentstate++ = -(w+2); *currentstate++ = -(w+1); addAnt(w+25,h+6); addAnt(w+23,h+6); addAnt(w+18,h+6); addAnt(w+17,h+6); addAnt(w+15,h+6); addAnt(w+11,h+6); addAnt(w+2,h+6); addAnt(w+1,h+6); *currentstate++ = h+7; *currentstate++ = -(w+25); *currentstate++ = -(w+17); *currentstate++ = -(w+11); addAnt(w+25,h+7); addAnt(w+17,h+7); addAnt(w+11,h+7); *currentstate++ = h+8; *currentstate++ = -(w+16); *currentstate++ = -(w+12); addAnt(w+16,h+8); addAnt(w+12,h+8); *currentstate++ = h+9; *currentstate++ = -(w+14); *currentstate++ = -(w+13); addAnt(w+14,h+9); addAnt(w+13,h+9); *currentstate = 0; currentstate = currentstart; }
void LifeFarm::moveAntsOld() { //Compute this frame's color const int P1 = 7, P2 = 11, P3 = 17; // std::cout << ++framecount << std::endl; int r = (framecount*P1/50 % 255); if (r < 128) r = 255 - r; int g = (framecount*P2/50 % 255); if (g < 128) g = 255 - g; int b = (framecount*P3/50 % 255); if (b < 128) b = 255 - b; ColorFloat fcolor = ColorInt(r,g,b,255); //Clear the each cell's neighbor list for (int i = 0; i < height; ++i) for (int j = 0; j < width; ++j) neighbors[i][j] = 0; //Populate the neighbor list of each living cell int tid = 0, nthreads = 1; for (int i = tid; i < listend ; i += nthreads) { int n = livelist[i]; int row = n / width, col = n % width; int xm = col - 1; if (xm < 0) xm = width - 1; int xp = col+1; if (xp > width-1) xp = 0; int ym = row-1; if (ym < 0) ym = height - 1; int yp = row+1; if (yp > height-1) yp = 0; ++neighbors[ym][xm]; ++neighbors[row][xm]; ++neighbors[yp][xm]; ++neighbors[ym][col]; ++neighbors[yp][col]; ++neighbors[ym][xp]; ++neighbors[row][xp]; ++neighbors[yp][xp]; } //Reset the end of the list for the next iteration listend = 0; //Redraw any cell whose living status has changed, and repopulate the living list bool lives, lived; for (int row = 0; row < height; ++row) { for (int col = 0; col < width; ++ col) { lived = alive[row][col]; lives = ( (neighbors[row][col] == 3) || ( lived && (neighbors[row][col] == 2) )); if (lives != lived) { if (lives) { can->drawPoint(col, row, fcolor); addAnt(col,row); } else if (drawdead) can->drawPoint(col, row, bgcolor); alive[row][col] = lives; } else if (lives) addAnt(col,row); } } }