GA Help Needed Urgently
Hi,
I'm stuck on how exactly to define the selectParent() method (the Selection method) because I have to take out samples of size 5 (the tournament size) from the population and then order those chromosomes ( in the contenders ArrayList) in order of fitness (the lower the fitness the better). But since I'm working with ArrayLists and since I have to extract each Chromosome object from the contender Arraylist, somehow call the fitness() method on each Chromosome, and then sort in order of fitness (lowest first), then store the 2 Chromosomes with the lowest fitness into a parent ArrayList, I'm lost with how to do this. Any ideas on this please? What's the simplest way of doing it?
Here's the code I have done - the commented stuff is the stuff I have tried to code to do the sorting, but it's all useless now. I am almost in a giving up situation here - Can anyone come up with a solution for how the tournament selection sort would be done in order for my code to run - or some sort of sorting algorithm to order the fitness from lowest to highest as this is the part I am find most trouble with - the thing I don't understand is that because the fitness value of each Chromosome is not stored anywhere (like a variable or ArrayList), how am I able to order the fitnesses from every sample of 5? PLEASE HELP URGENTLY :-(
import java.util.ArrayList;
import java.util.Random;
import java.util.Collections;
import java.math.*;
import java.util.Collection;
class Population{
Database d =new Database();
Random rand =new Random();
ArrayList population =new ArrayList();
ArrayList parents =new ArrayList();
ArrayList contenders =new ArrayList();
publicvoid initialPopulation(){
double[] reuters ={
8.0, 9.0, 12.5, 11.5, 6, 7, 4, 2, 3, 4, 3, 5, 5.5};
Share s1 =new Share(reuters);
d.addShare(s1);
for (int i = 0; i < 10; i++){
Chromosome c =new Chromosome(rand);
c.setDatabase(d);
population.add(c);
System.out.println("Chromosome Weights are: " + c);
System.out.println("Chromosome Fitness Value is: " + c.fitness(0));
System.out.println();
}
System.out.println(population.size());
System.out.println(population.toString());
System.out.println();
//System.out.println(d);
}
public ArrayList selectParents(int tournamentSize){
// Get as many chromosomes as specified by tournament size
for (int i = 0; i < tournamentSize; i++){
contenders.add(i, population.get(i));
}
// Sort the sub-population in order of fitness *****Having Huge Problems Here***
/* double[] fitnessVals = new double[5];
double[] temp = new double[5];
ArrayList tempArrayList = new ArrayList();
for (int i = 0; i < (contenders.size()); i++) {
Chromosome temp1 = (Chromosome)contenders.get(i);
fitnessVals[i] = temp1.fitness(0); }
System.out.println((fitnessVals[0]) +","+ (fitnessVals[1]));
for (int i = 0; i < (contenders.size()); i++) {
if (fitnessVals[i] > fitnessVals[i+1])
{
temp[i] = fitnessVals[i + 1];
fitnessVals[i + 1] = fitnessVals[i];
fitnessVals[i] = temp[i];
tempArrayList.add(contenders.get(i + 1));
contenders.set(i+1, contenders.get(i));
contenders.set(i, tempArrayList.get(i));
}
else {}
//Collections.max((Collection)contenders.get(0));
// System.out.println(contenders.toString());
}
*/
/* public void sortInOrderOfFitness(Comparable ArrayList ) {
for (int i = 0; i < contenders.size(); i++) {
Collections.sort(contenders);
}
System.out.println(contenders); ****Up to Here is Where I'm Lost***
} */
// Return the best two
parents.add(contenders.get(0));
parents.add(contenders.get(1));
System.out.println(parents.toString());
System.out.println(contenders.toString());
return parents;
}
publicstaticvoid main(String args[]){
Population p =new Population();
p.initialPopulation();
p.selectParents(5);
}
}
import java.util.ArrayList;
import java.util.Random;
class Chromosome
{int[] bit =newint[12];
Database db;
Chromosome(Random rand)
{for (int i = 0; i < 12; i++)
{ bit[i] = rand.nextInt(6);}
}
public String toString()
{ String res ="";
for (int i = 0; i < 12; i++)
{ res = res + bit[i] +",";}
return res;
}
publicvoid setDatabase(Database d)
{ db = d;}
publicdouble fitness(int share)
{double prediction = 0;
int divisor = 0;
Share sh = db.getShare(share);
for (int i = 0; i < 12; i++)
{ prediction = prediction + bit[i]*sh.getPrice(i);
divisor = divisor + bit[i];
}
if (divisor != 0)
{ prediction = prediction/divisor;}
else
{ prediction = 0;}
System.out.println("Prediction Value is: " + prediction);
double actualPrice = sh.getPrice(12);
return Math.abs(prediction - actualPrice);
}
}
import java.util.ArrayList;
import java.util.Random;
class Database
{ ArrayList sharedata =new ArrayList();
publicvoid addShare(Share s)
{ sharedata.add(s);}
public Share getShare(int i)
{return (Share) sharedata.get(i);}
}
class Share
{double[] prices =newdouble[13];
Share(double[] p)
{ prices = p;}
double getPrice(int i)
{return prices[i];}

