/**
* 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 <>
for (int j = 0; 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
print.println(i+1+".");
for (int j = 0; 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
//population
for(int Inipop=0;Inipop
else
{
for(int i=0;i
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
// 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
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
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<>=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
// 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
for(int min=0;min
print.print(aMatrix5[mini][min]+" ");
array1[min]=aMatrix5[mini][min];
}
}
}
//proirity index 1
for(int mini=0;mini
for(int min=0;min
//print.print(aMatrix6[mini][min]+" ");
array3[min]=aMatrix6[mini][min];
}
}
}
print.println(" ");
//index 2 (chromosome)
print.print(" chromosome index2 ");
for(int mini=0;mini
for(int min=0;min
print.print(aMatrix5[mini][min]+" ");
array2[min]=aMatrix5[mini][min];
}
}
}
//proirity index 2
for(int mini=0;mini
for(int min=0;min
//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
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) loc1 =" y.nextInt(8);" loc2 =" y.nextInt(8);" loc1 ="="" temp1=" ans[loc1];" temp2 =" ans[loc2];" temp3 =" temp1;" temp11=" ans[loc11];" temp22 =" ans[loc22];" temp33 =" temp11;" t =" 0;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
No comments:
Post a Comment