Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12911 was 12911, checked in by gkronber, 9 years ago

#2472: created project structure for HeuristicLab.Problems.GeneticProgramming and added implementations of ArtificialAnt and LawnMower

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