public class gabinary
{
public static int POPSIZE =20; /* population size */
public static int MAXGENS= 1000; /* max. number of generations */
public static int NVARS =10; /* no. of problem variables */
public static float PXOVER= 0.8f; /* probability of crossover */
public static float PMUTATION =0.75f; /* probability of mutation */
public static int TRUE =1;
public static int FALSE =0;
public static int generation; /* current generation no. */
public static int cur_best;
public static genotype population[]= new genotype[POPSIZE+1];
genotype newpopulation[]= new genotype[POPSIZE+1];
public gabinary()
{
}
public void initialize()
{
int i, j;
/* Initialise variables */
for (j=1; j < POPSIZE; j++)
for (i=0; i < NVARS; i++) {
System.out.println( "\n popsize \n"+POPSIZE);
for (j=1; j < POPSIZE; j++) {
population[j].fitness = 0;
population[j].rfitness = 0;
population[j].cfitness = 0;
population[j].gene[i] = randval();
}
}
}
/************************************************** *******************/
/* */
/* Random value generator: Generates a random binary digit */
/* */
/************************************************** *******************/
public float randval()
{
Random randg = new Random();
float rand = randg.nextFloat(100);
return (rand) ;
}
/************************************************** *******************/
/* */
/* Evaluate function: This takes a user defined function. */
/* Each time this is changed, the code has to be recompiled. */
/* The current function is : a simple 'sum of bits'. */
/* */
/************************************************** *******************/
public void evaluate ()
{
int mem;
int i;
for (mem=0; mem < POPSIZE; mem++) {
population[mem].fitness = 0;
for (i=0; i < NVARS; i++)
population[mem].fitness += population[mem].gene[i];
}
}
/************************************************** *******************/
/* */
/* Keep_the_best function: This function keeps track of the */
/* best member of the population. Note that the last entry in */
/* the array Population holds a copy of the best individual */
/* */
/************************************************** *******************/
public void keep_the_best()
{
int mem;
int i;
cur_best = 0; /* stores the index of the best individual */
for (mem=0; mem < POPSIZE; mem++) {
if (population[mem].fitness > population[POPSIZE].fitness) {
cur_best = mem;
population[POPSIZE].fitness = population[mem].fitness;
}
}
/* Once the best member in the population has been found, copy its genes */
for (i=0; i < NVARS; i++)
population[POPSIZE].gene[i] = population[cur_best].gene[i];
}
/************************************************** *******************/
/* */
/* Elitist function: The best member of the previous generation */
/* is stored as the last in the array. If the best member of */
/* the current generation is worse then the best member of the */
/* previous generation, the latter would replace the worst */
/* member of the current population. */
/* */
/************************************************** *******************/
public void elitist()
{
int i;
float best, worst; /* best and worst fitness values */
int best_mem=0, worst_mem=0; /* indexes of the best and worst member */
best = population[0].fitness;
worst = population[0].fitness;
for (i=0; i < POPSIZE-1; ++i) {
if (population[i].fitness > population[i+1].fitness) {
if (population[i].fitness >= best) {
best = population[i].fitness;
best_mem = i;
}
if (population[i+1].fitness <= worst) {
worst = population[i+1].fitness;
worst_mem = i + 1;
}
}
else {
if (population[i].fitness <= worst) {
worst = population[i].fitness;
worst_mem = i;
}
if (population[i+1].fitness >= best) {
best = population[i+1].fitness;
best_mem = i + 1;
}
}
}
/* if the best individual from the new population is better than */
/* the best individual from the previous population, then */
/* copy the best from the new population; else replace the */
/* worst individual from the current population with the */
/* best one from the previous generation. */
if (best >= population[POPSIZE].fitness) {
for (i=0; i < NVARS; i++) {
population[POPSIZE].gene[i] = population[best_mem].gene[i];
}
population[POPSIZE].fitness = population[best_mem].fitness;
}
else {
for (i=0; i < NVARS; i++) {
population[worst_mem].gene[i] = population[POPSIZE].gene[i];
}
population[worst_mem].fitness = population[POPSIZE].fitness;
}
}
/************************************************** *******************/
/* */
/* Selection function: A standard proportional (i.e. roulette */
/* wheel) selection for maximization problems incorporating the */
/* elitist model - makes sure that the best member survives. */
/* */
/************************************************** *******************/
public void select()
{
int mem, i, j, k;
float sum = 0;
float p;
/* Find total fitness of population */
for (mem=0; mem < POPSIZE; mem++) {
sum += population[mem].fitness;
}
/* Calculate relative fitness */
for (mem=0; mem < POPSIZE; mem++) {
population[mem].rfitness = population[mem].fitness / sum;
}
population[0].cfitness = population[0].rfitness;
/* Calculate cumulative fitness */
for (mem=1; mem < POPSIZE; mem++) {
population[mem].cfitness = population[mem-1].cfitness +
population[mem].rfitness ;
}
/* finally, select survivors using cumulative fitness */
for (i=0; i < POPSIZE; i++) {
Random rand=new Random();
float f=rand.nextFloat();
// p = rand() % 1000 / 1000.0;
p = f;
if (p < population[0].cfitness)
newpopulation[i] = population[0];
else {
for (j=0; j < POPSIZE; j++)
if (p >= population[j].cfitness && p < population[j+1].cfitness)
newpopulation[i] = population[j+1];
}
}
/* Once a new population is created, copy it back */
for (i=0; i < POPSIZE; i++)
population[i] = newpopulation[i];
}
/************************************************** *******************/
/* */
/* Crossover selection: selects 2 parents that take part in */
/* the crossover. */
/* */
/************************************************** *******************/
public void crossover()
{
int i, mem, one=0;
int first = 0; /* count of the number of members chosen */
float x;
for (mem=0; mem < POPSIZE; ++mem) {
Random rand=new Random();
x=rand.nextFloat();
// x = rand() % 1000 / 1000.0;
if (x < PXOVER) {
++first;
if (first % 2 == 0)
Xover (one, mem);
else
one = mem;
}
}
}
/************************************************** ***********************/
/* */
/* Crossover: performs uniform crossover of the two selected parents. */
/* */
/************************************************** ***********************/
public void Xover (int one, int two)
{
int i;
for (i = 0; i < NVARS; i++) {
if (randval() == 1)
swap (population[one].gene[i], population[two].gene[i]);
}
}
/************************************************** *******************/
/* */
/* Swap: A simple swap procedure that helps in swapping 2 variables */
/* */
/************************************************** *******************/
public void swap (float x, float y)
{
float temp;
temp = x;
x = y;
y = temp;
}
/************************************************** *******************/
/* */
/* Mutation: Random uniform mutation. A variable selected for */
/* mutation is replaced by a an inverted binary digit. */
/* */
/************************************************** *******************/
public void mutate()
{
int i, j;
float lbound, hbound;
float x;
for (i=0; i < POPSIZE; i++) {
for (j=0; j < NVARS; j++) {
Random rand=new Random();
x=rand.nextFloat();
// x = rand() % 1000 / 1000.0;
if (x < PMUTATION) {
/* Perform Mutation */
if (population[i].gene[j] == 1)
population[i].gene[j] = 0;
else
population[i].gene[j] = 1;
}
}
}
}
/************************************************** *******************/
/* */
/* Report function: Reports progress of the simulation. Data */
/* dumped into the output file are separated by commas */
/* */
/************************************************** *******************/
public void report()
{
int i;
float best_val; /* best population fitness */
float avg; /* avg population fitness */
float stddev; /* std. deviation of population fitness */
float sum_square; /* sum of square for std. calc */
float square_sum; /* square of sum for std. calc */
float sum; /* total population fitness */
sum = 0.0f;
sum_square = 0.0f;
for (i=0; i < POPSIZE; i++) {
sum += population[i].fitness;
sum_square += population[i].fitness * population[i].fitness;
}
avg = sum / (float) POPSIZE;
square_sum = avg * avg * (float) POPSIZE;
//stddev = Math.sqrt((float)((sum_square - square_sum) / (POPSIZE - 1)));
stddev = ((sum_square - square_sum) / (POPSIZE - 1));
best_val = population[POPSIZE].fitness;
System.out.println( generation+" "+best_val+" "+ avg+" "+ stddev);
}
/************************************************** *******************/
/* */
/* Main function: Each generation involves selecting the best */
/* members, performing crossover and mutation and then */
/* evaluating the resulting population, until the terminating */
/* condition is satisfied. */
/* */
/************************************************** *******************/
public static void main(String[] args)
{
int i;
int generation = 0;
gabinary g=new gabinary();
System.out.println (" generation best average standard ");
System.out.println(" number value fitness deviation ");
g.initialize();
g.evaluate();
g.keep_the_best();
while (generation < MAXGENS) {
generation++;
g.select();
g.crossover();
g.mutate();
g.report();
g.evaluate();
g.elitist();
}
System.out.println(" Simulation completed");
System.out.println( " Best Member: ");
for (i=0; i < NVARS; i++) {
System.out.println( i+ " "+ population[POPSIZE].gene[i]);
}
System.out.println(" Best fitness "+ population[POPSIZE].fitness);
System.out.println("Success!");
}
}
/************************************************** **************************/