-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNA.cs
More file actions
60 lines (53 loc) · 1.41 KB
/
DNA.cs
File metadata and controls
60 lines (53 loc) · 1.41 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Vector;
namespace SmartRockets
{
class DNA
{
public static int lifetime = 800;
public Vector2[] genes;
private static Random rnd = new Random();
public static float maxforce = 0.1f;
public DNA()
{
genes = new Vector2[lifetime];
for (int i = 0; i < lifetime; i++)
{
genes[i] = Vector2.Random();
genes[i].MultiplyRandom(0, maxforce);
}
}
public DNA Crossover(DNA other)
{
DNA child = new DNA();
int midpoint = rnd.Next(0, lifetime);
for (int i = 0; i < lifetime; i++)
{
if (i < midpoint)
{
child.genes[i] = genes[i];
}
else
{
child.genes[i] = other.genes[i];
}
}
return child;
}
public void mutate(float mutationRate)
{
for (int i = 0; i < lifetime; i++)
{
if ((rnd.Next(0, 10000) / 10000.0f) < mutationRate)
{
genes[i] = Vector2.Random();
genes[i].MultiplyRandom(0, maxforce);
}
}
}
}
}