-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndividual.cs
More file actions
60 lines (51 loc) · 1.41 KB
/
Copy pathIndividual.cs
File metadata and controls
60 lines (51 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 UnityEngine;
[Serializable]
public class Individual : IComparable<Individual>
{
public ExpressionNode root;
public float fitness;
public float mse;
public float complexity;
public float crowdingDistance;
public int dominationCount;
public Individual(ExpressionNode expressionRoot)
{
root = expressionRoot;
crowdingDistance = 0f;
dominationCount = 0;
}
public void CalculateFitness(float[] inputData, float[] outputData, float complexityWeight)
{
mse = 0f;
int validPoints = 0;
for (int i = 0; i < inputData.Length; i++)
{
float predicted = root.Evaluate(inputData[i]);
if (!float.IsNaN(predicted) && !float.IsInfinity(predicted))
{
float error = outputData[i] - predicted;
mse += error * error;
validPoints++;
}
}
if (validPoints > 0)
{
mse /= validPoints;
}
else
{
mse = float.MaxValue;
}
complexity = root.GetComplexity();
fitness = -(mse + complexityWeight * complexity);
}
public int CompareTo(Individual other)
{
return fitness.CompareTo(other.fitness);
}
public Individual Clone()
{
return new Individual(root.Clone());
}
}