-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMultiObjectiveGP.cs
More file actions
152 lines (129 loc) · 4.98 KB
/
MultiObjectiveGP.cs
File metadata and controls
152 lines (129 loc) · 4.98 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
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class MultiObjectiveGP
{
public bool Dominates(Individual a, Individual b)
{
bool betterOrEqual = a.mse <= b.mse && a.complexity <= b.complexity;
bool strictlyBetter = a.mse < b.mse || a.complexity < b.complexity;
return betterOrEqual && strictlyBetter;
}
public List<List<Individual>> FastNonDominatedSort(List<Individual> population)
{
List<List<Individual>> fronts = new List<List<Individual>>();
int[] dominationCount = new int[population.Count];
List<int>[] dominatedSolutions = new List<int>[population.Count];
for (int i = 0; i < population.Count; i++)
{
dominatedSolutions[i] = new List<int>();
}
List<int> firstFront = new List<int>();
for (int p = 0; p < population.Count; p++)
{
for (int q = 0; q < population.Count; q++)
{
if (p == q) continue;
if (Dominates(population[p], population[q]))
{
dominatedSolutions[p].Add(q);
}
else if (Dominates(population[q], population[p]))
{
dominationCount[p]++;
}
}
if (dominationCount[p] == 0)
{
firstFront.Add(p);
}
}
fronts.Add(firstFront.Select(idx => population[idx]).ToList());
int currentFront = 0;
while (fronts[currentFront].Count > 0)
{
List<int> nextFront = new List<int>();
foreach (int pIdx in firstFront)
{
foreach (int qIdx in dominatedSolutions[pIdx])
{
dominationCount[qIdx]--;
if (dominationCount[qIdx] == 0)
{
nextFront.Add(qIdx);
}
}
}
if (nextFront.Count > 0)
{
fronts.Add(nextFront.Select(idx => population[idx]).ToList());
firstFront = nextFront;
currentFront++;
}
else
{
break;
}
}
return fronts;
}
public void CalculateCrowdingDistance(List<Individual> front)
{
if (front.Count == 0) return;
foreach (Individual ind in front)
{
ind.crowdingDistance = 0f;
}
var sortedByMSE = front.OrderBy(ind => ind.mse).ToList();
sortedByMSE[0].crowdingDistance = float.MaxValue;
sortedByMSE[sortedByMSE.Count - 1].crowdingDistance = float.MaxValue;
float mseRange = sortedByMSE[sortedByMSE.Count - 1].mse - sortedByMSE[0].mse;
if (mseRange > 0)
{
for (int i = 1; i < sortedByMSE.Count - 1; i++)
{
sortedByMSE[i].crowdingDistance +=
(sortedByMSE[i + 1].mse - sortedByMSE[i - 1].mse) / mseRange;
}
}
var sortedByComplexity = front.OrderBy(ind => ind.complexity).ToList();
sortedByComplexity[0].crowdingDistance = float.MaxValue;
sortedByComplexity[sortedByComplexity.Count - 1].crowdingDistance = float.MaxValue;
float complexityRange = sortedByComplexity[sortedByComplexity.Count - 1].complexity -
sortedByComplexity[0].complexity;
if (complexityRange > 0)
{
for (int i = 1; i < sortedByComplexity.Count - 1; i++)
{
sortedByComplexity[i].crowdingDistance +=
(sortedByComplexity[i + 1].complexity - sortedByComplexity[i - 1].complexity) / complexityRange;
}
}
}
public List<Individual> NSGAIISelection(List<Individual> population, int targetSize)
{
List<List<Individual>> fronts = FastNonDominatedSort(population);
List<Individual> selected = new List<Individual>();
int frontIndex = 0;
while (frontIndex < fronts.Count && selected.Count + fronts[frontIndex].Count <= targetSize)
{
foreach (Individual ind in fronts[frontIndex])
{
selected.Add(ind);
}
frontIndex++;
}
if (selected.Count < targetSize && frontIndex < fronts.Count)
{
List<Individual> lastFront = fronts[frontIndex];
CalculateCrowdingDistance(lastFront);
var sortedByDistance = lastFront.OrderByDescending(ind => ind.crowdingDistance).ToList();
int remaining = targetSize - selected.Count;
for (int i = 0; i < remaining && i < sortedByDistance.Count; i++)
{
selected.Add(sortedByDistance[i]);
}
}
return selected;
}
}