-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChromosome.cs
160 lines (144 loc) · 4.42 KB
/
Chromosome.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Xml.Serialization;
namespace Georgia
{
/// <summary>
/// GA's "Individual"
/// </summary>
/// <remarks>Chromosomes are complete individuals within the scope of a genetic algorithm. They represent a possible solution to the problem.</remarks>
public class Chromosome<T>: IChromosome, IEnumerable, IComparable, INotifyPropertyChanged where T: IComparable
{
public event PropertyChangedEventHandler PropertyChanged;
protected T[] entries;
private double? _fitness;
public Chromosome() { }
public Chromosome(int capacity)
{
entries = new T[capacity];
}
public Chromosome(T[] entries)
{
if (entries != null)
{
this.entries = entries;
}
else
{
throw new ArgumentNullException("Array argument may not be null");
}
}
public object this[int index]
{
set
{
if (value.GetType() == typeof(T))
{
entries[index] = (T) value;
}
else
{
throw new ArgumentException("Incorrect class for assignment.");
}
}
get
{
return entries[index];
}
}
public int Count
{
get
{
return entries.Count();
}
}
public double? Fitness
{
set
{
_fitness = value;
NotifyPropertyChanged("Fitness");
}
get
{
return _fitness;
}
}
public void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
public int CompareTo(object obj)
{
if (obj is Chromosome<T> && obj != null)
{
Chromosome<T> other = (Chromosome<T>)obj;
if (this.Count == other.Count)
{
bool similar = true;
for (int i = 0; similar && i < Count; i++)
{
similar = (this.entries[i].CompareTo(other.entries[i]) == 0) ? true : false;
}
if (similar)
{
other.Fitness = this.Fitness;
return 0;
}
else
{
if (this.Fitness.HasValue && other.Fitness.HasValue)
{
return ((int)this.Fitness) - ((int)other.Fitness);
}
else
{
throw new ArgumentException("Both Chromosomes must have a Fitness value.");
}
}
}
else
{
throw new ArgumentException("Chromosomes are of unequal length.");
}
}
else
{
throw new InvalidCastException();
}
}
public virtual IChromosome GetCopy()
{
Chromosome<T> newChromosome = new Chromosome<T>(this.Count);
newChromosome.entries = new T[this.Count];
for (int i = 0; i < this.Count; i++)
{
newChromosome.entries[i] = this.entries[i];
}
return newChromosome;
}
#region IEnumerable<T> Members
IEnumerator IEnumerable.GetEnumerator()
{
return (entries != null) ? (entries as IEnumerable).GetEnumerator() : null;
}
#endregion
public override string ToString()
{
string str = string.Empty;
foreach (T s in this)
{
str += s + " ";
}
return str;
}
}
}