Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.ArtificialAnt/3.4/ArtificialAntProblem.cs @ 13945

Last change on this file since 13945 was 13278, checked in by gkronber, 9 years ago

#2472: merged r12895:12906 from trunk to stable

File size: 20.1 KB
RevLine 
[3223]1#region License Information
2/* HeuristicLab
[12009]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  [StorableClass]
[6939]38  public sealed class ArtificialAntProblem : SingleObjectiveHeuristicOptimizationProblem<Evaluator, ISymbolicExpressionTreeCreator>, IStorableContent {
[4419]39    public string Filename { get; set; }
[6942]40
[3239]41    #region constant for default world (Santa Fe)
42    private readonly bool[,] santaFeAntTrail = new bool[,] {
43      {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},
44      {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},
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, true, true, true, 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, true, false, false, false, false, true, 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, 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},
49      {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},
50      {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},
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, true, false, false},
53      {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},
54      {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},
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, true, 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, false, 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, true, true, true, false, false, false},
58      {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},
59      {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},
60      {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},
61      {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},
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, false, false, false, true, 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, false, 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, false, false, false, false, false, false, false, false, false, false, true, 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, true, false, false, false, false, false, false, false, false},
67      {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},
68      {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},
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, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
71      {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},
72      {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},
73      {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 },
74      {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 }
75    };
76    #endregion
77
[3223]78    #region Parameter Properties
[5686]79    public IValueParameter<ISymbolicExpressionGrammar> ArtificialAntExpressionGrammarParameter {
80      get { return (IValueParameter<ISymbolicExpressionGrammar>)Parameters["ArtificialAntExpressionGrammar"]; }
[3223]81    }
[5528]82    public IValueParameter<IntValue> MaxExpressionLengthParameter {
[5712]83      get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionLength"]; }
[3223]84    }
[5528]85    public IValueParameter<IntValue> MaxExpressionDepthParameter {
[5712]86      get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionDepth"]; }
[3223]87    }
[5528]88    public IValueParameter<IntValue> MaxFunctionDefinitionsParameter {
[5712]89      get { return (IValueParameter<IntValue>)Parameters["MaximumFunctionDefinitions"]; }
[3294]90    }
[5528]91    public IValueParameter<IntValue> MaxFunctionArgumentsParameter {
[5712]92      get { return (ValueParameter<IntValue>)Parameters["MaximumFunctionArguments"]; }
[3294]93    }
[5528]94    public IValueParameter<BoolMatrix> WorldParameter {
95      get { return (IValueParameter<BoolMatrix>)Parameters["World"]; }
[3239]96    }
[5528]97    public IValueParameter<IntValue> MaxTimeStepsParameter {
[5712]98      get { return (IValueParameter<IntValue>)Parameters["MaximumTimeSteps"]; }
[3239]99    }
[3223]100    #endregion
101
102    #region Properties
[3239]103    public BoolMatrix World {
104      get { return WorldParameter.Value; }
105      set { WorldParameter.Value = value; }
106    }
107    public IntValue MaxTimeSteps {
108      get { return MaxTimeStepsParameter.Value; }
109      set { MaxTimeStepsParameter.Value = value; }
110    }
111    public IntValue MaxExpressionLength {
112      get { return MaxExpressionLengthParameter.Value; }
113      set { MaxExpressionLengthParameter.Value = value; }
114    }
115    public IntValue MaxExpressionDepth {
116      get { return MaxExpressionDepthParameter.Value; }
117      set { MaxExpressionDepthParameter.Value = value; }
118    }
[3338]119    public IntValue MaxFunctionDefinitions {
120      get { return MaxFunctionDefinitionsParameter.Value; }
121      set { MaxFunctionDefinitionsParameter.Value = value; }
122    }
123    public IntValue MaxFunctionArguments {
124      get { return MaxFunctionArgumentsParameter.Value; }
125      set { MaxFunctionArgumentsParameter.Value = value; }
126    }
[5686]127    public ArtificialAntExpressionGrammar ArtificialAntExpressionGrammar {
128      get { return (ArtificialAntExpressionGrammar)ArtificialAntExpressionGrammarParameter.Value; }
[3223]129    }
[6939]130    public IEnumerable<IAntTrailAnalyzer> AntTrailAnalyzers {
131      get { return Operators.OfType<IAntTrailAnalyzer>(); }
[3223]132    }
[6939]133    #endregion
[3631]134
[6939]135    // BackwardsCompatibility3.3
136    #region Backwards compatible code, remove with 3.4
137    [Obsolete]
138    [Storable(Name = "operators")]
139    private IEnumerable<IOperator> oldOperators {
140      get { return null; }
141      set {
142        if (value != null && value.Any())
143          Operators.AddRange(value);
144      }
[3631]145    }
[3223]146    #endregion
147
[4098]148    [StorableConstructor]
[4106]149    private ArtificialAntProblem(bool deserializing) : base(deserializing) { }
[4722]150    [StorableHook(HookType.AfterDeserialization)]
151    private void AfterDeserialization() {
[7351]152      RegisterEventHandlers();
[4722]153    }
154
155    private ArtificialAntProblem(ArtificialAntProblem original, Cloner cloner)
156      : base(original, cloner) {
[7351]157      RegisterEventHandlers();
[4722]158    }
159    public override IDeepCloneable Clone(Cloner cloner) {
160      return new ArtificialAntProblem(this, cloner);
161    }
[3223]162    public ArtificialAntProblem()
[6939]163      : base(new Evaluator(), new ProbabilisticTreeCreator()) {
[3239]164      BoolMatrix world = new BoolMatrix(santaFeAntTrail);
[5712]165      Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100)));
166      Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionDepth", "Maximal depth of the expression to control the artificial ant.", new IntValue(10)));
167      Parameters.Add(new ValueParameter<IntValue>("MaximumFunctionDefinitions", "Maximal number of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
168      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]169      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", new ArtificialAntExpressionGrammar()));
[3239]170      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
[5712]171      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]172
[6939]173      Maximization.Value = true;
174      MaximizationParameter.Hidden = true;
[7442]175      BestKnownQuality = new DoubleValue(89);
[6939]176      SolutionCreator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolution";
177      ((ProbabilisticTreeCreator)SolutionCreator).SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
178      Evaluator.QualityParameter.ActualName = "FoodEaten";
[4098]179      InitializeOperators();
[7351]180      RegisterEventHandlers();
[11874]181
182      ArtificialAntExpressionGrammar.MaximumFunctionDefinitions = MaxFunctionDefinitions.Value;
183      ArtificialAntExpressionGrammar.MaximumFunctionArguments = MaxFunctionArguments.Value;
[3223]184    }
185
186    #region Events
[6939]187    protected override void OnSolutionCreatorChanged() {
188      base.OnSolutionCreatorChanged();
[5528]189      SolutionCreator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolution";
190      var grammarBased = SolutionCreator as ISymbolicExpressionTreeGrammarBasedOperator;
191      if (grammarBased != null)
192        grammarBased.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
193
[3223]194      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
[3631]195      ParameterizeAnalyzers();
[3223]196      ParameterizeOperators();
197    }
[6939]198    protected override void OnEvaluatorChanged() {
199      base.OnEvaluatorChanged();
200      Evaluator.QualityParameter.ActualName = "FoodEaten";
201      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
[3631]202      ParameterizeAnalyzers();
[3223]203      ParameterizeOperators();
204    }
[6939]205    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
[3631]206      ParameterizeAnalyzers();
[5517]207      ParameterizeOperators();
[3223]208    }
[3239]209    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
[3631]210      ParameterizeAnalyzers();
[5517]211      ParameterizeOperators();
[3239]212    }
[3223]213    #endregion
214
215    #region Helpers
[7351]216    private void RegisterEventHandlers() {
[3223]217      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
[3239]218      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
[3338]219      MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
220      MaxFunctionArguments.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
221      MaxFunctionDefinitionsParameter.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
222      MaxFunctionDefinitions.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
[3223]223    }
[3239]224
[5517]225    private void MaxFunctionDefinitionsParameter_ValueChanged(object sender, EventArgs e) {
[5686]226      ArtificialAntExpressionGrammar.MaximumFunctionDefinitions = MaxFunctionDefinitions.Value;
[5521]227      ParameterizeOperators();
228      ParameterizeAnalyzers();
[3338]229    }
[5517]230    private void MaxFunctionArgumentsParameter_ValueChanged(object sender, EventArgs e) {
[5686]231      ArtificialAntExpressionGrammar.MaximumFunctionArguments = MaxFunctionArguments.Value;
[5521]232      ParameterizeOperators();
233      ParameterizeAnalyzers();
[3338]234    }
235
[3239]236    private void InitializeOperators() {
[6939]237      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>().OfType<IOperator>());
238      Operators.Add(new BestAntTrailAnalyzer());
239      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
240      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
[3631]241      ParameterizeAnalyzers();
[3239]242      ParameterizeOperators();
243    }
244
[3631]245    private void ParameterizeAnalyzers() {
[3681]246      foreach (IAntTrailAnalyzer analyzer in AntTrailAnalyzers) {
[3631]247        analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
248        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
249        analyzer.WorldParameter.ActualName = WorldParameter.Name;
250        analyzer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
251      }
[3681]252      foreach (ISymbolicExpressionTreeAnalyzer analyzer in Operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
[3651]253        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
254      }
[3223]255    }
256
257    private void ParameterizeOperators() {
[5528]258      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
[5521]259
[6942]260      foreach (ISymbolicExpressionTreeGrammarBasedOperator op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
[5517]261        op.SymbolicExpressionTreeGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
[3223]262      }
[6942]263      foreach (ISymbolicExpressionTreeSizeConstraintOperator op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
[5517]264        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaxExpressionDepthParameter.Name;
265        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaxExpressionLengthParameter.Name;
266
267      }
[6942]268      foreach (Evaluator op in operators.OfType<Evaluator>()) {
[3223]269        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
[3239]270        op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
271        op.WorldParameter.ActualName = WorldParameter.Name;
[3223]272      }
[6942]273      foreach (ISymbolicExpressionTreeCrossover op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
[3238]274        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
[12706]275        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
[3238]276      }
[6942]277      foreach (ISymbolicExpressionTreeManipulator op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
[3294]278        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
279      }
[6942]280      foreach (ISymbolicExpressionTreeArchitectureAlteringOperator op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
[5517]281        op.MaximumFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
282        op.MaximumFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
[3294]283      }
[3223]284    }
285    #endregion
286  }
287}
Note: See TracBrowser for help on using the repository browser.