source: trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/ArtificialAnt/Problem.cs @ 12947

Last change on this file since 12947 was 12947, checked in by gkronber, 6 years ago

#2472 removed unnecessary Filename property

File size: 6.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
22using System;
23using System.Diagnostics.Contracts;
24using System.Drawing.Text;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34
35namespace HeuristicLab.Problems.GeneticProgramming.ArtificialAnt {
36  [Item("Artificial Ant Problem", "Represents the Artificial Ant problem.")]
37  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 170)]
38  [StorableClass]
39  public sealed class Problem : SymbolicExpressionTreeProblem, IStorableContent {
40
41    #region constant for default world (Santa Fe)
42
43    private static readonly char[][] santaFeAntTrail = new[] {
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(),
75      "                                ".ToCharArray()
76    };
77
78
79    #endregion
80
81    #region Parameter Properties
82    public IValueParameter<BoolMatrix> WorldParameter {
83      get { return (IValueParameter<BoolMatrix>)Parameters["World"]; }
84    }
85    public IValueParameter<IntValue> MaxTimeStepsParameter {
86      get { return (IValueParameter<IntValue>)Parameters["MaximumTimeSteps"]; }
87    }
88    #endregion
89
90    #region Properties
91    public BoolMatrix World {
92      get { return WorldParameter.Value; }
93      set { WorldParameter.Value = value; }
94    }
95    public IntValue MaxTimeSteps {
96      get { return MaxTimeStepsParameter.Value; }
97      set { MaxTimeStepsParameter.Value = value; }
98    }
99    #endregion
100
101    public override bool Maximization {
102      get { return true; }
103    }
104
105    public Problem()
106      : base() {
107      BoolMatrix world = new BoolMatrix(ToBoolMatrix(santaFeAntTrail));
108      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
109      Parameters.Add(new ValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
110
111      base.BestKnownQuality = 89;
112      var g = new SimpleSymbolicExpressionGrammar();
113      g.AddSymbols(new string[] { "IfFoodAhead", "Prog2" }, 2, 2);
114      g.AddSymbols(new string[] { "Prog3" }, 3, 3);
115      g.AddTerminalSymbols(new string[] { "Left", "Right", "Move" });
116      base.Encoding = new SymbolicExpressionTreeEncoding(g, 20, 10);
117    }
118
119
120    public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) {
121      var interpreter = new Interpreter(tree, World, MaxTimeSteps.Value);
122      interpreter.Run();
123      return interpreter.FoodEaten;
124    }
125
126    public override void Analyze(ISymbolicExpressionTree[] trees, double[] qualities, ResultCollection results, IRandom random) {
127      const string bestSolutionResultName = "Best Solution";
128      var bestQuality = Maximization ? qualities.Max() : qualities.Min();
129      var bestIdx = Array.IndexOf(qualities, bestQuality);
130
131      if (!results.ContainsKey(bestSolutionResultName)) {
132        results.Add(new Result(bestSolutionResultName, new Solution(World, trees[bestIdx], MaxTimeSteps.Value, qualities[bestIdx])));
133      } else if (((Solution)(results[bestSolutionResultName].Value)).Quality < qualities[bestIdx]) {
134        results[bestSolutionResultName].Value = new Solution(World, trees[bestIdx], MaxTimeSteps.Value, qualities[bestIdx]);
135      }
136    }
137
138    // persistence
139    [StorableConstructor]
140    private Problem(bool deserializing) : base(deserializing) { }
141    [StorableHook(HookType.AfterDeserialization)]
142    private void AfterDeserialization() {
143    }
144
145    // cloning
146    private Problem(Problem original, Cloner cloner)
147      : base(original, cloner) {
148    }
149    public override IDeepCloneable Clone(Cloner cloner) {
150      return new Problem(this, cloner);
151    }
152
153    #region helpers
154    private bool[,] ToBoolMatrix(char[][] ch) {
155      var rows = ch.Length;
156      var cols = ch[0].Length;
157      var b = new bool[rows, cols];
158      for (int r = 0; r < rows; r++) {
159        Contract.Assert(ch[r].Length == cols); // all rows must have the same number of columns
160        for (int c = 0; c < cols; c++) {
161          b[r, c] = ch[r][c] == '#';
162        }
163      }
164      return b;
165    }
166    #endregion
167  }
168}
Note: See TracBrowser for help on using the repository browser.