class DNA { // THE GENETIC SEQUENCE // Vector3D[] dna; // our array of DNAs // CONSTRUCTOR _MAKES A DNA OF RANDOM VECTORS_ // DNA(int num) { dna = new Vector3D[num]; for (int i = 0; i < dna.length; i++) { dna[i] = new Vector3D(random(-1,1),random(-1,1)); //dna[i].normalize(); } } // CONSTRUCTOR #2, CREATES THE INSTANCE BASED ON EXISTING ARRAY DNA(Vector3D[] newdna) { // dna = (Vector3D []) newdna.clone(); // not working as an applet? dna = newdna; } //returns one element from char array Vector3D getGene(int index) { return dna[index].copy(); } // *** CROSSOVER **** // // creates new DNA sequence from two (MATING) DNA mate(DNA partner) { Vector3D[] child = new Vector3D[dna.length]; // pick a midpoint // int crossover = int(random(dna.length)); // passer = crossover; // take half from one and half from other for (int i = 0; i < dna.length; i++) { if(i > crossover) child[i] = getGene(i); else child[i] = partner.getGene(i); } DNA newdna = new DNA(child); return newdna; } // based on mutation probability, picks a new random Vector void mutate(float m) { for (int i = 0; i < dna.length; i++) { if(random(1) < m) { dna[i] = new Vector3D(random(-1,1), random(-1,1)); //dna[i].normalize(); } } } }