Skip to content

Instantly share code, notes, and snippets.

@Vini2
Last active May 5, 2020 09:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vini2/36be9a156c8c3f9e189b775349a6d611 to your computer and use it in GitHub Desktop.
Save Vini2/36be9a156c8c3f9e189b775349a6d611 to your computer and use it in GitHub Desktop.
class Individual {
int fitness = 0;
int geneLength = 5;
int[] genes = new int[5];
public Individual() {
Random rn = new Random();
//Set genes randomly for each individual
for (int i = 0; i < geneLength; i++) {
genes[i] = rn.nextInt() % 2;
}
fitness = 0;
}
//Calculate fitness
public void calculateFitness() {
fitness = 0;
for (int i = 0; i < geneLength; i++) {
if (genes[i] == 1) {
++fitness;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment