1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Problems.Robocode
|
---|
12 | {
|
---|
13 | /// <summary>
|
---|
14 | /// Takes two parent individuals P0 and P1 each. Selects a random node N0 of P0 and a random node N1 of P1.
|
---|
15 | /// And replaces the branch with root0 N0 in P0 with N1 from P1 if the tree-size limits are not violated.
|
---|
16 | /// When recombination with N0 and N1 would create a tree that is too large or invalid the operator randomly selects new N0 and N1
|
---|
17 | /// until a valid configuration is found.
|
---|
18 | /// </summary>
|
---|
19 | [Item("RobocodeMethodCrossover", "An operator which performs crossover of randomly chosen statements with a method.")]
|
---|
20 | [StorableClass]
|
---|
21 | public class RobocodeMethodCrossover : SymbolicExpressionTreeCrossover//, ISymbolicExpressionTreeSizeConstraintOperator
|
---|
22 | {
|
---|
23 |
|
---|
24 | #region Parameter Names
|
---|
25 |
|
---|
26 | private const string HomologousCrossoverPrameterName = "HomologousCrossover";
|
---|
27 | private const string MaximumCrossoverMethodsParameterName = "MaximumCrossoverMethods";
|
---|
28 |
|
---|
29 | #endregion
|
---|
30 |
|
---|
31 | #region Parameter Properties
|
---|
32 |
|
---|
33 | public IValueLookupParameter<BoolValue> HomologousCrossoverParameter
|
---|
34 | {
|
---|
35 | get { return (IValueLookupParameter<BoolValue>)Parameters[HomologousCrossoverPrameterName]; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public IValueLookupParameter<IntValue> MaximumCrossoverMethodsParameter
|
---|
39 | {
|
---|
40 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumCrossoverMethodsParameterName]; }
|
---|
41 | }
|
---|
42 | #endregion
|
---|
43 |
|
---|
44 | #region Properties
|
---|
45 |
|
---|
46 | public BoolValue HomologousCrossover
|
---|
47 | {
|
---|
48 | get { return HomologousCrossoverParameter.ActualValue; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public IntValue MaximumCrossoverMethods
|
---|
52 | {
|
---|
53 | get { return MaximumCrossoverMethodsParameter.ActualValue; }
|
---|
54 | }
|
---|
55 |
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | [StorableConstructor]
|
---|
59 | protected RobocodeMethodCrossover(bool deserializing) : base(deserializing) { }
|
---|
60 | protected RobocodeMethodCrossover(RobocodeMethodCrossover original, Cloner cloner) :
|
---|
61 | base(original, cloner) { }
|
---|
62 | public RobocodeMethodCrossover()
|
---|
63 | : base()
|
---|
64 | {
|
---|
65 | Parameters.Add(new ValueLookupParameter<BoolValue>(HomologousCrossoverPrameterName,
|
---|
66 | "Specifies if the number of statements exchanged between the two parents is the same.", new BoolValue(false)));
|
---|
67 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumCrossoverMethodsParameterName,
|
---|
68 | "The maximal methods to apply crossover on (0 or a number higher than the count of " +
|
---|
69 | "methods means applying crossover to them all).", new IntValue(0)));
|
---|
70 | }
|
---|
71 |
|
---|
72 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
73 | {
|
---|
74 | return new RobocodeMethodCrossover(this, cloner);
|
---|
75 | }
|
---|
76 |
|
---|
77 | public override ISymbolicExpressionTree Crossover(IRandom random,
|
---|
78 | ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1)
|
---|
79 | {
|
---|
80 | return Cross(random, parent0, parent1, HomologousCrossover.Value,
|
---|
81 | MaximumCrossoverMethods.Value);
|
---|
82 | }
|
---|
83 |
|
---|
84 | public static ISymbolicExpressionTree Cross(IRandom random,
|
---|
85 | ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
|
---|
86 | bool homologous, int maximumCrossoverMethods)
|
---|
87 | {
|
---|
88 | // select a random crossover point in the first parent
|
---|
89 | List<CutPoint> crossoverPoints0;
|
---|
90 | SelectCrossoverPoint(random, parent0, maximumCrossoverMethods, out crossoverPoints0);
|
---|
91 |
|
---|
92 | foreach (CutPoint c in crossoverPoints0)
|
---|
93 | {
|
---|
94 | List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
|
---|
95 | parent1.Root.ForEachNodePostfix((n) =>
|
---|
96 | {
|
---|
97 | if (n.Symbol.GetType() == c.Child.Symbol.GetType())
|
---|
98 | allowedBranches.Add(n);
|
---|
99 | });
|
---|
100 |
|
---|
101 | if (allowedBranches.Count == 0)
|
---|
102 | {
|
---|
103 | break;
|
---|
104 | }
|
---|
105 | else
|
---|
106 | {
|
---|
107 | ISymbolicExpressionTreeNode branch = allowedBranches.FirstOrDefault();
|
---|
108 | int startBranchParent0 = (c.Child.SubtreeCount <= 2)? 0 : random.Next(0, c.Child.SubtreeCount - 2);
|
---|
109 | int endBranchParent0 = (c.Child.SubtreeCount <= 2) ? 1 : random.Next(startBranchParent0 + 1, c.Child.SubtreeCount - 1);
|
---|
110 | int Parent0Branches = endBranchParent0 - startBranchParent0;
|
---|
111 |
|
---|
112 | for (int i = 0; i < Parent0Branches; i++)
|
---|
113 | {
|
---|
114 | c.Child.RemoveSubtree(startBranchParent0);
|
---|
115 | }
|
---|
116 |
|
---|
117 | if (homologous)
|
---|
118 | {
|
---|
119 | for (int j = startBranchParent0; j <= endBranchParent0; j++)
|
---|
120 | {
|
---|
121 | c.Child.AddSubtree(branch.GetSubtree(j));
|
---|
122 | }
|
---|
123 | }
|
---|
124 | else
|
---|
125 | {
|
---|
126 | int startBranchParent1 = (branch.SubtreeCount <= 2) ? 0 : random.Next(0, branch.SubtreeCount - 2);
|
---|
127 | int endBranchParent1 = (branch.SubtreeCount <= 2) ? 1 : random.Next(startBranchParent1 + 1, branch.SubtreeCount - 1);
|
---|
128 | int Parent1Branches = endBranchParent1 - startBranchParent1;
|
---|
129 |
|
---|
130 | for (int j = startBranchParent1; j <= endBranchParent1 && c.Child.SubtreeCount < c.Child.Symbol.MaximumArity; j++)
|
---|
131 | {
|
---|
132 | c.Child.AddSubtree(branch.GetSubtree(j));
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | return parent0;
|
---|
139 | }
|
---|
140 |
|
---|
141 | private static void SelectCrossoverPoint(IRandom random, ISymbolicExpressionTree parent0,
|
---|
142 | int maximumCrossoverMethods, out List<CutPoint> crossoverPoints)
|
---|
143 | {
|
---|
144 | List<CutPoint> MethodPoints = new List<CutPoint>();
|
---|
145 | parent0.Root.ForEachNodePostfix((n) =>
|
---|
146 | {
|
---|
147 | if (n.SubtreeCount > 0 && n != parent0.Root)
|
---|
148 | {
|
---|
149 | foreach (var child in n.Subtrees)
|
---|
150 | {
|
---|
151 | if (child.Symbol is Run ||
|
---|
152 | child.Symbol is OnBulletHit ||
|
---|
153 | child.Symbol is OnBulletMissed ||
|
---|
154 | child.Symbol is OnHitByBullet ||
|
---|
155 | child.Symbol is OnHitRobot ||
|
---|
156 | child.Symbol is OnHitWall ||
|
---|
157 | child.Symbol is OnScannedRobot)
|
---|
158 | MethodPoints.Add(new CutPoint(n, child));
|
---|
159 | }
|
---|
160 | }
|
---|
161 | });
|
---|
162 |
|
---|
163 | if (maximumCrossoverMethods == 0)
|
---|
164 | crossoverPoints = MethodPoints;
|
---|
165 | else
|
---|
166 | {
|
---|
167 | crossoverPoints = new List<CutPoint>();
|
---|
168 | for (int i = 0; i < maximumCrossoverMethods; i++)
|
---|
169 | {
|
---|
170 | CutPoint c = MethodPoints.SelectRandom(random);
|
---|
171 | crossoverPoints.Add(c);
|
---|
172 | MethodPoints.Remove(c);
|
---|
173 | if (MethodPoints.Count == 0) break;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | /*private static ISymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<ISymbolicExpressionTreeNode> branches, double internalNodeProbability)
|
---|
179 | {
|
---|
180 | if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability");
|
---|
181 | List<ISymbolicExpressionTreeNode> allowedInternalBranches;
|
---|
182 | List<ISymbolicExpressionTreeNode> allowedLeafBranches;
|
---|
183 | if (random.NextDouble() < internalNodeProbability)
|
---|
184 | {
|
---|
185 | // select internal node if possible
|
---|
186 | allowedInternalBranches = (from branch in branches
|
---|
187 | where branch != null && branch.SubtreeCount > 0
|
---|
188 | select branch).ToList();
|
---|
189 | if (allowedInternalBranches.Count > 0)
|
---|
190 | {
|
---|
191 | return allowedInternalBranches.SelectRandom(random);
|
---|
192 | }
|
---|
193 | else
|
---|
194 | {
|
---|
195 | // no internal nodes allowed => select leaf nodes
|
---|
196 | allowedLeafBranches = (from branch in branches
|
---|
197 | where branch == null || branch.SubtreeCount == 0
|
---|
198 | select branch).ToList();
|
---|
199 | return allowedLeafBranches.SelectRandom(random);
|
---|
200 | }
|
---|
201 | }
|
---|
202 | else
|
---|
203 | {
|
---|
204 | // select leaf node if possible
|
---|
205 | allowedLeafBranches = (from branch in branches
|
---|
206 | where branch == null || branch.SubtreeCount == 0
|
---|
207 | select branch).ToList();
|
---|
208 | if (allowedLeafBranches.Count > 0)
|
---|
209 | {
|
---|
210 | return allowedLeafBranches.SelectRandom(random);
|
---|
211 | }
|
---|
212 | else
|
---|
213 | {
|
---|
214 | allowedInternalBranches = (from branch in branches
|
---|
215 | where branch != null && branch.SubtreeCount > 0
|
---|
216 | select branch).ToList();
|
---|
217 | return allowedInternalBranches.SelectRandom(random);
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }*/
|
---|
221 | }
|
---|
222 | }
|
---|