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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 | using HeuristicLab.Problems.ArtificialAnt;
|
---|
35 | using HeuristicLab.Problems.ArtificialAnt.Analyzers;
|
---|
36 | using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
|
---|
37 |
|
---|
38 | namespace HeuristicLab.Problems.GrammaticalEvolution {
|
---|
39 | [Item("Grammatical Evolution Artificial Ant Problem", "Represents the Artificial Ant problem, implemented in Grammatical Evolution.")]
|
---|
40 | [Creatable("Problems")]
|
---|
41 | [StorableClass]
|
---|
42 | public sealed class GEArtificialAntProblem : SingleObjectiveHeuristicOptimizationProblem<GEArtificialAntEvaluator, IIntegerVectorCreator>, IStorableContent {
|
---|
43 | public string Filename { get; set; }
|
---|
44 |
|
---|
45 | #region constant for default world (Santa Fe)
|
---|
46 | private readonly bool[,] santaFeAntTrail = new bool[,] {
|
---|
47 | {false, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
|
---|
48 | {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
|
---|
49 | {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, false, false, false, false},
|
---|
50 | {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, true, false, false},
|
---|
51 | {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, true, false, false},
|
---|
52 | {false, false, false, true, true, true, true, false, true, true, true, true, true, false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false, false, false, false},
|
---|
53 | {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false},
|
---|
54 | {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
|
---|
55 | {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
|
---|
56 | {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, true, false, false},
|
---|
57 | {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
|
---|
58 | {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
|
---|
59 | {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false},
|
---|
60 | {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
|
---|
61 | {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, true, true, true, false, false, false},
|
---|
62 | {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, true, false, false, false, false, false, false, false, false},
|
---|
63 | {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
|
---|
64 | {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
|
---|
65 | {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false},
|
---|
66 | {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false},
|
---|
67 | {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
|
---|
68 | {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
|
---|
69 | {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false},
|
---|
70 | {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false},
|
---|
71 | {false, false, false, true, true, false, false, true, true, true, true, true, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
|
---|
72 | {false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
|
---|
73 | {false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
|
---|
74 | {false, true, false, false, false, false, false, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
|
---|
75 | {false, true, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
|
---|
76 | {false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
|
---|
77 | {false, false, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false },
|
---|
78 | {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false }
|
---|
79 | };
|
---|
80 | #endregion
|
---|
81 |
|
---|
82 | #region Parameter Properties
|
---|
83 | public IValueParameter<ISymbolicExpressionGrammar> ArtificialAntExpressionGrammarParameter {
|
---|
84 | get { return (IValueParameter<ISymbolicExpressionGrammar>)Parameters["ArtificialAntExpressionGrammar"]; }
|
---|
85 | }
|
---|
86 | public IValueParameter<IntValue> MaxExpressionLengthParameter {
|
---|
87 | get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionLength"]; }
|
---|
88 | }
|
---|
89 | public IValueParameter<BoolMatrix> WorldParameter {
|
---|
90 | get { return (IValueParameter<BoolMatrix>)Parameters["World"]; }
|
---|
91 | }
|
---|
92 | public IValueParameter<IntValue> MaxTimeStepsParameter {
|
---|
93 | get { return (IValueParameter<IntValue>)Parameters["MaximumTimeSteps"]; }
|
---|
94 | }
|
---|
95 | public IValueParameter<IntMatrix> BoundsParameter {
|
---|
96 | get { return (IValueParameter<IntMatrix>)Parameters["Bounds"]; }
|
---|
97 | }
|
---|
98 | public IValueParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter {
|
---|
99 | get { return (IValueParameter<IGenotypeToPhenotypeMapper>)Parameters["GenotypeToPhenotypeMapper"]; }
|
---|
100 | }
|
---|
101 | #endregion
|
---|
102 |
|
---|
103 | #region Properties
|
---|
104 | public BoolMatrix World {
|
---|
105 | get { return WorldParameter.Value; }
|
---|
106 | set { WorldParameter.Value = value; }
|
---|
107 | }
|
---|
108 | public IntValue MaxTimeSteps {
|
---|
109 | get { return MaxTimeStepsParameter.Value; }
|
---|
110 | set { MaxTimeStepsParameter.Value = value; }
|
---|
111 | }
|
---|
112 | public IntValue MaxExpressionLength {
|
---|
113 | get { return MaxExpressionLengthParameter.Value; }
|
---|
114 | set { MaxExpressionLengthParameter.Value = value; }
|
---|
115 | }
|
---|
116 | public ArtificialAntExpressionGrammar ArtificialAntExpressionGrammar {
|
---|
117 | get { return (ArtificialAntExpressionGrammar)ArtificialAntExpressionGrammarParameter.Value; }
|
---|
118 | }
|
---|
119 | public IEnumerable<IAntTrailAnalyzer> AntTrailAnalyzers {
|
---|
120 | get { return Operators.OfType<IAntTrailAnalyzer>(); }
|
---|
121 | }
|
---|
122 | public IntMatrix Bounds {
|
---|
123 | get { return BoundsParameter.Value; }
|
---|
124 | set { BoundsParameter.Value = value; }
|
---|
125 | }
|
---|
126 | #endregion
|
---|
127 |
|
---|
128 | // BackwardsCompatibility3.3
|
---|
129 | #region Backwards compatible code, remove with 3.4
|
---|
130 | [Obsolete]
|
---|
131 | [Storable(Name = "operators")]
|
---|
132 | private IEnumerable<IOperator> oldOperators {
|
---|
133 | get { return null; }
|
---|
134 | set {
|
---|
135 | if (value != null && value.Any())
|
---|
136 | Operators.AddRange(value);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | #endregion
|
---|
140 |
|
---|
141 | [StorableConstructor]
|
---|
142 | private GEArtificialAntProblem(bool deserializing) : base(deserializing) { }
|
---|
143 | [StorableHook(HookType.AfterDeserialization)]
|
---|
144 | private void AfterDeserialization() {
|
---|
145 | RegisterEventHandlers();
|
---|
146 | }
|
---|
147 |
|
---|
148 | private GEArtificialAntProblem(GEArtificialAntProblem original, Cloner cloner)
|
---|
149 | : base(original, cloner) {
|
---|
150 | RegisterEventHandlers();
|
---|
151 | }
|
---|
152 |
|
---|
153 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
154 | return new GEArtificialAntProblem(this, cloner);
|
---|
155 | }
|
---|
156 |
|
---|
157 | public GEArtificialAntProblem()
|
---|
158 | : base(new GEArtificialAntEvaluator(), new UniformRandomIntegerVectorCreator()) {
|
---|
159 | BoolMatrix world = new BoolMatrix(santaFeAntTrail);
|
---|
160 | Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionLength", "Maximal length of the expression to control the artificial ant (genotype length).", new IntValue(30)));
|
---|
161 | Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", new ArtificialAntExpressionGrammar()));
|
---|
162 | Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
|
---|
163 | Parameters.Add(new ValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
|
---|
164 | IntMatrix m = new IntMatrix(new int[,] { { 0, 100 } });
|
---|
165 | Parameters.Add(new ValueParameter<IntMatrix>("Bounds", "The integer number range in which the single genomes of a genotype are created.", m));
|
---|
166 | Parameters.Add(new ValueParameter<IGenotypeToPhenotypeMapper>("GenotypeToPhenotypeMapper", "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree).", new DepthFirstMapper()));
|
---|
167 |
|
---|
168 | Maximization.Value = true;
|
---|
169 | MaximizationParameter.Hidden = true;
|
---|
170 | BestKnownQuality = new DoubleValue(89);
|
---|
171 |
|
---|
172 | SolutionCreator.IntegerVectorParameter.ActualName = "AntTrailSolutionIntegerVector";
|
---|
173 | Evaluator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolutionTree";
|
---|
174 | Evaluator.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
|
---|
175 | Evaluator.QualityParameter.ActualName = "FoodEaten";
|
---|
176 |
|
---|
177 | InitializeOperators();
|
---|
178 | RegisterEventHandlers();
|
---|
179 | }
|
---|
180 |
|
---|
181 | #region Events
|
---|
182 | protected override void OnSolutionCreatorChanged() {
|
---|
183 | base.OnSolutionCreatorChanged();
|
---|
184 |
|
---|
185 | SolutionCreator.IntegerVectorParameter.ActualName = "AntTrailSolutionIntegerVector";
|
---|
186 | SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
|
---|
187 |
|
---|
188 | ParameterizeAnalyzers();
|
---|
189 | ParameterizeOperators();
|
---|
190 | }
|
---|
191 |
|
---|
192 | protected override void OnEvaluatorChanged() {
|
---|
193 | base.OnEvaluatorChanged();
|
---|
194 |
|
---|
195 | Evaluator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolutionTree";
|
---|
196 | Evaluator.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
|
---|
197 | Evaluator.QualityParameter.ActualName = "FoodEaten";
|
---|
198 |
|
---|
199 | Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);
|
---|
200 | Evaluator.SymbolicExpressionTreeGrammarParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged);
|
---|
201 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
202 |
|
---|
203 | ParameterizeAnalyzers();
|
---|
204 | ParameterizeOperators();
|
---|
205 | }
|
---|
206 |
|
---|
207 | private void SolutionCreator_IntegerVectorParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
208 | ParameterizeAnalyzers();
|
---|
209 | ParameterizeOperators();
|
---|
210 | }
|
---|
211 |
|
---|
212 | private void Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
213 | ParameterizeAnalyzers();
|
---|
214 | ParameterizeOperators();
|
---|
215 | }
|
---|
216 |
|
---|
217 | private void Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
218 | ParameterizeAnalyzers();
|
---|
219 | ParameterizeOperators();
|
---|
220 | }
|
---|
221 |
|
---|
222 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
223 | ParameterizeAnalyzers();
|
---|
224 | ParameterizeOperators();
|
---|
225 | }
|
---|
226 | #endregion
|
---|
227 |
|
---|
228 | #region Helpers
|
---|
229 | private void RegisterEventHandlers() {
|
---|
230 | SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
|
---|
231 | Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);
|
---|
232 | Evaluator.SymbolicExpressionTreeGrammarParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged);
|
---|
233 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
234 | }
|
---|
235 |
|
---|
236 | private void InitializeOperators() {
|
---|
237 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>().OfType<IOperator>());
|
---|
238 | Operators.Add(new BestAntTrailAnalyzer());
|
---|
239 | Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
|
---|
240 | Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
|
---|
241 | ParameterizeAnalyzers();
|
---|
242 | ParameterizeOperators();
|
---|
243 | }
|
---|
244 |
|
---|
245 | private void ParameterizeAnalyzers() {
|
---|
246 | foreach (IAntTrailAnalyzer analyzer in AntTrailAnalyzers) {
|
---|
247 | analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
248 | analyzer.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
|
---|
249 | analyzer.WorldParameter.ActualName = WorldParameter.Name;
|
---|
250 | analyzer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
|
---|
251 | }
|
---|
252 | foreach (ISymbolicExpressionTreeAnalyzer analyzer in Operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
|
---|
253 | analyzer.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | private void ParameterizeOperators() {
|
---|
258 | var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
|
---|
259 |
|
---|
260 | foreach (ISymbolicExpressionTreeGrammarBasedOperator op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
|
---|
261 | op.SymbolicExpressionTreeGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
|
---|
262 | }
|
---|
263 | foreach (GEArtificialAntEvaluator op in operators.OfType<GEArtificialAntEvaluator>()) {
|
---|
264 | op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
|
---|
265 | op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
|
---|
266 | op.WorldParameter.ActualName = WorldParameter.Name;
|
---|
267 | }
|
---|
268 | foreach (IIntegerVectorCrossover op in operators.OfType<IIntegerVectorCrossover>()) {
|
---|
269 | op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
|
---|
270 | op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
|
---|
271 | }
|
---|
272 | foreach (IIntegerVectorManipulator op in operators.OfType<IIntegerVectorManipulator>()) {
|
---|
273 | op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
|
---|
274 | }
|
---|
275 | foreach (IIntegerVectorCreator op in operators.OfType<IIntegerVectorCreator>()) {
|
---|
276 | op.BoundsParameter.ActualName = BoundsParameter.Name;
|
---|
277 | op.LengthParameter.ActualName = MaxExpressionLengthParameter.Name;
|
---|
278 | }
|
---|
279 | }
|
---|
280 | #endregion
|
---|
281 | }
|
---|
282 | } |
---|