import java.util.Random; /** * @author arachlin * * To change this generated comment edit the template variable "typecomment": * Window>Preferences>Java>Templates. * To enable and disable the creation of type comments go to * Window>Preferences>Java>Code Generation. */ //Global Variables needed //Matrix of all rules //Reassign values after roulette wheel //Matrix of rules selected by roulette wheel //What type of breeding: //One-Point Crossover //int breeding_choice=1; //Two-Point Crossover int breeding_choice=2; //One-to-one //int breeding_choice=3; class rule { int[] array = new int[128]; int val; } public class GA { public static Random rand; static { rand = new java.util.Random();} public static void main(rule Rule) { int tot=0; rule[] tempRule = new rule[100]; rule[] tempRule2 = new rule[100]; EvalArray(Rule, tot); Roulette(Rule, tempRule, tempRule2, tot); for (int i=0; i<100; i++) { Mutation(tempRule2[i]); } } //Evaluate the quality of each rule (already done in CA) //Evaluate the total quality //Re-evaluate qualities as a proportion of total public static void EvalArray(rule Rule, int& tot) { for (int i=0; i<128; i++) { tot+=Rule[i].val; } } //Roulette Wheel //Use the evaluated quality to assign portions of the wheel //Random number generator //Store chosen rules in matrix public static void Roulette(rule Rule, rule tempRule, rule tempRule2, int tot) { int[] rnd = new int[100]; for (int c=0; c<100; c++) { rnd[c]=rand()%tot; } for (int j=0; j<100; j++) { int temp=0; int i=0; do { temp+=Rule[i].val; if (temp > rnd[i] || temp == rnd[i]) { tempRule[j]=Rule[i]; } i++; }while (i<100 && temp<rnd[i]); } for (int i=0; i<100; i++) { int one=rand()%tot; int two=rand()%tot; int var1,var2; int tempTot=0; for (int j=0; j<100; j++) { tempTot+=Rule[j]; if (tempTot>=one) { var1=j; } if (tempTot>=two) { var2=j; } } Breeding(Rule[var1], Rule[var2], tempRule2, i); } } //Breeding public static void Breeding(int[] Rule1, int[] Rule2, rule tempRule, n) { switch(breeding_choice) { case 1: One_Point_Crossover(Rule1, Rule2, tempRule, n) break; case 2: Two_Point_Crossover(Rule1, Rule2, tempRule, n) break; case 3: One_to_One(Rule1, Rule2, tempRule, n) break; } } //Crossover //One-Point public static void One_Point_Crossover(int[] Rule1, int[] Rule2, rule tempRule, n) { int[] t=new array[128]; int point=rand()%128; for (int i=0; i<128; i++) { if (i<point) { t[i]=Rule1[i]; } else { t[i]=Rule2[i]; } } tempRule[n]=t; } //Two-Point public static void Two_Point_Crossover(int[] Rule1, int[] Rule2, rule tempRule, n) { int[] t=new array[128]; int point1=rand()%128; int point2=rand()%128; if (point1<point2) { for (int i=0; i<128; i++) { if (i<point1) { t[i]=Rule1[i]; } else if(i<point2) { t[i]=Rule2[i]; } else { t[i]=Rule1[i]; } } } else { for (int i=0; i<128; i++) { if (i<point2) { t[i]=Rule1[i]; } else if(i<point1) { t[i]=Rule2[i]; } else { t[i]=Rule1[i]; } } } tempRule[n]=t; } //Random at each spot public static void One_to_one(int[] Rule1, int[] Rule2, rule tempRule, n) { int[] t=new array[128]; for (int i=0; i<128; i++) { int rnd=rand()%2; switch(rnd) { case 0: t[i]=Rule1[i]; break; case 1: t[i]=Rule2[i]; break; } } tempRule[n]=t; } //Mutation public static void Mutation(int[] Rule) { for (int i=0; i<128; i++) { int rnd=rand()%100; if (rnd == 0) { if (Rule[i] == 0) { Rule[i]=1; } else { Rule[i]=0; } } } } //Elitism public static void Elitism(int[] Rule, rule tempRule) { tempRule[0] = Rule; } }