2

I'm currently learning how to use genetic algorithms. However, I'm having difficulty with my method compareTo(). This method should compare the fitness values between two individuals. I've been trying to invoke my getFitness() method for it to be used in the compareTo() method, but I keep getting this error

Cannot invoke getFitness() on the array type int[]

I'm not sure where this error is coming from. Attached will be the mentioned methods and constructors, I will outline where this error occurs. Thank you.

public class Individual implements Comparable<Individual> {
  Random rand=new Random(); //Initialize random object
  private int size; //Represents N queens
  private int[] individual; //Represents the array of possible solutions

  public Individual(int[] permutation) {
    individual=permutation;
    size=individual.length;
  }

  public Individual(int size) {
    this.size=size;
    individual=Util.getPermutation(size); //Constructs an individual object that is an array of ints
  }

  public int getFitness() {
    //How many queens are attacking each other on the diagonal
    // 0 Represents the best fitness, this is the solution

    //Number of possible differences in permutation is found through factorial addition
    int newSize=0;
    for (int i=0; i<=size; i++){
      newSize+=i;
    }
    int fitness=0;
    int [] xDifference=new int[newSize]; //Array that stores distance between Columns
    int [] yDifference=new int[newSize]; //Array that stores distance between rows

    //Calculates the distance between columns and stores them
    for (int i=0; i<size; i++){
      for (int j=i+1; j<size; j++)
        xDifference[i]=j-i;
    }

    //Calculates distance between rows and stores them
    for (int i=0;i<size;i++){
      for (int j=i+1; j<size; j++)
          yDifference[i]=Math.abs(individual[j]-individual[i]);
    }

    //Compares xDifference and yDifference elements, if they are equal that means Queens are attacking
    for (int i=0; i<size; i++){
      if (xDifference[i]==yDifference[i]){
        fitness++;
      }
    }

    return fitness;
  }

  public int compareTo(Individual other) {
    int compare;

    if (individual.getFitness()<other.getFitness()){ //ERROR OCCURS HERE
      compare=-1;
    }else if (individual.getFitness()>other.getFitness()){// ERROR HERE
      compare=1;
    }else {
      compare=0;
    }

    return compare;
  }
}
1
  • 3
    individual is your array of possible solutions and it is an int[]. You want to use this instead of individual. Commented Feb 8, 2015 at 20:16

2 Answers 2

2

The problem is that you are calling individual.getFitness(). individual is indeed an int[], not an Individual object. Instead, you'll want to use

if (this.getFitness()<other.getFitness()){ 
     compare=-1;
}

You could also leave out this., but I personally find that it makes code easier to read.

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

Comments

1

In your compareTo method you are accessing the field individual which is an array, not an Individual.

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.