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

Last change on this file since 17270 was 17270, checked in by abeham, 3 years ago

#2521: worked on removing virtual from Maximization for single-objective problems

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