-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenome.cs
More file actions
125 lines (98 loc) · 3.46 KB
/
Copy pathGenome.cs
File metadata and controls
125 lines (98 loc) · 3.46 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleEvolutionaryAlgorithm {
public class Genome<T> : IComparable, IEquatable<Genome<T>> {
public double Fitness { set; get; }
public double MutationRate { set; get; }
public T[] Genes { set; get; }
public int Length { set; get; }
public Genome() {
}
public Genome(int length) {
Genes = new T[length];
Length = length;
}
public Genome<T> Copy() {
return new Genome<T>() {
Fitness = Fitness,
Genes = Genes,
Length = Length,
MutationRate = MutationRate
};
}
public static Genome<T> Generate(Func<T> randomizer, int length, double mutationRate) {
List<T> genes = new List<T>();
for(int i = 0; i < length; i++) {
genes.Add(randomizer());
}
return new Genome<T>() {
Genes = genes.ToArray(),
Fitness = 0,
Length = length,
MutationRate = mutationRate
};
}
public void Mutate(Func<T> randomizer) {
for (int i = 0; i < Length; i++) {
if (new Random().NextDouble() < MutationRate) {
this.Genes[i] = randomizer();
}
}
}
public ICollection<Genome<T>> Crossover(Genome<T> parent, int children) {
Genome<T>[] collection = new Genome<T>[children];
int[] steps = new int[children];
for (int i = 0; i < children; i++) {
collection[i] = new Genome<T>(Length);
steps[i] = (int)Math.Round((new Random().NextDouble() * Length));
}
steps = steps.OrderBy(g => g).ToArray();
for (int i = 0; i < children; i++) {
for (int j = 0; j < Length; j++) {
if(j < steps[i]) {
collection[i].Genes[j] = this.Genes[j];
}
else {
collection[i].Genes[j] = parent.Genes[j];
}
}
}
return collection;
}
public int CompareTo(object obj) {
if (!(obj is Genome<T>)) {
throw new ArgumentException($"Object of type {obj.GetType().FullName} is not of type {this.GetType().FullName}");
}
else {
return this.Fitness.CompareTo(((Genome<T>)obj).Fitness);
}
}
public bool Equals(Genome<T> obj) {
return Equals(obj as object);
}
public override bool Equals(object obj) {
if(obj == null || !(obj is Genome<T>)) {
return false;
}
int compareValue = this.CompareTo(obj);
if (compareValue == 0) {
return true;
}
else {
return false;
}
}
public override int GetHashCode() {
return base.GetHashCode() ^ (int)(Fitness * 100);
}
//public static bool operator ==(Genome<T> a, object b) {
// return a.Equals(b);
//}
//public static bool operator !=(Genome<T> a, object b) {
// return !(a == b);
//}
}
}