The Goal of my function is to use a 1 point crossover function between two Vector to get a new hybrid "Son" Vector having some element from the first Vector and some from the second one .
public Vector crossover(int Sol1,int Sol2){
int size;
Vector sol1 = new Vector();
Vector sol2 = new Vector();
sol1 = (Vector) allpop.get(Sol1);
sol2 = (Vector) allpop.get(Sol2);
int crosspoint = (int) sol1.size()/2 ;
Vector son = new Vector();
son= (Vector) sol1.clone() ;
if (sol1.size() < sol2.size())
size = sol1.size();
else size = sol2.size();
for(int j=(crosspoint-1);j<size;j++)
son.set(j,sol2.get(j));
return son;
}
sometimes it works good and sometimes it gives me the "java.lang.ArrayIndexOutOfBoundsException " error .. Some Ideas ?
Fixed already :) and here i share the answer with you :)
public Vector crossover(Vector sot1, Vector sot2) {
Vector sol1;
Vector sol2;
sol1 = copi(sot1);
sol2 = copi(sot2);
sol1.removeAll(Collections.singleton(null));
sol2.removeAll(Collections.singleton(null));
// int crosspoint = (int) sol1.size()/2 ;
Vector son = new Vector();
boolean awal = true;
int size;
if (sol1.size() < sol2.size()) {
size = sol1.size();
son.setSize(sol1.size());
Collections.copy(son, sol1);
} else {
size = sol2.size();
son.setSize(sol2.size());
Collections.copy(son, sol2);
awal = false;
}
int crosspoint = (int) (Math.random() * ((size * 2) / 3)) + 1;
System.out.println("cross point :" + crosspoint);
int j = crosspoint;
if (awal == true) {
for (j = crosspoint; j < size; j++) {
//son.removeElementAt(j);
// son.add(j, sol2.get(j));
son.set(j, (Capaciter_n_objet) sol2.get(j));
}
} else {
for (j = crosspoint; j < size; j++) {
// son.removeElementAt(j);
//son.add(j, sol1.get(j));
son.set(j, (Capaciter_n_objet) sol1.get(j));
}
}
son.removeAll(Collections.singleton(null));
correction(son);
son.removeAll(Collections.singleton(null));
/* for(int i=0;i<non_packed_objet.size();i++)
* System.out.println("non packed object : "+non_packed_objet.get(i));*/
return son;
}