-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndividual.cs
81 lines (72 loc) · 2.9 KB
/
Individual.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace GeneticProgrammingOptimizer
{
[DebuggerDisplay("{" + nameof(ToString) + "()}")]
public class Individual
{
public static List<string> AvailableLeaf = new List<string>(new string[] {
"A", "B", "C", "D","E",
"1", "2", "3", "4", "5"
});
public static List<string> AvailableOperators = new List<string>(new string[] { "+", "-", "*", "/" });
public BinaryTree<string> Genes;
public double Fitness { get; set; }
static Random rnd = new Random();
public static BinaryTree<string> GenerateSimpleTree()
{
var tree = new BinaryTree<string>();
tree.value = AvailableOperators[rnd.Next(0, AvailableOperators.Count)];
tree.right = new BinaryTree<string> {value = AvailableLeaf[rnd.Next(0, AvailableLeaf.Count)]};
tree.left = new BinaryTree<string>
{
value = AvailableOperators[rnd.Next(0, AvailableOperators.Count)],
left = new BinaryTree<string> { value = AvailableLeaf[rnd.Next(0, AvailableLeaf.Count)] },
right = new BinaryTree<string> { value = AvailableLeaf[rnd.Next(0, AvailableLeaf.Count)] },
};
return tree;
}
public static BinaryTree<string> GenerateRandomTree(int maxDepth = 5, int currentDepth = 0)
{
var tree = new BinaryTree<string>() { };
if (maxDepth >= currentDepth)
{
tree.left = GenerateRandomTree(maxDepth, ++currentDepth);
tree.right = GenerateRandomTree(maxDepth, ++currentDepth);
}
else
{
tree.left = new BinaryTree<string> { value = AvailableLeaf[rnd.Next(0, AvailableLeaf.Count)] };
tree.right = new BinaryTree<string> { value = AvailableLeaf[rnd.Next(0, AvailableLeaf.Count)] };
}
if (tree.left != null && tree.right != null)
tree.value = AvailableOperators[rnd.Next(0, AvailableOperators.Count)];
else
tree.value = AvailableLeaf[rnd.Next(0, AvailableLeaf.Count)];
return tree;
}
public override string ToString()
{
return BinaryTree<string>.ToExpression(this.Genes);
}
public Individual(int treeDepth)
{
if (treeDepth == 0)
Genes = GenerateSimpleTree();
Genes = GenerateRandomTree(treeDepth);
}
public Individual(BinaryTree<string> genes)
{
Genes = genes;
}
public Individual()
{
Genes = GenerateSimpleTree();
}
public Individual Clone()
{
return new Individual(this.Genes.Clone());
}
}
}