-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrossover.java
More file actions
76 lines (68 loc) · 2.04 KB
/
Crossover.java
File metadata and controls
76 lines (68 loc) · 2.04 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
74
75
76
/*********************************************************
* Filename: Crossover.java
* Author: Charles Walker
* Created: 11/08/23
* Modified: 12/1/23
* Fixed replicating children bug by having the createOffspringOddHalf() method reinitialize the children arrays every time its called.
*
* Purpose:
* Creates the offspring of parents not selected by the Elitism class and replaces
* the parents and two other individuals with the new offspring.
*
* Attributes:
* -firstInd: int
* -lastInd: int
*
* Methods:
* +mate(int):void
* +createOffspringOddHalf(object, object):void
* +seltLastInd(int):void
* +setFirstInd(int):void
* +getFirstInd(): int
*
*********************************************************/
public class Crossover extends Elitism{
private int firstInd;
private int lastInd;
@Override
public Individual[] mate(Individual[] population) {
//System.out.println(firstInd+" "+lastInd);
int count = lastInd-1;
for(int i = firstInd; i < (lastInd-firstInd)/2; i+=2) {
createOffspring(population[i].getChromosome(), population[i+1].getChromosome());
population[i].setChromosome(child1);
population[i+1].setChromosome(child2);
population[count].setChromosome(child3);
population[count-1].setChromosome(child4);
count -=2;
if((i-count) == 1) {
createOffspringOddHalf(population[i].getChromosome(), population[i+1].getChromosome());
population[i].setChromosome(child1);
population[i+1].setChromosome(child2);
}
}
return population;
}
public void createOffspringOddHalf(int[] p1, int[] p2) {
child1 = new int[TOTGENES];
child2 = new int[TOTGENES];
child1[0] = 0;
child2[0] = 0;
for (int i = 1; i < HGENES+1; i++) {
child1[i] = p1[i];
child2[HGENES+i] = p1[HGENES+i];
child1[HGENES+i] = p2[HGENES+i];
child2[i] = p2[I];
}
fixOffspring(child1, child2);
}
public void setLastInd(int num) {
lastInd = num;
}
public void setFirstInd(int num) {
firstInd = num;
}
public int getFirstInd() {
return firstInd;
}
}