/Genetic Learning "Rockets" //DNA arrays //Fitness Test to grade them, or you can use simulation //Breeding process: average 2 arrays and add a touch of random for natural goodness //Then cull the worst performing from the population //In this program you should see over time the rockets "learning" to fly up int breedTimer = 10; float gravity = 0.3; //I upgraded the class to use acceleration so the results would look more interesting //The DNA only consists of the acceleration thrust for the rocket. //x,y,sx,sy are just physics stuff, nothing special happens with those class Rocket { float[] DNA; float x, y,sx,sy; Rocket() { //0- ax //1- ay this.DNA = new float[2]; this.x = width/2;//Center of the screen this.y = height - 150; //Near the bottom this.sx = 0; this.sy = 0; this.DNA[0] = random(1) - 0.5; //Pick random directions to try and move this.DNA[1] = random(2) - 1; } float score() {//Unused for now return 1.0; } //Apply physicas and draw a nice line to look cool void process() { this.sx += this.DNA[0]; this.sy += this.DNA[1] + gravity; this.x += this.sx; this.y += this.sy; stroke(255, 0, 0); strokeWeight(2); line(this.x, this.y, this.x + this.DNA[0]*5, this.y + (this.DNA[1]) *5); //Very cool. } } //The global population of Rockets ArrayList pop; void setup() { size(800, 500); pop = new ArrayList(); for(int i=0; i < 20; i++) pop.add(new Rocket()); //Make a start population to get things going } void draw() { fill(255); rect(0, 0, 800, 500); for (int i=0; i < pop.size(); i++) pop.get(i).process(); //Count down to the production on new Rockets breedTimer--; if(breedTimer < 0){ breedTimer = 10; breed(); } //Now that theres lots of rockets, remove the wort ones so we don't use their awful values anymore if(pop.size() > 100) cull(); fill(0,255,255); text("Population: " + pop.size(), 100,100); } void breed() { //Little baby rocket! Rocket newRocket = new Rocket(); //Pick 2 parents Rocket p1 = pop.get(int(random(pop.size()))); Rocket p2 = pop.get(int(random(pop.size()))); //Make sure they aren't the same parent while (p1 == p2) p2 = pop.get(int(random(pop.size()))); //Get the average DNA values for the parents, plus a little random mutation for fun for (int i=0; i < newRocket.DNA.length; i++) { newRocket.DNA[i] = (p1.DNA[i] + p2.DNA[i]) / 2.0 + (random(3) - 1.5)/3.0; } //Release the new baby rocket into the wild. Good luck little rocket! pop.add(newRocket); } void cull(){ //Remove the worst performing rockets //Option 1: use a score that grades each rocket in the class definition. // then sort highest to lowest and remove the bottom half of the population //Option 2: Run via simulation. I'll just check for crashed rockets and remove those //I picked option 2. for(int i=0; i < pop.size(); i++){ if(pop.size() > 2){ if(pop.get(i).y > height) pop.remove(i); else if(pop.get(i). y > 0){ if(pop.get(i).x < 0 || pop.get(i).x > width) pop.remove(i); } } } } void mousePressed() { breed(); //If you click you can make new rockets. Its not needed, but maybe you just want to click things. }