0

I am attempting to write a simple Genetic Algorithm in Java after reading a book on Machine Learning and have stumbled on the basics. I'm out of practice with Java so I'm probably missing something extremely simple.

Individual

public class Individual {

    int n;
    int[] genes = new int[500];
    int fitnessValue;

    public int getFitnessValue() {
        return fitnessValue;
    }

    public void setFitnessValue(int fitnessValue) {
        this.fitnessValue = fitnessValue;
    }

    public int[] getGenes() {
        return genes;
    }

    public void setGenes(int index, int gene) {
        this.genes[index] = gene;
    }

    public int getN() {
        return n;
    }

    public void setN(int n) {
        this.n = n;
    }

    // Constructor
    public Individual() {


    }

}

Population

import java.util.Random;

public class Population {

    public Population() {

    }

    public static void main(String[] args) {
        Random rand = new Random();
        int p = rand.nextInt(10);
        int n = rand.nextInt(10);

        Individual pop[] = new Individual[p];

        System.out.println("P is: " + p + "\nN is: " + n);

        for(int j = 0; j <= p; j++) {
            for(int i = 0; i <= n; i++) {
                pop[j].genes[i] = rand.nextInt(2);
            }
        }
    }

    public void addPopulation() {

    }
}

The aim of this code is to populate the Population and the Genes with a random number. Could someone please take a look at my code to see where I'm going wrong?

5
  • 2
    Huh. I did just read "beginner" and "genetic algorithm" in the same question. Commented Oct 14, 2009 at 15:10
  • 1
    What is going wrong? and what do you expect to happen? Commented Oct 14, 2009 at 15:11
  • What you're currently doing is to populate a random portion of genes of a random amount of Individuals. What result did you get and what did you expect? Commented Oct 14, 2009 at 15:14
  • Sorry for the throwaway count and inability now to give a correct answer. I wanted to create an initial population so I could run a routine-wheel fitness method on them. Lou Franco has answered the question perfectly, so thank you for your help! Commented Oct 14, 2009 at 15:16
  • I think: j<=p in your for loop is off by one. Should be: j<p. Also nextInt can return 0 which could mess up your array (i.e. if p was 0) Commented Oct 15, 2009 at 2:28

4 Answers 4

4

before

pop[j].genes[i] = rand.nextInt(2);

add

pop[j] = new Individual();

the elements of the array are null.

Sign up to request clarification or add additional context in comments.

Comments

0

I believe you need to initialize pop[j] before doing pop[j].genes[i] = rand.nextInt();

    Individual pop[] = new Individual[p];

This just initializes the array, not the individual elements. Try to put pop[j] = new Individual() between your two loops.

1 Comment

And you should really access the object properties through accessor methods like getGenes() instead of direct access (.genes).
0

What they said...

Also, do you mean to call your setGenes method, or do you just want to directly access the gene array.

Comments

0

From what I understand of your code I think you need to do this:

for(int j = 0; j <= p; j++) {
    pop[j] = new Individual();
    for(int i = 0; i <= n; i++) {
        pop[j].setGenes(i, rand.nextInt(2));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.