class tree { String[] substitute; String[] genotype; String[] growstring; int no, x,y,w, generation, age; float stack_rot[], fitness, growspeed, deg = PI*0.25, initthick=6.0, thick; int pushlevel = 0, maxpush = 50, symbols, stack_x[], stack_y[]; int startlength = 3, rulelength = 4, freezone = 10; int maxgeneration = 17, ownmax = maxgeneration; int maxleft, maxright, maxtop, maxbottom, nowx, nowy; float boxscaler = 1.0, growfactor = 1.0; boolean over, elite = false, parent = false, fitness_check=true, f_counting=false; boolean growing = false, growmore=false; int toolong = 1000, growcounter = 0; int initstep = 40; float step; float stack_step[]; int f_push, f_arc, f_line, f_rot, f_jump, f_rotmax, f_circle, f_draw, f_not; tree(int n, int px, int py, int boxsize) { no = n; x=px; y=py; w=boxsize; symbols = abc.length; substitute = new String[symbols]; stack_rot = new float[maxpush]; stack_step = new float[maxpush]; genotype = new String[maxgeneration]; growstring = new String[maxgeneration]; stack_x = new int[maxpush]; stack_y = new int[maxpush]; startup(); } void startup() { over = false; parent = false; elite=false; growing=false; for(int i=0; i='A'&&abc[i]<='Z') || evolve_lowercase) { for(int j=0; jtoolong) { ownmax = g; for(; gx-w/2-d&&mouseXy-w/2-d&&mouseY4) { int tab = (i%(int)(Math.floor((width-100)/w))); translate( -w* (int) Math.min( Math.floor((width-100)/w), float(tab)) , w); } if(showone==no && i==g2-1) { w*=3; translate(w/3,w/3); } draw_frame(); if(genotype[i]!="") draw_tree(i); if(showone==no && i==g2-1) w/=3; if(i==g1) { fill(255); noStroke(); textFont(pixelfont, 8); text(fitness,-w/2-3,w/2+9); textFont(lucida, 11); } translate(w,0); //if(showone==no && (i%(int)(Math.floor((width-200)/w)))==0 && i!=0) { if(!fitness_check) { compute_fitness(); f_counting = false; } } popMatrix(); } void compute_fitness() { fitness_check=true; fitness = 0; int totallength = 0; for(int i=0; i=0) ? 1.0 : 1.0/(float)maxleft; int dim = 200; if(bt_f_dim200) fitness += (abs(dim-ww)==0) ? 0.5 : 0.5/abs(dim-ww); if(bt_f_dim200) fitness += (abs(dim-hh)==0) ? 0.5 : 0.5/abs(dim-hh); dim = 500; if(bt_f_dim500) fitness += (abs(dim-ww)==0) ? 0.5 : 0.5/abs(dim-ww); if(bt_f_dim500) fitness += (abs(dim-hh)==0) ? 0.5 : 0.5/abs(dim-hh); dim = 1000; if(bt_f_dim1000) fitness += (abs(dim-ww)==0) ? 0.5 : 0.5/abs(dim-ww); if(bt_f_dim1000) fitness += (abs(dim-hh)==0) ? 0.5 : 0.5/abs(dim-hh); if(bt_f_linesp) fitness += (float)f_line/100.0; if(bt_f_linesn) fitness += (f_line==0) ? 1 : 1.0/(float)f_line; if(bt_f_push) fitness += (float)f_push/100.0; if(bt_f_arc) fitness += (float)f_arc/100.0; if(bt_f_circle) fitness += (f_circle==0) ? 1 : 1.0/(float)f_circle; if(bt_f_jump) fitness += (f_jump==0) ? 1 : 1.0/(float)f_jump; if(bt_f_rot) fitness += (f_rot==0) ? 1 : 1.0/(float)f_rot; if(bt_f_rotmax) fitness += (f_rotmax==0) ? 1 : 1.0/(float)f_rotmax; if(bt_f_notdraw) fitness += (f_not==0) ? 1 : 1.0/(float)f_not; if(bt_f_draw) fitness += (float)f_draw/100.0; if(fitness < 0.1 || f_draw<=1) { startup(); fitness_check = false; } } void start_growing() { maxleft=0; maxright=0; maxtop=0; maxbottom=0; boxscaler=1.0; growmore = true; growing = true; growspeed = 0.05; growfactor = 0.0; age = 1; build_gen(); } void grow() { if(age=1.0) { age++; growfactor=0.0; //growcounter=10; if(genotype[age].length()>toolong || age==maxgeneration-1) { growmore=false; age--; growfactor=1.0; } } } else { growcounter--; } } else { growmore=false; growfactor=1.0; } pushMatrix(); translate(x,y); //draw_frame(); draw_tree(age); popMatrix(); } void draw_tree(int g) { maxleft=0; maxright=0; maxtop=0; maxbottom=0; boxscaler=1.0; nowx=0; nowy=0; step = initstep; stack_step[0] = step; int boxmedx = 0, boxmedy = 0; boolean testscale; float growfactor2 = 1.0; pushMatrix(); rotate(-PI/2); for(int t=0; t<2; t++) { testscale = (t==0) ? true : false; for(int i=0; i w ) boxscaler = float(w)/float(maximal); if(maximal!=0) { step = initstep * boxscaler; stack_step[0] = step; } thick = initthick * boxscaler; boxmedx = -int( (maxleft+(maxright-maxleft)/2)* boxscaler ); boxmedy = -int( (maxbottom+(maxtop-maxbottom)/2)*boxscaler ); //if(showone==no && !test) { // if(g==5) test=true; // print(g); print("\tl="+maxleft+"\tr="+maxright); // print("\tw="+abs(maxright-maxleft)); print("\th="+abs(maxtop-maxbottom)); // print("\tsc="+boxscaler); //print("\tst="+step); // print("\tx="+boxmedx+"\ty="+boxmedy); println(); //} } } popMatrix(); strokeWeight(1.0); } void check_overflow() { maxleft = (maxleftnowx) ? maxright : nowx; maxtop = (maxtop>nowy) ? maxtop : nowy; maxbottom = (maxbottomnowx+step*t*0.5) ? maxright : int(nowx+step*t*0.5); maxtop = (maxtop>nowy+step*t*0.5) ? maxtop : int(nowy+step*t*0.5); maxbottom = (maxbottom=0) { popMatrix(); pushlevel--; step = stack_step[pushlevel]; nowx = stack_x[pushlevel]; nowy = stack_y[pushlevel]; } } void do_plus(float t) { step = ((step + (initstep/3) * boxscaler)*t + step*(1.0-t)); //step += initstep/3 * boxscaler; stack_step[pushlevel] = step; if(f_counting) f_not++; } void do_minus(float t) { step = ((step - (initstep/3) * boxscaler)*t + step*(1.0-t)); //step -= initstep/3 * boxscaler; if(step<(initstep/3)*boxscaler) step = (initstep/3) * boxscaler; stack_step[pushlevel] = step; if(f_counting) f_not++; } }