-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPopulation.java
More file actions
73 lines (58 loc) · 1.85 KB
/
Population.java
File metadata and controls
73 lines (58 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Shell for the Population class of the GA assignment.
// Last modified: 1/16/20
public class Population {
Candidate[] candidates;
// Constructor
public Population(int populationSize) {
candidates = new Candidate[populationSize];
}
public void seedPop(int candidateLength) {
// Seed a population with candidates of length candidateLength
for(int i = 0; i < candidates.length; i++) {
candidates[i] = new Candidate(candidateLength);
candidates[i].seedCandidate();
}
}
public Candidate getCandidate(int index) {
// Return the candidate at a given index
return candidates[index];
}
public Candidate getFittest(Formula formula) {
// Returns the fittest candidate of the population with
// respect to a formula
Candidate top_candidate = candidates[0];
int top_fitness = top_candidate.getFitness(formula);
if(top_fitness == formula.clauses.size()) { //assuming can't go over
return top_candidate;
}
int fitness;
for (int i = 1; i < candidates.length; i++) {
fitness = candidates[i].getFitness(formula);
if (fitness > top_fitness) {
top_candidate = candidates[i];
top_fitness = fitness;
if(top_fitness == formula.clauses.size()) { //assuming can't go over
break;
}
}
}
return top_candidate;
}
public int size() {
// Returns the population size
return candidates.length;
}
public void saveCandidate(int index, Candidate candidate) {
// Adds a candidate to the population
candidates[index] = candidate;
}
/*
public static void main(String [] args) {
Population test = new Population(5);
test.seedPop(4);
for(int i = 0; i < test.size(); i++) {
test.getCandidate(i).printCandidate();
}
}
*/
}