-
Notifications
You must be signed in to change notification settings - Fork 3
/
Function.cs
174 lines (148 loc) · 4.08 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
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
using System;
using System.Collections.Generic;
using System.Text;
namespace SymbolicRegression
{
/// <summary>
/// 函数类型:常数、变量、其它函数
/// </summary>
enum FunctionType
{
Constant, Variable, Function
}
/// <summary>
/// 函数基类
/// </summary>
abstract class Function
{
public abstract int ParamCount { get; }
public abstract string Symbol { get; }
public virtual FunctionType Type { get; } = FunctionType.Function;
public abstract double Eval(params double[] p);
}
/// <summary>
/// 函数集
/// </summary>
class FunctionSet
{
private readonly List<Function> terminator = new List<Function>();
private readonly List<Function> nonterminator = new List<Function>();
public int MaxParamCount
{
get
{
int r = 0;
for (int i = 0; i < nonterminator.Count; ++i)
{
r = Math.Max(r, nonterminator[i].ParamCount);
}
return r;
}
}
public FunctionSet AddFunction(Function f)
{
if (f.ParamCount == 0)
{
terminator.Add(f);
}
else if (f.ParamCount > 0)
{
nonterminator.Add(f);
}
return this;
}
public Function RandomTerminator()
{
return RandomUtil.RandomElement(terminator);
}
public Function RandomNonterminator()
{
return RandomUtil.RandomElement(nonterminator);
}
public Function RandomFunction()
{
if (RandomUtil.Test(0.5))
{
return RandomTerminator();
}
else
{
return RandomNonterminator();
}
}
}
class Const : Function
{
public override int ParamCount { get; } = 0;
public override string Symbol { get; } = "c";
public override FunctionType Type { get; } = FunctionType.Constant;
public override double Eval(params double[] p)
{
throw new NotImplementedException();
}
}
class Var : Function
{
public override int ParamCount { get; } = 0;
public override string Symbol { get; } = "x";
public override FunctionType Type { get; } = FunctionType.Variable;
public override double Eval(params double[] p)
{
throw new NotImplementedException();
}
}
class Add : Function
{
public override int ParamCount { get; } = 2;
public override string Symbol { get; } = "+";
public override double Eval(params double[] p)
{
return p[0] + p[1];
}
}
class Sub : Function
{
public override int ParamCount { get; } = 2;
public override string Symbol { get; } = "-";
public override double Eval(params double[] p)
{
return p[0] - p[1];
}
}
class Mul : Function
{
public override int ParamCount { get; } = 2;
public override string Symbol { get; } = "*";
public override double Eval(params double[] p)
{
return p[0] * p[1];
}
}
class Div : Function
{
public override int ParamCount { get; } = 2;
public override string Symbol { get; } = "/";
public override double Eval(params double[] p)
{
return p[0] / p[1];
}
}
class Sin : Function
{
public override int ParamCount { get; } = 1;
public override string Symbol { get; } = "sin";
public override double Eval(params double[] p)
{
return Math.Sin(p[0]);
}
}
class Exp : Function
{
public override int ParamCount { get; } = 1;
public override string Symbol { get; } = "exp";
public override double Eval(params double[] p)
{
return Math.Exp(p[0]);
}
}
}