Repository navigation
Expand file tree
/
Copy pathEvolution.java
More file actions
156 lines (139 loc) · 4.21 KB
/
Copy pathEvolution.java
File metadata and controls
156 lines (139 loc) · 4.21 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
import java.awt.Graphics;
import java.util.Random;
/**
* This class is where the main methods of the evolution are located
* It has 3 main functions create variation in the population,
* foster natural selection
* and use the "heredity principle" to produce the new generation of bacteria
* @author ikramgabiyev
*
*/
public class Evolution
{
public static final int BAC_POPULATION = 200;
public static final int ANTI_POPULATION = 2;
public static BiologicalDS<Bacterium> bacteriaPopulation = new BiologicalDS<Bacterium>();
public static BiologicalDS<Antibiotic> antibioPopulation = new BiologicalDS<Antibiotic>();
public static Virus hiv = new Virus(); // the only virus controlled by the user
/**
* Creates the first random probes of bacteria.
* Should run just once at the beginning of the program.
* See Main.java
*/
public static void randomlyPopulate()
{
for(int j = 0; j < ANTI_POPULATION; j++)
{
Antibiotic anti = new Antibiotic();
}
for(int i = 0; i < BAC_POPULATION; i++)
{
Bacterium randomBacterium = new Bacterium();
}
}
/**
* Return a bacterium, which probably has a longer
* lifeTime, than other bacteria
* @return fittest Bacterium
*/
public static Bacterium fittestParent()
{
while(true)
{
Random rand = new Random();
int randomIndex = rand.nextInt(BAC_POPULATION);
BiologicalNode<Bacterium> tempTail = bacteriaPopulation.tail;
for (int i = bacteriaPopulation.length - 1; i >= randomIndex; i--)
{
if(rand.nextDouble() < tempTail.bioElem.piePiece)
{
return tempTail.bioElem;
}
tempTail = tempTail.nextBioNode;
}
}
}
/**
* This method employs the fittestParent()
* to find to parents, cross them, thereby
* introducing the child bacterium to the Population
*/
public static void naturalSelection()
{
Bacterium father = fittestParent();
Bacterium mother = fittestParent();
Bacterium.crossBacteria(father, mother);
}
/**
* Runs through every bacterium alive,
* and calls disappear for everyone
* disappear() checks if a bacterium was eaten by antibiotic
* and removes that bacterium from the data structure if that is the case.
* Note: removed elements cannot be drawn in the next generation.
*/
public static void disappearAllConsumed()
{
BiologicalNode<Bacterium> tempTail = bacteriaPopulation.tail;
for(int i = 0; i < bacteriaPopulation.length; i++)
{
tempTail.bioElem.disappear();
tempTail = tempTail.nextBioNode;
}
}
/**
* Draws all bacteria in the data structure
* at the same time it uses move(time) to move all bacteria
* and bounce() to make them bounce off the walls if they hit them
* Note: See bounce() and move(time) in KinematticObject.java
* @param Graphics g
*/
public static void drawAllBacteria(Graphics g)
{
BiologicalNode<Bacterium> tempTail = bacteriaPopulation.tail;
for(int i = 0; i < bacteriaPopulation.length; i++)
{
tempTail.bioElem.move(0.1);
tempTail.bioElem.bounce();
tempTail.bioElem.drawBacteria(g);
// do not go further if you reach the last element
if (i != bacteriaPopulation.length - 1)
{
tempTail = tempTail.nextBioNode;
}
}
}
/**
* Draws all antibiotics in the data structure
* at the same time it uses move(time) to move all antibiotics
* and bounce() to make them bounce off the walls if they hit them
* Note: See bounce() and move(time) in KinematticObject.java
* @param Graphics g
*/
public static void drawAllAntibiotics(Graphics g)
{
BiologicalNode<Antibiotic> tempTail = antibioPopulation.tail;
for(int i = 0; i < antibioPopulation.length; i++)
{
tempTail.bioElem.move(0.1);
tempTail.bioElem.bounce();
tempTail.bioElem.drawAntibiotic(g);
tempTail = tempTail.nextBioNode;
}
}
/**
* Returns the average fittnessValue of all bacteria
* This method is used mainly for debugging and for observation
* @return average fittness of the population
*/
public static double findAverageFitness()
{
double fitnessSum = 0;
BiologicalNode<Bacterium> tempTail = bacteriaPopulation.tail;
for(int i = 0; i < bacteriaPopulation.length; i++)
{
fitnessSum += tempTail.bioElem.genome.fitnessValue;
tempTail = tempTail.nextBioNode;
}
return fitnessSum/BAC_POPULATION;
}
}