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>

[3480 byte] By [brisJavaa] at [2007-11-27 8:53:41]
# 1
Do you have an actual question?
prometheuzza at 2007-7-12 21:11:33 > top of Java-index,Other Topics,Algorithms...
# 2

This is actually part of my assignment. I have done all the other stages. Just that the crossover part didn't work out.

I want to write a crossover function that can choose a point randomly from a tree string and then exchange the left branch.

pointNode1 and pointNode2 is the random point for crossover. component1 and component2 are the tree strings.

my method for now is to copy element0 until elementpointNode1 of component1to tempLeftC1 and remaining elementpointNode1+1 until end of component1 into tempRightC1. This goes the same for component2.

then, I want to perform the crossover function so that tempLeftC1 will be combined with tempRightC2 to form newC1 and tempLeftC2 combined with tempRightC2 to form newC2.

however, when i run the loop some of the for statements didn't execute.

i need help.

thank you for your support and help.

brisJavaa at 2007-7-12 21:11:33 > top of Java-index,Other Topics,Algorithms...
# 3
Thank god.. i managed to solve this task already.. TQ for all your concern..Cheers!
brisJavaa at 2007-7-12 21:11:33 > top of Java-index,Other Topics,Algorithms...