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