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];}

[9479 byte] By [Niru99a] at [2007-11-26 23:37:04]
# 1
Since it was so urgent, I must be too late by now...
prometheuzza at 2007-7-11 15:00:07 > top of Java-index,Other Topics,Algorithms...
# 2
No you're not I'm still here, any ideas?
Niru99a at 2007-7-11 15:00:07 > top of Java-index,Other Topics,Algorithms...
# 3

> No you're not I'm still here

I was making a point that it's rude to flag your questions as urgent. As you can see, a lot of the people who answer questions tend to ignore such posts.

>, any ideas?

Yes, throw that commented method away and read up on how to use Java's collections API to properly sort a list/collection of objects:

http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

Good luck.

prometheuzza at 2007-7-11 15:00:07 > top of Java-index,Other Topics,Algorithms...
# 4

> > No you're not I'm still here

>

> I was making a point that it's rude to flag your

> questions as urgent. As you can see, a lot of the

> people who answer questions tend to ignore such

> posts.

>

I didn't realise this until now since you've pointed it out - I saw other posts with 'urgent' and felt mine really was urgent which is why I did this.

>

> >, any ideas?

>

> Yes, throw that commented method away and read up on

> how to use Java's collections API to properly sort a

> list/collection of objects:

> http://java.sun.com/docs/books/tutorial/collections/in

> terfaces/order.html

>

> Good luck.

I'm going to have a good look at this now. Hopefully I'll be able to get somewhere.

Niru99a at 2007-7-11 15:00:07 > top of Java-index,Other Topics,Algorithms...
# 5

> > > No you're not I'm still here

> >

> > I was making a point that it's rude to flag your

> > questions as urgent. As you can see, a lot of the

> > people who answer questions tend to ignore such

> > posts.

> >

>

> I didn't realise this until now since you've pointed

> it out - I saw other posts with 'urgent' and felt

> mine really was urgent which is why I did this.

>

It's not urgent to me, nor to anyone except maybe you...

So why should anyone help you unless you pay them consultancy fees and a bonus for rapid delivery?

jwentinga at 2007-7-11 15:00:07 > top of Java-index,Other Topics,Algorithms...
# 6
No chance and if its urgent for me it IS urgent.Anyway I don't usually reply back to 'trash' like yours so please don't contribute here next time - it will simply be ignored...actually..this problem is now sorted so I don't need to refer to this thread again ..enough said :)
Niru99a at 2007-7-11 15:00:07 > top of Java-index,Other Topics,Algorithms...
# 7

> No chance and if its urgent for me it IS urgent.

You are wrong, and rude.

Please take the time to read (a part of) this link:

http://www.catb.org/~esr/faqs/smart-questions.html#urgent

> Anyway I don't usually reply back to 'trash'

> like yours

Then don't next time.

> so please don't contribute here next time

> - it will simply be ignored...

As will your future questions (by me at least).

prometheuzza at 2007-7-11 15:00:07 > top of Java-index,Other Topics,Algorithms...
# 8
I am not bothered in the slightest and for your info if someone is rude to me then they will get rudeness straight back...case closed.
Niru99a at 2007-7-11 15:00:07 > top of Java-index,Other Topics,Algorithms...
# 9
> I am not bothered in the slightest and for your info> if someone is rude to me then they will get rudeness> straight back...case closed.Hey knob jockey, I though you were going to ignore all relpies in this thread?
prometheuzza at 2007-7-11 15:00:07 > top of Java-index,Other Topics,Algorithms...
# 10

> if its urgent for me it IS urgent.

Many of the regulars take issue with this 'my question is more important' attitude

and will refrain from replying.

It's totally up to you if you will insist on marking your posts as urgent

and miss out on good advice.

> Anyway I don't usually reply back to 'trash'

> like yours so please don't contribute here next time

> - it will simply be ignored

What do you mean 'here'?

Everyone is free to contribute to any thread they wish to contribute to.

You are free to ignore replies - but if that is what you intend, why do you not just refrain from posting in the first place?

tschodta at 2007-7-11 15:00:07 > top of Java-index,Other Topics,Algorithms...