Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5/ArtificialAntProblem.cs @ 12901

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

#2422 reverse merge of r12900 (unintended commit)

File size: 8.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;
[12895]23using System.Diagnostics.Contracts;
[3223]24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
[4068]28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[3223]29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[12901]31using HeuristicLab.Problems.ArtificialAnt.Analyzers;
[3223]32
33namespace HeuristicLab.Problems.ArtificialAnt {
[3528]34  [Item("Artificial Ant Problem", "Represents the Artificial Ant problem.")]
[12504]35  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 170)]
[3223]36  [StorableClass]
[12898]37  public sealed class ArtificialAntProblem : SymbolicExpressionTreeProblem, IStorableContent {
[4419]38    public string Filename { get; set; }
[6942]39
[3239]40    #region constant for default world (Santa Fe)
[12895]41
42    private static readonly char[][] santaFeAntTrail = new[] {
43      " ###                            ".ToCharArray(),
44      "   #                            ".ToCharArray(),
45      "   #                    .###..  ".ToCharArray(),
46      "   #                    #    #  ".ToCharArray(),
47      "   #                    #    #  ".ToCharArray(),
48      "   ####.#####       .##..    .  ".ToCharArray(),
49      "            #       .        #  ".ToCharArray(),
50      "            #       #        .  ".ToCharArray(),
51      "            #       #        .  ".ToCharArray(),
52      "            #       #        #  ".ToCharArray(),
53      "            .       #        .  ".ToCharArray(),
54      "            #       .        .  ".ToCharArray(),
55      "            #       .        #  ".ToCharArray(),
56      "            #       #        .  ".ToCharArray(),
57      "            #       #  ...###.  ".ToCharArray(),
58      "            .   .#...  #        ".ToCharArray(),
59      "            .   .      .        ".ToCharArray(),
60      "            #   .      .        ".ToCharArray(),
61      "            #   #      .#...    ".ToCharArray(),
62      "            #   #          #    ".ToCharArray(),
63      "            #   #          .    ".ToCharArray(),
64      "            #   #          .    ".ToCharArray(),
65      "            #   .      ...#.    ".ToCharArray(),
66      "            #   .      #        ".ToCharArray(),
67      " ..##..#####.   #               ".ToCharArray(),
68      " #              #               ".ToCharArray(),
69      " #              #               ".ToCharArray(),
70      " #     .#######..               ".ToCharArray(),
71      " #     #                        ".ToCharArray(),
72      " .     #                        ".ToCharArray(),
73      " .####..                        ".ToCharArray(),
74      "                                ".ToCharArray()
[3239]75    };
[12895]76
77
[3239]78    #endregion
79
[3223]80    #region Parameter Properties
[5528]81    public IValueParameter<BoolMatrix> WorldParameter {
82      get { return (IValueParameter<BoolMatrix>)Parameters["World"]; }
[3239]83    }
[5528]84    public IValueParameter<IntValue> MaxTimeStepsParameter {
[5712]85      get { return (IValueParameter<IntValue>)Parameters["MaximumTimeSteps"]; }
[3239]86    }
[3223]87    #endregion
88
89    #region Properties
[3239]90    public BoolMatrix World {
91      get { return WorldParameter.Value; }
92      set { WorldParameter.Value = value; }
93    }
94    public IntValue MaxTimeSteps {
95      get { return MaxTimeStepsParameter.Value; }
96      set { MaxTimeStepsParameter.Value = value; }
97    }
[12895]98    #endregion
99
100    public override bool Maximization {
101      get { return true; }
[3239]102    }
[12895]103
104    public ArtificialAntProblem()
105      : base() {
106      BoolMatrix world = new BoolMatrix(ToBoolMatrix(santaFeAntTrail));
107      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
108      Parameters.Add(new ValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
109
110      base.BestKnownQuality = 89;
111      var g = new SimpleSymbolicExpressionGrammar();
112      g.AddSymbols(new string[] { "IfFoodAhead", "Prog2" }, 2, 2);
113      g.AddSymbols(new string[] { "Prog3" }, 3, 3);
114      g.AddTerminalSymbols(new string[] { "Left", "Right", "Move" });
115      base.Encoding = new SymbolicExpressionTreeEncoding(g, 20, 10);
[12901]116
117      InitializeOperators();
118      RegisterEventHandlers();
[3239]119    }
[3631]120
[12895]121
[12898]122    public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) {
123      var interpreter = new AntInterpreter(tree, World, MaxTimeSteps.Value);
[12895]124      interpreter.Run();
125      return interpreter.FoodEaten;
[3631]126    }
[3223]127
[12895]128    // persistence
[4098]129    [StorableConstructor]
[4106]130    private ArtificialAntProblem(bool deserializing) : base(deserializing) { }
[4722]131    [StorableHook(HookType.AfterDeserialization)]
132    private void AfterDeserialization() {
[12901]133      RegisterEventHandlers();
[4722]134    }
135
[12895]136    // cloning
[4722]137    private ArtificialAntProblem(ArtificialAntProblem original, Cloner cloner)
138      : base(original, cloner) {
[12901]139      RegisterEventHandlers();
[4722]140    }
141    public override IDeepCloneable Clone(Cloner cloner) {
142      return new ArtificialAntProblem(this, cloner);
143    }
[3223]144
[12901]145    #region Events
146    protected override void OnSolutionCreatorChanged() {
147      base.OnSolutionCreatorChanged();
148      ParameterizeAnalyzers();
149      ParameterizeOperators();
150    }
151    protected override void OnEvaluatorChanged() {
152      base.OnEvaluatorChanged();
153      ParameterizeAnalyzers();
154      ParameterizeOperators();
155    }
156    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
157      ParameterizeAnalyzers();
158      ParameterizeOperators();
159    }
160    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
161      ParameterizeAnalyzers();
162      ParameterizeOperators();
163    }
164    #endregion
165
166    #region Helpers
167    private void RegisterEventHandlers() {
168      Encoding.SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
169      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
170    }
171
172    private void InitializeOperators() {
173      Operators.Add(new BestAntTrailAnalyzer());
174      ParameterizeAnalyzers();
175      ParameterizeOperators();
176    }
177
178    private void ParameterizeAnalyzers() {
179      foreach (var analyzer in Operators.OfType<BestAntTrailAnalyzer>()) {
180        analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
181        analyzer.SymbolicExpressionTreeParameter.ActualName = Encoding.SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
182        analyzer.WorldParameter.ActualName = WorldParameter.Name;
183        analyzer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
184      }
185    }
186
187    private void ParameterizeOperators() {
188      // no problem-specific operators to parameterize
189    }
190
[12895]191    private bool[,] ToBoolMatrix(char[][] ch) {
192      var rows = ch.Length;
193      var cols = ch[0].Length;
194      var b = new bool[rows, cols];
195      for (int r = 0; r < rows; r++) {
196        Contract.Assert(ch[r].Length == cols); // all rows must have the same number of columns
197        for (int c = 0; c < cols; c++) {
198          b[r, c] = ch[r][c] == '#';
199        }
[3223]200      }
[12895]201      return b;
[3223]202    }
203    #endregion
204  }
205}
Note: See TracBrowser for help on using the repository browser.