/** * This class represents the whole GA and contains all recombination * and mutation methods. * * coding: h.sarg * modified: 01/98 */ public class Algorithm { /** * contains the actual population */ private Population pop1; /** * contains the recombinated individuals */ private Population pop2; /** * contains the unsorted mutated individuals, these individuals contains * also the information about the mutation operator to visualize the mutation * in the Generation frame */ private Population pop3; /** * contains the sorted mutated individuals, it becomes the next actual * population */ private Population pop4; /** * help pointer for exchange (pop1 - pop4) */ private Population popres; /** * contains the selected individuals (only the pointers to the individual * objects in the actual population) */ private Individual[] select; /** * number of individuals to select */ private int selectsize; /** * object which stores the information about the GA Parameter frame */ private DialogData dialog; /** * Object which holds the information about the towns */ private TravelData travel; /** * simulate the following crossover or not * if this flag is set (true) then necessary information about each * recombination is stored in each individuals VisRecomb object */ private boolean sim=false; /** * This constructor creates the populations and the array to * store the selected individuals, evaluates the selection, initializes the * start population and sorts it. * @param a DialogData * @param b TravelData */ public Algorithm(DialogData a, TravelData b) { int i; this.dialog = a; this.travel = b; pop1 = new Population(travel, dialog.pop, dialog.towns, 1); pop2 = new Population(travel, dialog.pop, dialog.towns, 0); pop3 = new Population(travel, dialog.pop, dialog.towns, 0); pop4 = new Population(travel, dialog.pop, dialog.towns, 0); pop1.evaluate(); pop1.qsort(); if(dialog.elitism) { selectsize=2*(dialog.pop-1);} else { selectsize=2*dialog.pop;} select = new Individual[selectsize]; } /** * sets the simulation flag */ public void setSim() { sim=true; } /** * unsets the simulation flag */ public void unsetSim() { sim=false; } /** * gets the value of the selection size * @return selection size */ public int getSelectionSize() { return selectsize; } /** * gets the array with the selected individuals * @return individual array */ public Individual[] getSelection() { return select; } /** * gets the spezified population * @param a number of the needed population * @return Population */ public Population getPop(int a) { if(a == 1) return pop1; if(a == 2) return pop2; if(a == 3) return pop3; else return pop4; } /** * does the selection out of the actual population * always using tournament selection with the selected group size */ public void selection() { int i,j; int k=dialog.group; //group size from the options dialog int rand; double bestfit=-1; //invalid best fitness Individual bestindiv=null; //invalid best individual Individual indiv; for(j=0;jrand2) { randh=rand1; rand1=rand2; rand2=randh; } ires.setMut(rand1,rand2); indiv.setMut(rand1,rand2); k=rand2; for(j=rand1;j<=rand2;j++) { gen=indiv.getGen(j); ires.setGen(k, gen); k--; } } } } /** * This mutator selects a gen of the individual per random and inserts * this gen at a random place in the new individual. */ public void insertion() { Individual ires; Individual indiv; int rand1; int rand2; int i,j; int gen; int genpos=0; for(i=0;irand2) { rand3=rand1; rand1=rand2; rand2=rand3; } indiv.setMut(rand1, rand2); if(rand1!=0 || rand2!=dialog.towns-1) { rand3=MyRandom.intRandom(0, dialog.towns-1); while(rand3>=rand1 && rand3<=rand2){ rand3=MyRandom.intRandom(0, dialog.towns-1); } if(rand3=rand1;j--) { ires.setGen(k, indiv.getGen(j)); k--; } for(j=rand3;j>rand2;j--) { ires.setGen(k, indiv.getGen(j)); k--; } } } } } } /** * This mutator selects a gen and a position per random. Then the * selected gen and the gen at the selected position are swapped. */ public void swap() { Individual ires; Individual indiv; int rand1; int rand2; int i,j; int gen; for(i=0;irand2) { randh=rand1; rand1=rand2; rand2=randh; } if(sim) { //area to keep information about the recombination ires.setVisRecomb(); vis=ires.getVisRecomb(); vis.setCoArea(rand1,rand2); vis.setMaxStep(1+rand2-rand1); } ires.copyIndiv(select[i+1]); //go through the crossover area for(k=rand1;k<=rand2;k++) { gen=select[i].getGen(k); genres=ires.getGen(k); //search for the gen for(j=0;jrand2) { randh=rand1; rand1=rand2; rand2=randh; } step=2; if(sim) { //area to keep information about the recombination ires.setVisRecomb(); vis=ires.getVisRecomb(); vis.setCoArea(rand1,rand2); } for(j=rand1;j<=rand2;j++) { gen=select[i+1].getGen(j); found=false; for(k=rand1;k<=rand2;k++) { if(gen==select[i].getGen(k)) { found=true; //System.out.println("habe gen: "+gen); break; } } if(!found) { if(sim) { //hold the steps of the recomb method vis.setStep(pos, step); vis.setInfo(pos, 4); } ires.setGen(pos,gen); pos++; } } //set the crossover area in child step++; for(k=rand1;k<=rand2;k++) { if(sim) { //hold the steps of the recomb method vis.setStep(pos, step); vis.setInfo(pos, 3); } gen=select[i].getGen(k); ires.setGen(pos,gen); pos++; } for(j=rand2+1;j-1 ) { if(heuristic) { if(edges[ edges[gen][j] ][0] !=-1) { heur[k]=edges[gen][j]; sum+=1/(travel.distance(gen, heur[k])); found=true; k++; } } else { if(edges[ edges[gen][j] ][0] !=-1 && edges[ edges[gen][j] ][0] <= shortest) { shortest=edges[ edges[gen][j] ][0]; shortestlist=edges[gen][j]; found=true; } } } j++; } if(heuristic && found) { heur[k]=-1; randdbl=MyRandom.dblRandom(sum); j=0; sum=0.0; while(heur[j]!=-1) { sum+=1/(travel.distance(gen, heur[j])); if(randdbl