source: trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/LawnMower/Problem.cs @ 13267

Last change on this file since 13267 was 13267, checked in by mkommend, 6 years ago

#2472: Minor code improvements in HL.Problems.GP (typos, code organization, code unification).

File size: 4.6 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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Random;
32
33namespace HeuristicLab.Problems.GeneticProgramming.LawnMower {
34  [StorableClass]
35  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 160)]
36  [Item("Lawn Mower Problem", "The lawn mower demo problem for genetic programming.")]
37  public class Problem : SymbolicExpressionTreeProblem {
38    private const string LawnWidthParameterName = "LawnWidth";
39    private const string LawnLengthParameterName = "LawnLength";
40
41    public IFixedValueParameter<IntValue> LawnWidthParameter {
42      get { return (IFixedValueParameter<IntValue>)Parameters[LawnWidthParameterName]; }
43    }
44    public IFixedValueParameter<IntValue> LawnLengthParameter {
45      get { return (IFixedValueParameter<IntValue>)Parameters[LawnLengthParameterName]; }
46    }
47
48    public override bool Maximization {
49      get { return true; }
50    }
51
52    [StorableConstructor]
53    protected Problem(bool deserializing) : base(deserializing) { }
54    [StorableHook(HookType.AfterDeserialization)]
55    private void AfterDeserialization() { }
56
57    protected Problem(Problem original, Cloner cloner) : base(original, cloner) { }
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new Problem(this, cloner);
60    }
61
62    public Problem()
63      : base() {
64      Parameters.Add(new FixedValueParameter<IntValue>(LawnWidthParameterName, "Width of the lawn.", new IntValue(8)));
65      Parameters.Add(new FixedValueParameter<IntValue>(LawnLengthParameterName, "Length of the lawn.", new IntValue(8)));
66
67      var g = new SimpleSymbolicExpressionGrammar();
68      g.AddSymbols(new string[] { "Sum", "Prog" }, 2, 2);
69      g.AddSymbols(new string[] { "Frog" }, 1, 1);
70      g.AddTerminalSymbols(new string[] { "Left", "Forward" });
71      // initialize 20 ephemeral random constants in [0..32[
72      var fastRand = new FastRandom(314159);
73      for (int i = 0; i < 20; i++) {
74        g.AddTerminalSymbol(string.Format("{0},{1}", fastRand.Next(0, 32), fastRand.Next(0, 32)));
75      }
76
77      Encoding = new SymbolicExpressionTreeEncoding(g, 1000, 17);
78    }
79
80    public override void Analyze(ISymbolicExpressionTree[] trees, double[] qualities, ResultCollection results, IRandom random) {
81      const string bestSolutionResultName = "Best Solution";
82      var bestQuality = Maximization ? qualities.Max() : qualities.Min();
83      var bestIdx = Array.IndexOf(qualities, bestQuality);
84
85      if (!results.ContainsKey(bestSolutionResultName)) {
86        results.Add(new Result(bestSolutionResultName, new Solution(trees[bestIdx], LawnLengthParameter.Value.Value, LawnWidthParameter.Value.Value, bestQuality)));
87      } else if (((Solution)(results[bestSolutionResultName].Value)).Quality < qualities[bestIdx]) {
88        results[bestSolutionResultName].Value = new Solution(trees[bestIdx], LawnLengthParameter.Value.Value, LawnWidthParameter.Value.Value, bestQuality);
89      }
90    }
91
92    public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) {
93      var length = LawnLengthParameter.Value.Value;
94      var width = LawnWidthParameter.Value.Value;
95
96      var lawn = Interpreter.EvaluateLawnMowerProgram(length, width, tree);
97      // count number of squares that have been mowed
98      int numberOfMowedCells = 0;
99      for (int i = 0; i < length; i++)
100        for (int j = 0; j < width; j++)
101          if (lawn[i, j]) {
102            numberOfMowedCells++;
103          }
104      return numberOfMowedCells;
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.