Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.4/ArtificialAntProblem.cs @ 12467

Last change on this file since 12467 was 12422, checked in by mkommend, 10 years ago

#2320: Merged the encoding class and all accompanying changes in the trunk.

File size: 20.1 KB
RevLine 
[3223]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3223]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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
[4068]28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[3223]29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
[3631]33using HeuristicLab.Problems.ArtificialAnt.Analyzers;
[3223]34
35namespace HeuristicLab.Problems.ArtificialAnt {
[3528]36  [Item("Artificial Ant Problem", "Represents the Artificial Ant problem.")]
[3223]37  [Creatable("Problems")]
38  [StorableClass]
[6939]39  public sealed class ArtificialAntProblem : SingleObjectiveHeuristicOptimizationProblem<Evaluator, ISymbolicExpressionTreeCreator>, IStorableContent {
[4419]40    public string Filename { get; set; }
[6942]41
[3239]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
[3223]79    #region Parameter Properties
[5686]80    public IValueParameter<ISymbolicExpressionGrammar> ArtificialAntExpressionGrammarParameter {
81      get { return (IValueParameter<ISymbolicExpressionGrammar>)Parameters["ArtificialAntExpressionGrammar"]; }
[3223]82    }
[5528]83    public IValueParameter<IntValue> MaxExpressionLengthParameter {
[5712]84      get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionLength"]; }
[3223]85    }
[5528]86    public IValueParameter<IntValue> MaxExpressionDepthParameter {
[5712]87      get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionDepth"]; }
[3223]88    }
[5528]89    public IValueParameter<IntValue> MaxFunctionDefinitionsParameter {
[5712]90      get { return (IValueParameter<IntValue>)Parameters["MaximumFunctionDefinitions"]; }
[3294]91    }
[5528]92    public IValueParameter<IntValue> MaxFunctionArgumentsParameter {
[5712]93      get { return (ValueParameter<IntValue>)Parameters["MaximumFunctionArguments"]; }
[3294]94    }
[5528]95    public IValueParameter<BoolMatrix> WorldParameter {
96      get { return (IValueParameter<BoolMatrix>)Parameters["World"]; }
[3239]97    }
[5528]98    public IValueParameter<IntValue> MaxTimeStepsParameter {
[5712]99      get { return (IValueParameter<IntValue>)Parameters["MaximumTimeSteps"]; }
[3239]100    }
[3223]101    #endregion
102
103    #region Properties
[3239]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    }
[3338]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    }
[5686]128    public ArtificialAntExpressionGrammar ArtificialAntExpressionGrammar {
129      get { return (ArtificialAntExpressionGrammar)ArtificialAntExpressionGrammarParameter.Value; }
[3223]130    }
[6939]131    public IEnumerable<IAntTrailAnalyzer> AntTrailAnalyzers {
132      get { return Operators.OfType<IAntTrailAnalyzer>(); }
[3223]133    }
[6939]134    #endregion
[3631]135
[6939]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      }
[3631]146    }
[3223]147    #endregion
148
[4098]149    [StorableConstructor]
[4106]150    private ArtificialAntProblem(bool deserializing) : base(deserializing) { }
[4722]151    [StorableHook(HookType.AfterDeserialization)]
152    private void AfterDeserialization() {
[7351]153      RegisterEventHandlers();
[4722]154    }
155
156    private ArtificialAntProblem(ArtificialAntProblem original, Cloner cloner)
157      : base(original, cloner) {
[7351]158      RegisterEventHandlers();
[4722]159    }
160    public override IDeepCloneable Clone(Cloner cloner) {
161      return new ArtificialAntProblem(this, cloner);
162    }
[3223]163    public ArtificialAntProblem()
[6939]164      : base(new Evaluator(), new ProbabilisticTreeCreator()) {
[3239]165      BoolMatrix world = new BoolMatrix(santaFeAntTrail);
[5712]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)));
[5686]170      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", new ArtificialAntExpressionGrammar()));
[3239]171      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
[5712]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)));
[3223]173
[6939]174      Maximization.Value = true;
175      MaximizationParameter.Hidden = true;
[7442]176      BestKnownQuality = new DoubleValue(89);
[6939]177      SolutionCreator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolution";
178      ((ProbabilisticTreeCreator)SolutionCreator).SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
179      Evaluator.QualityParameter.ActualName = "FoodEaten";
[4098]180      InitializeOperators();
[7351]181      RegisterEventHandlers();
[11504]182
183      ArtificialAntExpressionGrammar.MaximumFunctionDefinitions = MaxFunctionDefinitions.Value;
184      ArtificialAntExpressionGrammar.MaximumFunctionArguments = MaxFunctionArguments.Value;
[3223]185    }
186
187    #region Events
[6939]188    protected override void OnSolutionCreatorChanged() {
189      base.OnSolutionCreatorChanged();
[5528]190      SolutionCreator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolution";
191      var grammarBased = SolutionCreator as ISymbolicExpressionTreeGrammarBasedOperator;
192      if (grammarBased != null)
193        grammarBased.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
194
[3223]195      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
[3631]196      ParameterizeAnalyzers();
[3223]197      ParameterizeOperators();
198    }
[6939]199    protected override void OnEvaluatorChanged() {
200      base.OnEvaluatorChanged();
201      Evaluator.QualityParameter.ActualName = "FoodEaten";
202      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
[3631]203      ParameterizeAnalyzers();
[3223]204      ParameterizeOperators();
205    }
[6939]206    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
[3631]207      ParameterizeAnalyzers();
[5517]208      ParameterizeOperators();
[3223]209    }
[3239]210    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
[3631]211      ParameterizeAnalyzers();
[5517]212      ParameterizeOperators();
[3239]213    }
[3223]214    #endregion
215
216    #region Helpers
[7351]217    private void RegisterEventHandlers() {
[3223]218      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
[3239]219      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
[3338]220      MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
221      MaxFunctionArguments.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
222      MaxFunctionDefinitionsParameter.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
223      MaxFunctionDefinitions.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
[3223]224    }
[3239]225
[5517]226    private void MaxFunctionDefinitionsParameter_ValueChanged(object sender, EventArgs e) {
[5686]227      ArtificialAntExpressionGrammar.MaximumFunctionDefinitions = MaxFunctionDefinitions.Value;
[5521]228      ParameterizeOperators();
229      ParameterizeAnalyzers();
[3338]230    }
[5517]231    private void MaxFunctionArgumentsParameter_ValueChanged(object sender, EventArgs e) {
[5686]232      ArtificialAntExpressionGrammar.MaximumFunctionArguments = MaxFunctionArguments.Value;
[5521]233      ParameterizeOperators();
234      ParameterizeAnalyzers();
[3338]235    }
236
[3239]237    private void InitializeOperators() {
[6939]238      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>().OfType<IOperator>());
239      Operators.Add(new BestAntTrailAnalyzer());
240      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
241      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
[3631]242      ParameterizeAnalyzers();
[3239]243      ParameterizeOperators();
244    }
245
[3631]246    private void ParameterizeAnalyzers() {
[3681]247      foreach (IAntTrailAnalyzer analyzer in AntTrailAnalyzers) {
[3631]248        analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
249        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
250        analyzer.WorldParameter.ActualName = WorldParameter.Name;
251        analyzer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
252      }
[3681]253      foreach (ISymbolicExpressionTreeAnalyzer analyzer in Operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
[3651]254        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
255      }
[3223]256    }
257
258    private void ParameterizeOperators() {
[5528]259      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
[5521]260
[6942]261      foreach (ISymbolicExpressionTreeGrammarBasedOperator op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
[5517]262        op.SymbolicExpressionTreeGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
[3223]263      }
[6942]264      foreach (ISymbolicExpressionTreeSizeConstraintOperator op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
[5517]265        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaxExpressionDepthParameter.Name;
266        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaxExpressionLengthParameter.Name;
267
268      }
[6942]269      foreach (Evaluator op in operators.OfType<Evaluator>()) {
[3223]270        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
[3239]271        op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
272        op.WorldParameter.ActualName = WorldParameter.Name;
[3223]273      }
[6942]274      foreach (ISymbolicExpressionTreeCrossover op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
[3238]275        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
[12422]276        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
[3238]277      }
[6942]278      foreach (ISymbolicExpressionTreeManipulator op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
[3294]279        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
280      }
[6942]281      foreach (ISymbolicExpressionTreeArchitectureAlteringOperator op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
[5517]282        op.MaximumFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
283        op.MaximumFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
[3294]284      }
[3223]285    }
286    #endregion
287  }
288}
Note: See TracBrowser for help on using the repository browser.