-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChromosome.java
More file actions
161 lines (143 loc) · 5.07 KB
/
Chromosome.java
File metadata and controls
161 lines (143 loc) · 5.07 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.lang.Math;
public class Chromosome {
private BitSet chromo;
private int genecount;
private static int gc;
private boolean hasName = false;
private String[] alleleNames;
//creates a Chromosome that has two alleles and 1 gene
public Chromosome() {
chromo = new BitSet();
genecount = 1;
gc = 1;
}
//Chromosome with n genes and 2n alleles; the alleles are unspecified so they are randomly assigned
Chromosome(int numgenes) {
genecount = numgenes;
gc = numgenes;
chromo = new BitSet(genecount);
for (int i = 0; i < genecount * 2; i++) {
chromo.set(i, randomBoolean());
}
}
//Chromosome with n genes and 2n alleles; all genes were defined in the main method
private Chromosome(int numgenes, BitSet bs) {
genecount = numgenes;
gc = numgenes;
chromo = bs;
}
//Chromosome with 2 alleles and 1 gene; each allele is specified
public Chromosome(boolean allele1, boolean allele2) {
chromo = new BitSet(2);
chromo.set(0, allele1);
chromo.set(1, allele2);
genecount = 1;
gc = 1;
}
//Creates a chromosome with specified gene count and an array of alleles
public Chromosome(int numgenes, boolean[] ray) {
chromo = new BitSet(numgenes * 2);
genecount = numgenes;
gc = numgenes;
for (int i = 0; i < numgenes * 2; i += 2) {
chromo.set(i, ray[i]); //represents allele 1 of a certain gene
chromo.set(i + 1, ray[i + 1]); //represents allele 2 of a certain gene
}
}
//returns a pseudorandom boolean, representing a dominant (true) or recessive (false) trait
private static boolean randomBoolean() {
return Math.random() < 0.5;
}
//each gene chooses an allele from each parent; represents
//independent assortment during sexual reproduction
static Chromosome cross(Chromosome parent1, Chromosome parent2) {
BitSet childGenes = new BitSet();
for (int i = 0; i < getGeneCount() * 2; i += 2) {
boolean chooseA1 = Chromosome.randomBoolean();
boolean chooseA2 = Chromosome.randomBoolean();
//uses randomBoolean to choose one of the two alleles each parent passes on
if (chooseA1) {
childGenes.set(i, parent1.getAllele(i));
}
else {
childGenes.set(i, parent1.getAllele(i + 1));
}
if (chooseA2) {
childGenes.set(i + 1, parent2.getAllele(i));
}
else {
childGenes.set(i + 1, parent2.getAllele(i + 1));
}
}
return new Chromosome(getGeneCount(), childGenes);
}
private void set(int spot, boolean allele) {
chromo.set(spot, allele);
}
//returns a specific allele (true/false = dominant/recessive for that gene)
private boolean getAllele(int spot) {
return chromo.get(spot);
}
//returns the number of genes in the chromosome
private static int getGeneCount() {
return gc;
}
//customizes how each allele is represented; first letter in each
//string should represent the dominant trait
// public ArrayList<String> addName(String[] names) {
// hasName = true;
// if (names.length < genecount) {
// ArrayList<String> list = new ArrayList<>();
// for (int i = 0; i < genecount; i++) {
// list.add(i, names[i].substring(0,1));
// }
// return list;
// }
// return new ArrayList<String>();
// }
//prints the allele pairs of the chromosome;
//dominant allele is capitalized and recessive
//is lowercase
public String toString() {
String printer = "";
int j = 0;
if (hasName) {
for (int i = 0; i < genecount * 2; i++) {
if (getAllele(i)) {
char c = alleleNames[i].charAt(0);
String character = c + "";
printer += character.toUpperCase();
}
else {
char c = alleleNames[i].charAt(0);
String character = c + "";
printer += character.toLowerCase();
}
}
}
else {
for (int i = 0; i < genecount * 2; i++) {
if (getAllele(i)) { //true = dominant
char c = (char) (65 + j);
if (i % 2 == 1) {
j++;
}
printer += c;
} else {
char c = (char) (97 + j);
if (i % 2 == 1) {
j++;
}
printer += c;
}
if (i % 2 == 1) {
printer += ", ";
}
}
}
return "[" + printer.substring(0, printer.length() - 2) + "]";
}
}