/*------------------------------------------------------------- Copyright (C) 2000 Peter Clote. All Rights Reserved. Permission to use, copy, modify, and distribute this software and its documentation for NON-COMMERCIAL purposes and without fee is hereby granted provided that this copyright notice appears in all copies. THE AUTHOR AND PUBLISHER MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. -------------------------------------------------------------*/ /*----------------------------------- Genetic P. Clote, Feb 1999 This class implements a genetic algorithm, in order to determine the maximum of a function f : {0,1,2,...,2^n-1} ---> Reals where n = NumBits, a constant. -------------------------------------*/ /*----------------------------------------------- This class is used as a "record" for values population fitness, index in array of individuals to the fittest individual, and the fitness value of the fittest individual. ------------------------------------------------*/ class Fitness { double PopFit; double IndFit; int Best; } class Genetic { static final int MAX_STEPS = 10000; // Arbitrary upper bound for convergence static final int NumBits = 7; static final int MAX = (int) Math.pow(2,NumBits); // equals 2^NumBits static final int P = 2*NumBits; // population size is 2*log(MAX) static final double cprob = 0.25; // crossover prob static final double mprob = 0.001; // mutation prob static final double change = 0.00001; // termination condition /*-------------------------------- F is the function, whose maximum we want to compute. This function can be changed. --------------------------------*/ static double F(int x) { return (Math.sin(Math.pow(Math.sqrt(x),1.5)) + 2); // (-Math.pow(x,4)*Math.sin(x)+10*Math.pow(x,3)+20); } static void printFunctionValues() { System.out.println("\n\nFunction Values \n"); for (int i=0;i F(best)) best = fitness.Best; // best is best fitness value of an individual // in the current population refresh(pop,npop); stop = ((step >= MAX_STEPS) || (Math.abs(pfitness-nfitness) < change)); } while ( ! stop ); /*-------------- output ------------*/ System.out.println("Last population fitness values\n"); System.out.println(" INDIVIDUAL\t FITNESS\n"); System.out.println("--------------------" + "---------------------------"); for(i=0;i2; index--) { x = (int) (Math.random() * index); temp = C[index-1]; C[index-1] = C[x]; C[x] = temp;} /*---------------------------------------------- REMARK: Now perform crossover, using bit operations on the integer individuals ----------------------------------------------*/ for (y=0;y=co;j--){ tempFirst *= 2; tempSec *= 2; if ((r & C[y]) > 0) tempFirst++; if ((r & C[y+1]) > 0) tempSec++; r = r/2; } // Put left side of Dad with right side of Mom for (j=co-1;j>=0;j--) { tempFirst *= 2; tempSec *= 2; if ((r & C[y+1]) > 0) tempFirst++; if ((r & C[y]) > 0) tempSec++; r = r/2; } D[y] = tempFirst; D[y+1] = tempSec; } for (y=m;y maxFitness ) { bestindex = i; maxFitness = f;} } aux.PopFit = sum; aux.IndFit = maxFitness; aux.Best = pop[bestindex]; return(aux); } static void relativefitness(int pop[], double p[], double pfitness) { for(int i=0;i0) temp++; r = r/2; // toggle bit with probability mprob } pop[i]=temp; } } static void refresh(int pop[], int npop[]) { for(int i=0; i