Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.GeneticProgramming/3.3/LawnMower/Problem.cs @ 16813

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

#2521: Changed base ctor call in all GeneticProgrammingProblems.

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Random;
32
33namespace HeuristicLab.Problems.GeneticProgramming.LawnMower {
34  [StorableType("3F72F63C-CBEB-43BD-ADC0-B3F0AD58331B")]
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    #region item cloning and persistence
53    [StorableConstructor]
54    protected Problem(StorableConstructorFlag _) : base(_) { }
55    [StorableHook(HookType.AfterDeserialization)]
56    private void AfterDeserialization() { }
57
58    protected Problem(Problem original, Cloner cloner) : base(original, cloner) { }
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new Problem(this, cloner);
61    }
62    #endregion
63
64    public Problem() : base(new SymbolicExpressionTreeEncoding()) {
65      Parameters.Add(new FixedValueParameter<IntValue>(LawnWidthParameterName, "Width of the lawn.", new IntValue(8)));
66      Parameters.Add(new FixedValueParameter<IntValue>(LawnLengthParameterName, "Length of the lawn.", new IntValue(8)));
67
68      var g = new SimpleSymbolicExpressionGrammar();
69      g.AddSymbols(new string[] { "Sum", "Prog" }, 2, 2);
70      g.AddSymbols(new string[] { "Frog" }, 1, 1);
71      g.AddTerminalSymbols(new string[] { "Left", "Forward" });
72      // initialize 20 ephemeral random constants in [0..32[
73      var fastRand = new FastRandom(314159);
74      for (int i = 0; i < 20; i++) {
75        g.AddTerminalSymbol(string.Format("{0},{1}", fastRand.Next(0, 32), fastRand.Next(0, 32)));
76      }
77
78      Encoding.TreeLength = 1000;
79      Encoding.TreeDepth = 17;
80      Encoding.Grammar = g;
81    }
82
83    public override void Analyze(ISymbolicExpressionTree[] trees, double[] qualities, ResultCollection results, IRandom random) {
84      const string bestSolutionResultName = "Best Solution";
85      var bestQuality = Maximization ? qualities.Max() : qualities.Min();
86      var bestIdx = Array.IndexOf(qualities, bestQuality);
87
88      if (!results.ContainsKey(bestSolutionResultName)) {
89        results.Add(new Result(bestSolutionResultName, new Solution(trees[bestIdx], LawnLengthParameter.Value.Value, LawnWidthParameter.Value.Value, bestQuality)));
90      } else if (((Solution)(results[bestSolutionResultName].Value)).Quality < qualities[bestIdx]) {
91        results[bestSolutionResultName].Value = new Solution(trees[bestIdx], LawnLengthParameter.Value.Value, LawnWidthParameter.Value.Value, bestQuality);
92      }
93    }
94
95    public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) {
96      var length = LawnLengthParameter.Value.Value;
97      var width = LawnWidthParameter.Value.Value;
98
99      var lawn = Interpreter.EvaluateLawnMowerProgram(length, width, tree);
100      // count number of squares that have been mowed
101      int numberOfMowedCells = 0;
102      for (int i = 0; i < length; i++)
103        for (int j = 0; j < width; j++)
104          if (lawn[i, j]) {
105            numberOfMowedCells++;
106          }
107      return numberOfMowedCells;
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.