my work in Genetic Algo (Assignment preferences)

/**
* Write a description of class minipro here.
*
* @author (Siti Asmah Ab Aziz & Siti Farah Diana bt Yusoff)
* @version (BinaryTournament )
*/
import javax.swing.*;
import java.util.*;
import java.math.*;
import java.io.*;

public class miniproBinaryTournament
{
public static void main(String[]args)throws IOException
{

PrintWriter print = null;
try // try clause
{
String filename=JOptionPane.showInputDialog(null,”Enter name of file for output result”);     //location for output to display
print = new PrintWriter(new FileOutputStream(filename+”.txt”));

String tit=JOptionPane.showInputDialog(null,”Enter number of title offered”);
int title=Integer.parseInt(tit);

String stud=JOptionPane.showInputDialog(null,”Enter Number of Student”);
int student=Integer.parseInt(stud);

String pop=JOptionPane.showInputDialog(null,”Enter Number of population “);
int population=Integer.parseInt(pop);

String gen=JOptionPane.showInputDialog(null,”Enter Number of generation “);
int generation=Integer.parseInt(gen);

//number of student

Random generator = new Random();
int[][] aMatrix = new int[student][4];
String out=””;
// populate matrix
for (int i = 0; i < student; i++) {

for (int j = 0; j < 3; j++) {

//number of assignment
//String assign=JOptionPane.showInputDialog(null,”Enter Number of assignment you prefer student no: ” +(i+1));
//int assignment=Integer.parseInt(assign);
//int index=j+1;
int  r=generator.nextInt(title);
if(r ==1)
{
aMatrix[i][j] = r;
}else
{
aMatrix[i][j] = r;}
}
}

// print matrix
print.println(“Student”+”\t First Choice”+” \t Second Choice”+” \t Third Choice”);
print.println(“_____________________________________________________________”);
for (int i = 0; i <student; i++)
{

print.println(i+1+”.”);

for (int j = 0; j < 3; j++) {
print.print(“\t”+”\t”+aMatrix[i][j]);

}
print.println();
print.println(“_____________________________________________________________”);
}
print.println();

//create chromosome
int prio=0;
int choosenP=0;
int []chromosom=new int [student];
int [] a=new int[3];
int[][] aMatrix2 = new int[student][4];
int[][] aMatrix3 = new int[student][4];
String[][] aMatrix4 = new   String[student][4];
int[][] aMatrix5 = new int[population][student];
int[][] aMatrix6 = new int[population][student];

boolean found=false;
int count=0;
int asign=0;
int asign2=0;
int asign3=0;
int asign4=0;
int asign5=0;
int title2;
int  asignOr2=0;
int  asignOr3=0;
int  asignOr4=0;
int  asignOr5=0;
boolean found2=true;
boolean found3=true;
int index1=0;
int index2=0;
int add=0;
int answer=0;
int combine=0;
int total=0;

//store in new array for index 0

for(int gene=0;gene<generation;gene++)
{
print.println(“===================================================================================”);
print.println(” Genaration   “+(gene+1));
print.println(“===================================================================================”);
// int[][] aMatrix3 = new int[ population][4];
int asignOr;

//population
for(int Inipop=0;Inipop<population;Inipop++)
{
int fitness=0;
String Cfit=””;
// CHEKING PROSES TO ELEMINATE DUPLICATION
print.println(“*****************************”);
print.println(” population   “+Inipop);
print.println(“*****************************”);
for (int chL1=0; chL1<student; chL1++)
{
// print.println(“student  “+chL1);
if(chL1==0)
{
asignOr=aMatrix[0][0];
aMatrix2[chL1][0]=asignOr;
aMatrix2[chL1][1]=1;
}

else
{
for(int i=0;i<chL1;i++)
{
// print.println(“stored data : “+aMatrix2[chL1-(i+1)][0]);
// print.println(“first data  : “+aMatrix[chL1][0]);

if(aMatrix2[chL1-(i+1)][0]!=aMatrix[chL1][0])
{
asignOr=aMatrix[chL1][0];
//asignment no
aMatrix2[chL1][0]=asignOr;
//value count fitness
aMatrix2[chL1][1]=1;
// print.println(aMatrix2[chL1-(i+1)][0]+” is not ==”+aMatrix[chL1][0]);

}
else  {

asignOr=aMatrix[chL1][1];
// print.println(aMatrix2[chL1-(i+1)][0]+”==”+aMatrix[chL1][0]);
// print.println(“second choice “+asignOr);

for(int s=0;s<chL1;s++)
{
if( asignOr!=aMatrix2[chL1-(s+1)][0])
{   asign2=aMatrix[chL1][1];
//asignment no
aMatrix2[chL1][0]=asign2;
//i++;
//value count fitness
aMatrix2[chL1][1]=2;

//  print.println(aMatrix2[chL1-(s+1)][0]+” is not ==”+asign2);
}
else
{
//found = true;
asign3=aMatrix[chL1][2];
//  print.println(aMatrix2[chL1-(i+1)][0]+”==”+aMatrix[chL1][0]);
//   print.println(“third choice “+asign3);
for(int j=0;j<chL1;j++)
{
if( asignOr!=aMatrix2[chL1-(j+1)][0])
{   asign3=aMatrix[chL1][2];
//asignment no
aMatrix2[chL1][0]=asign3;
//i++;
//value count fitness
aMatrix2[chL1][1]=3;
// print.println(aMatrix2[chL1-(j+1)][0]+” is not ==”+asign3);
}
else
{
found=true;
asign3=aMatrix[chL1][2];
//print.println(aMatrix2[chL1-(s+1)][0]+”==”+aMatrix[chL1][0]);
//   print.println(“random number “);
//asignment no
int  r=generator.nextInt(title);
for(int ran=0;ran<chL1;ran++)
{

if(r!=aMatrix2[chL1-(ran+1)][0])
//print.println(“random number masuklah”);
aMatrix2[chL1][0]=r;
aMatrix2[chL1][1]=4;
}break;

}
}break;}

} break;
}

}

}

add=aMatrix2[chL1][1];
// print.println(“priority value  “+add);
combine=aMatrix2[chL1][0];

print.println((chL1+1)+ ” get project no: “+aMatrix2[chL1][0]);
print.println(” “);
count ++;

//value add of priority in chromosome
fitness+=add;
aMatrix2[chL1][2]=fitness;
answer= aMatrix3[chL1][0];
Cfit +=” “+aMatrix2[chL1][0];

//store assignment
aMatrix5[Inipop][chL1]= aMatrix2[chL1][0];
//store priority
aMatrix6[Inipop][chL1]= aMatrix2[chL1][1];
// print.println(“priority value  “+ aMatrix6[Inipop][chL1] );
}
print.println(“kumpulkan “+Cfit);
//store
aMatrix4[Inipop][0]= Cfit ;
print.println(” fitness “+Inipop+”: “+fitness);
aMatrix3[Inipop][0]=fitness;
// print.println(“jawapan fitness….. “+  aMatrix3[Inipop][0]);
print.println(“”);
print.println(“”);

}

/********************Binary tournament********************/
/*************** 1st random number of choosen value**********************************************************/
int randomCv;
int  nV=0;
int indexR;
String radomVal=””;
int tempVal;
String tempRAn;
int valueR=0;
boolean ran1=false;
boolean ran2=false;
int randomvalue1[]=new int[10];//store value random1
int randomvalue2[]=new int[10];//store value random2

int min1=aMatrix3[0][0];
int min2=aMatrix3[0][0];

randomCv=generator.nextInt(population);
print.println(“random Value : “+ randomCv);

if (randomCv>=2)
{
for (int randomLoop=0;randomLoop<randomCv; randomLoop++)
{

indexR=generator.nextInt(population-1);
tempVal=aMatrix3[indexR][0];
//  value=Integer.parseInt(tempVal,2);
radomVal+= “\t”+tempVal;
print.println(“jawapan storage 2: “+tempVal);
for(int minSearch=0;minSearch< randomLoop;minSearch++)
{
if(min1>=aMatrix3[minSearch][0])
{
min2=min1;
min1=aMatrix3[minSearch][0];
index2=index1;
index1=minSearch;

}
else if(min1>=aMatrix3[minSearch][0])
{
min2=aMatrix3[minSearch][0];
index2=minSearch;

}

}

}

print.println( ” “);
print.println(“minimum 1: “+min1);
print.println(“index1: “+(index1));
print.println(“minimum 2: “+(min2));
print.println(“index2: “+ (index2));
// print.println(“\n”);
// print.println(“\n”);
}

else
{ran1=true;
String buz = “no random value”;
print.println(buz);
print.println(“\n”);
}

/********************call cromosome choosen*******************************/
if(ran1==false)
{
int save=0;
int[] array1 = new int [student];//array isi chromose1 yang terpilih max1
int[] array2 = new int [student];//array isi chromose2 yang terpilih max2
int[] array3 = new int [student];
int[] array4 = new int [student];

print.println(“_______________________________________________________________________________________________________________________________________”);
print.println(“Population  “+”  “+” Chromosome “+”\t”+”\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “fitness “);
print.println(“_______________________________________________________________________________________________________________________________________”);
for(int call=0;call<population;call++)
{
print.println(“_______________________________________________________________________________________________________________________________________”);
print.print((call+1)+”. “);
print.print(”  “);

//  save=aMatrix2[call1][0];

print.print(”  “+ aMatrix4[call][0]);

print.print(“\t”+”\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+”\t”+ “\t”+ “\t”+ “\t”+ “\t”+ “\t”+   aMatrix3[call][0]);
print.println(“”);
}
print.println(“_________________________________________________________________________________________________________________________________”);

//chromosome the most minimum and 2nd minimum in population
//first index
//index 1(chromosome)
print.print(” chromosome index1 “);

for(int mini=0;mini<population;mini++ ){

for(int min=0;min<student;min++ )
{
if(index1==mini)
{

print.print(aMatrix5[mini][min]+” “);
array1[min]=aMatrix5[mini][min];

}

}

}

//proirity index 1
for(int mini=0;mini<population;mini++ ){

for(int min=0;min<student;min++ )
{
if(index1==mini)
{

//print.print(aMatrix6[mini][min]+” “);
array3[min]=aMatrix6[mini][min];

}

}

}

print.println(” “);

//index 2 (chromosome)
print.print(” chromosome index2 “);

for(int mini=0;mini<population;mini++ ){

for(int min=0;min<student;min++ )
{
if(index2==mini)
{

print.print(aMatrix5[mini][min]+” “);
array2[min]=aMatrix5[mini][min];
}

}

}

//proirity index 2
for(int mini=0;mini<population;mini++ ){

for(int min=0;min<student;min++ )
{
if(index2==mini)
{

//print.print(aMatrix6[mini][min]+” “);
array4[min]=aMatrix6[mini][min];

}

}

}

/****************************************************************UNIFORM CROSSOVER*************************************************/

Random y = new Random();

print.println(“”);

int [] ans = new int [student];//panjang allele 123456
int q=0;
int [] priority = new int [student];
for(int r= 0;r<student;r++)
{
q = y.nextInt(2);
if ( q == 1)
{
ans[r] = array1[r];//array 1 gantikan chromosome satu yang terpilih
priority[r]=array3[r];
}
else
{
ans[r] =  array2[r];//array 2 gantikan chromosome kedua yang terpilih
priority[r]=array4[r];
}
}
print.println(“”);
print.println(“Crosover result using uniform crosover “);
for (int z = 0;z<student;z++)
print.print(ans[z]+” “);

print.println(“”);
print.println(“”);
/**************************************************MUTATION SWAPPING (PENIPU)********************************************************/
int loc1 =0;
int loc2=0;
int loc11 =0;
int loc22=0;
double ran = Math.random();
if(ran <0.9)
{
do
{
loc1 = y.nextInt(8);
loc2 = y.nextInt(8);
}
while(loc1 == loc2);
//chromosome
int temp1= ans[loc1];
int temp2 = ans[loc2];
int temp3 = temp1;
ans[loc1] = temp2;
ans[loc2] = temp3;
//proiority
int temp11= ans[loc11];
int temp22 = ans[loc22];
int temp33 = temp11;
priority[loc1] = temp22;
priority[loc2] = temp33;
}
print.println(” mutation”);
//mutaion of the chromosome
for (int t = 0;t<student;t++)
{
print.print(ans[t]+” “);
}
print.println(” “);
//proirity
int addfit=0;
for (int t = 0;t<student;t++)
{
//print.print(priority[t]+” “);
addfit+=priority[t];

}
print.println(“”);
print.println(” fitness after mutation: “+addfit);
System.out.println(addfit+” “);
total+=addfit;
}
print.println(” ” );

}
int ave;
ave=total/generation;

print.println(” Average “+ave );

}//close try

catch(FileNotFoundException fnf)
{
System.out.println(“File Not Found”);
System.out.println(fnf.getMessage());
}
catch(IOException io)
{
System.out.println(io.getMessage());
}

catch(Exception a)
{
}

print.close();
}//close main
}//close minipro

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s