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.SymbolicExpressionTreeEncoding;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 | using HeuristicLab.Problems.ArtificialAnt.Analyzers;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.ArtificialAnt {
|
---|
36 | [Item("Artificial Ant Problem", "Represents the Artificial Ant problem.")]
|
---|
37 | [Creatable("Problems")]
|
---|
38 | [StorableClass]
|
---|
39 | public sealed class ArtificialAntProblem : SingleObjectiveHeuristicOptimizationProblem<Evaluator, ISymbolicExpressionTreeCreator>, IStorableContent {
|
---|
40 | public string Filename { get; set; }
|
---|
41 |
|
---|
42 | #region constant for default world (Santa Fe)
|
---|
43 | private readonly bool[,] santaFeAntTrail = new bool[,] {
|
---|
44 | {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},
|
---|
45 | {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},
|
---|
46 | {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},
|
---|
47 | {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},
|
---|
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, true, false, false, false, false, true, false, false},
|
---|
49 | {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},
|
---|
50 | {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},
|
---|
51 | {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},
|
---|
52 | {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},
|
---|
53 | {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},
|
---|
54 | {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},
|
---|
55 | {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},
|
---|
56 | {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},
|
---|
57 | {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},
|
---|
58 | {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},
|
---|
59 | {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},
|
---|
60 | {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},
|
---|
61 | {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},
|
---|
62 | {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},
|
---|
63 | {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},
|
---|
64 | {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},
|
---|
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, false, false, false, false, false, false, false, false},
|
---|
66 | {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},
|
---|
67 | {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},
|
---|
68 | {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},
|
---|
69 | {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},
|
---|
70 | {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},
|
---|
71 | {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},
|
---|
72 | {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},
|
---|
73 | {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},
|
---|
74 | {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 },
|
---|
75 | {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 }
|
---|
76 | };
|
---|
77 | #endregion
|
---|
78 |
|
---|
79 | #region Parameter Properties
|
---|
80 | public IValueParameter<ISymbolicExpressionGrammar> ArtificialAntExpressionGrammarParameter {
|
---|
81 | get { return (IValueParameter<ISymbolicExpressionGrammar>)Parameters["ArtificialAntExpressionGrammar"]; }
|
---|
82 | }
|
---|
83 | public IValueParameter<IntValue> MaxExpressionLengthParameter {
|
---|
84 | get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionLength"]; }
|
---|
85 | }
|
---|
86 | public IValueParameter<IntValue> MaxExpressionDepthParameter {
|
---|
87 | get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionDepth"]; }
|
---|
88 | }
|
---|
89 | public IValueParameter<IntValue> MaxFunctionDefinitionsParameter {
|
---|
90 | get { return (IValueParameter<IntValue>)Parameters["MaximumFunctionDefinitions"]; }
|
---|
91 | }
|
---|
92 | public IValueParameter<IntValue> MaxFunctionArgumentsParameter {
|
---|
93 | get { return (ValueParameter<IntValue>)Parameters["MaximumFunctionArguments"]; }
|
---|
94 | }
|
---|
95 | public IValueParameter<BoolMatrix> WorldParameter {
|
---|
96 | get { return (IValueParameter<BoolMatrix>)Parameters["World"]; }
|
---|
97 | }
|
---|
98 | public IValueParameter<IntValue> MaxTimeStepsParameter {
|
---|
99 | get { return (IValueParameter<IntValue>)Parameters["MaximumTimeSteps"]; }
|
---|
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 IntValue MaxExpressionDepth {
|
---|
117 | get { return MaxExpressionDepthParameter.Value; }
|
---|
118 | set { MaxExpressionDepthParameter.Value = value; }
|
---|
119 | }
|
---|
120 | public IntValue MaxFunctionDefinitions {
|
---|
121 | get { return MaxFunctionDefinitionsParameter.Value; }
|
---|
122 | set { MaxFunctionDefinitionsParameter.Value = value; }
|
---|
123 | }
|
---|
124 | public IntValue MaxFunctionArguments {
|
---|
125 | get { return MaxFunctionArgumentsParameter.Value; }
|
---|
126 | set { MaxFunctionArgumentsParameter.Value = value; }
|
---|
127 | }
|
---|
128 | public ArtificialAntExpressionGrammar ArtificialAntExpressionGrammar {
|
---|
129 | get { return (ArtificialAntExpressionGrammar)ArtificialAntExpressionGrammarParameter.Value; }
|
---|
130 | }
|
---|
131 | public IEnumerable<IAntTrailAnalyzer> AntTrailAnalyzers {
|
---|
132 | get { return Operators.OfType<IAntTrailAnalyzer>(); }
|
---|
133 | }
|
---|
134 | #endregion
|
---|
135 |
|
---|
136 | // BackwardsCompatibility3.3
|
---|
137 | #region Backwards compatible code, remove with 3.4
|
---|
138 | [Obsolete]
|
---|
139 | [Storable(Name = "operators")]
|
---|
140 | private IEnumerable<IOperator> oldOperators {
|
---|
141 | get { return null; }
|
---|
142 | set {
|
---|
143 | if (value != null && value.Any())
|
---|
144 | Operators.AddRange(value);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | #endregion
|
---|
148 |
|
---|
149 | [StorableConstructor]
|
---|
150 | private ArtificialAntProblem(bool deserializing) : base(deserializing) { }
|
---|
151 | [StorableHook(HookType.AfterDeserialization)]
|
---|
152 | private void AfterDeserialization() {
|
---|
153 | RegisterEventHandlers();
|
---|
154 | }
|
---|
155 |
|
---|
156 | private ArtificialAntProblem(ArtificialAntProblem original, Cloner cloner)
|
---|
157 | : base(original, cloner) {
|
---|
158 | RegisterEventHandlers();
|
---|
159 | }
|
---|
160 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
161 | return new ArtificialAntProblem(this, cloner);
|
---|
162 | }
|
---|
163 | public ArtificialAntProblem()
|
---|
164 | : base(new Evaluator(), new ProbabilisticTreeCreator()) {
|
---|
165 | BoolMatrix world = new BoolMatrix(santaFeAntTrail);
|
---|
166 | Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100)));
|
---|
167 | Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionDepth", "Maximal depth of the expression to control the artificial ant.", new IntValue(10)));
|
---|
168 | Parameters.Add(new ValueParameter<IntValue>("MaximumFunctionDefinitions", "Maximal number of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
|
---|
169 | Parameters.Add(new ValueParameter<IntValue>("MaximumFunctionArguments", "Maximal number of arguments of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
|
---|
170 | Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", new ArtificialAntExpressionGrammar()));
|
---|
171 | Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
|
---|
172 | Parameters.Add(new ValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
|
---|
173 |
|
---|
174 | Maximization.Value = true;
|
---|
175 | MaximizationParameter.Hidden = true;
|
---|
176 | BestKnownQuality = new DoubleValue(89);
|
---|
177 | SolutionCreator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolution";
|
---|
178 | ((ProbabilisticTreeCreator)SolutionCreator).SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
|
---|
179 | Evaluator.QualityParameter.ActualName = "FoodEaten";
|
---|
180 | InitializeOperators();
|
---|
181 | RegisterEventHandlers();
|
---|
182 | }
|
---|
183 |
|
---|
184 | #region Events
|
---|
185 | protected override void OnSolutionCreatorChanged() {
|
---|
186 | base.OnSolutionCreatorChanged();
|
---|
187 | SolutionCreator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolution";
|
---|
188 | var grammarBased = SolutionCreator as ISymbolicExpressionTreeGrammarBasedOperator;
|
---|
189 | if (grammarBased != null)
|
---|
190 | grammarBased.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
|
---|
191 |
|
---|
192 | SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
|
---|
193 | ParameterizeAnalyzers();
|
---|
194 | ParameterizeOperators();
|
---|
195 | }
|
---|
196 | protected override void OnEvaluatorChanged() {
|
---|
197 | base.OnEvaluatorChanged();
|
---|
198 | Evaluator.QualityParameter.ActualName = "FoodEaten";
|
---|
199 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
200 | ParameterizeAnalyzers();
|
---|
201 | ParameterizeOperators();
|
---|
202 | }
|
---|
203 | private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
204 | ParameterizeAnalyzers();
|
---|
205 | ParameterizeOperators();
|
---|
206 | }
|
---|
207 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
208 | ParameterizeAnalyzers();
|
---|
209 | ParameterizeOperators();
|
---|
210 | }
|
---|
211 | #endregion
|
---|
212 |
|
---|
213 | #region Helpers
|
---|
214 | private void RegisterEventHandlers() {
|
---|
215 | SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
|
---|
216 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
217 | MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
|
---|
218 | MaxFunctionArguments.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
|
---|
219 | MaxFunctionDefinitionsParameter.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
|
---|
220 | MaxFunctionDefinitions.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
|
---|
221 | }
|
---|
222 |
|
---|
223 | private void MaxFunctionDefinitionsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
224 | ArtificialAntExpressionGrammar.MaximumFunctionDefinitions = MaxFunctionDefinitions.Value;
|
---|
225 | ParameterizeOperators();
|
---|
226 | ParameterizeAnalyzers();
|
---|
227 | }
|
---|
228 | private void MaxFunctionArgumentsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
229 | ArtificialAntExpressionGrammar.MaximumFunctionArguments = MaxFunctionArguments.Value;
|
---|
230 | ParameterizeOperators();
|
---|
231 | ParameterizeAnalyzers();
|
---|
232 | }
|
---|
233 |
|
---|
234 | private void InitializeOperators() {
|
---|
235 | Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>().OfType<IOperator>());
|
---|
236 | Operators.Add(new BestAntTrailAnalyzer());
|
---|
237 | Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
|
---|
238 | Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
|
---|
239 | ParameterizeAnalyzers();
|
---|
240 | ParameterizeOperators();
|
---|
241 | }
|
---|
242 |
|
---|
243 | private void ParameterizeAnalyzers() {
|
---|
244 | foreach (IAntTrailAnalyzer analyzer in AntTrailAnalyzers) {
|
---|
245 | analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
246 | analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
247 | analyzer.WorldParameter.ActualName = WorldParameter.Name;
|
---|
248 | analyzer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
|
---|
249 | }
|
---|
250 | foreach (ISymbolicExpressionTreeAnalyzer analyzer in Operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
|
---|
251 | analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | private void ParameterizeOperators() {
|
---|
256 | var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
|
---|
257 |
|
---|
258 | foreach (ISymbolicExpressionTreeGrammarBasedOperator op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
|
---|
259 | op.SymbolicExpressionTreeGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
|
---|
260 | }
|
---|
261 | foreach (ISymbolicExpressionTreeSizeConstraintOperator op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
|
---|
262 | op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaxExpressionDepthParameter.Name;
|
---|
263 | op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaxExpressionLengthParameter.Name;
|
---|
264 |
|
---|
265 | }
|
---|
266 | foreach (Evaluator op in operators.OfType<Evaluator>()) {
|
---|
267 | op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
268 | op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
|
---|
269 | op.WorldParameter.ActualName = WorldParameter.Name;
|
---|
270 | }
|
---|
271 | foreach (ISymbolicExpressionTreeCrossover op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
|
---|
272 | op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
273 | op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
274 | }
|
---|
275 | foreach (ISymbolicExpressionTreeManipulator op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
|
---|
276 | op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
277 | }
|
---|
278 | foreach (ISymbolicExpressionTreeArchitectureAlteringOperator op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
|
---|
279 | op.MaximumFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
|
---|
280 | op.MaximumFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
|
---|
281 | }
|
---|
282 | }
|
---|
283 | #endregion
|
---|
284 | }
|
---|
285 | }
|
---|