genetic algorithm-crossover/reproduction
Random r=new Random();
int pointNode1=r.nextInt(component1.size());
int pointNode2=r.nextInt(component2.size());
if(action=="Reprod"){//do Reproduction or Crossover
System.out.println("--");
System.out.println("pointNode1:"+pointNode1);
System.out.println("pointNode2:"+pointNode2);
//System.out.println(">>>>"+component1.get(pointNode1));
System.out.println("original component1:");
for (int k=0; k<component1.size();k++){
System.out.println(component1.get(k));
}
System.out.println("original component2:");
for (int k=0; k><component2.size();k++){
System.out.println(component2.get(k));
}
System.out.println();
ArrayList><String> tempLeftC1=new ArrayList<String>();
ArrayList<String> tempLeftC2=new ArrayList<String>();
ArrayList<String> tempRightC1=new ArrayList<String>();
ArrayList<String> tempRightC2=new ArrayList<String>();
ArrayList<String> newC1=new ArrayList<String>();
ArrayList<String> newC2=new ArrayList<String>();
//preparing temp for left branch for c1
for(int lc1=0; lc1<pointNode1; lc1++){
tempLeftC1.add(component1.get(lc1).toString());
System.out.println("lc1:"+lc1+", left c1:"+tempLeftC1.get(lc1));
}
//preparing temp for right branch for c1
for(int rc1=pointNode1; rc1><=(component1.size()-pointNode1); rc1++){
tempRightC1.add(component1.get(rc1).toString());
System.out.println("rc1:"+rc1+", right c1:"+tempRightC1.get(rc1));
}
//preparing temp for left branch for c2
for(int lc2=0; lc2<=pointNode2; lc2++){
tempLeftC2.add(component2.get(lc2).toString());
System.out.println("lc2:"+lc2+", left c2:"+tempLeftC2.get(lc2));
}
//preparing temp for right branch for c2
for(int rc2=pointNode2; rc2<=(component2.size()-pointNode2); rc2++){
tempRightC2.add(component2.get(rc2).toString());
System.out.println("rc2:"+rc2+", right c2:"+tempRightC2.get(rc2));
}
//doCrossover for c1
for(int k=0; k<(tempLeftC1.size()+tempRightC1.size()); k++){
System.out.println(k);
if(k<tempLeftC1.size()){
newC1.add(tempLeftC1.get(k).toString());
}
if(k>tempLeftC1.size()) {
newC1.add(tempRightC1.get(k).toString());
}
}
for(int k=0; k<newC1.size();k++){
System.out.println("newC1"+newC1.get(k));
}
//doCrossover for c2
for(int k=0; k><(tempLeftC2.size()+tempRightC2.size()); k++){
if(k<tempLeftC2.size()) newC2.add(tempLeftC2.get(k).toString());
if(k>tempLeftC2.size()) newC2.add(tempRightC2.get(k).toString());
}
for(int k=0; k<newC2.size();k++){
System.out.println("newC2"+newC2.get(k));
}
}
example of component1 values are: business, streetname,number
example of component2 values are: streetending, anyWord
if i set pointNode1=1 and pointNode2=0;
the result of this coding should be:
leftC1: business
rightC1:streetname,number
leftC2:streetending
rightC2:anyWord
newC1: business,anyWord
newC2: streetending,streetname,number
thank you in advance for your help.
Message was edited by:
brisJava>

