int gW = 25; int gH = 25; int sW = 300; int sH = 300; int cellW, cellH; int maxC = 55; int step; int population=90; short cgen; Map m1; Pop p1; void setup() { size(sW,sH); background(235,242,245); cellW = sW/gW; cellH = sH/gH; step = 0; cgen = 0; m1 = new Map(); p1 = new Pop(); p1.Epoch(); framerate(10); } void loop() { if(step == maxC) { step = 0; p1.Epoch(); background(235,242,245); m1.Reset(); } background(235,242,245); m1.drawMap(); m1.drawStep(p1.gs[p1.best][cgen].instr[step++]); } void mousePressed() { step = 0; p1.Epoch(); background(235,242,245); m1.Reset(); } class Pop { Genome gs[][]; float FitTot; float mutRate = .052; int best; Pop() { FitTot = 0; gs = new Genome[population][2]; for(int i=0;i 0) { curx = tx; cury = ty; } } dFit = 1 / (1 + dist(curx,cury,gW-2,m1.end)); //print("Pos = " + curx + "," + cury); //print(" Fit = " + dFit + "\n"); //m1.drawCell(curx,cury); return dFit; } } class Map { int start, end, curx, cury; int[][] mapar; Map() { mapar = new int[gW][gH]; randomize(.05); curx = 1; cury = start; } void Reset() { curx = 1; cury = start; } void drawMap() { //background(235,242,245); stroke(0); for (int y=1; y < gH-1; y++) { for (int x=1; x < gW-1; x++) { if (mapar[x][y]==0) noFill(); else if (mapar[x][y]==1) fill(30,115,175); else if (mapar[x][y]==2) fill(0,0,0); rect(x*cellW,y*cellH,cellW,cellH); } } } void drawStep(int i) { int tx, ty; tx = curx; ty = cury; if(i == 0) curx++; //right else if(i == 1) curx--; //left else if(i == 2) cury++; //down else cury--; //up if (mapar[curx][cury] > 0) { curx = tx; cury = ty; } curx = min(curx,gW-2); curx = max(1,curx); cury = min(cury,gH-2); cury = max(1,cury); fill(110,110,110); rect(curx*cellW,cury*cellH,cellW,cellH); } void drawCell(int xi, int yi) { fill(110,110,110); rect(xi*cellW,yi*cellH,cellW,cellH); } void randomize(float density) { int r1, r2; for (int i=0;i