-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunction.cs
93 lines (76 loc) · 1.84 KB
/
Function.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace GP;
#region Handy Tree
//public enum Opertor
//{
// Sum,
// Sub,
// Mul,
// Div,
// Pow,
//}
//public class Node
//{
// public Opertor? Operator { get; set; } = null;
// public double Value { get; set; } = 0;
// public Node? Right { get; set; } = null;
// public Node? Left { get; set; } = null;
//}
#endregion
public class Function
{
public BinaryExpression Tree { get; set; }
public Expression<Func<double, double>>? le = null;
public double Fitness { get; set; } = -1;
public double CrossoverRate { get; set; } = 0.7;
public double MutationRate { get; set; } = 0.1;
public double CalculateFitness(ParameterExpression parameter, double[] inputs, double[] outputs)
{
// calculate fitness function
double diff = 0;
for(int i = 0; i < inputs.Length; i++)
{
var functionOutput = CalculateTree(Tree, parameter, inputs[i]);
if(functionOutput == double.NaN)
{
this.Fitness = double.MaxValue;
return double.MaxValue;
}
diff += Math.Pow(outputs[i] - functionOutput, 2);
}
var fitness = diff / inputs.Length;
Fitness = fitness;
// return fitness
return fitness;
}
public double CalculateTree(BinaryExpression tree, ParameterExpression parameter, double input)
{
if(le == null)
le = Expression.Lambda<Func<double, double>>(tree, parameter);
var result = le.Compile()(input);
return result;
}
//private static void DFS(Node node, bool[] visit, StringBuilder stringBuilder)
//{
// if(node.Operator == null)
// {
// stringBuilder.Append(node.Operator);
// }
// else
// {
// stringBuilder.Append(node.Value);
// }
// if(node.Left != null)
// {
// }
// else if(node.Right != null)
// {
// DFS(node.Right, visit, stringBuilder);
// }
//}
}