-
Notifications
You must be signed in to change notification settings - Fork 2
/
Population.java
66 lines (57 loc) · 1.39 KB
/
Population.java
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
import domain.Schedule;
import java.util.ArrayList;
import java.util.stream.IntStream;
/**
* Genetic Algorithm
*
* @author Arjuna Jayasinghe
* @version 1.0
*/
public class Population {
private ArrayList<Schedule> schedules;
private Schedule scheduleTemplate;
public Population(int size, Schedule scheduleTemplate2, boolean fillAssignment) {
this.schedules = new ArrayList<>(size);
this.scheduleTemplate = new Schedule(scheduleTemplate2);
IntStream.range(0, size).forEach(x -> {
this.schedules.add(fillAssignment && this.scheduleTemplate != null ? this.scheduleTemplate.fillSchedule() : null);
});
}
public Schedule getScheduleTemplate() {
return scheduleTemplate;
}
public ArrayList<Schedule> getSchedules() {
return schedules;
}
/**
* Sorts schedules by fitness.
*/
public void sortByFitness() {
this.schedules.sort((schedule1, schedule2) -> {
int returnValue = 0;
if (schedule1.getFitness() > schedule2.getFitness()) {
returnValue = -1;
} else if (schedule1.getFitness() < schedule2.getFitness()) {
returnValue = 1;
}
return returnValue;
});
}
/**
* returns the fitness of the first schedule after sorting.
*
* @return
*/
public double getBestFitness() {
sortByFitness();
return getFirst().getFitness();
}
/**
* Returns the first schedule.
*
* @return
*/
public Schedule getFirst() {
return getSchedules().get(0);
}
}