-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneticAlgorithm.java
66 lines (57 loc) · 2.82 KB
/
GeneticAlgorithm.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
package org.school.schedule;
import java.util.ArrayList;
import java.util.stream.IntStream;
public class GeneticAlgorithm {
private Data data;
public GeneticAlgorithm(Data data) {
this.data = data;
}
public Population evolve(Population population) {
return mutatePopulation(crossoverPopulation(population));
}
Population crossoverPopulation(Population population) {
Population crossoverPopulation = new Population(population.getSchedules().size(), data);
IntStream.range(0, Driver.NUMB_OF_ELITE_SCHEDULES).forEach(x -> crossoverPopulation.getSchedules().set(x, population.getSchedules().get(x)));
IntStream.range(Driver.NUMB_OF_ELITE_SCHEDULES, population.getSchedules().size()).forEach(x -> {
if (Driver.CROSSOVER_RATE > Math.random()) {
Schedule schedule1 = selectTournamentPopulation(population).sortByFitness().getSchedules().get(0);
Schedule schedule2 = selectTournamentPopulation(population).sortByFitness().getSchedules().get(0);
crossoverPopulation.getSchedules().set(x, crossoverSchedule(schedule1, schedule2));
} else {
crossoverPopulation.getSchedules().set(x, population.getSchedules().get(x));
}
});
return crossoverPopulation;
}
Schedule crossoverSchedule(Schedule schedule1, Schedule schedule2) {
Schedule crossoverSchedule = new Schedule(data).initialize();
IntStream.range(0, crossoverSchedule.getClasses().size()).forEach(x -> {
if (Math.random() > 0.5) crossoverSchedule.getClasses().set(x, schedule1.getClasses().get(x));
else crossoverSchedule.getClasses().set(x, schedule2.getClasses().get(x));
});
return crossoverSchedule;
}
Schedule mutateSchedule(Schedule mutateSchedule) {
Schedule schedule = new Schedule(data).initialize();
IntStream.range(0, mutateSchedule.getClasses().size()).forEach(x -> {
if (Driver.MUTATION_RATE > Math.random()) mutateSchedule.getClasses().set(x, schedule.getClasses().get(x));
});
return mutateSchedule;
}
Population mutatePopulation(Population population) {
Population mutatePopulation = new Population(population.getSchedules().size(), data);
ArrayList<Schedule> schedules = mutatePopulation.getSchedules();
IntStream.range(0, Driver.NUMB_OF_ELITE_SCHEDULES).forEach(x -> schedules.set(x, population.getSchedules().get(x)));
IntStream.range(Driver.NUMB_OF_ELITE_SCHEDULES, population.getSchedules().size()).forEach(x -> {
schedules.set(x, mutateSchedule(population.getSchedules().get(x)));
});
return mutatePopulation;
}
Population selectTournamentPopulation(Population population) {
Population tournamentPopulation = new Population(Driver.TOURNAMENT_SELECTION_SIZE, data);
IntStream.range(0, Driver.TOURNAMENT_SELECTION_SIZE).forEach(x -> {
tournamentPopulation.getSchedules().set(x, population.getSchedules().get((int)(Math.random() * population.getSchedules().size())));
});
return tournamentPopulation;
}
}