1 | using HeuristicLab.Common;
|
---|
2 | using HeuristicLab.Core;
|
---|
3 | using HeuristicLab.Data;
|
---|
4 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
5 | using HeuristicLab.Operators;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Problems.Robocode
|
---|
11 | {
|
---|
12 | [StorableClass]
|
---|
13 | [Item("Best Tank program Analyzer",
|
---|
14 | "Analyzer that stores the best tank program.")]
|
---|
15 | public class BestSolutionAnalyzer : SingleSuccessorOperator,
|
---|
16 | ISymbolicExpressionTreeAnalyzer
|
---|
17 | {
|
---|
18 | #region parameter names
|
---|
19 | private const string QualityParameterName = "Quality";
|
---|
20 | private const string SymbolicExpressionTreeParameterName =
|
---|
21 | "TankProgram";
|
---|
22 | private const string MovesParameterName = "Moves";
|
---|
23 | private const string ShotsParameterName = "Shots";
|
---|
24 | private const string BestSolutionParameterName = "Best solution";
|
---|
25 | private const string ResultsParameterName = "Results";
|
---|
26 | private const string RobocodePathParamaterName = "Path";
|
---|
27 | private const string CoevolutionParameterName = "Coevolution";
|
---|
28 | #endregion
|
---|
29 |
|
---|
30 |
|
---|
31 | #region parameters
|
---|
32 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter
|
---|
33 | {
|
---|
34 | get
|
---|
35 | {
|
---|
36 | return (IScopeTreeLookupParameter<DoubleValue>)
|
---|
37 | Parameters[QualityParameterName];
|
---|
38 | }
|
---|
39 | }
|
---|
40 | public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter
|
---|
41 | {
|
---|
42 | get
|
---|
43 | {
|
---|
44 | return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)
|
---|
45 | Parameters[SymbolicExpressionTreeParameterName];
|
---|
46 | }
|
---|
47 | }
|
---|
48 | //public ILookupParameter<IntValue> MovesParameter
|
---|
49 | //{
|
---|
50 | // get
|
---|
51 | // {
|
---|
52 | // return (ILookupParameter<IntValue>)
|
---|
53 | // Parameters[MovesParameterName];
|
---|
54 | // }
|
---|
55 | //}
|
---|
56 | //public ILookupParameter<IntValue> ShotsParameter
|
---|
57 | //{
|
---|
58 | // get
|
---|
59 | // {
|
---|
60 | // return (ILookupParameter<IntValue>)
|
---|
61 | // Parameters[ShotsParameterName];
|
---|
62 | // }
|
---|
63 | //}
|
---|
64 | public ILookupParameter<Solution> BestSolutionParameter
|
---|
65 | {
|
---|
66 | get
|
---|
67 | {
|
---|
68 | return (ILookupParameter<Solution>)
|
---|
69 | Parameters[BestSolutionParameterName];
|
---|
70 | }
|
---|
71 | }
|
---|
72 | public ILookupParameter<ResultCollection> ResultParameter
|
---|
73 | {
|
---|
74 | get
|
---|
75 | {
|
---|
76 | return (ILookupParameter<ResultCollection>)
|
---|
77 | Parameters[ResultsParameterName];
|
---|
78 | }
|
---|
79 | }
|
---|
80 | public ILookupParameter<StringValue> RobocodePathParameter
|
---|
81 | {
|
---|
82 | get
|
---|
83 | {
|
---|
84 | return (ILookupParameter<StringValue>)
|
---|
85 | Parameters[RobocodePathParamaterName];
|
---|
86 | }
|
---|
87 | }
|
---|
88 | public ILookupParameter<BoolValue> CoevolutionParameter
|
---|
89 | {
|
---|
90 | get
|
---|
91 | {
|
---|
92 | return (ILookupParameter<BoolValue>)
|
---|
93 | Parameters[CoevolutionParameterName];
|
---|
94 | }
|
---|
95 | }
|
---|
96 | #endregion
|
---|
97 |
|
---|
98 | [StorableConstructor]
|
---|
99 | protected BestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
100 | protected BestSolutionAnalyzer(BestSolutionAnalyzer original,
|
---|
101 | Cloner cloner)
|
---|
102 | : base(original, cloner)
|
---|
103 | {
|
---|
104 | }
|
---|
105 |
|
---|
106 | public BestSolutionAnalyzer()
|
---|
107 | {
|
---|
108 | Parameters.Add(
|
---|
109 | new ScopeTreeLookupParameter<DoubleValue>(
|
---|
110 | QualityParameterName,
|
---|
111 | "The solution quality of the tank program."));
|
---|
112 | Parameters.Add(
|
---|
113 | new ScopeTreeLookupParameter<ISymbolicExpressionTree>(
|
---|
114 | SymbolicExpressionTreeParameterName,
|
---|
115 | "The tank program to evaluate represented " +
|
---|
116 | "as symbolic expression tree."));
|
---|
117 | Parameters.Add(
|
---|
118 | new LookupParameter<Solution>(
|
---|
119 | BestSolutionParameterName, "The best tank program."));
|
---|
120 | //Parameters.Add(
|
---|
121 | // new LookupParameter<IntValue>(
|
---|
122 | // MovesParameterName, "The number of moves made."));
|
---|
123 | //Parameters.Add(
|
---|
124 | // new LookupParameter<IntValue>(
|
---|
125 | // ShotsParameterName, "The shots made."));
|
---|
126 | Parameters.Add(
|
---|
127 | new LookupParameter<ResultCollection>(
|
---|
128 | ResultsParameterName, "The result collection of the algorithm."));
|
---|
129 | Parameters.Add(
|
---|
130 | new LookupParameter<StringValue>(
|
---|
131 | RobocodePathParamaterName,
|
---|
132 | "Path of the Robocode installation."));
|
---|
133 | Parameters.Add(
|
---|
134 | new LookupParameter<BoolValue>(
|
---|
135 | CoevolutionParameterName,
|
---|
136 | "Use Coevolution"));
|
---|
137 | }
|
---|
138 |
|
---|
139 | public override IOperation Apply()
|
---|
140 | {
|
---|
141 | // get an array of all trees
|
---|
142 | // and an array of all qualities
|
---|
143 | var trees = SymbolicExpressionTreeParameter.ActualValue;
|
---|
144 | var qualities = QualityParameter.ActualValue;
|
---|
145 |
|
---|
146 | // find the tree with the best quality
|
---|
147 | double maxQuality = double.NegativeInfinity;
|
---|
148 | ISymbolicExpressionTree bestTree = null;
|
---|
149 | for (int i = 0; i < qualities.Length; i++)
|
---|
150 | {
|
---|
151 | if (qualities[i].Value > maxQuality)
|
---|
152 | {
|
---|
153 | maxQuality = qualities[i].Value;
|
---|
154 | bestTree = trees[i];
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | var coevolution = CoevolutionParameter.ActualValue.Value;
|
---|
159 | double actualQuality = 0;
|
---|
160 |
|
---|
161 | if (coevolution)
|
---|
162 | actualQuality = Interpreter.EvaluateTankProgram(bestTree, null, RobocodePathParameter.ActualValue.Value);
|
---|
163 |
|
---|
164 | // create a solution instance
|
---|
165 | //int shots = ShotsParameter.ActualValue.Value;
|
---|
166 | //int moves = MovesParameter.ActualValue.Value;
|
---|
167 | var bestSolution = new Solution(bestTree, RobocodePathParameter.ActualValue.Value);//, moves, shots);
|
---|
168 | // store the new solution in the best solution parameter
|
---|
169 | BestSolutionParameter.ActualValue = bestSolution;
|
---|
170 |
|
---|
171 | // also add the best solution as a result to the result collection
|
---|
172 | // or alternatively update the existing result
|
---|
173 | var resultCollection = ResultParameter.ActualValue;
|
---|
174 | if (!resultCollection.ContainsKey(BestSolutionParameterName))
|
---|
175 | {
|
---|
176 | resultCollection.Add(
|
---|
177 | new Result(BestSolutionParameterName,
|
---|
178 | "The best tank program", bestSolution));
|
---|
179 | //if(coevolution)
|
---|
180 | // resultCollection.Add(
|
---|
181 | // new Result("Actual Quality",
|
---|
182 | // "The actual quality of the best program", new DoubleValue(actualQuality)));
|
---|
183 | }
|
---|
184 | else
|
---|
185 | {
|
---|
186 | resultCollection[BestSolutionParameterName].Value = bestSolution;
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (coevolution)
|
---|
190 | {
|
---|
191 | if (!resultCollection.ContainsKey("Actual Quality"))
|
---|
192 | {
|
---|
193 | resultCollection.Add(
|
---|
194 | new Result("Actual Quality",
|
---|
195 | "The actual quality of the best program", new DoubleValue(actualQuality)));
|
---|
196 | }
|
---|
197 | else
|
---|
198 | {
|
---|
199 | resultCollection["Actual Quality"].Value = new DoubleValue(actualQuality);
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | // important return base.Apply() to make sure the
|
---|
204 | // next operator is queued for execution
|
---|
205 | return base.Apply();
|
---|
206 | }
|
---|
207 |
|
---|
208 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
209 | {
|
---|
210 | return new BestSolutionAnalyzer(this, cloner);
|
---|
211 | }
|
---|
212 |
|
---|
213 | // override this property to indicate that this analyzer
|
---|
214 | // should be enabled by default in the algorithm
|
---|
215 | public bool EnabledByDefault
|
---|
216 | {
|
---|
217 | get { return true; }
|
---|
218 | }
|
---|
219 | }
|
---|
220 | } |
---|