Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: working on refactoring

File size: 4.8 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.Threading;
23using HEAL.Attic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Random;
31
32namespace HeuristicLab.Problems.GeneticProgramming.LawnMower {
33  [StorableType("3F72F63C-CBEB-43BD-ADC0-B3F0AD58331B")]
34  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 160)]
35  [Item("Lawn Mower Problem", "The lawn mower demo problem for genetic programming.")]
36  public class Problem : SymbolicExpressionTreeProblem {
37    [Storable] public IFixedValueParameter<IntValue> LawnWidthParameter { get; private set; }
38    [Storable] public IFixedValueParameter<IntValue> LawnLengthParameter { get; private set; }
39
40    #region item cloning and persistence
41    [StorableConstructor]
42    protected Problem(StorableConstructorFlag _) : base(_) { }
43    [StorableHook(HookType.AfterDeserialization)]
44    private void AfterDeserialization() { }
45
46    protected Problem(Problem original, Cloner cloner) : base(original, cloner) {
47      LawnWidthParameter = cloner.Clone(original.LawnWidthParameter);
48      LawnLengthParameter = cloner.Clone(original.LawnLengthParameter);
49    }
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new Problem(this, cloner);
52    }
53    #endregion
54
55    public Problem() : base(new SymbolicExpressionTreeEncoding()) {
56      Maximization = true;
57      Parameters.Add(LawnWidthParameter = new FixedValueParameter<IntValue>("LawnWidth", "Width of the lawn.", new IntValue(8)));
58      Parameters.Add(LawnLengthParameter = new FixedValueParameter<IntValue>("LawnLength", "Length of the lawn.", new IntValue(8)));
59
60      var g = new SimpleSymbolicExpressionGrammar();
61      g.AddSymbols(new string[] { "Sum", "Prog" }, 2, 2);
62      g.AddSymbols(new string[] { "Frog" }, 1, 1);
63      g.AddTerminalSymbols(new string[] { "Left", "Forward" });
64      // initialize 20 ephemeral random constants in [0..32[
65      var fastRand = new FastRandom(314159);
66      for (int i = 0; i < 20; i++) {
67        g.AddTerminalSymbol(string.Format("{0},{1}", fastRand.Next(0, 32), fastRand.Next(0, 32)));
68      }
69
70      Encoding.TreeLength = 1000;
71      Encoding.TreeDepth = 17;
72      Encoding.Grammar = g;
73      Encoding.GrammarParameter.ReadOnly = GrammarRefParameter.ReadOnly = true;
74    }
75
76    public override void Analyze(ISingleObjectiveSolutionContext<ISymbolicExpressionTree>[] solutionContexts, IRandom random) {
77      base.Analyze(solutionContexts, random);
78
79      //TODO: reimplement code below using results directly
80
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 ISingleObjectiveEvaluationResult Evaluate(ISymbolicExpressionTree tree, IRandom random, CancellationToken cancellationToken) {
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      var quality = numberOfMowedCells;
105      return new SingleObjectiveEvaluationResult(quality);
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.