-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIslandModelGP.cs
More file actions
277 lines (240 loc) · 9.19 KB
/
IslandModelGP.cs
File metadata and controls
277 lines (240 loc) · 9.19 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using System.Linq;
public class IslandModelGP : MonoBehaviour
{
[Header("Island Model Parameters")]
public int numberOfIslands = 4;
public int migrationInterval = 20; // Generations between migrations
public int migrantsPerIsland = 3;
public MigrationTopology topology = MigrationTopology.Ring;
public enum MigrationTopology { Ring, FullyConnected, Star, Random }
private List<Island> islands;
[System.Serializable]
public class Island
{
public int id;
public List<Individual> population;
public GeneticOperations geneticOps;
public System.Random random;
// Island-specific parameters (heterogeneous islands)
public float mutationRate;
public float complexityWeight;
public int initialMaxDepth;
public Island(int islandId, int popSize, float mutRate, float compWeight, int maxDepth)
{
id = islandId;
mutationRate = mutRate;
complexityWeight = compWeight;
initialMaxDepth = maxDepth;
random = new System.Random(islandId * 1000);
geneticOps = new GeneticOperations(islandId * 1000);
// Initialize population
population = new List<Individual>();
for (int i = 0; i < popSize; i++)
{
ExpressionNode tree = geneticOps.GenerateRandomTree(initialMaxDepth);
population.Add(new Individual(tree));
}
}
public void EvolveGeneration(float[] inputData, float[] outputData, float deathRate, int eliteCount)
{
// Evaluate fitness
foreach (Individual ind in population)
{
ind.CalculateFitness(inputData, outputData, complexityWeight);
}
// Sort by fitness
population.Sort();
population.Reverse();
int killCount = Mathf.FloorToInt(population.Count * deathRate);
int surviveCount = population.Count - killCount;
List<Individual> nextGen = new List<Individual>();
// Elitism
for (int i = 0; i < eliteCount && i < surviveCount; i++)
{
nextGen.Add(population[i].Clone());
}
// Reproduce
while (nextGen.Count < population.Count)
{
Individual parent = TournamentSelection(5);
Individual child = parent.Clone();
// Apply mutations
if (random.NextDouble() < mutationRate)
{
ApplyRandomMutation(child);
}
nextGen.Add(child);
}
population = nextGen;
}
private void ApplyRandomMutation(Individual child)
{
double mutType = random.NextDouble();
if (mutType < 0.25)
geneticOps.MutateConstant(child.root, 1f);
else if (mutType < 0.5)
geneticOps.MutateSubtree(child.root, 3);
else if (mutType < 0.75)
geneticOps.MutateDelete(child.root);
else
geneticOps.MutateSimplify(child.root);
}
private Individual TournamentSelection(int tournamentSize)
{
Individual best = null;
for (int i = 0; i < tournamentSize; i++)
{
Individual candidate = population[random.Next(population.Count)];
if (best == null || candidate.fitness > best.fitness)
best = candidate;
}
return best;
}
}
public void InitializeIslands(int popPerIsland, float[] inputData, float[] outputData)
{
islands = new List<Island>();
// Create heterogeneous islands with different parameters
for (int i = 0; i < numberOfIslands; i++)
{
float mutRate = 0.7f + (i * 0.05f); // Vary mutation rates
float compWeight = 1f + (i * 0.5f); // Vary complexity preferences
int maxDepth = 3 + i; // Vary initial tree depths
islands.Add(new Island(i, popPerIsland, mutRate, compWeight, maxDepth));
}
}
public void RunIslandModel(float[] inputData, float[] outputData, int generations, float deathRate, int eliteCount)
{
InitializeIslands(250, inputData, outputData); // 4 islands x 250 = 1000 total
for (int gen = 0; gen < generations; gen++)
{
// Evolve all islands in parallel
Parallel.ForEach(islands, island =>
{
island.EvolveGeneration(inputData, outputData, deathRate, eliteCount);
});
// Perform migration
if (gen > 0 && gen % migrationInterval == 0)
{
PerformMigration();
}
// Report best across all islands
if (gen % 10 == 0)
{
Individual globalBest = GetGlobalBest();
Debug.Log($"Gen {gen}: Best MSE={globalBest.mse:F6}, " +
$"Complexity={globalBest.complexity}, " +
$"Expression={globalBest.root}");
}
}
}
private void PerformMigration()
{
List<Individual> migrants = new List<Individual>();
// Collect migrants from each island (best individuals)
foreach (Island island in islands)
{
island.population.Sort();
island.population.Reverse();
for (int i = 0; i < migrantsPerIsland; i++)
{
migrants.Add(island.population[i].Clone());
}
}
// Distribute migrants based on topology
switch (topology)
{
case MigrationTopology.Ring:
MigrateRing(migrants);
break;
case MigrationTopology.FullyConnected:
MigrateFullyConnected(migrants);
break;
case MigrationTopology.Star:
MigrateStar(migrants);
break;
case MigrationTopology.Random:
MigrateRandom(migrants);
break;
}
}
private void MigrateRing(List<Individual> migrants)
{
// Send best individuals to next island in ring
for (int i = 0; i < islands.Count; i++)
{
int targetIsland = (i + 1) % islands.Count;
int startIdx = i * migrantsPerIsland;
for (int j = 0; j < migrantsPerIsland; j++)
{
// Replace worst individuals with migrants
int worstIdx = islands[targetIsland].population.Count - 1 - j;
islands[targetIsland].population[worstIdx] = migrants[startIdx + j];
}
}
}
private void MigrateFullyConnected(List<Individual> migrants)
{
// Broadcast best from each island to all others
foreach (Island island in islands)
{
foreach (Individual migrant in migrants)
{
int worstIdx = island.population.Count - 1;
if (migrant.fitness > island.population[worstIdx].fitness)
{
island.population[worstIdx] = migrant.Clone();
island.population.Sort();
island.population.Reverse();
}
}
}
}
private void MigrateStar(List<Individual> migrants)
{
// Island 0 is hub - exchanges with all others
Island hub = islands[0];
for (int i = 1; i < islands.Count; i++)
{
// Send hub's best to island i
for (int j = 0; j < migrantsPerIsland; j++)
{
int worstIdx = islands[i].population.Count - 1 - j;
islands[i].population[worstIdx] = hub.population[j].Clone();
}
// Send island i's best to hub
for (int j = 0; j < migrantsPerIsland; j++)
{
int worstIdx = hub.population.Count - 1 - j;
hub.population[worstIdx] = islands[i].population[j].Clone();
}
}
}
private void MigrateRandom(List<Individual> migrants)
{
System.Random rng = new System.Random();
foreach (Individual migrant in migrants)
{
int targetIsland = rng.Next(islands.Count);
int worstIdx = islands[targetIsland].population.Count - 1;
islands[targetIsland].population[worstIdx] = migrant.Clone();
}
}
private Individual GetGlobalBest()
{
Individual best = null;
foreach (Island island in islands)
{
island.population.Sort();
island.population.Reverse();
if (best == null || island.population[0].fitness > best.fitness)
{
best = island.population[0];
}
}
return best;
}
}